Expectation–maximization: improve a bound with latent expectations
EM 算法
Derive the E/M bound argument, track a runnable mixture fit and compare it with a separate closed-form optimum.
expectation-maximizationEstimate with missing labels, not invented labels
Each of seven batches comes from one of two known coins. A low-success coin has success probability 1/4; a high-success coin has probability 3/4. A coin is chosen once per batch and tossed twice. We observe counts 0,0,1,1,2,2,2, but not which coin produced each batch. The unknown parameter π is the chance of choosing the high-success coin.
If the labels were known, estimating π would just count high-coin batches. EM replaces those unavailable counts with posterior expected counts under the current parameter, then refits. It does not turn uncertain labels into observed facts. Read likelihood and Bayesian inference first.
A tight lower bound gives a controlled update
Let x denote all observed data and z the latent variables. For a distribution q over z and a well-defined positive model on the relevant support, the log-likelihood decomposes as:
The KL term is nonnegative, so F is a lower bound. It touches the likelihood when q is the current posterior. Expand the ratio pθ(x,z)=pθ(z|x)pθ(x) to verify the identity directly. The general EM notes by Andrew Ng derive the same bound through Jensen’s inequality and choose the posterior to make it tight.
The E-step computes that posterior or its needed sufficient-statistic expectations. The M-step maximizes the expected complete-data log-likelihood, keeping q fixed:
The entropy of q does not depend on the candidate θ during this M-step. For exact E and M steps, the bound argument gives:
A generalized EM step need only increase the expected complete-data objective to preserve this argument, assuming the exact posterior E-step and the usual support conditions. An approximate E-step does not automatically preserve observed-likelihood monotonicity. Dempster, Laird and Rubin, Section 3, distinguishes monotone likelihood behavior from stronger convergence conclusions. The paper’s 1977 publication is separate from its December 1976 presentation.
Derive the update for the two-coin batches
The low-coin count probabilities for k=0,1,2 are(9,6,1)/16; the high-coin probabilities are(1,6,9)/16. Call them fₖ and gₖ. Independence across batches gives each posterior responsibility rᵢ=πgₖᵢ/[(1−π)fₖᵢ+πgₖᵢ]. The expected complete-data objective contains Σᵢ[rᵢlogπ+(1−rᵢ)log(1−π)], plus constants. Differentiating and solving gives π_new=(Σᵢrᵢ)/n.
At π=1/2, responsibilities for counts 0,1,2 are 1/10,1/2,9/10. Their weighted total for our seven observations is 39/10, so the first update is 39/70. A count of one has the same probability under either coin and therefore supplies no component preference at that step; the program still includes its uncertainty.
An independent optimum for this particular model
Ignoring constants independent of π, our observed log-likelihood is 2log(9−8π)+3log(1+8π). Its derivative is−16/(9−8π)+24/(1+8π), which vanishes at π=5/8. The second derivative is strictly negative throughout[0,1], so this particular one-parameter model has a unique global maximum. That favorable geometry comes from fixed component distributions; it is not a general property of EM.
Track parameters and the observed likelihood
This complete Python 3.12 program accepts 1–10000 integer two-trial counts and a finite initial mixture weight in[10⁻⁶,1−10⁻⁶]. It performs a requested 0–200 iterations. It reports a finite run, not an automatic declaration of convergence. Both component distributions remain fixed.
from math import fsum, isfinite, log
LOW=(9/16,6/16,1/16)
HIGH=(1/16,6/16,9/16)
def em_mixture_weight(data, initial=0.5, steps=100):
data=list(data)
if not 1<=len(data)<=10000 or any(type(k) is not int or k not in (0,1,2) for k in data):
raise ValueError('one to10000 two-trial success counts required')
if type(initial) not in (int,float) or not isfinite(initial) or not 1e-6<=initial<=1-1e-6:
raise ValueError('finite initial weight in [1e-6,1-1e-6] required')
if type(steps) is not int or not 0<=steps<=200:
raise ValueError('integer steps in [0,200] required')
pi=float(initial)
def log_likelihood(weight):
return fsum(log((1-weight)*LOW[k]+weight*HIGH[k]) for k in data)
history=[(pi,log_likelihood(pi))]
for _ in range(steps):
r=[pi*HIGH[k]/((1-pi)*LOW[k]+pi*HIGH[k]) for k in data]
pi=fsum(r)/len(r)
history.append((pi,log_likelihood(pi)))
return history
if __name__ == '__main__':
data=[0,0,1,1,2,2,2]
history=em_mixture_weight(data)
for step in [0,1,2,10,100]:
weight,ll=history[step]
print('step',step,'weight:',f'{weight:.9f}','log-likelihood:',f'{ll:.9f}')
print('closed-form optimum:',5/8)
print('minimum observed likelihood increment:',f'{min(b[1]-a[1] for a,b in zip(history,history[1:])):.3e}')
Output:
step 0 weight: 0.500000000 log-likelihood: -7.777412555
step 1 weight: 0.557142857 log-likelihood: -7.706713736
step 2 weight: 0.588016177 log-likelihood: -7.685722539
step 10 weight: 0.624681440 log-likelihood: -7.676735664
step 100 weight: 0.625000000 log-likelihood: -7.676734987
closed-form optimum: 0.625
minimum observed likelihood increment: -8.882e-16
The first step matches 39/70 and the final weight approaches 5/8. Logarithms and updates use floating-point arithmetic; tiny negative increments at the level of rounding would need a numerical tolerance, not a claim that the exact theorem failed. The tests compare actual likelihood changes with a small tolerance and check the known optimum independently.
An E/M iteration scans n observations, costing O(n) time and O(n) temporary responsibility storage here. Aggregating the three possible counts would reduce repeated work, but the observation-wise loop shows where latent expectations enter. A general EM problem can have a much more expensive E-step, including inference over a large latent space.
An increasing objective does not certify the model
A monotone bounded sequence of likelihood values converges as numbers; this alone does not prove that parameters converge, that their limit is globally optimal, or that predictions generalize. Singular models can have unbounded likelihoods. Multiple starting points, constraints or regularization may be necessary in other mixture models.
Initializing π at zero or one in this example would make the missing component’s posterior responsibility identically zero. Such boundary values can become fixed points even when an interior solution is better, which is why the interface requires an interior start. Extremely small components can also be numerically lost during repeated updates.
EM does not generally mean “fill every missing value with its mean and fit as if observed.” Expected sufficient statistics can include second moments and dependencies. Nor is a responsibility the posterior probability of the entire model being correct. Missingness mechanisms and the observation model still need justification.
Identify which quantity was improved
- What is the responsibility of the high-success coin for a count of zero at π=1/2?
Answer 1: 1/10, from the two component likelihoods 1/16 and 9/16.
- What is the first mixture-weight update for the seven batches?
Answer 2: 39/70, the mean of two 0.1, two 0.5 and three 0.9 responsibilities.
- Does exact EM guarantee a global maximum for every latent-variable model?
Answer 3: No. Its likelihood ascent guarantee is weaker. Global optimality here follows from the separate strict-concavity calculation.
- Can the M-step update q while optimizing θ and still use the stated fixed-q proof unchanged?
Answer 4: No. The proof’s M-step holds q fixed; a different update needs its own argument.
Read convergence statements with their assumptions
The 1977 paper organized a general incomplete-data framework that includes earlier special cases. The lower-bound view connects EM with coordinate optimization over parameters and latent distributions; restricted latent families lead toward variational methods, where improving a bound may not improve the original likelihood.
Project: change the counts so the optimum lies at a boundary. Derive that optimum before running EM, examine how a finite interior run approaches it, and report both parameter error and likelihood gap. Then compare with a model that also learns component probabilities to see why the easy one-dimensional argument no longer applies.
Make a prediction, then check
Does increasing observed likelihood prove that general EM reached a global maximum?
Learning objectives
- Derive posterior responsibilities and the fixed-posterior M-step.
- Separate likelihood ascent from parameter and global convergence claims.
Prerequisites & connections
Explore related knowledge →References
- Maximum Likelihood from Incomplete Data via the EM Algorithm
A. P. Dempster; N. M. Laird; D. B. Rubin · 1977 - CS229 Lecture Notes, PartIX — The EM Algorithm
Andrew Ng
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-06 · Codex single-agent authored technical review: cited primary-source scope, explicit assumptions, original calculations and counterexamples, runnable tests and bilingual review; not independent human or second-agent review.
- Single-agent authored technical review: bounded primary-source locations, assumptions, original derivations, exact/literal references and matching bilingual equations/examples/exercises. Not independent human or second-agent review.
- Four focused CPython3.12.4 tests passed per chapter. Markov and MH matrices use exact fractions; EM and BP are floating-point and tested against independent analytic or enumerated references. Seeded MCMC frequencies are an observed run, not a convergence certificate.
- Promoted the full prose, equations, code and exercises. Tests read formal MDX only; removed temporary fallbacks/draft skips and updated review assertion to checked. Algorithm and independent references are unchanged; final record binds both locale hashes.
Source locations checked:
- Maximum Likelihood from Incomplete Data via the EM Algorithm · Original39-page university-hosted scan re-read: Section3 Theorem1 and following discussion printed7–8 distinguish monotone likelihood and convergence to maxima;1976 presentation versus1977 publication.
- CS229 Lecture Notes, PartIX — The EM Algorithm · PartIX pp3–6: Jensen lower bound, posterior E-step, fixed-Q M-step and monotonicity chain. Original known-component two-coin example and scalar optimum independently derived.
Related topics