Expectation-Maximization
Learning with missing labels by alternating guesses. Fill in the hidden assignments softly, refit the model, and repeat until neither moves.
Why Does This Exist?
Many models have hidden assignments: which Gaussian produced each point, which topic wrote each word, which state a tracker was in. Maximum likelihood wants complete data, but the labels are missing. Expectation-Maximization fills them in softly (as probabilities, not hard calls), refits the model to the filled-in data, and repeats. Likelihood rises every round, which makes EM one of the few iterative algorithms with a built-in progress certificate.
EM is the parent of a large family: k-means is its hard-assignment limit, Baum-Welch for hidden Markov models is EM on sequences, and variational inference generalizes its lower bound. This page teaches the core loop; mixtures and sequences get their own pages.
The one idea you need first
Maximum likelihood picks parameters maximizing . With hidden , the likelihood has the sum inside the log, which blocks closed forms. EM instead maximizes a lower bound built from a guess at , tightens the guess, and repeats. The MLE background is in maximum likelihood estimation.
Think of It Like This
Two overlapping piles of exam papers
Two classes took the same exam and the piles got mixed. You must recover each class's average. Start with a guess (class A averages 0, class B averages 6). For each paper, estimate the probability it belongs to A versus B from the current averages (E step, soft sorting). Then recompute each average weighting papers by those probabilities (M step). Repeat.
Hard sorting (each paper wholly to one class) is k-means and it jitter. Soft sorting is EM: a borderline paper counts 60 percent toward A and 40 percent toward B, so one ambiguous paper cannot yank an average across the room. The averages and the sortings chase each other to a agreement neither could reach alone.
Where the analogy stops: real EM also refits variances and mixing weights each round, and the likelihood guarantee needs the exact posterior over assignments, not an approximation.
How It Actually Works
Parameters , observed , hidden . The E step computes responsibilities . The M step maximizes the expected complete log-likelihood:
Each round tightens a lower bound on (E step) then climbs it (M step), so likelihood never decreases. EM converges to a local optimum, not necessarily the global one: starts matter.
Worked example
Four 1-D points , two unit-variance Gaussians with equal weights, means started at , . E step: responsibility of A for is proportional to versus B's .
- : A unnormalized , B . .
- : A , B . .
- : A , B . .
- : A , B . .
M step: . Symmetrically . Hmm, that barely moved from a lucky start. Make the start unlucky instead: , (both left of the right cluster).
- : A , B . .
- : A , B . .
- : A , B . .
- : A , B . .
M step: . . One pass moved the means from to , reaching toward the true clusters near 1 and 5. Continue and they settle near ; the soft 0.182 assignment of is what lets B claim it without a hard fight.
Code
One EM pass on the four points from the unlucky start, matching the hand numbers.
import numpy as np
x = np.array([0.0, 2.0, 4.0, 6.0]) # datamu = np.array([0.0, 3.0]) # unlucky startlike = np.exp(-0.5 * (x[:, None] - mu[None, :]) ** 2) # Gaussian likelihoodsresp = like / like.sum(axis=1, keepdims=True) # E step: responsibilitiesmu = (resp * x[:, None]).sum(axis=0) / resp.sum(axis=0) # M stepprint(np.round(resp[:, 0], 3)) # -> A responsibilitiesprint(np.round(mu, 2)) # -> [0.31 4.11]Responsibilities print and means . Loop the last two lines twenty times and the means land near with likelihood higher every round.
Watch Out For
Local optima from a single start
EM climbs to the nearest peak, and mixture likelihoods have plenty: one component hogging everything, two components splitting one cluster, a variance collapsing onto a single point (infinite likelihood, useless model). Never run once. Run from many starts (k-means++ seeding helps) and keep the highest-likelihood result; collapse variances with a small floor.
Hardening responsibilities too early
Rounding soft assignments to 0/1 inside the loop turns EM into k-means mid-flight: the likelihood guarantee dies and borderline points swing whole means. Keep responsibilities soft until convergence, then harden once for reporting if labels are needed.
The Quick Version
- EM alternates soft assignment of hidden labels (E step) with refitting to them (M step).
- Likelihood rises every round, converging to a local optimum.
- From the unlucky start , one pass moves the means to , heading for .
- Soft 0.182 assignments let clusters share borderline points instead of fighting over them.
- k-means is the hard-assignment limit; Baum-Welch is EM on sequences.
- Run many starts, floor the variances, and never harden responsibilities mid-loop.