Probability problem set, part a: axioms, Bayes, random variables, Box-Muller
Overview
Section titled “Overview”| Module | S-M07a · solve · none · Pass 2 · 4 to 5 h |
| You build | answers in solve/S-M07a.toml (29 checked by SymPy) and 1 proof in solve/S-M07a/q6.md (self-graded against its rubric) |
| Contract | none: a pen and paper set |
| Tests | course/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 |
| Needs | S-M05 (counting, sets). Reading: the Probability and Statistics topic, probability and random variable sections, and the Gaussian integral of S-M02 |
| Used by | no 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 |
| Milestone | MS-P2 (the Pass 2 gate runs ol check on every solve part of the pass) |
| Optional depth | Blitzstein and Hwang, Introduction to Probability (free), ch. 1 to 5; Box and Muller, “A Note on the Generation of Random Normal Deviates” (1958) |
Key Takeaways
Section titled “Key Takeaways”- 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 ; variances of independent sums add, and a scale multiplies variance by (q7, q8, q14).
- An inverse CDF turns one uniform into any distribution, and its comparison is strict: , so a 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).
How to work this chapter
Section titled “How to work this chapter”ol start S-M07a # writes solve/S-M07a.toml and the proof fileol 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 proof1. Why now
Section titled “1. Why now”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.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| sample space: the set of possible outcomes | set | |
| events | sets | |
| probability of | real in | |
| conditional probability, for | real | |
| random variables: functions from to | ||
| probability mass function, | real | |
| density and cumulative distribution function, | real | |
| expectation, or | real | |
| variance, | real | |
| a uniform random variable on | real | |
| a standard normal, density | real | |
(lam) | rate of a Poisson or exponential distribution | positive real |
2.1 The axioms and what follows
Section titled “2.1 The axioms and what follows”A probability assigns each event a number such that , , and the probability of a countable union of pairwise disjoint events is the sum of their probabilities. Everything else is derived: , , and inclusion-exclusion (q6). On a finite of equally likely outcomes, , which makes probability a counting problem (S-M05).
2.2 Conditioning, independence, Bayes
Section titled “2.2 Conditioning, independence, Bayes”Conditional probability restricts the sample space to and renormalizes. Events are independent when , equivalently . The law of total probability splits on a partition of : . Bayes’ rule inverts a conditional: For a classifier, is its sensitivity (recall), its false positive rate, and its precision, which depends on the base rate as much as on the classifier.
2.3 Discrete random variables
Section titled “2.3 Discrete random variables”A random variable is a numeric function of the outcome. Its distribution is described by the mass function . Expectation is linear, , for any and , independent or not. Variance with ; it satisfies , and for independent , .
| Distribution | Values | ||
|---|---|---|---|
| Bernoulli() | for 1, for 0 | ||
| Binomial() | (a sum of Bernoullis) | ||
| Geometric() | derive it in q9 | ||
| Poisson() | |||
| Categorical() |
A language model’s next-token distribution is a categorical over the vocabulary. To sample it from one uniform , walk the ids in order with a running total and return the first id with (spec/sampling.md step 11). The id returned is exactly when , an interval of length , so each id comes up with its probability.
2.4 Continuous random variables
Section titled “2.4 Continuous random variables”A continuous has a density with , and ; its CDF is . Expectation becomes an integral, , with the same linearity and variance rules. The uniform on has there. The exponential with rate has for . Inverse-CDF sampling sets : then , so one uniform becomes a draw from any distribution whose CDF you can invert.
The standard normal has density , mean 0, and variance 1; the constant comes from the Gaussian integral (q16). is normal with mean and variance . Its CDF has no closed form, so inverse-CDF sampling is awkward; Box-Muller sidesteps it. For two independent standard normals, the squared radius is exponential with rate and the angle is uniform on , independent of it. Reversing that: draw , set (inverse-CDF sampling of the exponential, with so the log is finite) and , and return and . spec/pcg32.md fixes the order: is returned, is kept as the spare for the next call.
3. Worked example by hand
Section titled “3. Worked example by hand”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: . Flagged good: . Flagged in total: . So . The same through the formula: , and . In solve/ this is answer = "40/49"; answer = "0.08/0.098" fails as inexact.
Box-Muller. Take and . Then and , so and the spare is . Check the bookkeeping that M07.0 tests: two uniforms in, two normals out, first.
4. The problem set
Section titled “4. The problem set”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 as lam, as E or exp(1), and as pi.
Axioms, conditional probability, and Bayes
Section titled “Axioms, conditional probability, and Bayes”q1. Events and have , , and .
(a) . (b) . [number] (c) Are and independent? [bool]
q2. Roll two fair six-sided dice. (a) . (b) . (c) . [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 . Pick one of the two uniformly at random and flip it. (a) . (b) . [number]
q6. From the three axioms (; ; of a countable union of pairwise disjoint events is the sum of their probabilities) prove for any events and . [proof]
Discrete random variables
Section titled “Discrete random variables”q7. is a fair die roll, uniform on . (a) . (b) . (c) . [number]
q8. (a) Give for . [expr in p] (b) Give for , the number of successes in independent Bernoulli() trials. [expr in n, p]
q9. counts independent Bernoulli() trials up to and including the first success, so . Give . [expr in p]
q10. A sampler draws a token id from the categorical distribution over ids . (a) Give . The sampler then uses the inverse CDF of spec/sampling.md: walk the ids in ascending order adding to a running total , and return the first id with . Which id does it return for (b) and (c) (exactly)? [number]
q11. has . Give . [expr in lam]
Continuous random variables, the normal, and Box-Muller
Section titled “Continuous random variables, the normal, and Box-Muller”q12. is uniform on . (a) . (b) . [number]
q13. has CDF for .
(a) Give the inverse-CDF sampler: the with . [expr in u, lam] (b) Give . [expr in lam]
q14. is a standard normal and are constants. Give . [expr in a, b]
q15. spec/pcg32.md turns two uniforms into two normals by Box-Muller: , then is returned and is kept as the spare. For with and , give (a) and (b) . [number]
q16. Give , the constant that normalizes the standard normal density. [number]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Adding probabilities of overlapping events | for large events | q1 (canary 9/10), q2 (canary 1/3) |
| Confusing with | a classifier’s precision quoted as its recall | q3 (canary 9/10) |
| Treating unequal outcomes as equally likely | the 11 dice sums given probability 1/11 each | q2 (canary 1/11) |
| Variance as , or with in a population formula | initialization scales off by the squared mean | q7 (canaries 49/4 and 7/2), q12 (canary 1/3) |
| A shift changing variance | normal_init with a nonzero mean breaks M07.3’s variance argument | q14 (canary a^2 + b) |
| instead of in the inverse CDF | a Python and a Rust sampler disagree at boundaries | q10 (canary 1) |
| Swapping sine and cosine, or forgetting the radius | normals differ from spec/pcg32.md | q15 (canaries 1 and sqrt(3)/2) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M05 | counting finite sample spaces (q2, q4) |
| Forward | M07.0 | normal, expectation, variance over PCG32, checked by sample moments and a chi-square test |
| Forward | M07.3 | variance propagation picks xavier_* and kaiming_normal scales |
| Forward | M07.1 | inverse CDF, Gumbel-max, and alias sampling of categoricals (S-M07b follows it) |
| Forward | L8.1 | the sampler’s inverse-CDF step with the strict comparison of q10 |
| Forward | gw.08 | the policy head’s precision and recall, read with q3 |