Search

Search papers and lessons

Expectation-Maximization Algorithm

Level 3 · 10 min read

Expectation-maximization fits a model with hidden variables by alternating between guessing the hidden parts and refitting the parameters, and each round never lowers the likelihood.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Inference
  • Discrete math
  • Information theory
  • Optimization
  • Sign in to see your progress

TL;DR

Expectation-maximization (EM) finds maximum-likelihood parameters when some variables are hidden. It alternates two steps. The E-step computes, for each data point, the probability of each hidden value given the current parameters. The M-step refits the parameters as if those probabilities were known. Each round never decreases the likelihood of the data.

Where you'll see it: fitting a mixture of Gaussians to clustered data, estimating the parameters of a hidden Markov model, filling in missing entries in a data table, and the training of many latent-variable models.

Intuition

Suppose you have heights from two groups, men and women, but the group label was lost. If you knew which group each person came from, fitting two bell curves would be easy: take the average and spread of each group. If you knew the two bell curves, deciding which group each person came from would also be easy: ask which curve gives each height a higher density. Neither piece is known, so you alternate. Guess the curves, assign people softly to them, refit the curves using the soft assignments, and repeat.

This is the core of EM. The hidden label is the missing information. The E-step fills it in as a probability rather than a hard choice, so a person near the boundary counts partly for each group. The M-step is then an ordinary maximum-likelihood fit with weighted data. Each full round climbs the likelihood a little, and the process stops when the climb is negligible.

The method exists because direct maximisation is often blocked. The likelihood of a mixture has a sum inside a logarithm, and setting its derivatives to zero gives equations that cannot be solved in closed form. Splitting the problem into "guess the hidden part" and "fit the rest" turns one hard problem into two easy ones, repeated.

The math

Let xx be the observed data, zz the hidden variables, and θ\theta the parameters. The quantity we want to maximise is the observed-data log-likelihood log⁡p(x∣θ)\log p(x \mid \theta), where the hidden variables have been summed or integrated out. Let θt\theta_t be the current parameter estimate.

Definition: EM iteration

The E-step computes the expected complete-data log-likelihood under the current posterior over the hidden variables:

Q(θ∣θt)=Ez∼p(z∣x,θt)[log⁡p(x,z∣θ)].Q(\theta \mid \theta_t) = \mathbb{E}_{z \sim p(z \mid x, \theta_t)}\big[\log p(x, z \mid \theta)\big].

The M-step chooses the next parameters to maximise it:

θt+1=arg⁡max⁡θ Q(θ∣θt).\theta_{t+1} = \arg\max_{\theta} \, Q(\theta \mid \theta_t).

The expectation in QQ is the average over zz, weighted by how probable each hidden value is given xx and the current parameters. The logarithm sits inside the expectation, so the sum over zz does not block the log, and the maximisation in the M-step is usually easy.

Why the likelihood never decreases

Use the identity from the evidence lower bound with q(z)=p(z∣x,θt)q(z) = p(z \mid x, \theta_t):

log⁡p(x∣θ)=L(q,θ)+DKL(p(z∣x,θt) ∥ p(z∣x,θ)).\log p(x \mid \theta) = \mathcal{L}(q, \theta) + D_{\mathrm{KL}}\big(p(z \mid x, \theta_t)\,\|\,p(z \mid x, \theta)\big).

The E-step makes the lower bound tight at θt\theta_t, because the KL term vanishes when θ=θt\theta = \theta_t. The M-step increases L\mathcal{L} over θ\theta (the only θ\theta-dependent part of L\mathcal{L} is QQ, so maximising QQ maximises L\mathcal{L}), and the KL term is never negative. So

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).

The likelihood climbs at every step. It does not guarantee the global maximum, only a stationary point, so the starting values matter.

The Gaussian mixture case

For a mixture of KK Gaussians with weights πk\pi_k, means μk\mu_k and variances σk2\sigma_k^2, the hidden variable is the component label ziz_i of each point xix_i. The E-step computes responsibilities, the posterior probability that point ii came from component kk:

γik=πk N(xi∣μk,σk2)∑jπj N(xi∣μj,σj2).\gamma_{ik} = \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)}.

The M-step re-estimates each component using these soft counts. With nn points and Nk=∑iγikN_k = \sum_i \gamma_{ik},

πk=Nkn,μk=1Nk∑iγik xi,σk2=1Nk∑iγik(xi−μk)2.\pi_k = \frac{N_k}{n}, \qquad \mu_k = \frac{1}{N_k}\sum_{i} \gamma_{ik} \, x_i, \qquad \sigma_k^2 = \frac{1}{N_k}\sum_{i} \gamma_{ik} (x_i - \mu_k)^2.

These are ordinary means and variances in which each point counts with weight γik\gamma_{ik} rather than 0 or 1. The Mixture Model lesson covers the model itself.

Worked examples

Example 1: one EM step for two Gaussians

Two data points, x=0x = 0 and x=2x = 2. Both components have variance 1 and start with equal weights π1=π2=0.5\pi_1 = \pi_2 = 0.5. The initial means are μ1=0\mu_1 = 0 and μ2=3\mu_2 = 3.

Example
One round of EM with two unit-variance Gaussians
  1. Densities at the start. For a unit-variance normal, N(x∣μ,1)=0.39894 e−(x−μ)2/2\mathcal{N}(x \mid \mu, 1) = 0.39894\, e^{-(x - \mu)^2 / 2}.
  2. Point x=0x = 0: N(0∣0,1)=0.39894\mathcal{N}(0 \mid 0, 1) = 0.39894 and N(0∣3,1)=0.39894 e−4.5=0.00443\mathcal{N}(0 \mid 3, 1) = 0.39894\, e^{-4.5} = 0.00443.
  3. Point x=2x = 2: N(2∣0,1)=0.39894 e−2=0.05399\mathcal{N}(2 \mid 0, 1) = 0.39894\, e^{-2} = 0.05399 and N(2∣3,1)=0.39894 e−0.5=0.24197\mathcal{N}(2 \mid 3, 1) = 0.39894\, e^{-0.5} = 0.24197.
  4. E-step, responsibilities. For x=0x = 0: γ1=0.39894/(0.39894+0.00443)=0.98901\gamma_1 = 0.39894 / (0.39894 + 0.00443) = 0.98901, so γ2=0.01099\gamma_2 = 0.01099. For x=2x = 2: γ1=0.05399/(0.05399+0.24197)=0.18243\gamma_1 = 0.05399 / (0.05399 + 0.24197) = 0.18243, so γ2=0.81757\gamma_2 = 0.81757.
  5. M-step, weights: N1=0.98901+0.18243=1.17144N_1 = 0.98901 + 0.18243 = 1.17144 and N2=0.01099+0.81757=0.82856N_2 = 0.01099 + 0.81757 = 0.82856. So π1=1.17144/2=0.58572\pi_1 = 1.17144 / 2 = 0.58572 and π2=0.41428\pi_2 = 0.41428.
  6. M-step, means: μ1=(0.98901×0+0.18243×2)/1.17144=0.31144\mu_1 = (0.98901 \times 0 + 0.18243 \times 2) / 1.17144 = 0.31144. μ2=(0.01099×0+0.81757×2)/0.82856=1.97348\mu_2 = (0.01099 \times 0 + 0.81757 \times 2) / 0.82856 = 1.97348.
  7. Check the likelihood. Before the round, log⁡p(x)=log⁡(0.5⋅0.40337)+log⁡(0.5⋅0.29596)=−1.6010−1.9107=−3.5117\log p(x) = \log(0.5 \cdot 0.40337) + \log(0.5 \cdot 0.29596) = -1.6010 - 1.9107 = -3.5117. After the round, the same calculation with the new weights and means (variances kept at 1) gives −2.9095-2.9095. The likelihood rose.

Interpretation: the point at 0 now belongs almost entirely to component 1 and the point at 2 is pulled towards component 2. Two rounds would move the means closer to the true structure, and the likelihood would keep rising towards a stationary point.

Example 2: two biased coins, chosen at random

Each session, one of two coins is picked at random and flipped 10 times. You only see the head counts, not which coin was used. The hidden variable is the coin label of each session. Let the biases start at θA=0.6\theta_A = 0.6 and θB=0.5\theta_B = 0.5. The observed head counts are 5, 9 and 8. Assume the coins are picked with equal probability, so the weights cancel.

Example
One round of EM for a mixture of two coins
  1. Likelihood of a session with hh heads under bias θ\theta is proportional to θh(1−θ)10−h\theta^h (1 - \theta)^{10 - h}. The binomial coefficient is the same for both coins and cancels in the responsibilities.
  2. Session with 5 heads. Under A: 0.65⋅0.45=0.0007960.6^5 \cdot 0.4^5 = 0.000796. Under B: 0.510=0.0009770.5^{10} = 0.000977. Responsibility for A: 0.000796/(0.000796+0.000977)=0.4490.000796 / (0.000796 + 0.000977) = 0.449.
  3. Session with 9 heads. Under A: 0.69⋅0.4=0.0040310.6^9 \cdot 0.4 = 0.004031. Under B: 0.0009770.000977. Responsibility for A: 0.004031/0.005008=0.8050.004031 / 0.005008 = 0.805.
  4. Session with 8 heads. Under A: 0.68⋅0.42=0.0026870.6^8 \cdot 0.4^2 = 0.002687. Under B: 0.0009770.000977. Responsibility for A: 0.002687/0.003664=0.7330.002687 / 0.003664 = 0.733.
  5. M-step for A. Expected heads from A are 0.449×5+0.805×9+0.733×8=15.3580.449 \times 5 + 0.805 \times 9 + 0.733 \times 8 = 15.358, and expected flips from A are 10×(0.449+0.805+0.733)=19.87610 \times (0.449 + 0.805 + 0.733) = 19.876. So θA=15.358/19.876=0.773\theta_A = 15.358 / 19.876 = 0.773.
  6. M-step for B. Responsibilities for B are 0.5510.551, 0.1950.195 and 0.2670.267. Expected heads are 6.6426.642 and expected flips 10.12410.124, so θB=0.656\theta_B = 0.656.

Interpretation: after one round, coin A is the coin that gives many heads, at 0.77, and coin B is the one closer to fair, at 0.66. The two high-count sessions are mostly attributed to A, and the middle session is split. Repeating the round would refine both biases. Note that the answer depends on the starting guesses. A different start can converge to a different labelling of the coins.

Tradeoffs and failure modes

EM is attractive when the complete-data problem is easy and the hidden variables are a small, well-understood part of the model. It becomes difficult in several common situations.

SituationProblemBetter choice
Starting values are poorEM converges to a local maximum, and different starts give different answersRun several random starts, or initialise with k-means
A mixture component collapses onto a single pointIts variance shrinks towards zero and the likelihood grows without boundPut a lower bound on variances, or use a Bayesian prior on them
The number of components is unknownEM with fixed KK cannot choose KKCompare fits with an information criterion such as BIC, or use a Bayesian nonparametric model
The E-step posterior has no closed formThe expectation cannot be computed exactlyVariational EM, which uses a tractable qq, or Monte Carlo EM with [[monte-carlo-method
Convergence is slow near a flat regionMany iterations make little progressAccelerated EM variants, or switch to direct gradient methods
Full posterior uncertainty is neededEM gives only a point estimateBayesian inference with [[markov-chain-monte-carlo

A common mistake is to treat the responsibilities as hard labels too early. Using them as labels turns EM into k-means, which is a useful limit when the variances are tiny but gives a different and less accurate fit in general.

History

Special cases of EM appeared long before the general framework. In the 1890s Karl Pearson fitted a mixture of two normal curves using the method of moments, which is a different method. Later authors derived particular EM-like procedures for specific models, including the Baum–Welch algorithm for hidden Markov models in the 1960s and 1970s.

The general method was named and analysed in 1977 by Arthur Dempster, Nan Laird and Donald Rubin in a paper read to the Royal Statistical Society. They showed that the procedure always increases the likelihood and identified the E-step and M-step as a general pattern for incomplete-data problems. The paper made EM a standard tool in statistics and machine learning.

It caught on because it reduced hard maximum-likelihood problems with missing data to repeated simple fits. Later work on the variational lower bound explained EM as one case of a broader family of methods, which is why the same ideas appear in modern latent-variable models.

Check yourself

QuestionRecall: what are the two steps of EM, and what does each compute?Show answer

The E-step computes the expected complete-data log-likelihood, where the expectation is taken over the hidden variables using their posterior under the current parameters. The M-step chooses new parameters that maximise that expected log-likelihood. In a mixture, the E-step produces responsibilities and the M-step refits the weights, means and variances using them.

QuestionComputation: a point is equally likely under two unit-variance Gaussians with means 0 and 2, and the weights are equal. What is its responsibility for the first component?Show answer

The density at the point is the same for both components, so the ratio of their terms is 1 times the weight ratio. With equal weights, the responsibility is 0.5/(0.5+0.5)=0.50.5 / (0.5 + 0.5) = 0.5. The point is shared evenly between the two components.

QuestionWhy: why does EM never decrease the likelihood, even though the M-step only maximises a lower bound?Show answer

The E-step makes the lower bound tight at the current parameters, so it equals the log-likelihood there. The M-step increases the bound, and because the bound never exceeds the log-likelihood, the log-likelihood at the new parameters is at least as large as the bound, which is at least as large as the old log-likelihood. The gap between them is a KL divergence, which is never negative.

QuestionApplied: you fit a two-component mixture to data and one component's variance drops to nearly zero, with the likelihood still rising. What is happening, and what would you change?Show answer

One component has collapsed onto a few points, so its density at those points grows without bound as the variance shrinks. The likelihood is not a sensible guide to fit in this case. Constrain the variance from below, or place a prior on it, and check that the model is not overfitting a small cluster. Restarting from different initial values is also useful.

Sources