Machine Learning Techniques · IIT Madras BS Degree

Gaussian mixtures and the EM algorithm

Lectures 22 to 26 model clustered data with a two-step story: roll a K-sided die to pick a Gaussian, then draw the point from that Gaussian. The likelihood has a sum inside a log, so Jensen's inequality turns it into a lower bound that the EM algorithm climbs by alternating two closed-form steps.

Part 9 of 18 Instructor Prof. Arun Rajkumar About 104 min of video

At a glance

  • Data with several dense regions needs a density with several peaks. A Gaussian mixture model generates each point in two steps: pick component \(k\) with probability \(\pi_k\), then draw \(x_i \sim \mathcal N(\mu_k, \sigma_k^2)\). The chosen component \(z_i\) is a latent variable.
  • The likelihood is \(\prod_i \sum_k \pi_k\,\mathcal N(x_i;\mu_k,\sigma_k^2)\). Its log has a sum inside the log, so setting derivatives to zero gives no closed form.
  • The log is concave, so Jensen's inequality applies: \(\log\sum_k \lambda_k a_k \ge \sum_k \lambda_k \log a_k\) for weights \(\lambda_k \ge 0\) that sum to 1.
  • Multiplying and dividing by new weights \(\lambda_{ik}\) and applying Jensen gives a modified log-likelihood that is a lower bound for every \(\lambda\). With \(\lambda\) fixed, the best \(\theta\) is a weighted mean, variance and fraction; with \(\theta\) fixed, the best \(\lambda_{ik}\) is \(P(z_i = k \mid x_i)\) from Bayes' theorem.
  • EM alternates the two steps (E: update \(\lambda\); M: update \(\theta\)) until \(\theta\) stops changing. It is a soft version of Lloyd's algorithm that also learns variances, and it converges to a local maximum, so it is usually started from a K-means clustering.

The unsupervised half of the course has so far covered representation learning (PCA and kernel PCA, Parts 3 to 5), clustering with Lloyd's algorithm (Parts 6 and 7) and estimation by maximum likelihood and Bayesian methods (Part 8). This part continues with estimation, for data that is a little more realistic. The examples are one-dimensional because that is easiest to draw; everything carries over to higher dimensions.

Take real numbers on a line that fall into three separate groups. Estimation means proposing a generative story for the data and fitting its parameters. The values are real numbers, not 0s and 1s, so a Bernoulli model is out. The obvious choice is a single Gaussian. Maximum likelihood puts its mean at the sample mean, \(\hat\mu_{\text{ML}}\) (Part 8); take the variance as known, say 1.

Now look at the fitted density. Its peak sits in the middle of the data, and the two outer groups lie where the Gaussian has almost no probability. The model says data there should be rare, yet we saw plenty of it.

Maximum likelihood treats the assumed model as true and finds its best member. If no Gaussian can produce three separated groups, the best Gaussian is still a poor explanation, and maximum likelihood cannot do better than the family it searches.

What we want is a density with three modes, one high-density region for each group, so that the observed points come from places the model considers likely. A Gaussian has exactly one peak, so no single Gaussian will do. Each dense region on its own does look like a Gaussian, though. That suggests the new model: a mixture of Gaussians, in this example a mixture of three. The data clearly has a clustering behaviour, and the model will become a probabilistic way to describe it.

One Gaussian, fitted by maximum likelihood mean = sample mean −0.03, variance known (σ² = 1) A mixture of three Gaussians one bump per dense region μ̂ML data here,density ≈ 0 data here,density ≈ 0 mode 1 mode 2 mode 3 −6−4−20246
Same data, two models. Twelve illustrative points in three groups. Top: the maximum-likelihood Gaussian (mean at the sample mean, variance 1) gives the outer groups almost zero density. Bottom: a mixture of three Gaussians puts a mode on each group, so every point sits in a high-density region.

The generative story, in two steps

A generative story is the probabilistic mechanism we imagine produced the data. The stories so far had one step: toss a coin, or sample from a Gaussian. That is too simple for clustered data, and the mixture story has two steps. For now assume the number of mixtures \(K\) is given. "Mixture" and "cluster" mean the same thing here; mixture is the usual word.

  1. Generating one data point \(x_i\)
  2. Pick a mixture. Roll a die with \(K\) faces; face \(k\) comes up with probability \(\pi_k\). Call the face \(z_i\), the mixture that point \(i\) comes from.
  3. Draw from that mixture. Every mixture has its own Gaussian with its own mean and variance. Draw \(x_i\) from the Gaussian of mixture \(z_i\).
  4. To build a data set, repeat both steps for \(i = 1, \dots, n\). Each point gets its own roll of the die.
\[ P(z_i = k) = \pi_k, \qquad \sum_{k=1}^K \pi_k = 1, \quad 0 \le \pi_k \le 1; \qquad\qquad x_i \mid z_i \sim \mathcal N\big(\mu_{z_i},\, \sigma_{z_i}^2\big) \]

Standard name: the \(\pi_k\) are the mixing proportions. The lecture's example: with three mixtures and \(\pi_1 = 0.5\), \(\pi_2 = 0.25\), \(\pi_3 = 0.25\), half the points come from the first mixture and a quarter from each of the other two.

Step 2 is pictured as doors. If the die shows 5, walk through the fifth door; behind it sit a mean \(\mu_5\) and a variance \(\sigma_5^2\), and you sample a point from that Gaussian. The indicator \(z_i\) plays the same role as the cluster indicator \(z_i\) of K-means in Part 6, except that here it is drawn at random.

Step 1 · pick a mixture K Gaussians, one per face Step 2 · draw the point Die with K faces P(zᵢ = k) = πₖ π₁ + … + πK = 1 k = 1 : μ₁, σ₁² k = 2 : μ₂, σ₂² ⋮ k = K : μK, σK² zᵢ = 2 xᵢ ∼ N(μ₂, σ₂²) one real number zᵢ is never seen: latent xᵢ is what we observe repeat independently for i = 1, …, n
The two-step story as boxes. The first box holds \(\pi\) and outputs a face \(z_i\). The face selects one of the \(K\) Gaussian boxes, which outputs \(x_i\). Only \(x_i\) is recorded; the face stays hidden.

Latent variables and the parameter count

The data \(x_1, \dots, x_n\) are observed. The model also contains quantities we never see: the faces \(z_1, \dots, z_n\). Unobserved quantities that the observations depend on are called latent variables, and a model built on them is a latent variable model. The Gaussian mixture model (GMM) is one of the most commonly used. Its output depends on parameters we want to estimate and also on these hidden choices.

Which parameters pin the model down completely?

ParametersMeaningCount
\(\pi_1, \dots, \pi_K\)Mixing proportions. They sum to 1, so the last one follows from the others.K − 1 free
\(\mu_1, \dots, \mu_K\)One mean per mixtureK
\(\sigma_1^2, \dots, \sigma_K^2\)One variance per mixtureK
TotalOf order \(3K\)3K − 1

In Part 8 we estimated a single number, the bias of a coin or the mean of a Gaussian. Now there is a whole host of parameters, and looking at the data gives no obvious guess for the \(\pi\)'s, \(\mu\)'s and \(\sigma^2\)'s. Maximum likelihood is a principled method that gives an answer for any well-specified probabilistic model, so it is the next thing to try.

The likelihood depends on the unknowns \(\mu_1, \dots, \mu_K\), \(\sigma_1^2, \dots, \sigma_K^2\), \(\pi_1, \dots, \pi_K\) and on the observed data. As in Part 8, the data are held fixed and we maximize over the parameters. Write \(\theta\) for all the parameters together.

The i.i.d. assumption still holds. Every point goes through the same two steps (identically distributed), and the outcome for one point does not change the probabilities for another (independent). So the likelihood is a product over points of one density, the mixture density \(f(x_i;\theta)\):

\[ L(\theta) = \prod_{i=1}^n f(x_i; \theta) \]

The big \(\prod\) is an ordinary product. It has nothing to do with the mixing proportions \(\pi_k\).

What is the density of one point? We never saw which face the die showed for \(x_i\), so we account for every possibility.

  • "\(x_i\) came from mixture 1", "from mixture 2", …, "from mixture \(K\)" are mutually exclusive events, so their chances add up.
  • Coming from mixture \(k\) needs two things to happen together: the die shows \(k\) (probability \(\pi_k\)), and mixture \(k\)'s Gaussian produces \(x_i\) (density \(\mathcal N(x_i;\mu_k,\sigma_k^2)\)). So these multiply.
The GMM likelihood \[ L(\theta) = \prod_{i=1}^n \sum_{k=1}^K \pi_k\,\mathcal N(x_i;\mu_k,\sigma_k^2) = \prod_{i=1}^n \sum_{k=1}^K \pi_k\,\frac{1}{\sqrt{2\pi}\,\sigma_k}\exp\!\Big(-\frac{(x_i-\mu_k)^2}{2\sigma_k^2}\Big) \]

The \(\pi\) inside \(\sqrt{2\pi}\) is the constant 3.14…, not a mixing proportion. Standard name: adding over the possible values of \(z_i\) like this is called marginalizing out the latent variable.

L23 · 5:10

Which component produced a point?

The lecture's example has three mixtures with means \(-20\), \(0\) and \(15\), and an observed point \(x_1 = -15\). It is tempting to say \(x_1\) must have come from mixture 1, whose mean is closest. That does not follow. Mixture 2 could have been picked, in which case its Gaussian explains \(x_1\) with a smaller density. Mixture 3 gives an even smaller density, tiny but not zero, since a Gaussian is positive everywhere. None of the three can be dismissed.

Closeness to a mean is only half the story; the mixing proportions matter too. Suppose \(\pi_3 = 0.9\) and \(\pi_1 = \pi_2 = 0.05\). Then mixture 1 is picked only 5% of the time and mixture 3 90% of the time. The density of \(x_1\) weighs both effects: how often each mixture is chosen, and how well its Gaussian explains the point. That is exactly what each term \(\pi_k\,\mathcal N(x_1;\mu_k,\sigma_k^2)\) of the sum measures.

0.34
5.0
−15.0
Share from mixture 1–
Share from 2–
Share from 3–
Mixture density p(x)–
Three weighted components and their sum, using the lecture's means \(-20\), \(0\) and \(15\). The lecture gives no variances, so here all three share one σ that you can set. Dashed curves are \(\pi_k\,\mathcal N(x;\mu_k,\sigma^2)\); the solid curve, their sum, is the mixture density \(p(x)\). The readouts give each component's share of \(p(x)\) at the chosen point. With the lecture's \(\pi\) and σ around 13 or more, mixture 3 contributes the most at \(x = -15\) despite its distant mean. Lecture 25 shows that this share is the quantity \(\lambda_{ik}\).

A product is harder to handle than a sum, so take logs as usual. The lecture labels the result ★:

\[ \log L(\theta) = \sum_{i=1}^n \log\Big( \sum_{k=1}^K \pi_k\,\frac{1}{\sqrt{2\pi}\,\sigma_k}\exp\!\Big(-\frac{(x_i-\mu_k)^2}{2\sigma_k^2}\Big) \Big) \qquad (\star) \]

In Part 8 the log did two useful jobs: it turned the product over points into a sum, and it cancelled the exponential in the Gaussian density. Here it only does the first. A sum now sits inside the log, and there is no nice rule for the log of a sum. (A product inside a log would split into a sum of logs.)

So how do we maximize ★ over all the \(\mu_k\), \(\sigma_k^2\) and \(\pi_k\)?

  • No closed form. Setting the derivative with respect to \(\mu_k\) to zero does not give a formula like "\(\hat\mu_{\text{ML}}\) is the average of the data points". In general the equations cannot be solved analytically.
  • A constraint. The \(\pi_k\) are not free variables: they must be nonnegative and sum to 1. Maximizing over them is a constrained optimization problem, which is harder still.
  • General-purpose methods ignore the structure. One could write down the gradient and run gradient ascent, since this is a maximization (gradient methods appear in Part 10). It works for any function and would give some estimates. But it does not use what we know about this model: the data were generated in two well-defined steps, first a cluster indicator, then a Gaussian draw.

Goal: an efficient way to maximize ★ that exploits its two-step structure. The lecture first takes a short detour through convex functions.

Convexity could fill a course of its own, and convex functions appear all over machine learning. Only the bare essentials are needed here. Pick two points \(a\) and \(b\) and their midpoint \(\frac{a+b}{2}\). Compare two heights above the midpoint. One is the function itself, \(f\big(\frac{a+b}{2}\big)\). The other is the straight line joining \((a, f(a))\) to \((b, f(b))\), the linear interpolation between them, whose height there is \(\frac{f(a)+f(b)}{2}\), exactly halfway between \(f(a)\) and \(f(b)\).

Definitions (midpoint form) \[ \begin{aligned} \text{convex:}&\quad f\Big(\frac{a+b}{2}\Big) \le \frac{f(a)+f(b)}{2} \\ \text{concave:}&\quad f\Big(\frac{a+b}{2}\Big) \ge \frac{f(a)+f(b)}{2} \end{aligned} \qquad \text{for every } a, b \]

The inequality must hold for every choice of \(a\) and \(b\), not just one pair you happened to draw. The names match the convex and concave mirrors of school physics.

  • Both convex and concave. Both inequalities hold together only as an equality, \(f\big(\frac{a+b}{2}\big) = \frac{f(a)+f(b)}{2}\) for all \(a, b\). That is a straight line: interpolating linearly between two points of a line lands back on the line.
  • Neither. A wavy function can have one pair of points whose chord lies above the curve, so it is not concave, and another pair whose chord lies below the curve, so it is not convex.
Convex ab(a+b)/2 f(mid) ≤ chord curve below the chord Concave ab(a+b)/2 f(mid) ≥ chord curve above the chord Linear: both ab(a+b)/2 f(mid) = chord equality for all a, b Neither left: curve below chord right: curve above chord f((a+b)/2), on the curve(f(a)+f(b))/2, midpoint of the chord
The midpoint test in four cases. Yellow dot: the function at the midpoint. Ring: the chord at the midpoint. A convex curve lies below the chord, a concave one above it, and a straight line on it. The wave fails both tests, using one chord for each.

From two points to many: Jensen's inequality

Write the midpoint as \(\frac12 a + \frac12 b\). For a convex function the definition reads \(f(\frac12 a + \frac12 b) \le \frac12 f(a) + \frac12 f(b)\). If this holds for every \(a\) and \(b\), one can argue that it holds for any weight \(\lambda\) between 0 and 1:

\[ f\big(\lambda a + (1-\lambda) b\big) \;\le\; \lambda f(a) + (1-\lambda) f(b), \qquad 0 \le \lambda \le 1 \qquad (\text{convex}) \]

As \(\lambda\) goes from 1 to 0, the point \(\lambda a + (1-\lambda)b\) travels from \(a\) to \(b\), and at every stop along the way the function stays at or below the chord. For a concave function the inequality reverses.

The same idea extends to any number of points \(a_1, \dots, a_K\), with weights \(\lambda_1, \dots, \lambda_K\) that lie between 0 and 1 and sum to 1. Written for concave functions, the form used from here on:

Jensen's inequality (concave f) \[ f\Big(\sum_{k=1}^K \lambda_k a_k\Big) \;\ge\; \sum_{k=1}^K \lambda_k\, f(a_k), \qquad 0 \le \lambda_k \le 1, \quad \sum_{k=1}^K \lambda_k = 1 \]

For a convex \(f\) the inequality points the other way. This is the only fact from convexity the rest of the part needs.

In one dimension this adds little intuition, so the lecture pictures a concave function on the plane. Take three points \(a_1, a_2, a_3\). Varying the weights moves \(\lambda_1 a_1 + \lambda_2 a_2 + \lambda_3 a_3\) around the triangle they span, called their convex hull; for example \(0.1a_1 + 0.5a_2 + 0.4a_3\) is one point inside it. Over every point of the hull, the concave surface lies above the flat triangle joining the three function values. Jensen says this for any number of points.

L24 · 13:28

The connection: log is concave

Why convexity in a part about mixtures? The logarithm in the log-likelihood is a concave function, so Jensen's inequality applies to it. For positive \(a_k\) and valid weights:

\[ \log\Big(\sum_{k=1}^K \lambda_k a_k\Big) \;\ge\; \sum_{k=1}^K \lambda_k \log a_k \]

Read it left to right. The left side has a sum inside a log, the very thing that blocked us in ★. The right side has the sum outside the log. Jensen lets us pull the sum out, at a price: we get an inequality instead of an equality. Lecture 25 shows how to live with that price.

12345678−112 log x a₁ = 1, λ₁ = 0.2 a₂ = 3, λ₂ = 0.5 a₃ = 7, λ₃ = 0.3 log(Σ λₖaₖ) = log 3.8 = 1.34 Σ λₖ log aₖ = 1.13 Σ λₖaₖ = 3.8
Jensen for the logarithm, with three points. Here \(a = 1, 3, 7\) with weights \(0.2, 0.5, 0.3\), an example chosen for this page. The weighted average of the three curve points (ring) lies inside their triangle, which lies below the concave curve: \(\sum_k\lambda_k\log a_k = 1.13 \le \log\big(\sum_k\lambda_k a_k\big) = \log 3.8 = 1.34\).

The lecture leaves the concavity of the log as an exercise. One way, with the midpoint definition: for \(a, b > 0\),

\[ \log\frac{a+b}{2} \;\ge\; \frac{\log a + \log b}{2} = \log\sqrt{ab} \iff \frac{a+b}{2} \ge \sqrt{ab} \iff \big(\sqrt a - \sqrt b\big)^2 \ge 0, \]

and the last statement is always true. The first step is reversible because the log is increasing. (The middle inequality is the familiar fact that the arithmetic mean is at least the geometric mean.)

Multiply and divide by λ: a lower bound

The video's YouTube title mentions supervised learning, but L25 is entirely about estimating the GMM's parameters.

Go back to ★. Inside each log is a plain sum of \(K\) numbers, but Jensen needs a weighted combination whose weights sum to 1. There are no such weights, so the lecture introduces them artificially, making the problem bigger on purpose. For every data point \(i\), add \(K\) new parameters \(\lambda_{i1}, \dots, \lambda_{iK}\) with

\[ \sum_{k=1}^K \lambda_{ik} = 1, \qquad 0 \le \lambda_{ik} \le 1 \qquad \text{for all } i, k \]

For each point they form a probability distribution over the \(K\) mixtures. Their meaning comes later; for now they are just extra parameters. Multiply and divide every term by \(\lambda_{ik}\), which changes nothing:

\[ \log L(\theta) = \sum_{i=1}^n \log \sum_{k=1}^K \lambda_{ik}\,\frac{\pi_k\,\mathcal N(x_i;\mu_k,\sigma_k^2)}{\lambda_{ik}} \]

Each log now holds a \(\lambda\)-weighted combination of the numbers \(\pi_k\mathcal N(x_i;\mu_k,\sigma_k^2)/\lambda_{ik}\), and the log is concave. Jensen moves the sum outside:

The modified log-likelihood: a lower bound for every λ \[ \log L(\theta) \;\ge\; \sum_{i=1}^n \sum_{k=1}^K \lambda_{ik}\,\log\frac{\pi_k\,\mathcal N(x_i;\mu_k,\sigma_k^2)}{\lambda_{ik}} \;=:\; \ell(\theta, \lambda), \qquad \mathcal N(x;\mu,\sigma^2) = \frac{1}{\sqrt{2\pi}\,\sigma}\,e^{-(x-\mu)^2/2\sigma^2} \]

The lecture calls \(\ell(\theta,\lambda)\) the modified log-likelihood. It is a different function from \(\log L\), which is why we get an inequality. It depends on \(\theta = (\mu_1, \dots, \mu_K, \sigma_1^2, \dots, \sigma_K^2, \pi_1, \dots, \pi_K)\) and on \(\lambda = (\lambda_{11}, \dots, \lambda_{1K}, \dots, \lambda_{n1}, \dots, \lambda_{nK})\).

Convention: a term with \(\lambda_{ik} = 0\) counts as 0, the limit of \(\lambda\log(c/\lambda)\) as \(\lambda \to 0\).

Why accept a different function with more parameters? L25 · 7:46

At first sight this is a bad trade. \(\ell\) is not the function we wanted to maximize, and it adds \(n \times K\) parameters. To be worth it, something must become very simple. Two things do.

  • It is a lower bound for any valid λ. Hand me any \(\theta\). I can choose any \(\lambda\) I like (nonnegative, summing to 1 for each point), evaluate \(\ell(\theta,\lambda)\), and the number will never exceed the true log-likelihood \(\log L(\theta)\). That is Jensen, nothing more.
  • Each half is easy when the other is fixed 10:10. With \(\lambda\) fixed, maximizing \(\ell\) over \(\theta\) is easy. With \(\theta\) fixed, maximizing over \(\lambda\) is easy. The next two sections check both claims by writing down the closed-form answers.

Key insight: one hard problem in θ becomes two easy problems, one in θ and one in λ.

Treat every \(\lambda_{ik}\) as a constant. The log of a product splits into a sum of logs, and the log cancels the exponential:

\[ \ell(\theta,\lambda) = \sum_{i=1}^n \sum_{k=1}^K \Big[ \lambda_{ik}\log\pi_k \;-\; \lambda_{ik}\frac{(x_i-\mu_k)^2}{2\sigma_k^2} \;-\; \lambda_{ik}\log\big(\sqrt{2\pi}\,\sigma_k\big) \;-\; \lambda_{ik}\log\lambda_{ik} \Big] \]

The last term involves only \(\lambda\), so it is a constant here and can be ignored. The first three terms contain the real parameters, and each appears in a simple way. Taking derivatives gives closed forms for all of them. The lecture states the results and recommends deriving them as an exercise; the steps are short.

  • Means. \(\dfrac{\partial\ell}{\partial\mu_k} = \sum_i \lambda_{ik}\,\dfrac{x_i-\mu_k}{\sigma_k^2} = 0\) gives \(\mu_k = \sum_i\lambda_{ik}x_i \big/ \sum_i\lambda_{ik}\).
  • Variances. Write \(v = \sigma_k^2\), so \(\log(\sqrt{2\pi}\,\sigma_k) = \frac12\log 2\pi + \frac12\log v\). Then \(\dfrac{\partial\ell}{\partial v} = \sum_i \lambda_{ik}\Big(\dfrac{(x_i-\mu_k)^2}{2v^2} - \dfrac{1}{2v}\Big) = 0\) gives \(v = \sum_i\lambda_{ik}(x_i-\mu_k)^2 \big/ \sum_i\lambda_{ik}\).
  • Mixing proportions. Only \(\sum_i\sum_k\lambda_{ik}\log\pi_k\) involves \(\pi\), and the \(\pi_k\) must be nonnegative and sum to 1. The lecture solves this constrained problem with the method of Lagrange multipliers (constrained optimization comes back in Part 16). With \(N_k = \sum_i\lambda_{ik}\), set the derivative of \(\sum_k N_k\log\pi_k + \nu\big(1 - \sum_k\pi_k\big)\) to zero: \(N_k/\pi_k = \nu\), so \(\pi_k = N_k/\nu\). Summing over \(k\) fixes \(\nu = \sum_k N_k = \sum_i\sum_k\lambda_{ik} = n\).
With λ fixed (modified maximum likelihood, MML) \[ \hat\mu_k^{\text{MML}} = \frac{\sum_{i=1}^n \lambda_{ik}\,x_i}{\sum_{i=1}^n \lambda_{ik}}, \qquad \hat\sigma_k^{2,\text{MML}} = \frac{\sum_{i=1}^n \lambda_{ik}\,\big(x_i-\hat\mu_k^{\text{MML}}\big)^2}{\sum_{i=1}^n \lambda_{ik}}, \qquad \hat\pi_k^{\text{MML}} = \frac{\sum_{i=1}^n \lambda_{ik}}{n} \]

A weighted mean, a weighted sample variance and an averaged weight, where point \(i\) counts towards mixture \(k\) with weight \(\lambda_{ik}\). The lecture calls these modified maximum likelihood estimates because they maximize \(\ell\), not the original likelihood.

Reading λ as a clustering L25 · 16:22

Think of \(\lambda_{ik}\) as the probability that point \(i\) belongs to cluster \(k\).

  • Hard clustering. Put each point entirely into one cluster: \(\lambda_{ik} = 1\) for its cluster and 0 for the rest. Then \(\hat\mu_k\) is the mean of the points assigned to cluster \(k\), \(\hat\sigma_k^2\) their sample variance, and \(\hat\pi_k\) the fraction of points assigned to \(k\).
  • Soft clustering. With \(\lambda_{ik}\) between 0 and 1, every point tells each cluster how much it belongs there, and is weighted by that importance. The estimates become weighted means and variances, and \(\hat\pi_k\) is the average chance that a point belongs to cluster \(k\).

Now hold all the \(\mu_k\), \(\sigma_k^2\) and \(\pi_k\) fixed. Write \(a_{ik} = \pi_k\,\mathcal N(x_i;\mu_k,\sigma_k^2)\), the weighted Gaussian density, which is a constant now. Splitting the log of the ratio leaves

\[ \ell(\theta,\lambda) = \sum_{i=1}^n \sum_{k=1}^K \big( \lambda_{ik}\log a_{ik} - \lambda_{ik}\log\lambda_{ik} \big) \]

No term mixes the \(\lambda\)'s of two different points. The function is a sum of separate pieces, one per data point, that share no variables, so maximizing each piece on its own maximizes the whole. Fix any point \(i\) and solve

\[ \max_{\lambda_{i1},\dots,\lambda_{iK}} \; \sum_{k=1}^K \big(\lambda_{ik}\log a_{ik} - \lambda_{ik}\log\lambda_{ik}\big) \quad\text{subject to}\quad \sum_{k=1}^K\lambda_{ik} = 1,\;\; 0 \le \lambda_{ik} \le 1 \]

This is a constrained problem again, and again not a hard one; Lagrange multipliers solve it analytically. The lecture skips the step, which goes like this: the derivative of \(\sum_k(\lambda_{ik}\log a_{ik} - \lambda_{ik}\log\lambda_{ik}) + \nu_i\big(1-\sum_k\lambda_{ik}\big)\) with respect to \(\lambda_{ik}\) is \(\log a_{ik} - \log\lambda_{ik} - 1 - \nu_i\). Setting it to zero makes \(\lambda_{ik}\) proportional to \(a_{ik}\), and the constraint fixes the scale.

With θ fixed \[ \hat\lambda_{ik}^{\text{MML}} = \frac{a_{ik}}{\sum_{l=1}^K a_{il}} = \frac{\pi_k\,\sigma_k^{-1}\exp\!\big(-(x_i-\mu_k)^2/2\sigma_k^2\big)}{\sum_{l=1}^K \pi_l\,\sigma_l^{-1}\exp\!\big(-(x_i-\mu_l)^2/2\sigma_l^2\big)} \]

In the second form the common factor \(1/\sqrt{2\pi}\) of the Gaussian densities has cancelled.

It is Bayes' theorem L25 · 27:35

The formula looks complicated, but its meaning is plain. \(\lambda_{ik}\) was meant to be the chance that point \(i\) comes from cluster \(k\). If all the parameters are known and we are shown \(x_i\), that chance is \(P(z_i = k \mid x_i)\), and Bayes' theorem gives

\[ P(z_i = k \mid x_i) = \frac{P(x_i \mid z_i = k)\,P(z_i = k)}{P(x_i)} = \frac{\mathcal N(x_i;\mu_k,\sigma_k^2)\,\pi_k}{\sum_{l=1}^K \mathcal N(x_i;\mu_l,\sigma_l^2)\,\pi_l} \]

Here \(P(x_i \mid z_i = k)\) is the density of \(x_i\) under mixture \(k\), \(P(z_i = k) = \pi_k\) is the chance from step 1 of the story, and the denominator adds up every way of generating \(x_i\), which is \(P(x_i)\). So maximizing the modified likelihood over \(\lambda\) gives exactly what Bayes' theorem gives. Standard name: \(\lambda_{ik}\) is often called the responsibility of mixture \(k\) for point \(i\).

Both halves are now simple formulas. Fix λ: weighted mean, weighted variance and weighted fraction. Fix θ: the posterior P(zᵢ = k | xᵢ) by Bayes' theorem.

Going back and forth between the two easy maximizations gives an iterative algorithm. The superscript \(t\) counts iterations.

  1. EM for a Gaussian mixture
  2. Initialize \(\theta^0\): some means \(\mu_1, \dots, \mu_K\), variances \(\sigma_1^2, \dots, \sigma_K^2\) and proportions \(\pi_1, \dots, \pi_K\).
  3. E step. \(\lambda^{t+1} = \arg\max_\lambda \ell(\theta^t, \lambda)\): for every \(i\) and \(k\), \(\lambda_{ik}^{t+1} = \pi_k\mathcal N(x_i;\mu_k,\sigma_k^2) \big/ \sum_l \pi_l\mathcal N(x_i;\mu_l,\sigma_l^2)\), with the parameters taken from \(\theta^t\).
  4. M step. \(\theta^{t+1} = \arg\max_\theta \ell(\theta, \lambda^{t+1})\): the weighted means, variances and proportions, with weights \(\lambda^{t+1}\).
  5. Repeat the E and M steps until \(\lVert\theta^{t+1} - \theta^t\rVert \le \epsilon\), a tolerance such as \(10^{-3}\) or \(10^{-2}\): stop once the estimates barely change.

By introducing the \(\lambda\)'s and using Jensen, the problem splits into two parts, each easy when the other is held fixed, and we go back and forth between them. This is an instance of a famous algorithm, the EM algorithm. E stands for expectation and M for maximization. The first step is called the expectation step because the modified log-likelihood, and the \(\lambda^{t+1}\) it produces, can be written as an expectation of some quantity. (For instance, \(\sum_k\lambda_{ik}\log\frac{a_{ik}}{\lambda_{ik}}\) is an average of \(\log\frac{a_{ik}}{\lambda_{ik}}\) with weights \(\lambda_{i1},\dots,\lambda_{iK}\).) The course does not develop the general version.

The algorithm was developed in the 1970s. It was known in other forms before, but Dempster's paper set it down and gave it the name EM (standard reference: Dempster, Laird and Rubin, 1977), and it has been widely used ever since. The lecture also says one can argue that it converges, without giving a proof.

Why the log-likelihood never decreases

This standard argument is not in the lecture, but it shows what EM achieves and explains the widget below. It needs one more fact: after an E step, the bound is tight. Put \(\lambda_{ik} = a_{ik}/\sum_l a_{il}\) into \(\ell\). Then \(a_{ik}/\lambda_{ik} = \sum_l a_{il} = f(x_i;\theta)\), the mixture density of \(x_i\), for every \(k\). So

\[ \ell(\theta^t, \lambda^{t+1}) = \sum_{i=1}^n \sum_{k=1}^K \lambda_{ik}^{t+1}\,\log f(x_i;\theta^t) = \sum_{i=1}^n \log f(x_i;\theta^t) = \log L(\theta^t) \]

since the \(\lambda_{ik}^{t+1}\) sum to 1 over \(k\). Now chain the three facts:

\[ \log L(\theta^{t+1}) \;\underset{\text{Jensen}}{\ge}\; \ell(\theta^{t+1}, \lambda^{t+1}) \;\underset{\text{M step}}{\ge}\; \ell(\theta^t, \lambda^{t+1}) \;\underset{\text{tight}}{=}\; \log L(\theta^t) \]

The M step can only raise \(\ell\), because \(\theta^{t+1}\) maximizes it and \(\theta^t\) was one of the candidates. So every iteration keeps or raises the true log-likelihood.

start
Iteration t–
log L(θ)–
Bound ℓ(θ, λ)–
Gap–
EM on 90 synthetic points, drawn for this page from three Gaussians with a seeded generator. Each press of Next runs one E step or one M step; the default start is built from a Lloyd's clustering. Top: histogram, the weighted components \(\pi_k\mathcal N(x;\mu_k,\sigma_k^2)\) (dashed) and their sum (solid). Middle: the soft assignment \(P(z = k \mid x)\) from the last E step, stacked to 1; at each data point it equals \(\lambda_{ik}\), and the dots show each point's most likely mixture. Bottom: \(\log L\) after each iteration for this start (bold) and the other start (faint); the ring is the bound \(\ell(\theta,\lambda)\). After an E step the ring sits on the dot; after an M step \(\log L\) has risen above it. The poor start ends on a lower peak.

EM as soft clustering, compared with K-means

EM is closely related to Lloyd's algorithm (Part 6). The \(\lambda_{ik}\) it ends with can be read as the chance that point \(i\) belongs to cluster \(k\), so EM produces a soft clustering where Lloyd's produces a hard one. At the end you have the estimates of \(\mu\), \(\sigma^2\) and \(\pi\), and also the optimal \(\lambda\)'s, which you can use as a clustering. Lloyd's algorithm also alternated two steps, and they line up with EM's:

Lloyd's (K-means)EM for a GMM
AssignmentsHard: each \(z_i\) is one clusterSoft: \(\lambda_{ik} \in [0,1]\), \(\sum_k\lambda_{ik} = 1\)
Assignments fixedCompute the meansM step: means, variances and mixing proportions
Parameters fixedReassign each point to its nearest meanE step: update how likely each point is to come from each cluster
Spread of a clusterNot estimatedEstimated (a covariance matrix in higher dimensions)
Cluster regionsVoronoi regions (Part 7)Need not be Voronoi regions

Estimating variances matters. In higher dimensions they become covariance matrices, so each cluster gets its own shape. If the points of one cluster spread along one direction and those of a neighbouring cluster along another, Lloyd's algorithm may do poorly where the two overlap. EM can pick up that each cluster has its own direction of spread, simply because it estimates more parameters.

To turn EM's output into a hard clustering, run the algorithm, then put each point \(i\) in the cluster with the largest of \(\lambda_{i1}, \lambda_{i2}, \dots, \lambda_{iK}\). Because the variances shape these clusters, they need not be Voronoi regions 8:55. Especially where clusters overlap, the lecture argues EM assigns points better than Lloyd's, even after this conversion to hard clusters.

from Gaussian 1from Gaussian 2the two meansVoronoi boundary(K-means rule)GMM boundary(equal density)the two rulesdisagree
Clusters with different orientations. 110 synthetic points from two equally weighted Gaussians, one stretched at about 60° and one nearly horizontal (one point falls outside the frame). The dashed line is the Voronoi boundary between the two means, the rule a K-means assignment uses. The solid curve, with two branches, is where the two generating Gaussians are equally likely: the boundary of the largest-\(\lambda\) rule. The rules disagree on the 13 ringed points, and the Gaussian rule matches the true source for 10 of them. Overall the straight boundary puts 17 points on the wrong side, the curved one 10.

How does EM relate to the problem we started with, maximizing \(\log L(\theta)\)? We wrote down the log-likelihood and then worked entirely with the modified one instead. Picture the parameter space as a horizontal axis with \(\log L\) plotted above it. For simple models such as a single Gaussian or a Bernoulli, this curve is a nice concave function and easy to maximize. For a Gaussian mixture it is a complicated landscape, and we want the \(\theta^*\) at its highest point.

EM never climbs \(\log L\) directly. At \(\theta^t\), the E step picks \(\lambda^{t+1}\). As a function of \(\theta\), the bound \(\ell(\theta, \lambda^{t+1})\) is a nice function that, by Jensen, stays below \(\log L\) everywhere. The M step moves to its top, \(\theta^{t+1}\). The next E step builds a new bound there, its top gives \(\theta^{t+2}\), and so on. The guesses keep getting better, but they can settle on a peak that is not the highest one.

θₜθₜ₊₁θₜ₊₂ θ* log L(θ) bound with λ chosen at θₜ (touches log L there) next bound, λ chosen at θₜ₊₁ top of a bound: the next θ EM stops: local max global max
EM climbs a ladder of lower bounds. A schematic log-likelihood over one parameter, with two peaks. Each bound lies below \(\log L\) and touches it at the current \(\theta\); the M step moves to its top. Started on the left, the iterates settle on the left peak, a local maximum, and never reach the global maximum \(\theta^*\).
What EM guarantees

EM converges to a local maximum of the log-likelihood. It may not find \(\theta^*\); the lecture adds that, in general, no known algorithm can guarantee the global maximum of this log-likelihood.

Where EM ends up therefore depends on where it starts. A very bad initialization may only reach a peak much lower than the best one. The "poor start" in the EM widget shows this: it converges, but to a log-likelihood about 8 below what the Lloyd's start reaches on the same data.

L26 · 16:23

Initialize EM with Lloyd's algorithm

Since initialization decides which maximum EM reaches, the usual practice is to start EM from a K-means solution.

  1. Building θ⁰ from Lloyd's
  2. Run Lloyd's algorithm with the same \(K\) to get a hard clustering (Part 6; Part 7 covers its own initialization).
  3. For each cluster, compute the sample mean and the sample variance of its points: these are \(\mu_k\) and \(\sigma_k^2\) in \(\theta^0\).
  4. Set \(\pi_k\) to the fraction of data points in cluster \(k\).
  5. Run EM from this \(\theta^0\).

These are the M-step formulas of Lecture 25 applied to hard (0 or 1) weights, so \(\theta^0\) is what one M step would produce from the K-means assignment.

The thread of these five lectures: data with cluster structure called for a latent variable model whose latent variable is a cluster indicator. Maximizing its log-likelihood was hard. Jensen's inequality introduced new variables so that the problem could be solved by alternate maximization: fix one set of variables and maximize over the other, then swap. Initialize with Lloyd's, run EM, and use the estimates however the later task needs, for instance converting the \(\lambda\)'s into a clustering.

For estimation this is a very powerful technique. In practice EM converges fast and typically gives very good parameter estimates. The principle reaches far beyond Gaussian mixtures: it applies to any reasonable latent variable model whose log-likelihood has a similar structure that can be separated. The course writes down EM only for the Gaussian mixture.

This ends unsupervised learning in the course: representation learning (PCA and kernel PCA), clustering (K-means) and estimation (maximum likelihood, Bayesian estimation, and mixture models fitted by EM). Part 10 begins a different paradigm, supervised learning.

Key terms

Gaussian mixture model (GMM)
A density \(\sum_k \pi_k\mathcal N(x;\mu_k,\sigma_k^2)\): pick a component with probability \(\pi_k\), then draw from its Gaussian.
Mixing proportions \(\pi_k\)
The probabilities of the \(K\) faces of the die; nonnegative and summing to 1.
Generative story
The probabilistic mechanism assumed to have produced the data; for a GMM it has two steps.
Latent variable
An unobserved quantity the data depend on; here the component label \(z_i\) of each point.
Multimodal density
A density with several peaks. A single Gaussian has only one, so it cannot fit clustered data.
Convex function
\(f(\frac{a+b}{2}) \le \frac{f(a)+f(b)}{2}\) for all \(a, b\): the curve lies below every chord.
Concave function
The reverse inequality: the curve lies above every chord. The logarithm is concave.
Jensen's inequality
For concave \(f\) and weights \(\lambda_k \ge 0\) summing to 1: \(f(\sum_k\lambda_k a_k) \ge \sum_k\lambda_k f(a_k)\).
Modified log-likelihood \(\ell(\theta,\lambda)\)
Jensen's lower bound on \(\log L(\theta)\), valid for every valid \(\lambda\) and easy to maximize in each argument.
Responsibility \(\lambda_{ik}\)
The weight of point \(i\) on mixture \(k\); after an E step it equals \(P(z_i = k \mid x_i)\).
Soft clustering
Each point gets a probability for every cluster instead of a single label; take the largest to get a hard clustering.
EM algorithm
Alternate the E step (best \(\lambda\) for the current \(\theta\)) and the M step (best \(\theta\) for that \(\lambda\)) until \(\theta\) stops changing.
Local maximum
A peak of \(\log L\) that need not be the highest; EM converges to one, chosen by its initialization.

Test yourself

Q1How many free parameters does a one-dimensional Gaussian mixture with \(K = 4\) have?

4 means, 4 variances and 3 free mixing proportions (the fourth is 1 minus the others): \(3K - 1 = 11\).

Q2Data fall in three well-separated groups. Why does the maximum-likelihood Gaussian explain them badly, and why would more data not help?

Maximum likelihood only finds the best Gaussian, and every Gaussian has one peak. Its mean lands near the middle and the outer groups fall in low-density regions. More data from the same groups leaves the model family, and hence the mismatch, unchanged.

Q3Two components with \(\pi = (0.5, 0.5)\), \(\mu = (0, 2)\) and \(\sigma_1 = \sigma_2 = 1\). Find \(\lambda_{i1}\) for the point \(x_i = 0\).

The \(\pi\)'s and the factors \(1/\sqrt{2\pi}\) cancel, leaving \(\lambda_{i1} = e^{0}/(e^{0} + e^{-2}) = 1/(1 + e^{-2}) \approx 0.88\), so \(\lambda_{i2} \approx 0.12\).

Q4Three points \(x = 1, 2, 6\) have weights \(\lambda_{i1} = 1, 1, 0.5\) for component 1. Compute \(\hat\mu_1\), \(\hat\sigma_1^2\) and \(\hat\pi_1\) from the M step.

\(N_1 = 2.5\). \(\hat\mu_1 = (1 + 2 + 0.5 \cdot 6)/2.5 = 6/2.5 = 2.4\). \(\hat\sigma_1^2 = \big(1.4^2 + 0.4^2 + 0.5 \cdot 3.6^2\big)/2.5 = (1.96 + 0.16 + 6.48)/2.5 = 3.44\). \(\hat\pi_1 = 2.5/3 \approx 0.83\).

Q5Check Jensen's inequality for \(\log\) with \(a = 1\), \(b = 4\) and equal weights.

\(\log 2.5 \approx 0.916\), while \(\frac12(\log 1 + \log 4) = \log 2 \approx 0.693\). The log of the average is larger, as concavity requires.

Q6Why is \(\ell(\theta,\lambda)\) below \(\log L(\theta)\) for every valid \(\lambda\), and when are they equal?

Multiplying and dividing by \(\lambda_{ik}\) writes each term of \(\log L\) as the log of a \(\lambda\)-weighted combination; since log is concave, Jensen puts the weighted combination of logs, \(\ell\), below it. They are equal when \(\lambda_{ik} = P(z_i = k \mid x_i)\), the E-step choice, because then every ratio inside the log equals the same number \(f(x_i;\theta)\).

Q7How is EM like Lloyd's algorithm, and how is it different?

Both alternate two steps: compute parameters from assignments (means, or in EM means, variances and proportions) and update assignments from parameters. Lloyd's assignments are hard and give Voronoi regions; EM's are probabilities \(\lambda_{ik}\), it models each cluster's spread, and its clusters need not be Voronoi regions.

Q8Two EM runs on the same data stop at different log-likelihoods. Is one of them a bug?

Not necessarily. EM only guarantees a local maximum, and which one it reaches depends on the initialization. Starting from a Lloyd's clustering, or keeping the run with the higher log-likelihood, is the usual remedy.