Skip to content

Solve set: joint and covariance, MLE

ModuleS-M07b · solve · none · Pass 3 · 3 to 4 h
You buildanswers in solve/S-M07b.toml (15 checked by SymPy) and 1 proof in solve/S-M07b/q9.md (self-graded against its rubric)
Contractnone: a pen and paper set
Testscourse/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
NeedsS-M07a (axioms, conditioning, expectation, variance). Reading: M07.2 MLE, Laplace, absolute discounting and the Probability and Statistics topic, joint distributions and estimation sections
Used byno 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
MilestoneMS-P3 (the Pass 3 gate runs ol check on every solve part of the pass)
Optional depthBlitzstein and Hwang, Introduction to Probability (free), ch. 7 “Joint Distributions”; Wasserman, All of Statistics, ch. 9 “Parametric Inference”
  • 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 E[XY]−E[X]E[Y]E[XY] - E[X]E[Y] measures linear co-movement; it is the cross term in Var⁡(aX+bY)\operatorname{Var}(aX + bY), 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).
Terminal window
ol start S-M07b # writes solve/S-M07b.toml and the proof file
ol 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 proof

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 σ2/n\sigma^2/n. 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.

SymbolMeaningType / shape
pX,Y(x,y)p_{X,Y}(x, y)joint mass function, P(X=x,Y=y)P(X = x, Y = y)real
pX(x)=∑ypX,Y(x,y)p_X(x) = \sum_y p_{X,Y}(x, y)marginal of XXreal
P(X=x∣Y=y)P(X = x \mid Y = y)conditional, pX,Y(x,y)/pY(y)p_{X,Y}(x, y) / p_Y(y) for pY(y)>0p_Y(y) > 0real
Cov⁡(X,Y)\operatorname{Cov}(X, Y)covariance, E[(X−EX)(Y−EY)]=E[XY]−E[X]E[Y]E[(X - E X)(Y - E Y)] = E[XY] - E[X]E[Y]real
σX2\sigma_X^2 (vx)variance of XX, Cov⁡(X,X)\operatorname{Cov}(X, X)real
ρ\rhocorrelation, Cov⁡(X,Y)/(σXσY)\operatorname{Cov}(X, Y) / (\sigma_X \sigma_Y)real in [−1,1][-1, 1]
θ\thetathe parameter of a model, pθp_\thetareal or vector
L(θ)L(\theta), ℓ(θ)\ell(\theta)likelihood ∏ipθ(xi)\prod_i p_\theta(x_i) and log-likelihood ∑iln⁡pθ(xi)\sum_i \ln p_\theta(x_i)real
θ^\hat\thetathe maximum-likelihood estimate, arg⁡max⁡θℓ(θ)\arg\max_\theta \ell(\theta)real or vector
ckc_k, NN, VVcount of outcome kk, total count, vocabulary sizeintegers

Two discrete random variables on the same outcome have a joint mass function pX,Yp_{X,Y}, 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. XX and YY are independent when pX,Y(x,y)=pX(x) pY(y)p_{X,Y}(x, y) = p_X(x)\,p_Y(y) for every cell; one cell that fails is enough to refute it. A bigram model (L2.1) is exactly a table of conditionals P(next∣current)P(\text{next} \mid \text{current}).

Cov⁡(X,Y)=E[XY]−E[X]E[Y]\operatorname{Cov}(X, Y) = E[XY] - E[X]E[Y] 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 aX+bY−E[aX+bY]aX + bY - E[aX + bY] gives Var⁡(aX+bY)=a2σX2+b2σY2+2ab Cov⁡(X,Y).\operatorname{Var}(aX + bY) = a^2 \sigma_X^2 + b^2 \sigma_Y^2 + 2ab\,\operatorname{Cov}(X, Y). Independence makes the covariance 0 (because E[XY]=E[X]E[Y]E[XY] = E[X]E[Y]), but not the other way round: covariance only sees linear dependence, and Y=X2Y = X^2 with XX symmetric around 0 is completely determined by XX with zero covariance (q4). For nn losses with common variance σ2\sigma^2 and pairwise covariance cc, the variance of their mean is σ2/n+(n−1)c/n\sigma^2/n + (n - 1)c/n, which does not go to 0 when c>0c > 0: correlated evaluation tokens give wider error bars than independent ones.

Given data x1,…,xnx_1, \dots, x_n drawn independently from pθp_\theta, the likelihood L(θ)=∏ipθ(xi)L(\theta) = \prod_i p_\theta(x_i) is how probable the data is under θ\theta, and the MLE θ^\hat\theta maximizes it. Take logs first: products become sums, and ln⁡\ln is increasing, so the maximizer is the same. For a smooth one-parameter model, solve ℓ′(θ)=0\ell'(\theta) = 0 and check it is a maximum.

  • Bernoulli. kk successes in nn trials: ℓ(p)=kln⁡p+(n−k)ln⁡(1−p)\ell(p) = k \ln p + (n - k)\ln(1 - p), and ℓ′(p)=0\ell'(p) = 0 at the success fraction (q6).
  • Exponential. Gaps xix_i with density λe−λx\lambda e^{-\lambda x}: ℓ(λ)=nln⁡λ−λ∑ixi\ell(\lambda) = n \ln \lambda - \lambda \sum_i x_i (q7).
  • Categorical. Counts ckc_k: ℓ(p)=∑kckln⁡pk\ell(p) = \sum_k c_k \ln p_k subject to ∑kpk=1\sum_k p_k = 1; the maximizer is pk=ck/Np_k = c_k / N (q9, the proof). An outcome never seen gets probability 0, which is why M07.2 smooths: Laplace (add-one) uses (ck+1)/(N+V)(c_k + 1)/(N + V), one pseudo-count for every entry of the vocabulary, seen or not (q5).

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 a+ba + b or as abab. 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.

This is a sibling of q1, q2, and q8, not one of the graded problems.

Joint table. X,Y∈{0,1}X, Y \in \{0, 1\} with P(0,0)=1/2P(0, 0) = 1/2, P(0,1)=0P(0, 1) = 0, P(1,0)=1/4P(1, 0) = 1/4, P(1,1)=1/4P(1, 1) = 1/4. Marginals: P(X=1)=1/2P(X = 1) = 1/2, P(Y=1)=1/4P(Y = 1) = 1/4. Conditional: P(Y=1∣X=1)=(1/4)/(1/2)=1/2P(Y = 1 \mid X = 1) = (1/4)/(1/2) = 1/2. Independence fails at the cell (0,1)(0, 1): 0≠(1/2)(1/4)0 \ne (1/2)(1/4). E[XY]=1⋅1⋅1/4=1/4E[XY] = 1 \cdot 1 \cdot 1/4 = 1/4, E[X]E[Y]=1/8E[X] E[Y] = 1/8, so Cov⁡(X,Y)=1/8>0\operatorname{Cov}(X, Y) = 1/8 > 0: YY is 1 only when XX is.

One EM step. A Unigram with pieces xx, yy, xyxy at probabilities 1/21/2, 1/41/4, 1/41/4, and one observed word “xy”. Segmentations: x+yx + y with probability 1/2⋅1/4=1/81/2 \cdot 1/4 = 1/8, and xyxy with 1/41/4. Their sum is 3/83/8, so the posteriors are 1/31/3 and 2/32/3. Expected counts: xx is used once in the first, so 1/31/3; yy also 1/31/3; xyxy is 2/32/3. The total is 4/34/3, and the M-step gives P(xy)=(2/3)/(4/3)=1/2P(xy) = (2/3)/(4/3) = 1/2, up from 1/41/4, and P(x)=P(y)=1/4P(x) = P(y) = 1/4. In solve/ this would be answer = "1/2"; answer = "0.5" fails as inexact.

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 σX2\sigma_X^2 as vx, σY2\sigma_Y^2 as vy, the covariance as c, and the sum of the gaps in q7 as s.

q1. X∈{0,1}X \in \{0, 1\} and Y∈{0,1,2}Y \in \{0, 1, 2\} have this joint mass function P(X=x,Y=y)P(X = x, Y = y):

Y=0Y = 0Y=1Y = 1Y=2Y = 2
X=0X = 01/81/81/41/41/81/8
X=1X = 11/41/41/81/81/81/8

(a) P(Y=1)P(Y = 1). (b) P(X=1∣Y=0)P(X = 1 \mid Y = 0). [number] (c) Are XX and YY independent? [bool]

q2. For the same table: (a) E[XY]E[XY]. (b) Cov⁡(X,Y)\operatorname{Cov}(X, Y). [number]

q3. XX and YY have variances σX2\sigma_X^2 (vx) and σY2\sigma_Y^2 (vy) and covariance cc; aa and bb are constants. Give Var⁡(aX+bY)\operatorname{Var}(aX + bY). [expr in a, b, vx, vy, c]

q4. XX is uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2. (a) Cov⁡(X,Y)\operatorname{Cov}(X, Y). [number] (b) Are XX and YY independent? [bool]

q5. A tokenized corpus holds N=10N = 10 tokens: “the” 4 times, “cat” 3, “sat” 2, “mat” 1. The vocabulary also has a fifth word, “dog”, never seen, so V=5V = 5. (a) The maximum-likelihood estimate of p(cat)p(\text{cat}). (b) The Laplace (add-one) estimate of p(dog)p(\text{dog}) over the V=5V = 5 words. [number]

q6. nn independent Bernoulli(pp) trials give kk successes (0<k<n0 < k < n). (a) Give the log-likelihood ℓ(p)=ln⁡P(data∣p)\ell(p) = \ln P(\text{data} \mid p) of the observed sequence. [expr in k, n, p] (b) Give the pp that maximizes it. [expr in k, n]

q7. The gaps between requests at your gateway are modeled as independent Exponential(λ\lambda) with density λe−λx\lambda e^{-\lambda x} for x≥0x \ge 0. You observe nn gaps whose sum is ss. Give the maximum-likelihood estimate of λ\lambda. [expr in n, s]

q8. A Unigram tokenizer (L1.4) has three pieces with probabilities P(a)=1/4P(a) = 1/4, P(b)=1/4P(b) = 1/4, P(ab)=1/2P(ab) = 1/2, and the training corpus is the single word “ab”. It can be segmented as a+ba + b or as abab, 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 aa and (b) the new P(ab)P(ab). [number]

q9. Counts c1,…,cV≥0c_1, \dots, c_V \ge 0 with N=c1+⋯+cV>0N = c_1 + \dots + c_V > 0 are observed from a categorical distribution p=(p1,…,pV)p = (p_1, \dots, p_V). Prove that the log-likelihood ∑kckln⁡pk\sum_k c_k \ln p_k is maximized over all probability vectors pp by pk=ck/Np_k = c_k / N. [proof]

PitfallSymptomCaught by
Reading one cell as a marginalP(Y=1)P(Y = 1) quoted as a joint probabilityq1 (canary 1/4)
Confusing a joint with a conditionalP(X=1∣Y=0)P(X = 1 \mid Y = 0) answered as P(X=1,Y=0)P(X = 1, Y = 0)q1 (canary 1/4)
E[XY]=E[X]E[Y]E[XY] = E[X]E[Y] without independencecovariance reported as 0 for dependent variablesq2 (canary 7/16)
Dropping or halving the covariance termerror bars on correlated losses too narrowq3 (canaries without 2abc2abc, with abcabc)
Zero covariance read as independencea deterministic relation treated as noiseq4 (canary true)
Laplace over the seen words only, or added to NN onlyunseen words get the wrong mass; M07.2’s rows do not sum to 1q5 (canaries 1/14 and 1/11)
The likelihood instead of its log, or the failures droppeda derivative that cannot be solved by hand, or p^=1\hat p = 1q6 (canaries)
The mean gap instead of the ratearrival rates inverted in the load generatorq7 (canary s/n)
Skipping the M-step normalization in EMprobabilities that do not sum to 1 after one stepq8 (canary 8/9)
DirectionModuleHow it uses this
BackS-M07aaxioms, conditioning, expectation and variance of one variable
BackM07.2mle and laplace are q5 and q9 in code (reading)
ForwardL1.4Unigram EM: expected counts by forward-backward (q8), then count over total (q9)
ForwardL2.1bigram and n-gram tables are joint and conditional distributions over tokens (q1)
ForwardS-M07cthe law of large numbers and confidence intervals build on q3’s variance of a sum
Forwardload.01Poisson arrivals with exponential gaps: q7 fits their rate from a trace