Solve set: joint and covariance, MLE
Overview
Section titled “Overview”| Module | S-M07b · solve · none · Pass 3 · 3 to 4 h |
| You build | answers in solve/S-M07b.toml (15 checked by SymPy) and 1 proof in solve/S-M07b/q9.md (self-graded against its rubric) |
| Contract | none: a pen and paper set |
| Tests | course/solve/S-M07b/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M07b/problems.md and in section 4 |
| Needs | S-M07a (axioms, conditioning, expectation, variance). Reading: M07.2 MLE, Laplace, absolute discounting and the Probability and Statistics topic, joint distributions and estimation sections |
| Used by | no call site (a solve set). Do it alongside M07.2 and before L1.4 (Unigram EM: q8 is one of its steps by hand, q9 is its M-step); part S-M07c follows M07.4 |
| Milestone | MS-P3 (the Pass 3 gate runs ol check on every solve part of the pass) |
| Optional depth | Blitzstein and Hwang, Introduction to Probability (free), ch. 7 “Joint Distributions”; Wasserman, All of Statistics, ch. 9 “Parametric Inference” |
Key Takeaways
Section titled “Key Takeaways”- 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 (q1).
- Covariance measures linear co-movement; it is the cross term in , and zero covariance does not mean independence (q2, q3, q4).
- The maximum-likelihood estimate of a categorical is count over total; Laplace smoothing adds one to every vocabulary entry, seen or not (q5, q9).
- 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 (q6, q7).
- 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
L1.4’s trainer fits on a page (q8).
How to work this chapter
Section titled “How to work this chapter”ol start S-M07b # writes solve/S-M07b.toml and the proof fileol check S-M07b # SymPy checks the answers, then asks the proof rubric (y/n)ol check S-M07b --regrade # ask the rubric again after you change the proof1. Why now
Section titled “1. Why now”Pass 3 turns counts into probabilities everywhere. M07.2 estimates a categorical distribution from token counts and smooths it so unseen words keep some mass. L1.4 trains a Unigram tokenizer whose training data never says which segmentation produced a word, so it re-estimates piece probabilities from expected counts, again and again (EM). The language models of L2 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 . This set gives you the tools behind all of it: joint and conditional distributions, covariance, and maximum likelihood, including the one EM step you will implement.
2. Principles
Section titled “2. Principles”| 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 |
2.1 Joint, marginal, conditional
Section titled “2.1 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; one cell that fails is enough to refute it. A bigram model (L2.1) is exactly a table of conditionals .
2.2 Covariance and the variance of a sum
Section titled “2.2 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 with zero covariance (q4). 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.
2.3 Maximum likelihood
Section titled “2.3 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 it is a maximum.
- Bernoulli. successes in trials: , and at the success fraction (q6).
- Exponential. Gaps with density : (q7).
- Categorical. Counts : subject to ; the maximizer is (q9, the proof). An outcome never seen gets probability 0, which is why
M07.2smooths: Laplace (add-one) uses , one pseudo-count for every entry of the vocabulary, seen or not (q5).
2.4 Hidden data and EM
Section titled “2.4 Hidden data and EM”Sometimes the data that would make counting easy is hidden. A Unigram tokenizer (L1.4) 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.
3. Worked example by hand
Section titled “3. Worked example by hand”This is a sibling of q1, q2, and q8, not one of the graded problems.
Joint table. with , , , . Marginals: , . Conditional: . Independence fails at the cell : . , , so : is 1 only when is.
One EM step. A Unigram with pieces , , at probabilities , , , and one observed word “xy”. Segmentations: 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 . In solve/ this would be answer = "1/2"; answer = "0.5" fails as inexact.
4. The problem set
Section titled “4. The problem set”Write each answer in solve/S-M07b.toml; lettered parts are their own tables:
[q1.a]answer = "3/8"[q3]answer = "a^2*vx + b^2*vy + 2*a*b*c"[q6.b]answer = "k/n"[q9]proof = "S-M07b/q9.md"Probabilities are exact fractions; 0.375 fails a question marked exact. Write as vx, as vy, the covariance as c, and the sum of the gaps in q7 as s.
Joint distributions and covariance
Section titled “Joint distributions and covariance”q1. and have this joint mass function :
(a) . (b) . [number] (c) Are and independent? [bool]
q2. For the same table: (a) . (b) . [number]
q3. and have variances (vx) and (vy) and covariance ; and are constants. Give . [expr in a, b, vx, vy, c]
q4. is uniform on and . (a) . [number] (b) Are and independent? [bool]
Maximum likelihood
Section titled “Maximum likelihood”q5. 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]
q6. 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]
q7. 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]
q8. A Unigram tokenizer (L1.4) 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]
q9. Counts with are observed from a categorical distribution . Prove that the log-likelihood is maximized over all probability vectors by . [proof]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Reading one cell as a marginal | quoted as a joint probability | q1 (canary 1/4) |
| Confusing a joint with a conditional | answered as | q1 (canary 1/4) |
| without independence | covariance reported as 0 for dependent variables | q2 (canary 7/16) |
| Dropping or halving the covariance term | error bars on correlated losses too narrow | q3 (canaries without , with ) |
| Zero covariance read as independence | a deterministic relation treated as noise | q4 (canary true) |
| Laplace over the seen words only, or added to only | unseen words get the wrong mass; M07.2’s rows do not sum to 1 | q5 (canaries 1/14 and 1/11) |
| The likelihood instead of its log, or the failures dropped | a derivative that cannot be solved by hand, or | q6 (canaries) |
| The mean gap instead of the rate | arrival rates inverted in the load generator | q7 (canary s/n) |
| Skipping the M-step normalization in EM | probabilities that do not sum to 1 after one step | q8 (canary 8/9) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M07a | axioms, conditioning, expectation and variance of one variable |
| Back | M07.2 | mle and laplace are q5 and q9 in code (reading) |
| Forward | L1.4 | Unigram EM: expected counts by forward-backward (q8), then count over total (q9) |
| Forward | L2.1 | bigram and n-gram tables are joint and conditional distributions over tokens (q1) |
| Forward | S-M07c | the law of large numbers and confidence intervals build on q3’s variance of a sum |
| Forward | load.01 | Poisson arrivals with exponential gaps: q7 fits their rate from a trace |