Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Bayesian statistics

The Expectation–Maximization (EM) Algorithm

Many models have hidden variables: which cluster a point came from, whether a sensor reading is an outlier, when a censored part would have failed. EM finds maximum-likelihood estimates by alternating two easy steps: guess the hidden variables' distribution given the current parameters (E-step), then refit the parameters as if those guesses were data (M-step). You will derive EM from the ELBO, prove it never decreases the likelihood, fit Gaussian mixtures, reject lidar outliers, handle censored lifetimes, and see its weaknesses: local optima and slow convergence.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Sorting a mixed bag

Imagine a bag of marbles from two factories. One factory makes small marbles and the other makes big ones, but nobody labelled them. To learn each factory’s typical size:

  1. Guess the two typical sizes.
  2. For each marble, decide how likely it is to be from each factory.
  3. Re-average the sizes using those likelihoods as weights.
  4. Repeat until nothing changes.

That loop is Expectation–Maximization. Each round fits the data at least as well as the last.

Guess the hidden labels, refit, repeat: EM for a mixture
log-likelihood
weights 0.50/0.50 · means -2.50, -2.00 · sds 1.50, 1.50 · log-lik -554.0
Truth: 40% from N(-1.5, 0.6²), 60% from N(1.5, 1²). E-step: colour each point by how likely it is to belong to each component (faded = uncertain). M-step: refit each component as a weighted average of the points. The log-likelihood never goes down. When the clusters overlap, convergence slows. Some starting points end in a poorer fit.
Why engineers care

EM is how robots learn maps with uncertain data associations, how radar trackers separate clutter from targets, how speech and gesture recognizers train hidden Markov models, and how reliability engineers estimate failure rates when some parts haven’t failed yet.

📝 Quick check (Level 0)
0/3 answered
  1. 1. EM is useful when the data have…
  2. 2. EM alternates between…
  3. 3. Each EM round makes the fit…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The two steps for a Gaussian mixture

Model: p(x)=∑kπk N(x∣μk,σk2)p(x) = \sum_k\pi_k\,\mathcal N(x\mid\mu_k, \sigma_k^2), with a hidden label ziz_i for each point.

E-step (responsibilities):

rik=P(zi=k∣xi)=πk N(xi∣μk,σk2)∑jπj N(xi∣μj,σj2)r_{ik} = P(z_i = k\mid x_i) = \frac{\pi_k\,\mathcal N(x_i\mid\mu_k, \sigma_k^2)}{\sum_j\pi_j\,\mathcal N(x_i\mid\mu_j, \sigma_j^2)}

M-step (weighted MLEs), with Nk=∑irikN_k = \sum_ir_{ik}:

πk=Nkn,μk=1Nk∑irikxi,σk2=1Nk∑irik(xi−μk)2\pi_k = \frac{N_k}n, \qquad \mu_k = \frac1{N_k}\sum_ir_{ik}x_i, \qquad \sigma_k^2 = \frac1{N_k}\sum_ir_{ik}(x_i - \mu_k)^2

If the labels were known, the MLE would be a separate average per group. EM replaces each unknown label with its probability. With hard assignments (each rikr_{ik} 0 or 1) and equal fixed variances, EM becomes k-means.

📝 Quick check (Level 1)
0/3 answered
  1. 1. A responsibility r_ik is…
  2. 2. In the M-step, the new mean μ_k is…
  3. 3. k-means is EM with…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

General EM

Observed data xx, hidden zz, parameters θ\theta. Repeat:

  • E-step: compute q(z)=p(z∣x,θ(t))q(z) = p(z\mid x, \theta^{(t)}) and form Q(θ∣θ(t))=Eq[log⁡p(x,z∣θ)]Q(\theta\mid\theta^{(t)}) = \mathbb E_{q}[\log p(x, z\mid\theta)].
  • M-step: θ(t+1)=arg⁡max⁡θQ(θ∣θ(t))\theta^{(t+1)} = \arg\max_\theta Q(\theta\mid\theta^{(t)}).

EM is useful when the complete-data log-likelihood log⁡p(x,z∣θ)\log p(x, z\mid\theta) is easy to maximize (often closed form) but the marginal log⁡∑zp(x,z∣θ)\log\sum_zp(x, z\mid\theta) isn’t.

Practicalities

  • Local optima. EM only climbs. Use several random restarts or k-means++ initialization, and keep the best log-likelihood.
  • Degeneracy. A component can collapse onto one point (σk→0\sigma_k\to0, likelihood → ∞). Add a variance floor or a prior, which turns EM into MAP-EM.
  • Speed. Convergence is linear, with rate set by the fraction of missing information. Overlapping clusters converge slowly.
  • Choosing K: BIC (A5.2-5) or cross-validated likelihood.
  • Work in log space (log-sum-exp) for the E-step.
🐍 Exercise 1: EM for a Gaussian mixture
Implement the log-space E-step and the M-step for a K-component 1-D mixture. Check that the log-likelihood never decreases and that the true weights, means and sds are recovered.
📝 Quick check (Level 2)
0/3 answered
  1. 1. EM helps when this is easy to maximize:
  2. 2. To escape poor local optima, use…
  3. 3. A component shrinking onto a single point makes the likelihood…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

EM as coordinate ascent on the ELBO

For any q(z)q(z) (A5.2-9):

log⁡p(x∣θ)=Eq[log⁡p(x,z∣θ)]+H[q]⏟L(q,θ)+KL⁡(q ∥ p(z∣x,θ))\log p(x\mid\theta) = \underbrace{\mathbb E_q[\log p(x, z\mid\theta)] + H[q]}_{\mathcal L(q, \theta)} + \operatorname{KL}(q\,\|\,p(z\mid x, \theta))
  • E-step: maximize L\mathcal L over qq. With θ\theta fixed, the optimum is q=p(z∣x,θ(t))q = p(z\mid x, \theta^{(t)}). The KL becomes 0 and the bound touches: L(q,θ(t))=log⁡p(x∣θ(t))\mathcal L(q, \theta^{(t)}) = \log p(x\mid\theta^{(t)}).
  • M-step: maximize L\mathcal L over θ\theta. H[q]H[q] doesn’t depend on θ\theta, so this maximizes QQ.

Monotonicity: log⁡p(x∣θ(t+1))≥L(q,θ(t+1))≥L(q,θ(t))=log⁡p(x∣θ(t))\log p(x\mid\theta^{(t+1)}) \ge\mathcal L(q, \theta^{(t+1)}) \ge\mathcal L(q, \theta^{(t)}) = \log p(x\mid\theta^{(t)}). ∎ Under mild conditions, EM converges to a stationary point (Wu, 1983). When the exact posterior is intractable, restricting qq gives variational EM.

The censored-exponential example

Lifetimes T∼Exp(λ)T\sim\text{Exp}(\lambda), with some units still alive at time tit_i. By memorylessness, E[T∣T>t]=t+1/λ\mathbb E[T\mid T > t] = t + 1/\lambda. The M-step is λ=n/∑iE[Ti]\lambda = n/\sum_i\mathbb E[T_i]. Its fixed point solves n/λ=∑ti+c/λn/\lambda = \sum t_i + c/\lambda (with cc censored units), so λ=(n−c)/∑ti=d/∑ti\lambda = (n - c)/\sum t_i = d/\sum t_i, the closed-form censored MLE.

Exercise 3.1: Show that the E-step makes the bound tight

With q=p(z∣x,θ)q = p(z\mid x, \theta), KL⁡(q ∥ p(z∣x,θ))=0\operatorname{KL}(q\,\|\,p(z\mid x, \theta)) = 0, so L=log⁡p(x∣θ)\mathcal L = \log p(x\mid\theta). Directly: Eqlog⁡p(x,z)p(z∣x)=Eqlog⁡p(x)=log⁡p(x)\mathbb E_q\log\frac{p(x, z)}{p(z\mid x)} = \mathbb E_q\log p(x) = \log p(x).

📝 Quick check (Level 3)
0/3 answered
  1. 1. After the E-step, the ELBO equals…
  2. 2. EM's monotonicity follows from…
  3. 3. E[T | T > t] for Exp(λ) is…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Rejecting spurious lidar returns

A lidar beam aimed at a wall returns mostly good ranges N(μ,σ2)\mathcal N(\mu, \sigma^2). Some returns are spurious (dust, multipath, rain), which we model as Uniform(rmin⁡,rmax⁡)\text{Uniform}(r_{\min}, r_{\max}) with probability ε\varepsilon. The beam-model mixture in Thrun, Burgard and Fox (2005) has exactly this form. EM estimates (μ,σ,ε)(\mu, \sigma, \varepsilon), and the responsibilities label each return as inlier or outlier. The plain mean is dragged far off; EM is not.

🐍 Exercise 2 (research): EM range estimation with outliers
Fit a Gaussian-plus-uniform mixture to lidar ranges by EM. Recover the true range, noise and outlier rate, classify readings, and compare with the naive mean.

EM in AI, robotics and aerospace

  • Hidden Markov models: Baum–Welch is EM with the forward–backward E-step. It’s used in speech, gesture and fault-mode recognition (Rabiner, 1989).
  • Mapping and SLAM: EM over unknown data associations or correspondences (Thrun, Burgard and Fox, 2005). Point-cloud registration as EM over soft correspondences, as in Coherent Point Drift (Myronenko and Song, 2010).
  • Target tracking: probabilistic multi-hypothesis tracking (PMHT) applies EM to measurement-to-target assignment (Streit and Luginbuhl, 1995).
  • System identification: EM for linear state-space models with a Kalman-smoother E-step (Shumway and Stoffer, 1982), which learns process and measurement noise.
  • Reliability: censored and interval-censored lifetime data from fleets and test campaigns.

References

  1. A. Dempster, N. Laird, D. Rubin (1977). “Maximum likelihood from incomplete data via the EM algorithm.” JRSS B, 39(1).
  2. C. F. J. Wu (1983). “On the convergence properties of the EM algorithm.” Annals of Statistics, 11(1).
  3. R. Neal, G. Hinton (1998). “A view of the EM algorithm that justifies incremental, sparse, and other variants.” In Learning in Graphical Models.
  4. G. McLachlan, T. Krishnan (2008). The EM Algorithm and Extensions, 2nd ed. Wiley.
  5. C. Do, S. Batzoglou (2008). “What is the expectation maximization algorithm?” Nature Biotechnology, 26(8).
  6. L. Rabiner (1989). “A tutorial on hidden Markov models and selected applications in speech recognition.” Proceedings of the IEEE, 77(2).
  7. S. Thrun, W. Burgard, D. Fox (2005). Probabilistic Robotics. MIT Press.
  8. A. Myronenko, X. Song (2010). “Point set registration: Coherent Point Drift.” IEEE TPAMI, 32(12).
  9. R. Shumway, D. Stoffer (1982). “An approach to time series smoothing and forecasting using the EM algorithm.” Journal of Time Series Analysis, 3(4).
  10. R. Streit, T. Luginbuhl (1995). “Probabilistic multi-hypothesis tracking.” NUWC-NPT Technical Report 10,428.