rand_resp#

  • Series: lecture-python.myst

  • File: lectures/rand_resp.md

  • Audit date: 2026-08-26

  • Corpus snapshot: e25fdf2345

  • Categories audited: writing, math, code, references, links (JAX out of scope)

  • Overall score: 8.1 / 10

  • Priority: HIGH

Score breakdown#

Category

Score

One-line note

Writing

3.5/10

qe-writing-006 ×3; qe-writing-005 ×3; qe-writing-003 ×4, +3 more.

Math

9.5/10

qe-math-009 ×3.

Code

7.5/10

qe-code-001 ×5.

JAX

out of scope

JAX rules target lecture-jax.

Figures

N/A

no figures or plotting code.

References

10/10

no mechanical violations detected.

Links

10/10

no mechanical violations detected.

Admonitions

N/A

no admonitions, exercises or solutions.

Issues#

Critical#

None found.

High severity#

  • [qe-code-001] (reviewer) — Follow PEP8 unless closer to mathematical notation. Count: 5. Lines: 220, 203, 199, 230, 212. Example: 220 is 137 characters, is reached by a backslash continuation from 219, and holds the whole MSE-ratio denominator on one line: (df['Bias']**2 + ((A * df['T_a'] + (1 - A) * (1 - df['T_b'])) * (1 - A * df['T_a'] - (1 - A) * (1 - df['T_b'])) / n)) - the bias-squared-plus-variance expression of {eq}`eq:ten` and {eq}`eq:eleven`, which is exactly the line a reader most needs to check against the algebra. Naming the two pieces would fix the length and the readability together. 203-206 hand-align the TaTb literal with runs of two to four spaces after commas (E241 seven times) and leave trailing whitespace on three of the four rows (W291); 210 then omits the space after a comma in the same block, df[['T_a','T_b']] (E231). 199 is the only class in the lecture and neither it nor any of its three methods (200, 213, 225) has a docstring. 230-231 and 240-243 mix self.n and the local n two lines apart for the same value. 212 and 224 are blank lines carrying whitespace (W293), and 208 is 87 characters. Two flake8 items I would leave alone: the E226 pair on 219 is p - 1/2, where the tight / inside a spaced - is what PEP8 actually recommends.

  • [qe-writing-006] — Capitalize lecture titles properly. Count: 3. Lines: 38, 151, 318. Example: H2 Title Case: “Warner’s Strategy” (Strategy).

  • [qe-writing-008] — Remove excessive whitespace between words. Count: 49. Lines: 19, 21, 23, 25, 27, 31, 33, 55, 58, 59, …. Example: 2 spaces.

Medium severity#

  • [qe-math-009] (reviewer) — Choose simplicity in mathematical notation. Count: 3. Lines: 163, 121, 69. Example: \(\hat\pi\) names two different estimators of the same parameter, and both get an \(\mathbb{E}(\hat\pi)\) display. {eq}`eq:four` at 101 is the randomized-response MLE and {eq}`eq:five` at 108 gives its mean; {eq}`eq:eight` at 163 is the non-randomized sample mean \(\frac{1}{n}\sum Y_i\) and {eq}`eq:nine` at 170 gives its mean, 60 lines later, under the identical left-hand side. {eq}`eq:six` and {eq}`eq:eleven` are likewise both “\(Var(\hat\pi) = \ldots\)” with different right-hand sides, and 190’s MSE Ratio divides one by the other - so the ratio the lecture is built around is a ratio of two quantities written with the same symbol. \(\hat\pi_R\) and \(\hat\pi_N\), or the code’s own distinction (pi_hat at 237 against pi_trad at 243), would cost two subscripts. Second, {eq}`eq:six` switches parameterisation mid-block: 118-120 write the denominator as \((2p-1)^2\) and 121 writes \(16(p-\frac12)^2\), which is \(4(2p-1)^2\) - the same quantity in two dresses in consecutive lines - and {eq}`eq:seven` then keeps the \((p-\frac12)\) form while 89, 95, 101 and 118 all use \((2p-1)\). Third, 69 defines the indicator as an inline \begin{cases} block stitched from five \text{} fragments and a stray \ : X_{i}=\begin{cases}1,\text{ if the } i\text{th} \ \text{ subject  says yes}\\0,\ldots\end{cases}, where \(X_i = \mathbb{1}\{\text{subject } i \text{ answers yes}\}\) says the same thing in one line.

  • [qe-writing-002] (reviewer) — Keep writing clear, concise, and valuable. Count: 4. Lines: 55, 25, 316, 19. Example: 55 and 56 are the same sentence twice, in two grammatical moods, inside a seven-item list. 25 is 41 words with four internal multi-space runs and two bolded words doing the work of a contrast that a shorter sentence would make structurally: “Warner used elementary probability to construct a way to protect the privacy of individual respondents to surveys while still estimating the fraction of a collection of individuals who have a socially stigmatized characteristic or who engage in a socially stigmatized activity.” Several sentences are also ungrammatical in ways that cost the reader a re-read: 19 “reluctant to participate a sample survey” (missing “in”), 48 “every person in population” (missing “the”), 63 “constructed a maximum likelihood estimators”, 157 “Members of Group A tells the truth”, 187 “It is useful to define a” dangling into the display that follows, and 316 “the randomized response method does better performance in more situations”. These are the two sentences over 32 words in the file, so length is not the problem - the copy is.

  • [qe-writing-003] (reviewer) — Maintain logical flow. Count: 4. Lines: 119, 55, 196, 146. Example: {eq}`eq:six` loses a factor of \(n\) in the middle of its own derivation. 118 correctly writes \(Var(\hat\pi) = \frac{n\,Var(X_i)}{(2p-1)^2 n^2}\); 119 then substitutes \(Var(X_i) = [\pi p + (1-\pi)(1-p)][(1-\pi)p + \pi(1-p)]\) but keeps \((2p-1)^2 n^2\) underneath, dropping the leading \(n\), and 120 carries the same \(n^2\); 121 then states the answer as \(\frac{1}{n}\left[\frac{1}{16(p-\frac12)^2} - (\pi-\frac12)^2\right]\), which is correct - I verified it by substituting \(u = \pi - \tfrac12\), \(v = p - \tfrac12\), giving \(Var(X_i) = \tfrac14 - 4u^2v^2\) and \(Var(\hat\pi) = \frac{1}{n}\left[\frac{1}{16v^2} - u^2\right]\) - and the code at 219 uses that correct form (/ n, not / n**2). So two consecutive lines of one aligned block are off by \(n\) and the reader who checks them against the result is stopped. Second, 53 calls 55-61 “the following procedure” and it is not one: 55 and 56 are the same instruction written twice (“A random sample of \(n\) people is drawn with replacement from the population and each person is interviewed” / “Draw \(n\) random samples from the population with replacement and interview each person”), 60 is garbled (“If the spinner points to the group that the spinner belongs” - the subject belongs to a group, not the spinner), and 61 is an assumption rather than a step. 57 also has the spinner point to “the Letter A” while 48 puts people in “Group A” and 58-59 treat the spinner outcome as a group; the identification of Letter A with Group A is never stated. Third, \(\pi_A\) appears at 196, 254, 278, 282 and 300 and is never defined - 67 defines \(\pi\) as “True probability of A in the population” and the code calls the same quantity A (200), so one number has three names and only two of them are introduced. Fourth, 146 announces “From expressions {eq}`eq:five` and {eq}`eq:seven` we can deduce that:” and delivers a single-item list (148).

  • [qe-writing-005] (reviewer) — Use bold for definitions, italic for emphasis. Count: 3. Lines: 25, 21, 58. Example: there is not one italic span in the file - I checked every single-asterisk pair, and all five matches are multiplication signs inside code cells (220, 235, 237, 242) - so all ten emphasis spans in the lecture are bold, and four of them are emphasis rather than definition. 25 bolds two ordinary words to set up a contrast, “to protect the privacy of individual respondents to surveys while still estimating the fraction of a collection of individuals” - that is emphasis and wants italic. 21 bolds one word inside a term it is not defining: “These problems induce selection biases” (the term is “selection bias”, and the sentence is not its definition). 58 bolds a verb phrase for emphasis, “an outcome (A or B) that the interviewer does not observe”. The other six are correct definitions on first use - noise and signal (27), plausible deniability (31), differential privacy (33), random spinner (57), unbiased estimator (125) - so the rule is understood and only half applied.

  • [qe-writing-007] (reviewer) — Use visual elements to enhance understanding. Count: 4. Lines: 261, 130, 55, 29. Example: the whole results section is three pairs of bare DataFrame reprs (261-265, 267-270, 287-296, 305-314) and the lecture has no figure at all. Every comparative-statics claim it makes is a statement about how one number moves - 148 “The MSE of \(\hat{\pi}\) decreases as \(p\) increases”, 274 “especially when the bias is not small, the MSE of the randomized-sampling methods is smaller”, 276 “These differences become larger as \(p\) increases”, 316 “as \(n\) increases, the randomized response method does better … in more situations” - and each is left to be extracted by eye from a 12-row by 4-column table printed six times. One plot of the MSE ratio against \(p\), a curve per \((T_a, T_b)\) pair and a horizontal line at 1, carries all four claims at once. Second, 133 says {eq}`eq:seven` shows “the variance of \(\hat{\pi}\) can be represented as a sum of the variance due to sampling plus the variance due to the random device” - the lecture’s one conceptual result, and a two-band stacked plot against \(p\) in three lines of code; there is none. Third, Warner’s device is the canonical 2-by-2 diagram over (true group, spinner outcome) and 55-61 renders it as seven prose bullets, two duplicated and one garbled. Fourth, the file uses no admonitions anywhere (Admonitions is scored N/A), yet 29-31 - “Knowing about the structure of the noise assures the respondent that the survey maker does not observe his answer” and the plausible deniability it buys - is exactly a {note}.

Low severity#

None found.

Strengths#

  • The maximum-likelihood derivation is complete and every step of it is right: the likelihood {eq}`eq:one`, its log {eq}`eq:two`, the first-order condition at 89, the collapse to \(\pi p + (1-\pi)(1-p) = n_1/n\) at 95 and the estimator {eq}`eq:four` all check out, including the \(p \neq \tfrac12\) caveat at 98 that the division by \(2p-1\) requires.

  • The final variance expression at 121 is correct and non-obvious - substituting \(u = \pi - \tfrac12\) and \(v = p - \tfrac12\) turns \(Var(X_i)\) into \(\tfrac14 - 4u^2v^2\) and gives exactly \(\frac1n[\frac{1}{16v^2} - u^2]\) - and the intermediate line 120, \(\tfrac14 + (2p^2-2p+\tfrac12)(-2\pi^2+2\pi-\tfrac12)\), is the same identity written out, so the algebra is shown rather than asserted.

  • {eq}`eq:seven` (130) is the lecture’s real idea and it is derived rather than quoted: re-splitting the variance into a sampling term \(\frac{\frac14 - (\pi-\frac12)^2}{n}\) and a randomization term \(\frac{\frac{1}{16(p-\frac12)^2} - \frac14}{n}\) is exactly what justifies 133’s reading, and the two pieces do sum back to {eq}`eq:six`.

  • The theoretical formulas and the Monte Carlo are computed from the same class over the same twelve \((T_a, T_b)\) designs (203-206) and the same parameter grid, so 272’s “The theoretical calculations do a good job of predicting Monte Carlo results” is a claim the reader can check row by row rather than take on faith - and MCsimulation reuses one sample array across all four values of \(p\) (230, 234), which is the right way to make the comparison across \(p\) a paired one.

  • The randomized and non-randomized designs are compared on a single explicit scalar defined up front - “MSE Ratio = Mean Square Error Randomized / Mean Square Error Regular” (190) - and the definition is oriented so that values below one favour randomization, which is how 274 and 276 then read the tables.

  • 137-144 states the boundary cases before using the formulas - \(p = \tfrac12\) degenerates the likelihood to a constant, \(p \in \{0, 1\}\) collapses to the non-randomized estimator - and 142-144 restricts attention to \(p \in (\tfrac12, 1)\) while noting the symmetric case, so the domain of the whole analysis is pinned down.

  • 33-35 connects the 1965 construction to modern differential privacy, and 318-323 hands off to {doc}`util_rand_resp` with a specific reason (Ljungqvist’s utilitarian analysis of the alternatives), so the lecture places itself both historically and within the series.