On this page
Two coins, A and B, have unknown biases. A friend picks one, tosses it 10 times and writes down the heads, five times over, without saying which coin. If you knew the coins, you would just count heads: ordinary maximum likelihood estimation. The labels are hidden, and EM was built for exactly that.
What is the expectation–maximisation algorithm?
The EM algorithm is "a broadly applicable algorithm for computing maximum likelihood estimates from incomplete data", in the words of the 1977 paper by A. P. Dempster, N. M. Laird and D. B. Rubin. That paper explained the method and gave it its name, as Wikipedia's article on the algorithm notes. Each round has two steps, repeated until the estimates stop changing:
E-step (expectation): from your current guesses, work out the probability of each hidden value for every data point (its responsibilities).
M-step (maximisation): re-estimate the parameters, treating the responsibilities as fractional counts.
How does EM work on the two-coin problem?
Our data: five runs of 10 tosses, with 5, 9, 8, 4 and 7 heads (33 heads in 50 tosses). We start from θA = 0.6 and θB = 0.5, with each coin equally likely to be picked.
E-step. For a run with h heads, the likelihood is proportional to 0.6^h × 0.4^(10−h) under coin A and 0.5^10 under coin B; the binomial coefficient cancels. A's responsibility is L_A / (L_A + L_B), Bayes' rule, as in Beta distribution Bayesian updating. The ratio L_A / L_B = 1.2^h × 0.8^(10−h). For 9 heads that is 1.2^9 × 0.8 ≈ 4.13, so A's responsibility is 4.13 / 5.13 ≈ 0.80.
Run 1, 5 heads: A 0.45, B 0.55
Run 2, 9 heads: A 0.80, B 0.20
Run 3, 8 heads: A 0.73, B 0.27
Run 4, 4 heads: A 0.35, B 0.65
Run 5, 7 heads: A 0.65, B 0.35
M-step. Split each run's heads and tails between the coins by responsibility. Coin A gets 0.45 × 5 + 0.80 × 9 + 0.73 × 8 + 0.35 × 4 + 0.65 × 7 ≈ 21.3 expected heads (21.2 with the rounded responsibilities) out of 29.9 expected tosses, so the new θA ≈ 0.71. Coin B gets the other 11.7 heads out of 20.1 tosses, so θB ≈ 0.58.
To check the round helped, compute the log-likelihood of the five runs (natural logs, a 1/2 chance for each coin, binomial coefficients left out since they only add a constant). By our calculation it rises from about −33.09 at (0.6, 0.5) to about −31.86 at the new estimates. Repeat until the estimates stop changing in the third decimal place.
Why does the likelihood never decrease in EM?
The 1977 paper derived "the monotone behaviour of the likelihood" and the convergence of the algorithm (Dempster, Laird and Rubin). Wikipedia notes that their convergence analysis was flawed and that C. F. Jeff Wu published a correct one in 1983. The monotone part is short. Because log is concave, Jensen's inequality lets the E-step build a lower bound on the log-likelihood that touches it at your current parameters. The M-step pushes the bound as high as it goes, and the true curve never dips below it, so log L(new) ≥ bound(new) ≥ bound(old) = log L(old).
Never going down is not finding the best answer. As Wikipedia puts it, "multiple maxima may occur, with no guarantee that the global maximum will be found", and EM can stop at a local maximum or a saddle point. With θA = θB, every responsibility is 0.5, both coins update to 33/50 = 0.66, and EM stays there for good. So start from several guesses and keep the run with the highest final log-likelihood.
How is EM used for Gaussian mixtures?
The 1977 paper lists finite mixture models among its examples. Replace the coins with K bell curves, each with mean μk, variance σk² and weight πk:
E-step: r_ik = πk N(x_i; μk, σk²) divided by the sum of πj N(x_i; μj, σj²) over all components j.
M-step: Nk = Σ r_ik, then μk = Σ r_ik x_i / Nk, σk² = Σ r_ik (x_i − μk)² / Nk and πk = Nk / n.
These are the usual mean and variance, with each point weighted by its responsibility. Hidden Markov models are trained with a special case of EM, the Baum–Welch algorithm; our hidden Markov model forward algorithm walkthrough shows the model it trains.
A checklist for any EM problem
Name the hidden variable (which coin, cluster or state).
Write the likelihood as if you could see it.
E-step: each point's responsibilities add up to 1.
M-step: use weighted versions of the full-data formulas.
Track the log-likelihood. If it ever falls, you have a bug.
Use several starts, never with identical components.
If Bayes' rule feels shaky, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning.



