13.9 Probability problem set, part a: axioms, Bayes, random variables, Box-Muller
Every random number in an LLM system comes from one generator, PCG32, and it feeds weight initialization, dropout, data shuffling, and sampling. Random variables, expectation, variance, and normal draws by Box-Muller turns its uniforms into normals, and Variance propagation and initialization chooses initialization scales so that activations neither explode nor vanish through 20 layers, which is an argument about variances. The token sampler of Sampling and logit processors in AI Engineering walks a cumulative distribution with a strict comparison that must agree bit for bit with a Rust port, and a gateway’s usage-policy classifier (Usage policy enforcement in AI Engineering) reports a precision that can only be interpreted with Bayes’ rule.
Concepts from other sections (15)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks. Read more: Chapter 2: Deep Learning in AI Engineering
- Random variable: a number assigned to each outcome of a random experiment; a discrete one is a table of values and probabilities. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller
- Normal distribution / Gaussian distribution: the bell-shaped density with mean and variance ; about 68%, 95%, and 99.7% of its mass lies within one, two, and three standard deviations of the mean, and the central limit theorem makes it the limit of sample means. Read more: Chapter 13: Probability and Statistics
- Inclusion-exclusion: counting a union by alternately adding and subtracting the sizes of intersections. Read more: 12.5 Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences
- Law of total probability: for a partition of the sample space; it computes a probability by adding up the mutually exclusive ways the event can happen, and it supplies the denominator in Bayes’ rule. Read more: Chapter 13: Probability and Statistics
- Standard normal: the distribution with density . Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller
- Sample space: the set of all possible outcomes of an experiment; an event is a subset of , and probability assigns each event a number between 0 and 1. Read more: Chapter 13: Probability and Statistics
- Probability mass function (PMF): for a discrete random variable, the function giving the probability of each value; the values are nonnegative and sum to 1. Read more: Chapter 13: Probability and Statistics
- Cumulative distribution function (CDF): , defined for every random variable, discrete or continuous; it rises from 0 to 1, and for a continuous variable its derivative is the density. Read more: Chapter 13: Probability and Statistics
- Exponential distribution: the waiting-time distribution with . Read more: 13.2 Categorical sampling: inverse CDF, Gumbel-max, alias method
- Standard error: , the estimated standard deviation of the sample mean. Read more: 13.5 LLN, CLT, confidence intervals, bootstrap
- Standard deviation: , the typical distance of a random variable from its mean, in the same units as the variable itself. Read more: Chapter 13: Probability and Statistics
- Polar coordinates: describing a point by its distance from the origin and angle , with and ; a polar curve sweeps area . Read more: Chapter 8: Calculus 2
- Variance propagation: tracking how carries a signal’s size from layer to layer. Read more: 13.4 Variance propagation and initialization
This problem set builds the vocabulary for all of that: the axioms, conditional probability, Bayes, discrete and continuous random variables, their moments, and the normal distribution. It is a pen-and-paper set with 29 exact answers (counting lettered parts) and one proof. The section first reviews the ideas and works two examples by hand; then come the problems, each followed by a collapsed worked answer with the final result, the derivation, and the common wrong answers and why they are wrong. Try each problem before opening its answer. Work it before reading the random-variables and initialization sections of this chapter; the later parts of the set, B, C, and D, follow the categorical-sampling, limit-theorem, and hypothesis-test sections.
You need counting and sets from the Discrete math 1 problem set, the probability and random-variable material in the chapter opening, and the Gaussian integral from the Calculus 2 problem set.
Learning objectives
- Derive inclusion-exclusion and conditional probability from the three axioms.
- Use Bayes’ rule and the law of total probability to turn a classifier’s per-class rates into its precision.
- Compute expectations and variances of discrete and continuous distributions, using linearity and the scaling rules.
- Sample any invertible distribution from one uniform, with the strict inverse-CDF comparison.
- Turn two uniforms into two standard normals by Box-Muller, in the order the generator spec fixes.
Notation
Section titled “Notation”The set uses these symbols.
| 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 |
The axioms and what follows
Section titled “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 (Problem 6). On a finite of equally likely outcomes, , which makes probability a counting problem.
Conditioning, independence, Bayes
Section titled “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.
Discrete random variables
Section titled “Discrete random variables”A random variable is a numeric function of the outcome, and its distribution is described by the mass function . Expectation is linear, , for any and , independent or not. Variance is with ; it satisfies , and for independent , . The distributions the set uses are these.
| Distribution | Values | ||
|---|---|---|---|
| Bernoulli() | for 1, for 0 | ||
| Binomial() | (a sum of Bernoullis) | ||
| Geometric() | derive it in Problem 9 | ||
| 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.
Continuous random variables and the normal
Section titled “Continuous random variables and the normal”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 can be inverted.
The standard normal has density , mean 0, and variance 1; the constant comes from the Gaussian integral (Problem 16). is normal with mean and variance . Its CDF has no closed form, so inverse-CDF sampling is awkward, and 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, and is kept as the spare for the next call.
Two examples by hand
Section titled “Two examples by hand”These are siblings of Problems 3 and 15, not problems of the set.
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 .
Box-Muller. Take and . Then and , so and the spare is . This is the bookkeeping the random-variables section relies on: two uniforms in, two normals out, first.
The problems
Section titled “The problems”Probabilities are exact fractions (, not 0.7), and lettered parts are answered separately. The tag after each problem says what kind of answer is expected: [number] an exact value, [expr in ...] a formula in the named variables, [bool] true or false, [proof] a written proof.
Axioms, conditional probability, and Bayes
Section titled “Axioms, conditional probability, and Bayes”The first six problems exercise the axioms and conditioning, ending with a proof of inclusion-exclusion.
Problem 1. Events and have , , and .
(a) . (b) . [number] (c) Are and independent? [bool]
Answer
(a) . (b) . (c) Yes. (a) Inclusion-exclusion (Problem 6): . A common mistake is , which adds and counts the overlap twice. (b) . A common mistake is , which is : the conditioning event goes in the denominator. (c) , so they are independent; equivalently .
Problem 2. Roll two fair six-sided dice. (a) . (b) . (c) . [number]
Answer
(a) . (b) . (c) . The 36 ordered pairs are equally likely. (a) Six pairs sum to 7, : . A common mistake is , treating the 11 possible sums as equally likely; they are not (one way to make 2, six ways to make 7). (b) The complement is “no 6 on either die”, , so . Counting directly: 6 pairs with a 6 first, 6 with a 6 second, minus counted twice. A common mistake is , which double-counts . (c) Conditioning shrinks the sample space to the 11 pairs with a 6; of those, and sum to 7: . Common mistakes: ignores the condition, and is the joint probability , not the conditional.
Problem 3. The gateway’s usage-policy classifier 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]
Answer
(a) . (b) . (a) Total probability over violate / allowed: . A common mistake is , which counts only the true positives and forgets that 99% of traffic is allowed, so 5% of it is a much larger pool of flags. (b) Bayes: . In 10 000 requests: 100 violate and 90 are flagged; 9900 are allowed and 495 are flagged; of 585 flags, 90 are right. The common mistake is , the sensitivity quoted as if it were the precision; the safety report of Bias and safety evals in AI Engineering must quote precision, which the base rate drags down.
Problem 4. Draw 3 bytes independently and uniformly from the 256 byte values. What is the probability that all three are different? [number]
Answer
. The first byte is anything; the second avoids one value, the third two: . This is the birthday computation over the byte alphabet. A common mistake is : the union bound says , which is an inequality, not the exact answer ( is slightly too small because the three pair-collision events overlap).
Problem 5. 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]
Answer
(a) . (b) . (a) Total probability: . A common mistake is , the biased coin’s rate, as if it were always picked. (b) Bayes: . A common mistake is , the joint probability, without dividing by . Heads is evidence for the biased coin, so the posterior rises above the prior .
Problem 6. From the three axioms (; ; of a countable union of pairwise disjoint events is the sum of their probabilities) prove for any events and . [proof]
Answer (model proof)
Claim. For any events , .
Method. Split each set into disjoint pieces and apply the third axiom. A finite union is a countable one padded with empty sets, and follows from additivity applied to together with . Write .
- , and and are disjoint (an element of is not in ). By additivity, .
- , and these two are disjoint (one lies inside , the other outside it). By additivity, , so .
- Substituting step 2 into step 1: .
Every step used only set identities and the additivity axiom. A complete proof must (i) write both and as disjoint unions, (ii) apply additivity to each, (iii) eliminate , and (iv) name each step. A common gap is to “subtract the overlap because it was counted twice”, which is the intuition, not a proof from the axioms. The model proof is also in course/solutions/solve/S-M07a/q6.md.
Discrete random variables
Section titled “Discrete random variables”The next five problems compute means and variances of the standard discrete distributions and walk the inverse CDF of a categorical.
Problem 7. is a fair die roll, uniform on . (a) . (b) . (c) . [number]
Answer
(a) . (b) . (c) . (a) ; a common slip is 3, which is not the midpoint of . (b) . A common mistake is : the mean of the squares is not the square of the mean. (c) , matching the two-pass computation in Random variables, expectation, variance, and normal draws by Box-Muller. Common mistakes: , the sum of squared deviations not divided by 6; and , that sum divided by , which is the sample variance formula, not the variance of a known distribution.
Problem 8. (a) Give for . [expr in p] (b) Give for , the number of successes in independent Bernoulli() trials. [expr in n, p]
Answer
(a) . (b) . (a) for a 0/1 variable, so and . Common mistakes: (the mean) and (the squared mean). (b) with independent , and variances of independent sums add: . A common mistake is , which is : one trial scaled by , perfectly correlated, not independent trials. The difference is why the standard error of an accuracy shrinks like (LLN, CLT, confidence intervals, bootstrap, and the evaluation harness of The evaluation harness and the model zoo).
Problem 9. counts independent Bernoulli() trials up to and including the first success, so . Give . [expr in p]
Answer
. First-step analysis: one trial is always made; with probability it fails and the process starts afresh, so , giving . Directly: , using . A common mistake is , the expected number of failures before the first success; it differs by the success trial itself.
Problem 10. 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]
Answer
(a) . (b) . (c) . (a) . A common mistake is , the plain mean of the ids, which ignores . (b) The running totals are ; and fail, holds: id 2. (c) fails and fails (strict), holds: id 2. Id 1 owns , half open, so its right end belongs to id 2. The common mistake is id 1, from ; then a Python and a Rust sampler that disagree on the comparison return different tokens at boundaries. (In float64 the running total is , slightly above the double , so a float sampler returns id 1 here; the question asks for the exact-arithmetic rule, and the spec fixes the float operations so every port rounds the same way.)
Problem 11. has . Give . [expr in lam]
Answer
. Set : . This is also the probability that an exponential waiting time with rate exceeds 1, which is how Poisson arrivals (in Categorical sampling) and the exponential connect. A common mistake is , which is .
Continuous random variables, the normal, and Box-Muller
Section titled “Continuous random variables, the normal, and Box-Muller”The last five problems move to densities: the uniform and exponential, scaling a normal, Box-Muller, and the Gaussian integral that normalizes it all.
Problem 12. is uniform on . (a) . (b) . [number]
Answer
(a) . (b) . (a) . (b) , so . A common mistake is , which is without subtracting the squared mean. Scaling gives , the fact behind Xavier-uniform’s bound in Variance propagation and initialization.
Problem 13. has CDF for .
(a) Give the inverse-CDF sampler: the with . [expr in u, lam] (b) Give . [expr in lam]
Answer
(a) . (b) . (a) . This is exponential_icdf from Categorical sampling. Common mistakes: , multiplying by the rate (a higher rate must mean shorter waits), and , the survival function evaluated at , not its inverse. (b) . A common mistake is : the rate is events per unit time, and the mean wait is its reciprocal.
Problem 14. is a standard normal and are constants. Give . [expr in a, b]
Answer
. , since and . normal_init(std) in Variance propagation and initialization is , with variance . Common mistakes: (a shift does not spread the distribution) and , which confuses the variance with the standard deviation .
Problem 15. 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]
Answer
(a) . (b) . and . (a) . (b) . Common mistakes for (a): , from swapping sine and cosine (the spec emits the cosine first, and every port must agree); and , from forgetting the radius. For (b), is the same swap.
Problem 16. Give , the constant that normalizes the standard normal density. [number]
Answer
. Square the integral and switch to polar coordinates: , so . The same polar picture, run backwards, is Box-Muller (see “Continuous random variables and the normal” above). Common mistakes: , which is (no factor in the exponent), and , which is the integral of the already normalized density.
What can go wrong
Section titled “What can go wrong”The wrong answers in this set cluster around a handful of habits, and each has a downstream cost. Adding the probabilities of overlapping events can push above 1 for large events; the fix is to subtract the overlap (Problems 1a and 2b). Confusing with quotes a classifier’s recall as its precision; Bayes with the base rate in the numerator and below gets it right (Problems 3b and 5b). Treating unequal outcomes as equally likely gives each of the 11 dice sums probability 1/11; counting the 36 equally likely ordered pairs does not (Problem 2a).
Warning: Computing variance as , or with in a population formula, puts initialization scales off by the squared mean. Use over the known distribution (Problems 7c and 12b). Likewise, a shift never changes variance, (Problem 14), so a
normal_initwith a nonzero mean would break the variance argument of the initialization section.
The two sampling pitfalls matter for cross-language parity. Using instead of in the inverse CDF makes a Python and a Rust sampler disagree at boundaries; half-open intervals send to id 2 (Problem 10c). Swapping sine and cosine, or forgetting the radius, makes normals differ from spec/pcg32.md; the spec has , with in Problem 15.
Where this leads
Section titled “Where this leads”The counting behind Problems 2 and 4 comes from the Discrete math 1 problem set. Next in this chapter, Random variables, expectation, variance, and normal draws by Box-Muller builds normal, expectation, and variance over PCG32, checked by sample moments and a chi-square test; Variance propagation and initialization uses variance propagation to pick the xavier_* and kaiming_normal scales; and Categorical sampling: inverse CDF, Gumbel-max, alias method samples categoricals three ways, followed by problem set B. Further on, the sampler of Sampling and logit processors in AI Engineering uses the inverse-CDF step with the strict comparison of Problem 10, and Usage policy enforcement in AI Engineering reports the policy head’s precision and recall, which you now read with Problem 3.
Further reading
Section titled “Further reading”- 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 terms
Section titled “Key terms”- Probability axioms: non-negativity, , and countable additivity over disjoint events; every other rule is derived from them.
- Conditional probability: , probability restricted to and renormalized.
- Bayes’ rule: , which inverts a conditional using the base rate.
- Precision: , the fraction of flags that are right; unlike sensitivity it depends on the base rate.
- Variance: ; it scales by and adds over independent sums.
- Inverse-CDF sampling: , which turns one uniform into a draw from .
- Box-Muller: the transform , that turns two uniforms into two independent standard normals.
Summary
Section titled “Summary”- Probability is a function on events with three axioms; inclusion-exclusion and conditioning follow from them (Problems 1 and 6).
- 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 (Problem 3).
- Expectation is linear and variance is ; variances of independent sums add, and a scale multiplies variance by (Problems 7, 8, and 14).
- 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 (Problems 10 and 13).
- Box-Muller turns two uniforms into two independent standard normals, cosine first, sine as the spare (Problem 15).