---
title: Mixture models and EM
heading: Clustering with k-means, Gaussian mixture densities and the responsibility each component takes for a point, the expectation-maximization algorithm's two steps worked by hand, why the likelihood never decreases, and the collapse that regularization prevents
eyebrow: S14 · Lesson 36 · Mixture models and EM
xp: 50
---

::: hook
A batch of measurements from two production lines, mixed together without labels, forms a lumpy distribution that no single Gaussian fits. A mixture model explains it as several Gaussians added together, and fitting one is a chicken-and-egg problem: knowing which line each measurement came from would make the fit trivial, and knowing the fit would tell you which line each came from. The expectation-maximization algorithm breaks the circle by alternating between the two, softly, and it's guaranteed to improve every round. The same idea trains hidden Markov models (lesson 42) and decodes many communication codes.
:::

::: play
- A mixture of two unit-variance Gaussians with means a and b and weights π and 1 − π, and the **responsibility** of the first component: the posterior probability that a point at x came from it.
- Between the means, responsibility switches from one component to the other; the switch moves toward the lighter component as π grows.
- Overlapping components share responsibility over a wide range, which makes the clusters harder to separate.
:::

```sim plot
view: graph
x: x = -4..7
y: p = 0..1.05
slider: a = -2..3 step 0.1 = 0
slider: b = 0..5 step 0.1 = 3
slider: P = 0.05..0.95 step 0.01 = 0.5
plot: P*exp(-(x - a)^2/2)/sqrt(2*pi) + (1 - P)*exp(-(x - b)^2/2)/sqrt(2*pi) | samples: 400 | label: mixture density
plot: P*exp(-(x - a)^2/2)/(P*exp(-(x - a)^2/2) + (1 - P)*exp(-(x - b)^2/2)) | samples: 400 | color: ch3 | label: responsibility of component 1
readout: responsibility at x of 1 = P*exp(-(1 - a)^2/2)/(P*exp(-(1 - a)^2/2) + (1 - P)*exp(-(1 - b)^2/2)) | digits: 4
```

::: predict
## Two equally weighted unit-variance components sit at 0 and 3. What responsibility does the first take for a point at x = 1?

- [ ] 0.5
- [ ] 0.67
- [x] About 0.82
- [ ] 1

::: reveal
The densities at x = 1 are proportional to $e^{-1/2}$ = 0.607 and $e^{-2}$ = 0.135, so the first component's share is 0.607/0.742 = 0.818. Distance alone (1 against 2) would suggest 2/3; the Gaussian tails make the nearer component much more likely.
:::
:::

::: nameit
## k-means

Pick K centers; assign each point to its nearest center; move each center to the mean of its points; repeat. Each step lowers the total squared distance $J = \sum_n\|\mathbf{x}_n - \boldsymbol{\mu}_{k(n)}\|^2$, so it stops, though perhaps at a local minimum. It makes hard, all-or-nothing assignments.

## Gaussian mixtures

$$p(\mathbf{x}) = \sum_{k=1}^K\pi_k\mathcal{N}(\mathbf{x}|\boldsymbol{\mu}_k, \Sigma_k), \qquad \sum_k\pi_k = 1$$

The **responsibility** of component k for point n is its posterior probability:

$$\gamma_{nk} = \frac{\pi_k\mathcal{N}(\mathbf{x}_n|\boldsymbol{\mu}_k, \Sigma_k)}{\sum_j\pi_j\mathcal{N}(\mathbf{x}_n|\boldsymbol{\mu}_j, \Sigma_j)}$$

In D dimensions with full covariances, K components have $K\left(D + \frac{D(D + 1)}{2}\right) + K - 1$ parameters.

## Expectation-maximization

- **E step:** compute every $\gamma_{nk}$ from the current parameters.
- **M step:** refit each component as a weighted Gaussian. With $N_k = \sum_n\gamma_{nk}$:

$$\boldsymbol{\mu}_k = \frac{1}{N_k}\sum_n\gamma_{nk}\mathbf{x}_n, \qquad \Sigma_k = \frac{1}{N_k}\sum_n\gamma_{nk}(\mathbf{x}_n - \boldsymbol{\mu}_k)(\mathbf{x}_n - \boldsymbol{\mu}_k)^T, \qquad \pi_k = \frac{N_k}{N}$$

Each round never decreases the log-likelihood $\sum_n\ln p(\mathbf{x}_n)$, so EM converges, to a local maximum that depends on the start. k-means is the limit of EM with equal, vanishing variances.

## Collapse

If a component shrinks onto a single data point, its density there grows without bound and so does the likelihood. Practical EM floors the variances or adds a prior on them.
:::

::: example
## A component has responsibilities 0.9, 0.7 and 0.1 for the points 0, 1 and 4. Carry out its M step.

::: solution
1. **Effective count and weight.**
   $$\begin{aligned} N_1 &= 1.7 \\ \pi_1 &= 1.7/3 \\ &= 0.567 \end{aligned}$$
2. **Mean.**
   $$\begin{aligned} \mu_1 &= (0.9 \times 0 + 0.7 \times 1 \\ &\quad + 0.1 \times 4)/1.7 \\ &= 0.647 \end{aligned}$$
3. **Variance.**
   $$\begin{aligned} \sigma_1^2 &= [0.9(0.647)^2 \\ &\quad + 0.7(0.353)^2 \\ &\quad + 0.1(3.353)^2]/1.7 \\ &= 0.934 \end{aligned}$$
4. **The far point.** The point at 4 counts only a tenth, but it still widens the component.

**Answer:** $\pi_1$ = 0.567, the mean is 0.647 and the variance is 0.934.
:::
:::

::: example
## What's the log-likelihood of a point at x = 1 under the predict question's mixture, and how many parameters does a three-component mixture in two dimensions have?

::: solution
1. **Density at x = 1.**
   $$\begin{aligned} p(1) &= 0.5 \times 0.242 \\ &\quad + 0.5 \times 0.054 \\ &= 0.148 \end{aligned}$$
   so ln p = −1.911.
2. **Three components in 2D.**
   $$3 \times (2 + 3) + 2 = 17$$
3. **Five components in 10D.**
   $$5 \times (10 + 55) + 4 = 329$$

**Answer:** the log-likelihood is −1.911, and the three-component mixture in two dimensions has 17 parameters.
:::
:::

::: example
## Run k-means with k = 2 on the points 0, 1, 4, 5 and 9, starting with centers at 0 and 9.

::: solution
1. **Assign.** Each point goes to its nearer center: 0, 1 and 4 go to the first (4 is 4 from 0 and 5 from 9), 5 and 9 to the second.
2. **Update.** The new centers are the means:
   $$\begin{aligned} \mu_1 &= (0 + 1 + 4)/3 \\ &= 1.67 \\ \mu_2 &= (5 + 9)/2 \\ &= 7 \end{aligned}$$
3. **Reassign.** Nothing changes (4 is 2.33 from 1.67 and 3 from 7; 5 is 3.33 and 2), so the algorithm has converged.
4. **Distortion.**
   $$\begin{aligned} J &= 2.78 + 0.44 + 5.44 + 4 + 4 \\ &= 16.7 \end{aligned}$$
5. **Link to EM.** k-means is EM's limit for equal, vanishing variances: hard assignments in place of responsibilities.

**Answer:** k-means converges to clusters {0, 1, 4} and {5, 9}, with centers 1.67 and 7 and a distortion of 16.7.
:::
:::

::: check
given: a = -1 | 0; b = 1.5 | 2.5; P = 0.2 | 0.3 | 0.8; x = 0.5 | 1
answer: P*exp(-(x - a)^2/2)/(P*exp(-(x - a)^2/2) + (1 - P)*exp(-(x - b)^2/2))
tolerance: 0.5%

## A mixture has unit-variance components at {a} and {b} with weights {P} and 1 − {P}. What responsibility does the first take for a point at {x}?

::: right
Right. $\pi_1\mathcal{N}_1/(\pi_1\mathcal{N}_1 + \pi_2\mathcal{N}_2)$ = {answer}.
:::

::: misses
- [exp(-(x - a)^2/2)/(exp(-(x - a)^2/2) + exp(-(x - b)^2/2))] Include the mixing weights.
- [any] Not yet. Bayes' theorem over the components.
:::
:::

::: check
given: g = 0.2 | 0.8; h = 0.4 | 0.9; k = 0.1 | 0.6; x = 0 | 1; y = 2 | 3; z = 5 | 7
answer: (g*x + h*y + k*z)/(g + h + k)
tolerance: 0.5%

## A component's responsibilities for the points {x}, {y} and {z} are {g}, {h} and {k}. What's its new mean in the M step?

::: right
Right. $\sum\gamma x/\sum\gamma$ = {answer}.
:::

::: misses
- [(g*x + h*y + k*z)/3] Divide by the component's total responsibility, $N_k$.
- [any] Not yet. A responsibility-weighted average.
:::
:::

::: check
given: g = 0.2 | 0.8; h = 0.4 | 0.9; k = 0.1 | 0.6; N = 3 | 10
answer: (g + h + k)/N
tolerance: 0.5%

## Out of {N} points, a component's responsibilities are {g}, {h} and {k} (zero for the rest). What's its new mixing weight?

::: right
Right. $N_k/N$ = {answer}.
:::

::: misses
- [g + h + k] That's $N_k$; divide by the number of points.
- [any] Not yet. Effective count over total.
:::
:::

::: check
given: g = 0.5 | 0.9; h = 0.3 | 0.8; x = 0 | 1; y = 3 | 5
answer: (g*(x - (g*x + h*y)/(g + h))^2 + h*(y - (g*x + h*y)/(g + h))^2)/(g + h)
tolerance: 0.5%

## A component takes responsibilities {g} and {h} for the points {x} and {y}. What's its new variance?

::: right
Right. $\sum\gamma(x - \mu)^2/N_k$ with the new mean: {answer}.
:::

::: misses
- [any] Not yet. Find the weighted mean first, then the weighted squared deviations.
:::
:::

::: check
given: K = 2 | 4 | 10; D = 2 | 5 | 20
answer: K*(D + D*(D + 1)/2) + K - 1
tolerance: 0.5%

## How many parameters does a {K}-component Gaussian mixture with full covariances have in {D} dimensions?

::: right
Right. $K(D + D(D + 1)/2) + K - 1$ = {answer}.
:::

::: misses
- [K*(D + D*D) + K - 1] A covariance is symmetric: $D(D + 1)/2$ free entries.
- [any] Not yet. Means, covariances and weights.
:::
:::

::: check
given: a = 0 | 1; b = 3 | 4; P = 0.3 | 0.6; x = 0.5 | 2
answer: ln(P*exp(-(x - a)^2/2)/sqrt(2*pi) + (1 - P)*exp(-(x - b)^2/2)/sqrt(2*pi))
tolerance: 0.5%

## What's the log-likelihood of a point at {x} under a mixture of unit-variance Gaussians at {a} and {b} with weights {P} and 1 − {P}?

::: right
Right. $\ln\sum_k\pi_k\mathcal{N}(x|\mu_k, 1)$ = {answer}.
:::

::: misses
- [any] Not yet. Add the weighted densities, then take the log.
:::
:::

::: check
kind: parts
given: K = 2 | 3 | 5; D = 2 | 8 | 20; N = 500 | 10000

## A Gaussian mixture with {K} components and diagonal covariances models {N} points in {D} dimensions.

::: part
answer: K*(2*D + 1) - 1
unit: parameters
tolerance: 0

### How many free parameters does it have?

::: misses
- [K*(2*D + 1)] The mixing weights sum to 1, so one is not free.
- [K*(D + D*(D + 1)/2 + 1) - 1] That's for full covariances.
- [any] Means, variances and weights.
:::
:::

::: part
answer: (K*(2*D + 1) - 1)/2*ln(N)
tolerance: 0.5%

### How much does BIC subtract from the log-likelihood for it?

::: misses
- [(K*(2*D + 1) - 1)*ln(N)] BIC's penalty is half of p ln N.
- [any] $\frac p2\ln N$.
:::
:::
:::

::: check
kind: derivation
vars: x = -5..5, a = -3..3, b = -3..3, s = 0.3..3, u = 0.3..3, P = 0.05..0.95

## A two-component mixture has weights P and 1 − P, means a and b, and standard deviations s and u; a point x arrives.

::: step
answer: P*exp(-(x - a)^2/(2*s^2))/s/(P*exp(-(x - a)^2/(2*s^2))/s + (1 - P)*exp(-(x - b)^2/(2*u^2))/u)

### What responsibility does the first component take for it?
:::

::: step
answer: ln(P*exp(-(x - a)^2/(2*s^2))/(s*sqrt(2*pi)) + (1 - P)*exp(-(x - b)^2/(2*u^2))/(u*sqrt(2*pi)))

### What is the point's log-likelihood?
:::

::: right
Right. With unequal widths, the 1/σ factors matter: a narrow component claims points near its mean more strongly.
:::
:::

::: explain
## Why does EM need responsibilities, and why does the likelihood never go down?

::: model
If each point's component were known, fitting a mixture would be easy: fit one Gaussian to each group. The labels are hidden, so the E step replaces them with their posterior probabilities under the current parameters, and the M step fits each Gaussian to all the points weighted by those probabilities. The M step maximizes the expected complete-data log-likelihood, which is a lower bound on the true log-likelihood that touches it at the current parameters. Raising the bound can only raise the true likelihood at least as much, so every round improves the fit or leaves it unchanged, and the algorithm climbs to a local maximum.
:::
:::

::: practice

::: check
given: a = -2 | 1; b = 2 | 5; P = 0.1 | 0.9; x = -1 | 3
answer: P*exp(-(x - a)^2/2)/(P*exp(-(x - a)^2/2) + (1 - P)*exp(-(x - b)^2/2))
tolerance: 0.5%

## Responsibility: components at {a}, {b}, weight {P}, x = {x}?

::: misses
- [any] $\pi_1\mathcal{N}_1/\sum\pi\mathcal{N}$.
:::
:::

::: check
given: g = 0.05 | 0.95; h = 0.05 | 0.95; x = -3 | 2; y = 4 | 9
answer: (g*x + h*y)/(g + h)
tolerance: 0.5%

## M-step mean: γ = {g}, {h} for x = {x}, {y}?

::: misses
- [any] $\sum\gamma x/\sum\gamma$.
:::
:::

::: check
given: K = 2 | 20; D = 1 | 50
answer: K*(D + D*(D + 1)/2) + K - 1
tolerance: 0.5%

## GMM parameters: K = {K}, D = {D}?

::: misses
- [any] $K(D + D(D + 1)/2) + K - 1$.
:::
:::
:::
