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 be the observed data, the hidden variables, and the parameters. The quantity we want to maximise is the observed-data log-likelihood , where the hidden variables have been summed or integrated out. Let be the current parameter estimate.
The E-step computes the expected complete-data log-likelihood under the current posterior over the hidden variables:
The M-step chooses the next parameters to maximise it:
The expectation in is the average over , weighted by how probable each hidden value is given and the current parameters. The logarithm sits inside the expectation, so the sum over 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 :
The E-step makes the lower bound tight at , because the KL term vanishes when . The M-step increases over (the only -dependent part of is , so maximising maximises ), and the KL term is never negative. So
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 Gaussians with weights , means and variances , the hidden variable is the component label of each point . The E-step computes responsibilities, the posterior probability that point came from component :
The M-step re-estimates each component using these soft counts. With points and ,
These are ordinary means and variances in which each point counts with weight 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, and . Both components have variance 1 and start with equal weights . The initial means are and .
- Densities at the start. For a unit-variance normal, .
- Point : and .
- Point : and .
- E-step, responsibilities. For : , so . For : , so .
- M-step, weights: and . So and .
- M-step, means: . .
- Check the likelihood. Before the round, . After the round, the same calculation with the new weights and means (variances kept at 1) gives . 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 and . The observed head counts are 5, 9 and 8. Assume the coins are picked with equal probability, so the weights cancel.
- Likelihood of a session with heads under bias is proportional to . The binomial coefficient is the same for both coins and cancels in the responsibilities.
- Session with 5 heads. Under A: . Under B: . Responsibility for A: .
- Session with 9 heads. Under A: . Under B: . Responsibility for A: .
- Session with 8 heads. Under A: . Under B: . Responsibility for A: .
- M-step for A. Expected heads from A are , and expected flips from A are . So .
- M-step for B. Responsibilities for B are , and . Expected heads are and expected flips , so .
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.
| Situation | Problem | Better choice |
|---|---|---|
| Starting values are poor | EM converges to a local maximum, and different starts give different answers | Run several random starts, or initialise with k-means |
| A mixture component collapses onto a single point | Its variance shrinks towards zero and the likelihood grows without bound | Put a lower bound on variances, or use a Bayesian prior on them |
| The number of components is unknown | EM with fixed cannot choose | Compare fits with an information criterion such as BIC, or use a Bayesian nonparametric model |
| The E-step posterior has no closed form | The expectation cannot be computed exactly | Variational EM, which uses a tractable , or Monte Carlo EM with [[monte-carlo-method |
| Convergence is slow near a flat region | Many iterations make little progress | Accelerated EM variants, or switch to direct gradient methods |
| Full posterior uncertainty is needed | EM gives only a point estimate | Bayesian 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 . 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.