Skip to content

Probability problem set, part a: axioms, Bayes, random variables, Box-Muller

ModuleS-M07a · solve · none · Pass 2 · 4 to 5 h
You buildanswers in solve/S-M07a.toml (29 checked by SymPy) and 1 proof in solve/S-M07a/q6.md (self-graded against its rubric)
Contractnone: a pen and paper set
Testscourse/solve/S-M07a/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M07a/problems.md and in section 4
NeedsS-M05 (counting, sets). Reading: the Probability and Statistics topic, probability and random variable sections, and the Gaussian integral of S-M02
Used byno call site (a solve set). Do it before M07.0 (random variables, the normal, and Box-Muller over PCG32) and M07.3 (variance propagation for initialization); later parts S-M07b to S-M07d follow M07.1, M07.4, and M07.5
MilestoneMS-P2 (the Pass 2 gate runs ol check on every solve part of the pass)
Optional depthBlitzstein and Hwang, Introduction to Probability (free), ch. 1 to 5; Box and Muller, “A Note on the Generation of Random Normal Deviates” (1958)
  • Probability is a function on events with three axioms; inclusion-exclusion and conditioning follow from them (q1, q6).
  • Bayes’ rule turns a classifier’s per-class rates into the probability a flag is right, and with a 1% base rate a 90%-sensitive classifier is right only 2 times in 13 (q3).
  • Expectation is linear and variance is E[X2]−E[X]2E[X^2] - E[X]^2; variances of independent sums add, and a scale aa multiplies variance by a2a^2 (q7, q8, q14).
  • An inverse CDF turns one uniform into any distribution, and its comparison is strict: u<cu < c, so a uu landing exactly on a boundary goes to the next id (q10, q13).
  • Box-Muller turns two uniforms into two independent standard normals, cosine first, sine as the spare (q15).
Terminal window
ol start S-M07a # writes solve/S-M07a.toml and the proof file
ol check S-M07a # SymPy checks the answers, then asks the proof rubric (y/n)
ol check S-M07a --regrade # ask the rubric again after you change the proof

Every random number in your system will come from one generator, PCG32 (M06.3), and from Pass 2 on it feeds weight initialization, dropout, data shuffling, and sampling. M07.0 turns its uniforms into normals with Box-Muller, and M07.3 chooses initialization scales so that activations neither explode nor vanish through 20 layers, which is an argument about variances. The sampler you write in L8.1 walks a cumulative distribution with a strict comparison that must agree bit for bit with your Rust port. Your gateway’s usage-policy classifier (gw.08) will report a precision you can only interpret with Bayes’ rule. This set gives you the vocabulary for all of it: the axioms, conditional probability, Bayes, discrete and continuous random variables, their moments, and the normal distribution.

SymbolMeaningType / shape
Ω\Omegasample space: the set of possible outcomesset
A,B⊆ΩA, B \subseteq \Omegaeventssets
P(A)P(A)probability of AAreal in [0,1][0, 1]
P(A∣B)P(A \mid B)conditional probability, P(A∩B)/P(B)P(A \cap B)/P(B) for P(B)>0P(B) > 0real
X,YX, Yrandom variables: functions from Ω\Omega to R\mathbb{R}
pX(k)p_X(k)probability mass function, P(X=k)P(X = k)real
fX(x),FX(x)f_X(x), F_X(x)density and cumulative distribution function, FX(x)=P(X≤x)F_X(x) = P(X \le x)real
E[X]E[X]expectation, ∑kk pX(k)\sum_k k\, p_X(k) or ∫xfX(x) dx\int x f_X(x)\, dxreal
Var⁡(X)\operatorname{Var}(X)variance, E[(X−E[X])2]E[(X - E[X])^2]real
UUa uniform random variable on [0,1)[0, 1)real
ZZa standard normal, density φ(z)=e−z2/2/2π\varphi(z) = e^{-z^2/2}/\sqrt{2\pi}real
λ\lambda (lam)rate of a Poisson or exponential distributionpositive real

A probability assigns each event A⊆ΩA \subseteq \Omega a number P(A)P(A) such that P(A)≥0P(A) \ge 0, P(Ω)=1P(\Omega) = 1, and the probability of a countable union of pairwise disjoint events is the sum of their probabilities. Everything else is derived: P(∅)=0P(\emptyset) = 0, P(Ac)=1−P(A)P(A^c) = 1 - P(A), and inclusion-exclusion P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B) (q6). On a finite Ω\Omega of equally likely outcomes, P(A)=∣A∣/∣Ω∣P(A) = \lvert A \rvert / \lvert \Omega \rvert, which makes probability a counting problem (S-M05).

Conditional probability P(A∣B)=P(A∩B)/P(B)P(A \mid B) = P(A \cap B)/P(B) restricts the sample space to BB and renormalizes. Events are independent when P(A∩B)=P(A)P(B)P(A \cap B) = P(A)P(B), equivalently P(A∣B)=P(A)P(A \mid B) = P(A). The law of total probability splits on a partition B1,…,BkB_1, \dots, B_k of Ω\Omega: P(A)=∑iP(A∣Bi)P(Bi)P(A) = \sum_i P(A \mid B_i) P(B_i). Bayes’ rule inverts a conditional: P(B∣A)=P(A∣B) P(B)P(A).P(B \mid A) = \frac{P(A \mid B)\,P(B)}{P(A)}. For a classifier, P(flag∣bad)P(\text{flag} \mid \text{bad}) is its sensitivity (recall), P(flag∣ok)P(\text{flag} \mid \text{ok}) its false positive rate, and P(bad∣flag)P(\text{bad} \mid \text{flag}) its precision, which depends on the base rate P(bad)P(\text{bad}) as much as on the classifier.

A random variable XX is a numeric function of the outcome. Its distribution is described by the mass function pX(k)=P(X=k)p_X(k) = P(X = k). Expectation E[X]=∑kk pX(k)E[X] = \sum_k k\, p_X(k) is linear, E[aX+bY]=aE[X]+bE[Y]E[aX + bY] = aE[X] + bE[Y], for any XX and YY, independent or not. Variance Var⁡(X)=E[(X−μ)2]=E[X2]−μ2\operatorname{Var}(X) = E[(X - \mu)^2] = E[X^2] - \mu^2 with μ=E[X]\mu = E[X]; it satisfies Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X), and for independent X,YX, Y, Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y).

DistributionValuesP(X=k)P(X = k)E[X]E[X]
Bernoulli(pp){0,1}\{0, 1\}pp for 1, 1−p1 - p for 0pp
Binomial(n,pn, p){0,…,n}\{0, \dots, n\}(nk)pk(1−p)n−k\binom{n}{k} p^k (1-p)^{n-k}npnp (a sum of nn Bernoullis)
Geometric(pp){1,2,… }\{1, 2, \dots\}(1−p)k−1p(1-p)^{k-1} pderive it in q9
Poisson(λ\lambda){0,1,… }\{0, 1, \dots\}e−λλk/k!e^{-\lambda} \lambda^k / k!λ\lambda
Categorical(qq){0,…,V−1}\{0, \dots, V-1\}qkq_k∑kk qk\sum_k k\, q_k

A language model’s next-token distribution is a categorical over the vocabulary. To sample it from one uniform uu, walk the ids in order with a running total cc and return the first id with u<cu < c (spec/sampling.md step 11). The id returned is kk exactly when q0+⋯+qk−1≤u<q0+⋯+qkq_0 + \dots + q_{k-1} \le u < q_0 + \dots + q_k, an interval of length qkq_k, so each id comes up with its probability.

A continuous XX has a density fX≥0f_X \ge 0 with ∫fX=1\int f_X = 1, and P(a≤X≤b)=∫abfX(x) dxP(a \le X \le b) = \int_a^b f_X(x)\,dx; its CDF is FX(x)=∫−∞xfXF_X(x) = \int_{-\infty}^x f_X. Expectation becomes an integral, E[g(X)]=∫g(x)fX(x) dxE[g(X)] = \int g(x) f_X(x)\,dx, with the same linearity and variance rules. The uniform on [0,1][0, 1] has f=1f = 1 there. The exponential with rate λ\lambda has F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} for x≥0x \ge 0. Inverse-CDF sampling sets X=F−1(U)X = F^{-1}(U): then P(X≤x)=P(U≤F(x))=F(x)P(X \le x) = P(U \le F(x)) = F(x), so one uniform becomes a draw from any distribution whose CDF you can invert.

The standard normal ZZ has density φ(z)=e−z2/2/2π\varphi(z) = e^{-z^2/2}/\sqrt{2\pi}, mean 0, and variance 1; the constant comes from the Gaussian integral (q16). X=μ+σZX = \mu + \sigma Z is normal with mean μ\mu and variance σ2\sigma^2. Its CDF has no closed form, so inverse-CDF sampling is awkward; Box-Muller sidesteps it. For two independent standard normals, the squared radius R2=Z02+Z12R^2 = Z_0^2 + Z_1^2 is exponential with rate 1/21/2 and the angle is uniform on [0,2π)[0, 2\pi), independent of it. Reversing that: draw u1,u2u_1, u_2, set r=−2ln⁡(1−u1)r = \sqrt{-2 \ln(1 - u_1)} (inverse-CDF sampling of the exponential, with 1−u1>01 - u_1 > 0 so the log is finite) and θ=2πu2\theta = 2\pi u_2, and return z0=rcos⁡θz_0 = r\cos\theta and z1=rsin⁡θz_1 = r \sin\theta. spec/pcg32.md fixes the order: z0z_0 is returned, z1z_1 is kept as the spare for the next call.

This is a sibling of q3 and q15, not one of the graded problems.

Bayes. A spam filter catches 80% of spam and wrongly flags 2% of good mail, and 10% of mail is spam. What fraction of flagged mail is spam?

Take 1000 messages: 100 spam, 900 good. Flagged spam: 0.8⋅100=800.8 \cdot 100 = 80. Flagged good: 0.02⋅900=180.02 \cdot 900 = 18. Flagged in total: 9898. So P(spam∣flag)=80/98=40/49≈0.816P(\text{spam} \mid \text{flag}) = 80/98 = 40/49 \approx 0.816. The same through the formula: P(flag)=0.8⋅0.1+0.02⋅0.9=0.098P(\text{flag}) = 0.8 \cdot 0.1 + 0.02 \cdot 0.9 = 0.098, and P(spam∣flag)=0.08/0.098=40/49P(\text{spam} \mid \text{flag}) = 0.08 / 0.098 = 40/49. In solve/ this is answer = "40/49"; answer = "0.08/0.098" fails as inexact.

Box-Muller. Take 1−u1=e−1/21 - u_1 = e^{-1/2} and u2=1/4u_2 = 1/4. Then r=−2⋅(−1/2)=1r = \sqrt{-2 \cdot (-1/2)} = 1 and θ=2π/4=π/2\theta = 2\pi/4 = \pi/2, so z0=cos⁡(π/2)=0z_0 = \cos(\pi/2) = 0 and the spare is z1=sin⁡(π/2)=1z_1 = \sin(\pi/2) = 1. Check the bookkeeping that M07.0 tests: two uniforms in, two normals out, z0z_0 first.

Write each answer in solve/S-M07a.toml; lettered parts are their own tables:

[q1.a]
answer = "7/10"
[q8.a]
answer = "p*(1 - p)"
[q13.a]
answer = "-log(1 - u)/lam"
[q6]
proof = "S-M07a/q6.md"

Probabilities are exact fractions; 0.7 fails a question marked exact. Write λ\lambda as lam, ee as E or exp(1), and π\pi as pi.

Axioms, conditional probability, and Bayes

Section titled “Axioms, conditional probability, and Bayes”

q1. Events AA and BB have P(A)=1/2P(A) = 1/2, P(B)=2/5P(B) = 2/5, and P(A∩B)=1/5P(A \cap B) = 1/5. (a) P(A∪B)P(A \cup B). (b) P(A∣B)P(A \mid B). [number] (c) Are AA and BB independent? [bool]

q2. Roll two fair six-sided dice. (a) P(the sum is 7)P(\text{the sum is } 7). (b) P(at least one die shows 6)P(\text{at least one die shows } 6). (c) P(the sum is 7∣at least one die shows 6)P(\text{the sum is } 7 \mid \text{at least one die shows } 6). [number]

q3. Your gateway’s usage-policy classifier (gw.08) flags 90% of violating requests and 5% of allowed ones, and 1% of requests violate the policy. (a) What fraction of all requests is flagged? (b) Given that a request is flagged, what is the probability that it violates the policy? [number]

q4. Draw 3 bytes independently and uniformly from the 256 byte values. What is the probability that all three are different? [number]

q5. A bag holds a fair coin and a coin that lands heads with probability 3/43/4. Pick one of the two uniformly at random and flip it. (a) P(heads)P(\text{heads}). (b) P(the biased coin was picked∣heads)P(\text{the biased coin was picked} \mid \text{heads}). [number]

q6. From the three axioms (P(E)≥0P(E) \ge 0; P(Ω)=1P(\Omega) = 1; PP of a countable union of pairwise disjoint events is the sum of their probabilities) prove P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B) for any events AA and BB. [proof]

q7. XX is a fair die roll, uniform on {1,…,6}\{1, \dots, 6\}. (a) E[X]E[X]. (b) E[X2]E[X^2]. (c) Var⁡(X)\operatorname{Var}(X). [number]

q8. (a) Give Var⁡(X)\operatorname{Var}(X) for X∼Bernoulli(p)X \sim \mathrm{Bernoulli}(p). [expr in p] (b) Give Var⁡(Y)\operatorname{Var}(Y) for Y∼Binomial(n,p)Y \sim \mathrm{Binomial}(n, p), the number of successes in nn independent Bernoulli(pp) trials. [expr in n, p]

q9. XX counts independent Bernoulli(pp) trials up to and including the first success, so X∈{1,2,3,… }X \in \{1, 2, 3, \dots\}. Give E[X]E[X]. [expr in p]

q10. A sampler draws a token id from the categorical distribution q=(1/10,2/10,3/10,4/10)q = (1/10, 2/10, 3/10, 4/10) over ids 0,1,2,30, 1, 2, 3. (a) Give E[id]E[\text{id}]. The sampler then uses the inverse CDF of spec/sampling.md: walk the ids in ascending order adding qiq_i to a running total cc, and return the first id with u<cu < c. Which id does it return for (b) u=0.35u = 0.35 and (c) u=0.3u = 0.3 (exactly)? [number]

q11. X∼Poisson(λ)X \sim \mathrm{Poisson}(\lambda) has P(X=k)=e−λλk/k!P(X = k) = e^{-\lambda} \lambda^k / k!. Give P(X=0)P(X = 0). [expr in lam]

Continuous random variables, the normal, and Box-Muller

Section titled “Continuous random variables, the normal, and Box-Muller”

q12. UU is uniform on [0,1][0, 1]. (a) E[U]E[U]. (b) Var⁡(U)\operatorname{Var}(U). [number]

q13. X∼Exponential(λ)X \sim \mathrm{Exponential}(\lambda) has CDF F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} for x≥0x \ge 0. (a) Give the inverse-CDF sampler: the xx with F(x)=uF(x) = u. [expr in u, lam] (b) Give E[X]E[X]. [expr in lam]

q14. ZZ is a standard normal and a,ba, b are constants. Give Var⁡(aZ+b)\operatorname{Var}(aZ + b). [expr in a, b]

q15. spec/pcg32.md turns two uniforms into two normals by Box-Muller: r=−2ln⁡(1−u1)r = \sqrt{-2 \ln(1 - u_1)}, then z0=rcos⁡(2πu2)z_0 = r \cos(2\pi u_2) is returned and z1=rsin⁡(2πu2)z_1 = r \sin(2\pi u_2) is kept as the spare. For u1u_1 with 1−u1=e−21 - u_1 = e^{-2} and u2=1/12u_2 = 1/12, give (a) z0z_0 and (b) z1z_1. [number]

q16. Give ∫−∞∞e−x2/2 dx\int_{-\infty}^{\infty} e^{-x^2/2}\, dx, the constant that normalizes the standard normal density. [number]

PitfallSymptomCaught by
Adding probabilities of overlapping eventsP(A∪B)>1P(A \cup B) > 1 for large eventsq1 (canary 9/10), q2 (canary 1/3)
Confusing P(A∣B)P(A \mid B) with P(B∣A)P(B \mid A)a classifier’s precision quoted as its recallq3 (canary 9/10)
Treating unequal outcomes as equally likelythe 11 dice sums given probability 1/11 eachq2 (canary 1/11)
Variance as E[X2]E[X^2], or with n−1n - 1 in a population formulainitialization scales off by the squared meanq7 (canaries 49/4 and 7/2), q12 (canary 1/3)
A shift changing variancenormal_init with a nonzero mean breaks M07.3’s variance argumentq14 (canary a^2 + b)
u≤cu \le c instead of u<cu < c in the inverse CDFa Python and a Rust sampler disagree at boundariesq10 (canary 1)
Swapping sine and cosine, or forgetting the radiusnormals differ from spec/pcg32.mdq15 (canaries 1 and sqrt(3)/2)
DirectionModuleHow it uses this
BackS-M05counting finite sample spaces (q2, q4)
ForwardM07.0normal, expectation, variance over PCG32, checked by sample moments and a chi-square test
ForwardM07.3variance propagation picks xavier_* and kaiming_normal scales
ForwardM07.1inverse CDF, Gumbel-max, and alias sampling of categoricals (S-M07b follows it)
ForwardL8.1the sampler’s inverse-CDF step with the strict comparison of q10
Forwardgw.08the policy head’s precision and recall, read with q3