Skip to content

The EM Algorithm

Three updates, each of which needs the others’ answers. §11.3 does the only thing available: run them in a loop and hope. The reason it is not merely hope is one sentence:

Every step in the EM algorithm increases the log-likelihood function (Neal and Hinton, 1999).

  • The algorithm as §11.3 states it: initialise, E-step (Equation 11.53), M-step (Equations 11.54–11.56), repeat.
  • Example 11.6’s claim that “after five iterations, the EM algorithm converges” — reproduced, and made precise: ΔL<106\lvert\Delta L\rvert < 10^{-6} first at iteration 5\mathbf{5}, though the parameters keep moving until 1111.
  • The monotonicity guarantee, measured to the last bit. Over 180,000180{,}000 individual EM steps from 30003000 random starts: 11,16311{,}163 register a negative change, none larger than 8.9×10158.9\times10^{-15}, with a median of 1.776×10151.776\times10^{-15} — exactly one ULP of a log-likelihood near 14-14.
  • That EM converges linearly, and what sets the rate: component overlap. 10451045 iterations when 53%53\% of points are ambiguous, 33 when none are.
  • What a better initialisation buys: K-means first cuts the median from 5858 iterations to 19\mathbf{19} and takes the best optimum from 96.0%96.0\% to 100%\mathbf{100\%} of runs.
  • Why “converged” needs a definition: the log-likelihood settles several iterations before the parameters do.

Intuition: two problems, each easy given the other

Section titled “Intuition: two problems, each easy given the other”

The obstacle page 1102 measured was circular. The responsibilities need the parameters; the parameters need the responsibilities. Neither can be written down first.

EM breaks the circle by refusing to solve both at once. Fix the parameters and the responsibilities are a formula. Fix the responsibilities and the parameters are three weighted averages. Alternate.

What makes this more than wishful thinking is that the alternation is not merely plausible — each half is an exact maximisation of a bound on the log-likelihood, so the bound can never fall, and §11.4.5 will show where that bound comes from.

diagram Section 11.3's loop mermaid

1. Initialise μk\boldsymbol\mu_k, Σk\boldsymbol\Sigma_k, πk\pi_k.

2. E-step. Evaluate the responsibilities — “posterior probability of data point nn belonging to mixture component kk:

rnk=πkN(xnμk,Σk)jπjN(xnμj,Σj)(11.53)r_{nk} = \frac{\pi_k\mathcal{N}(\mathbf{x}_n\mid\boldsymbol\mu_k,\boldsymbol\Sigma_k)}{\sum_j\pi_j\mathcal{N}(\mathbf{x}_n\mid\boldsymbol\mu_j,\boldsymbol\Sigma_j)} \qquad \text{(11.53)}

3. M-step. Reestimate the parameters using those responsibilities:

μk=1Nknrnkxn,Σk=1Nknrnk(xnμk)(xnμk),πk=NkN(11.54)–(11.56)\boldsymbol\mu_k = \frac{1}{N_k}\sum_{n}r_{nk}\mathbf{x}_n, \quad \boldsymbol\Sigma_k = \frac{1}{N_k}\sum_{n}r_{nk}(\mathbf{x}_n-\boldsymbol\mu_k)(\mathbf{x}_n-\boldsymbol\mu_k)^\top, \quad \pi_k = \frac{N_k}{N} \qquad \text{(11.54)–(11.56)}

Page 1105 measured why the order inside the M-step matters: using the pre-update means in Equation 11.55 gives 19.9819.98 where the book’s Example 11.4 gives 1.531.53.

iterationL-Lchange in LLlargest parameter move
0028.32553628.325536
1114.41048514.410485+1.391505×101+1.391505\times10^{1}4.2957134.295713
2213.97705813.977058+4.334278×101+4.334278\times10^{-1}1.657959×1011.657959\times10^{-1}
3313.97334213.973342+3.715965×103+3.715965\times10^{-3}2.158136×1022.158136\times10^{-2}
4413.97332413.973324+1.786044×105+1.786044\times10^{-5}5.611063×1035.611063\times10^{-3}
5513.973323\mathbf{13.973323}+8.687247×107+8.687247\times10^{-7}1.323449×1031.323449\times10^{-3}
8813.97332313.973323+1.626841×1010+1.626841\times10^{-10}1.810021×1051.810021\times10^{-5}
figure Figure 11.8(b), and what 'increases the log-likelihood' means in floating point matplotlib
Left, a curve dropping steeply from 28.3255 to 14.4105 in one step and then flattening, with a dashed vertical marker at iteration five. Right, a log-scale histogram with a wide blue distribution spanning fifteen orders of magnitude and a single narrow red spike sitting on a dashed vertical line at the far left. Left, a curve dropping steeply from 28.3255 to 14.4105 in one step and then flattening, with a dashed vertical marker at iteration five. Right, a log-scale histogram with a wide blue distribution spanning fifteen orders of magnitude and a single narrow red spike sitting on a dashed vertical line at the far left.
On the right, every one of the 2,268 steps that registered a decrease falls in a single bin at the rounding error of the quantity itself — the dashed line is one unit in the last place of a log-likelihood near minus 14. The increases span from there to more than 100 nats.

The book states Neal and Hinton’s result and moves on. Measured over 30003000 random restarts on the book’s seven points, 180,000180{,}000 individual EM steps:

value
steps registering any negative change11,16311{,}163
steps decreasing by more than 101210^{-12}0\mathbf{0}
steps decreasing by more than 101010^{-10}0\mathbf{0}
worst single decrease8.882×1015-8.882\times10^{-15}
median of the negative changes1.776×10151.776\times10^{-15}
one ULP of a log-likelihood near 14-141.776×10151.776\times10^{-15}
largest single increase180.564829180.564829

EM converges linearly — the error shrinks by a constant factor each step, not quadratically as Newton’s method would. The factor is set by how much the components overlap. Two clusters of 100100 points each at ±d\pm d with unit variance:

ddambiguous points (maxkrnk<0.9\max_k r_{nk} < 0.9)iterations to ΔL<108\lvert\Delta L\rvert < 10^{-8}measured rate
1.01.053.0%53.0\%1045\mathbf{1045}0.98720.9872
1.51.516.0%16.0\%46460.68970.6897
2.02.02.0%2.0\%15150.19430.1943
3.03.00.0%0.0\%770.00440.0044
5.05.00.0%0.0\%3\mathbf{3}
figure Overlap is what makes EM slow, and a cheap initialisation is what makes it fast matplotlib
Left, a log-scale curve of iterations against percentage of ambiguous points, climbing from 3 to over a thousand. Right, two overlapping log-scale histograms of iteration counts, the blue one tightly concentrated near twenty and the red one spread from twenty to over a thousand. Left, a log-scale curve of iterations against percentage of ambiguous points, climbing from 3 to over a thousand. Right, two overlapping log-scale histograms of iteration counts, the blue one tightly concentrated near twenty and the red one spread from twenty to over a thousand.
At a separation of 1.0 more than half the points are genuinely ambiguous and EM needs 1045 iterations; at 5.0 nothing is ambiguous and it needs 3. On the right, 120 restarts each on the same 300 points: running K-means first concentrates the iteration count and removes the long tail entirely.

A factor of 348348 between the two ends, on data that differs only in how far apart the clusters are. That is the practical meaning of “linear convergence with a problem-dependent rate”: when the responsibilities are near 0.50.5, each E-step barely moves them, so each M-step barely moves the parameters.

300300 points from three clusters, K=3K = 3, 200200 restarts of each strategy:

initialisationmedian iterationsmean iterationsshare reaching the best optimum
random data points5858128.6128.696.0%96.0\%
K-means first19\mathbf{19}19.0\mathbf{19.0}100%\mathbf{100\%}

The mean and median coincide for the K-means start, which is the interesting part: the long tail is gone entirely. Random starts occasionally land in a basin that takes hundreds of iterations to crawl out of; the K-means start never does, on this data.

This is why §11.5 points at K-means in the same breath as the GMM, and why every library implementation defaults to a K-means or K-means++ initialisation. It costs a few cheap iterations and removes both the worst case and the failures.

pch.quizTag Is EM clear?
  1. What does the E-step compute, and what does the M-step do with it?

    pch.quizShowAnswer

    B — The E-step computes the responsibilities from the current parameters; the M-step recomputes the parameters as three responsibility-weighted averages — Each half is easy given the other, and neither can be done first — that is the circularity page 1102 measured. EM alternates, and because each half exactly maximises a bound on the log-likelihood, the bound can never fall.

  2. Over 180,000 EM steps, how often did the log-likelihood decrease?

    pch.quizShowAnswer

    B — 11,163 times, but never by more than 8.9e-15 — the median decrease is exactly one unit in the last place — Reporting this as 'never decreased' would be wrong. The honest statement is that no step decreased it by more than the rounding error of the quantity itself. The practical consequence: test new_L < old_L - tol, not new_L < old_L, or your convergence check will fire spuriously.

  3. What sets how many iterations EM needs?

    pch.quizShowAnswer

    B — How much the components overlap — 1045 iterations when 53 percent of points are ambiguous, 3 when none are — A factor of 348 between the two ends, on data differing only in cluster separation. When responsibilities sit near 0.5 the E-step barely moves them, so the M-step barely moves the parameters — that is linear convergence with a problem-dependent rate.

  4. What does initialising with K-means buy?

    pch.quizShowAnswer

    B — Fewer iterations — median 19 against 58 — and it removes the long tail entirely, but the optimum it reaches is the same one — Mean and median coincide at 19 for the K-means start, against a mean of 128.6 for random starts. Both find the same best value of -702.671823. No initialisation can outrun an unbounded objective — that needs a variance floor or a prior.

Exercise 1 – Run Example 11.6 and define “converged”

Section titled “Exercise 1 – Run Example 11.6 and define “converged””

Exercise 3 – Overlap is what makes it slow

Section titled “Exercise 3 – Overlap is what makes it slow”
  • EM breaks a circular problem by refusing to solve both halves at once: fix the parameters and the responsibilities are a formula; fix the responsibilities and the parameters are three weighted averages.
  • The E-step is Equation 11.53, the responsibilities. The M-step is Equations 11.54 to 11.56, in that order, each using what the step before produced.
  • Example 11.6’s ‘after five iterations’ is the 1e-06 log-likelihood criterion, reproduced exactly; at 1e-09 it takes eight.
  • The log-likelihood settles before the parameters do, at every tolerance, because it is flat near an optimum. Five against eleven at 1e-06.
  • Neal and Hinton’s guarantee, measured over 180,000 steps: 11,163 register a negative change and none is larger than 8.9e-15.
  • The median decrease is 1.776e-15, exactly one unit in the last place of a log-likelihood near minus fourteen. The theorem is about real numbers; this is what checking it in float64 looks like.
  • So a convergence test should compare against a tolerance, not against zero, or it will fire spuriously.
  • EM converges linearly, and overlap sets the rate: 1045 iterations when 53 percent of points are ambiguous, 3 when none are — a factor of 348.
  • When responsibilities sit near one half the E-step barely moves them, so the M-step barely moves the parameters.
  • A K-means initialisation cuts the median from 58 iterations to 19 and makes mean and median coincide — the long tail disappears.
  • It also takes the share of runs reaching the best optimum from 96 to 100 percent, which is why every library implementation defaults to it.
  • But it does not make the optimum global. No initialisation can outrun an unbounded objective; that needs a variance floor or a prior.

Next: The Latent Variable Perspective — §11.4, which explains where the responsibilities come from and why EM is the algorithm it is.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading