13.10 Probability problem set, part b: joint distributions, covariance, MLE
Once a system has text, it turns counts into probabilities everywhere. MLE, Laplace, absolute discounting estimates a categorical distribution from token counts and smooths it so unseen words keep some mass. The Unigram language model tokenizer in AI Engineering trains a tokenizer whose training data never says which segmentation produced a word, so it re-estimates piece probabilities from expected counts, again and again: that is EM. The statistical language models of n-gram language model with interpolated modified Kneser-Ney are tables of conditional distributions over pairs of tokens, which is a joint distribution read one row at a time. And when you evaluate, per-token losses on one document are correlated, so the variance of their mean is not .
Concepts from other sections (8)
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.
- Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller
- Maximum-likelihood estimate (MLE): the distribution that maximizes the log-likelihood; for counts, the frequency . Read more: 13.3 MLE, Laplace, absolute discounting
- Log-likelihood: , the log probability of the observed counts under a distribution . Read more: 13.3 MLE, Laplace, absolute discounting
- 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
- bigram model: a language model that predicts the next token from the current token alone, stored as a table. Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- Forward-backward: the log-sum-exp recursions and that give each edge’s posterior probability. Read more: 7.4 The Unigram language model tokenizer in AI Engineering
- Law of large numbers: the sample mean converges to the true mean as grows. Read more: 13.5 LLN, CLT, confidence intervals, bootstrap
- Confidence interval: a procedure that, over repeated samples, produces intervals covering the fixed true parameter at the stated rate. Read more: 13.11 Probability problem set, part c: limit theorems and confidence intervals
This problem set covers the tools behind all of that: joint and conditional distributions, covariance, and maximum likelihood, including one EM step of the kind the Unigram trainer runs. It is a pen-and-paper set with 15 exact answers (counting lettered parts) and one proof. A short review and two examples by hand come first; then 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. Read it alongside MLE, Laplace, absolute discounting and before the Unigram tokenizer; problem set C follows the limit-theorems section.
The set builds on problem set A (axioms, conditioning, expectation, and variance of one variable) and on the joint-distribution and estimation material in the chapter opening.
Learning objectives
- Read marginals, conditionals, and independence off a joint table.
- Compute covariance and use it in the variance of a linear combination, and explain why zero covariance is not independence.
- Derive maximum-likelihood estimates for Bernoulli, exponential, and categorical models, and apply Laplace smoothing.
- Run one EM step by hand: expected counts from posteriors, then the categorical MLE.
- Prove that count over total maximizes the categorical log-likelihood.
Notation
Section titled “Notation”The set uses these symbols.
| Symbol | Meaning | Type / shape |
|---|---|---|
| joint mass function, | real | |
| marginal of | real | |
| conditional, for | real | |
| covariance, | real | |
(vx) | variance of , | real |
| correlation, | real in | |
| the parameter of a model, | real or vector | |
| , | likelihood and log-likelihood | real |
| the maximum-likelihood estimate, | real or vector | |
| , , | count of outcome , total count, vocabulary size | integers |
Joint, marginal, conditional
Section titled “Joint, marginal, conditional”Two discrete random variables on the same outcome have a joint mass function , a table that sums to 1. Summing a row gives the marginal of one variable; dividing a cell by its column sum gives the conditional of the row variable given the column. and are independent when for every cell, so one cell that fails is enough to refute it. A bigram model is exactly a table of conditionals .
Covariance and the variance of a sum
Section titled “Covariance and the variance of a sum”is positive when the variables tend to be large together and negative when one is large while the other is small. Expanding the square of gives Independence makes the covariance 0 (because ), but not the other way round: covariance only sees linear dependence, and with symmetric around 0 is completely determined by yet has zero covariance with it (Problem 4). For losses with common variance and pairwise covariance , the variance of their mean is , which does not go to 0 when : correlated evaluation tokens give wider error bars than independent ones.
Maximum likelihood
Section titled “Maximum likelihood”Given data drawn independently from , the likelihood is how probable the data is under , and the MLE maximizes it. Take logs first: products become sums, and is increasing, so the maximizer is the same. For a smooth one-parameter model, solve and check that it is a maximum. Three models recur.
- Bernoulli. successes in trials: , and at the success fraction (Problem 6).
- Exponential. Gaps with density : (Problem 7).
- Categorical. Counts : subject to ; the maximizer is (Problem 9 is the proof). An outcome never seen gets probability 0, which is why the estimation section smooths: Laplace (add-one) uses , one pseudo-count for every entry of the vocabulary, seen or not (Problem 5).
Hidden data and EM
Section titled “Hidden data and EM”Sometimes the data that would make counting easy is hidden. A Unigram tokenizer sees the word “ab” but not whether it was produced as or as . EM replaces each count by its expected value under the current model: weight each possible segmentation by its posterior probability given the word (its probability divided by the sum over all segmentations), count the pieces in each, and add up. Then take the categorical MLE of those expected counts, count over total. Repeating never lowers the likelihood of the observed words.
Two examples by hand
Section titled “Two examples by hand”These are siblings of Problems 1, 2, and 8, not problems of the set.
Joint table. Let with , , , . The marginals are and . The conditional is . Independence fails at the cell : . and , so : is 1 only when is.
One EM step. Take a Unigram with pieces , , at probabilities , , , and one observed word “xy”. The segmentations are with probability , and with . Their sum is , so the posteriors are and . Expected counts: is used once in the first, so ; also ; is . The total is , and the M-step gives , up from , and . Keep it as the exact fraction : every answer in this set is exact.
The problems
Section titled “The problems”Probabilities are exact fractions (, not 0.375), and lettered parts are answered separately. is written vx, is vy, the covariance is , and the sum of the gaps in Problem 7 is . The tag after each problem says what kind of answer is expected.
Joint distributions and covariance
Section titled “Joint distributions and covariance”The first four problems work with one joint table and then with a pair of variables that are dependent but uncorrelated.
Problem 1. and have this joint mass function :
(a) . (b) . [number] (c) Are and independent? [bool]
Answer
(a) . (b) . (c) No. (a) A marginal sums the joint over the other variable: . (b) A conditional renormalizes a column: , so . (c) Independence needs every cell to factor. and , so independence would need , but the table says ; one failing cell is enough. Common mistakes: for (a), reading one cell instead of the column sum; for (b), the joint rather than the conditional; for (b), the marginal .
Problem 2. For the same table: (a) . (b) . [number]
Answer
(a) . (b) . (a) Only cells with and contribute: . (b) and , so . The sign says tends to be a little smaller when (row puts its weight on ). Common mistakes: for (a), which is and equals only when the covariance is zero; for (b), the subtraction reversed; for (b), without centering.
Problem 3. and have variances (vx) and (vy) and covariance ; and are constants. Give . [expr in a, b, vx, vy, c]
Answer
(a^2*vx + b^2*vy + 2*a*b*c). Let and . Then , which is the formula. The cross term is why correlated per-token losses average down slower than independent ones. Common mistakes: dropping the covariance term (true only when ); , forgetting that scaling a variable by scales its variance by ; and instead of , counting the cross term once when the square has two of them.
Problem 4. is uniform on and . (a) . [number] (b) Are and independent? [bool]
Answer
(a) . (b) No. (a) by symmetry and , so . (b) is a function of : , while . Zero covariance rules out only a linear relation. Common mistakes: for (a), which is ; “yes” for (b), reading zero covariance as independence.
Maximum likelihood
Section titled “Maximum likelihood”The remaining five problems estimate parameters: smoothing a categorical, two calculus MLEs, one EM step, and the proof that count over total is optimal.
Problem 5. A tokenized corpus holds tokens: “the” 4 times, “cat” 3, “sat” 2, “mat” 1. The vocabulary also has a fifth word, “dog”, never seen, so . (a) The maximum-likelihood estimate of . (b) The Laplace (add-one) estimate of over the words. [number]
Answer
(a) . (b) . (a) The categorical MLE is count over total: . (b) Laplace adds one pseudo-count to every vocabulary entry, so the denominator grows by : . The five smoothed estimates sum to 1. Common mistakes: for (a), one over the number of distinct seen words; for (a), the Laplace estimate of “cat” when the MLE was asked; for (b), the unsmoothed MLE; for (b), a vocabulary of the 4 seen words only; for (b), adding one to only, after which the estimates no longer sum to 1.
Problem 6. independent Bernoulli() trials give successes (). (a) Give the log-likelihood of the observed sequence. [expr in k, n, p] (b) Give the that maximizes it. [expr in k, n]
Answer
(a) . (b) . (a) The observed sequence has probability (independent trials multiply); its log is the sum. (b) gives , so . It is a maximum because . Common mistakes: for (a), dropping the failures (its maximizer is ); for (a), the likelihood rather than its log; for (b), Laplace’s rule of succession, a smoothed estimate rather than the MLE; and .
Problem 7. The gaps between requests at your gateway are modeled as independent Exponential() with density for . You observe gaps whose sum is . Give the maximum-likelihood estimate of . [expr in n, s]
Answer
. The log-likelihood is . Setting gives , and makes it a maximum. The rate is the reciprocal of the mean gap ; the arrival schedule of the open-loop load generator is fitted this way. Common mistakes: , the mean gap instead of the rate; , the unbiased estimator of , which is not the MLE.
Problem 8. A Unigram tokenizer has three pieces with probabilities , , , and the training corpus is the single word “ab”. It can be segmented as or as , and a segmentation’s probability is the product of its pieces’ probabilities. One EM step computes each piece’s expected count (each segmentation’s count of the piece, weighted by the segmentation’s posterior probability given the word), then sets each probability to its expected count divided by the total expected count. Give (a) the expected count of and (b) the new . [number]
Answer
(a) . (b) . E-step: and , total , so the posteriors given the word are and . Expected counts: appears once in , so ; also ; is . The total expected count is . M-step: (and ; the three sum to 1). Common mistakes: for (a), the prior probability of rather than its expected count; for (a), weighting both segmentations equally instead of by their posteriors; for (b), the expected count without dividing by the total ; for (b), the old probability, with no M-step.
Problem 9. Counts with are observed from a categorical distribution . Prove that the log-likelihood is maximized over all probability vectors by . [proof]
Answer
Claim. Let with . Over all probability vectors (, ), is maximized by , with the convention .
Zero counts. If some with , then , so such a is never a maximizer; assume wherever . Terms with contribute 0 whatever is, and there.
Proof (Gibbs’ inequality). Let . Then where does not depend on . Since with equality only at , with equality only if for every and off . So , with equality exactly at : count over total is the unique maximizer.
A Lagrange-multiplier argument also works: stationarity of gives , so and ; it must then be argued that this stationary point is the maximum (the objective is concave). A complete proof states the claim, handles zero counts, and proves the maximum is attained only at . The model proof is also in course/solutions/solve/S-M07b/q9.md.
What can go wrong
Section titled “What can go wrong”Most wrong answers here come from reading the wrong piece of a table or dropping a term. Reading one cell as a marginal quotes as a joint probability; sum the column instead (Problem 1a). Confusing a joint with a conditional answers as ; divide the cell by its column sum (Problem 1b). Assuming without independence reports zero covariance for dependent variables; compute cell by cell (Problem 2). And reading zero covariance as independence treats a deterministic relation as noise; check a cell of the joint, not the covariance (Problem 4b).
Warning: Dropping or halving the covariance term makes error bars on correlated losses too narrow. Expand the square and keep both cross terms, (Problem 3).
The estimation pitfalls are just as specific. Applying Laplace over the seen words only, or adding one to only, gives unseen words the wrong mass and leaves the smoothed rows of the estimation section not summing to 1; add one per vocabulary entry and to the denominator (Problem 5b). Differentiating the likelihood instead of its log, or dropping the failures, gives a derivative that cannot be solved by hand, or ; differentiate the full log-likelihood (Problem 6). Reporting the mean gap instead of the rate inverts arrival rates in the load generator; solve for the rate, (Problem 7). And skipping the M-step normalization in EM leaves probabilities that do not sum to 1 after one step; divide each expected count by the total, in Problem 8b.
Where this leads
Section titled “Where this leads”This set extends the single-variable expectation and variance of problem set A, and MLE, Laplace, absolute discounting implements mle and laplace, which are Problems 5 and 9 in code. In AI Engineering, The Unigram language model tokenizer computes the expected counts of Problem 8 by forward-backward and then takes count over total (Problem 9); n-gram language model with interpolated modified Kneser-Ney builds bigram and n-gram tables that are joint and conditional distributions over tokens (Problem 1); and the open-loop load generator uses Poisson arrivals with exponential gaps, whose rate Problem 7 fits from a trace. Problem set C builds the law of large numbers and confidence intervals on Problem 3’s variance of a sum.
Further reading
Section titled “Further reading”- Blitzstein and Hwang, Introduction to Probability (free), ch. 7 “Joint Distributions”.
- Wasserman, All of Statistics, ch. 9 “Parametric Inference”.
Key terms
Section titled “Key terms”- Joint distribution: the table over pairs of values; it determines marginals and conditionals.
- Marginal: the distribution of one variable, obtained by summing the joint over the other.
- Covariance: , a measure of linear co-movement; zero does not imply independence.
- Likelihood: , the probability of the observed data as a function of the parameter.
- Maximum-likelihood estimate: the parameter value that maximizes the (log-)likelihood.
- Laplace smoothing: adding one pseudo-count per vocabulary entry, .
- EM: expectation-maximization; replace hidden counts by expected counts under the current model, then take the MLE, and repeat.
Summary
Section titled “Summary”- A joint distribution holds everything: marginals are its row and column sums, conditionals are a renormalized row or column, and independence means every cell factors (Problem 1).
- Covariance measures linear co-movement; it is the cross term in , and zero covariance does not mean independence (Problems 2, 3, and 4).
- The maximum-likelihood estimate of a categorical is count over total; Laplace smoothing adds one to every vocabulary entry, seen or not (Problems 5 and 9).
- MLE by calculus: write the log-likelihood, differentiate, set to zero; the Bernoulli MLE is the success fraction and the exponential rate is the reciprocal of the mean gap (Problems 6 and 7).
- When the data is hidden (which segmentation produced a word), EM replaces counts by expected counts and then takes the same MLE; one step of the Unigram trainer fits on a page (Problem 8).