Unit 8, lesson 57

Gaussian Mixtures and Expectation-Maximization

How a Gaussian mixture model describes data with hidden component labels, how responsibilities give soft assignments, how the E step and M step of expectation-maximization fit it, and why covariance collapse needs safeguards.

  • Video 57
  • Distance and Clustering

A sample of measurements can mix two overlapping subpopulations, so that a value lying between them could plausibly come from either. A hard clustering such as k-means must still put that value in exactly one group. A Gaussian mixture model instead describes the whole distribution as a weighted sum of normal densities and gives every observation a probability of coming from each component. This lesson defines the model and its hidden component labels, computes those probabilities, carries out one exact iteration of the expectation-maximization algorithm on four constructed observations, and shows why the likelihood that the algorithm increases can grow without bound.

Key terms

Gaussian mixture model

Definition A Gaussian mixture model with KK components gives a feature vector x\mathbf{x} in Rp\mathbb{R}^p the density

p(x)=∑k=1Kπk N(x∣μk,Σk),\begin{aligned} &p(\mathbf{x}) \\[0.4em] &\quad = \sum_{k=1}^{K} \pi_k \, \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k), \end{aligned}

where the mixing weights satisfy πk≥0\pi_k \ge 0 and π1+⋯+πK=1\pi_1 + \dots + \pi_K = 1, each mean μk\boldsymbol{\mu}_k lies in Rp\mathbb{R}^p, each covariance matrix Σk\Sigma_k is symmetric positive definite, and

N(x∣μ,Σ)=exp⁡(−12 q(x))(2π)p/2det⁡(Σ)1/2\begin{aligned} &\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}, \Sigma) \\[0.4em] &\quad = \frac{\exp\bigl(-\tfrac12\, q(\mathbf{x})\bigr)}{(2\pi)^{p/2} \det(\Sigma)^{1/2}} \end{aligned}

is the normal density with q(x)=(x−μ)⊤Σ−1(x−μ)q(\mathbf{x}) = (\mathbf{x} - \boldsymbol{\mu})^{\top} \Sigma^{-1} (\mathbf{x} - \boldsymbol{\mu}). In one dimension a component has a mean μk\mu_k and a variance σk2>0\sigma_k^2 > 0, and

N(x∣μ,σ2)=12πσ2 e−(x−μ)2/(2σ2).\begin{aligned} &\mathcal{N}(x \mid \mu, \sigma^2) \\[0.4em] &\quad = \frac{1}{\sqrt{2\pi\sigma^2}}\, e^{-(x - \mu)^2/(2\sigma^2)} . \end{aligned}

The parameters θ\boldsymbol{\theta} are all weights, means and covariances, and the number of components KK is chosen by the analyst, for example by comparing log likelihoods on validation data. Exchanging the labels of two components together with their parameters gives the same density, so a component label has no meaning of its own.

In plain words The model blends KK bell-shaped densities, each with its own center, spread and share of the data. A blend of two components need not have two humps. With equal weights and a common variance σ2\sigma^2, two one-dimensional components give a single hump whenever their means are at most 2σ2\sigma apart. A component is a piece of a density model, so it need not correspond to a real subgroup, and one skewed group can need several components.

Example Four constructed one-dimensional observations, without units, are −2-2, −1-1, 22 and 33. A starting model has K=2K = 2, weights π1=π2=12\pi_1 = \pi_2 = \tfrac12, means μ1=−1\mu_1 = -1 and μ2=2\mu_2 = 2, and variances σ12=σ22=1\sigma_1^2 = \sigma_2^2 = 1. Its means are 3 apart, more than 2σ=22\sigma = 2, so its density has two humps. At x=−1x = -1 the component densities are about 0.398942 and 0.004432, and the mixture density is their average, about 0.201687. Swapping the two components gives the same density at every xx.

Latent variable

Definition A latent variable is a random variable of a model that is never observed. In a Gaussian mixture each observation ii has a latent component label ZiZ_i with values in {1,…,K}\{1, \dots, K\}, and the model generates the data in two stages,

P(Zi=k)=πk,Xi∣Zi=k∼N(μk,Σk),\begin{gathered} P(Z_i = k) = \pi_k, \\[0.4em] \mathbf{X}_i \mid Z_i = k \sim \mathcal{N}(\boldsymbol{\mu}_k, \Sigma_k), \end{gathered}

with the pairs (Xi,Zi)(\mathbf{X}_i, Z_i) independent across ii. The joint density of an observation and its label is πk N(x∣μk,Σk)\pi_k\, \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k), and summing it over kk gives back the mixture density, so the mixture is the distribution of Xi\mathbf{X}_i once the label is ignored. The pairs (xi,zi)(\mathbf{x}_i, z_i) are called the complete data and the observations xi\mathbf{x}_i alone the observed data.

In plain words Picture each observation as made in two moves. First a hidden component is drawn with probabilities πk\pi_k, then a value is drawn from that component’s normal distribution, and only the value is recorded. If the labels were known, fitting would reduce to an ordinary proportion, mean and variance for each group. Not knowing them is what makes the problem harder. The hidden label is part of a model’s account of how the data could arise, not a measured fact.

Example In the starting model, P(Z=1)=12P(Z = 1) = \tfrac12, and given Z=1Z = 1 the observation is drawn from N(−1,1)\mathcal{N}(-1, 1). The observation −1-1 could also have come from component 2, since a normal density is positive everywhere. It is only much less likely. If the labels of the four observations were known to be 1, 1, 2 and 2, the group means would be −1.5-1.5 and 2.52.5, and the group variances, dividing by the group size, would be 0.25 each.

Responsibility

Definition Write Nk(x)\mathcal{N}_k(\mathbf{x}) for N(x∣μk,Σk)\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k). Under parameters θ\boldsymbol{\theta}, the responsibility of component kk for observation xi\mathbf{x}_i is the posterior probability of its label given the observation, obtained from Bayes’ rule with densities,

rik=P(Zi=k∣xi)=πk Nk(xi)∑j=1Kπj Nj(xi).\begin{aligned} r_{ik} &= P(Z_i = k \mid \mathbf{x}_i) \\[0.4em] &= \frac{\pi_k\, \mathcal{N}_k(\mathbf{x}_i)}{\sum_{j=1}^{K} \pi_j\, \mathcal{N}_j(\mathbf{x}_i)} . \end{aligned}

Each rikr_{ik} lies in [0,1][0, 1], and ri1+⋯+riK=1r_{i1} + \dots + r_{iK} = 1. A soft assignment gives observation ii the whole vector (ri1,…,riK)(r_{i1}, \dots, r_{iK}). A hard assignment keeps only a component with the largest responsibility, with a rule for ties. The values Nk(xi)\mathcal{N}_k(\mathbf{x}_i) are densities, not probabilities. They can exceed 1 and need not add to 1 over kk, and they become probabilities only after weighting by πk\pi_k and normalizing. With equal weights and a common covariance σ2I\sigma^2 I, the largest responsibility belongs to the nearest mean, which is the k-means assignment rule. In computation, work with log⁡πk+log⁡Nk(xi)\log \pi_k + \log \mathcal{N}_k(\mathbf{x}_i), subtract the largest of these values, exponentiate and normalize, which gives the same result without underflow.

In plain words A responsibility answers the question of how likely each component is to have produced this observation, given the current model. The weights play the part of prior probabilities and the densities say how well each component explains the value. A point near one mean and far from the others gets a share near 1, and a point between overlapping components gets sizable shares in several.

Example For x=−1x = -1 under the starting model, the densities are 1/2π≈0.3989421/\sqrt{2\pi} \approx 0.398942 and e−4.5/2π≈0.004432e^{-4.5}/\sqrt{2\pi} \approx 0.004432, and the equal weights cancel, so r11=1/(1+e−4.5)≈0.989013r_{11} = 1/(1 + e^{-4.5}) \approx 0.989013. For any xx in this model,

log⁡N(x∣−1,1)N(x∣2,1) =12[(x−2)2−(x+1)2] =1.5−3x,\begin{aligned} &\log \frac{\mathcal{N}(x \mid -1, 1)}{\mathcal{N}(x \mid 2, 1)} \\[0.4em] &\ = \tfrac12 \bigl[(x - 2)^2 - (x + 1)^2\bigr] \\[0.4em] &\ = 1.5 - 3x , \end{aligned}

so the responsibility of component 1 is 1/(1+e−(1.5−3x))1/(1 + e^{-(1.5 - 3x)}), the logistic sigmoid of 1.5−3x1.5 - 3x.

Observationri1r_{i1}ri2r_{i2}
−2-20.9994470.9994470.0005530.000553
−1-10.9890130.9890130.0109870.010987
220.0109870.0109870.9890130.989013
330.0005530.0005530.9994470.999447

A hard assignment gives the labels 1, 1, 2 and 2 and discards the small chance of the other component. At x=0.5x = 0.5, halfway between the means, the two densities are equal, so with equal weights the responsibilities are 12\tfrac12 each and a hard assignment needs its tie rule. With weights 34\tfrac34 and 14\tfrac14 the responsibility of component 1 at 0.50.5 becomes 34\tfrac34, all of it from the prior.

E step and M step

Definition One iteration of the expectation-maximization algorithm for a Gaussian mixture starts from current parameters and has two steps. The E step computes every responsibility rikr_{ik} from the current parameters. The M step holds those responsibilities fixed and sets

Nk=∑i=1nrik,πknew=Nk/n,μknew=1Nk∑i=1nrik xi,Σknew=1Nk∑i=1nrik dikdik⊤,\begin{aligned} N_k &= \sum_{i=1}^{n} r_{ik}, \\[0.4em] \pi_k^{\text{new}} &= N_k / n, \\[0.4em] \boldsymbol{\mu}_k^{\text{new}} &= \frac{1}{N_k} \sum_{i=1}^{n} r_{ik}\, \mathbf{x}_i, \\[0.4em] \Sigma_k^{\text{new}} &= \frac{1}{N_k} \sum_{i=1}^{n} r_{ik}\, \mathbf{d}_{ik} \mathbf{d}_{ik}^{\top}, \end{aligned}

where dik=xi−μknew\mathbf{d}_{ik} = \mathbf{x}_i - \boldsymbol{\mu}_k^{\text{new}}. In one dimension the last line is a weighted variance with (xi−μknew)2(x_i - \mu_k^{\text{new}})^2 in place of dikdik⊤\mathbf{d}_{ik} \mathbf{d}_{ik}^{\top}. These values maximize the expected complete-data log likelihood

Q(θ)=∑i=1n∑k=1Krik[log⁡πk+log⁡Nk(xi)]\begin{aligned} Q(\boldsymbol{\theta}) &= \sum_{i=1}^{n} \sum_{k=1}^{K} r_{ik} \bigl[\log \pi_k \\[0.4em] &\qquad + \log \mathcal{N}_k(\mathbf{x}_i)\bigr] \end{aligned}

over all parameters, for the fixed responsibilities, provided every Nk>0N_k > 0 and every Σknew\Sigma_k^{\text{new}} is positive definite. Setting the derivative with respect to μk\boldsymbol{\mu}_k to zero gives the weighted mean, and maximizing the weighted sum of log⁡πk\log \pi_k under π1+⋯+πK=1\pi_1 + \dots + \pi_K = 1 gives Nk/nN_k / n. The effective count NkN_k need not be a whole number, and the covariance divides by NkN_k, as maximum likelihood does, not by Nk−1N_k - 1. A component with Nk=0N_k = 0 or a singular Σknew\Sigma_k^{\text{new}} has no valid update and needs a declared rule.

In plain words The E step asks, for each observation, how much each component explains it. The M step refits every component as if each observation belonged to it with that fractional weight, giving a weighted proportion, a weighted mean and a weighted spread. With hard labels these would be the ordinary group proportions, means and variances.

Example From the responsibility table, N1=0.999447+0.989013+0.010987+0.000553=2N_1 = 0.999447 + 0.989013 + 0.010987 + 0.000553 = 2, exactly, because the shares at −2-2 and 33 and at −1-1 and 22 mirror each other. So N2=2N_2 = 2 and both weights stay 12\tfrac12. The weighted sum ∑iri1xi\sum_i r_{i1} x_i is about −2.964275-2.964275, so μ1new≈−1.482138\mu_1^{\text{new}} \approx -1.482138. The data and the starting model are symmetric about 0.50.5, so μ2new=1−μ1new≈2.482138\mu_2^{\text{new}} = 1 - \mu_1^{\text{new}} \approx 2.482138. The weighted squared deviations of component 1 from its new mean are about 0.268033, 0.229903, 0.133220 and 0.011105, so

(σ12)new≈0.6422612≈0.321130,\begin{aligned} (\sigma_1^2)^{\text{new}} &\approx \frac{0.642261}{2} \\[0.4em] &\approx 0.321130 , \end{aligned}

and component 2 gets the same variance. The means moved outward toward −1.5-1.5 and 2.52.5, and the variances shrank from 1 to about 0.321.

Expectation-maximization algorithm

Definition The expectation-maximization (EM) algorithm is an iterative method for maximum likelihood estimation in a model with observed data x\mathbf{x}, latent variables z\mathbf{z} and parameters θ\boldsymbol{\theta}. It seeks a large value of the log likelihood ℓ(θ)=log⁡p(x∣θ)\ell(\boldsymbol{\theta}) = \log p(\mathbf{x} \mid \boldsymbol{\theta}), where the density of the observed data has the latent variables summed out. For a Gaussian mixture with independent observations, ℓ(θ)=∑ilog⁡p(xi)\ell(\boldsymbol{\theta}) = \sum_i \log p(\mathbf{x}_i). Starting from θ(0)\boldsymbol{\theta}^{(0)}, iteration tt computes the posterior distribution of z\mathbf{z} under θ(t)\boldsymbol{\theta}^{(t)} in the E step, and in the M step chooses θ(t+1)\boldsymbol{\theta}^{(t+1)} to maximize the expected complete-data log likelihood under that posterior. It stops when the change in log likelihood or in the parameters falls below a stated tolerance, or after a stated maximum number of iterations. For every probability distribution qq on the latent values that is positive at each value, Jensen’s inequality for the concave logarithm gives

log⁡p(x∣θ) ≥∑zq(z)log⁡p(x,z∣θ)q(z),\begin{aligned} &\log p(\mathbf{x} \mid \boldsymbol{\theta}) \\[0.4em] &\ \ge \sum_{\mathbf{z}} q(\mathbf{z}) \log \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{q(\mathbf{z})} , \end{aligned}

with equality when qq is the posterior under θ\boldsymbol{\theta}. Let Bt(θ)B_t(\boldsymbol{\theta}) be the right side with qq equal to the posterior under θ(t)\boldsymbol{\theta}^{(t)}. The M step maximizes BtB_t, because BtB_t differs from the expected complete-data log likelihood by a term that does not depend on θ\boldsymbol{\theta}. With exact steps,

ℓ(θ(t+1))≥Bt(θ(t+1))≥Bt(θ(t))=ℓ(θ(t)).\begin{aligned} &\ell(\boldsymbol{\theta}^{(t+1)}) \ge B_t(\boldsymbol{\theta}^{(t+1)}) \\[0.4em] &\quad \ge B_t(\boldsymbol{\theta}^{(t)}) \\[0.4em] &\quad = \ell(\boldsymbol{\theta}^{(t)}) . \end{aligned}

So the log likelihood never decreases, and when it is bounded above its values converge. The limit can be a local maximum or another stationary point rather than a global maximum, and different starting values can lead to different fits.

In plain words EM alternates between guessing the hidden labels softly from the current parameters and refitting the parameters to those soft guesses. Each guess builds a lower bound on the log likelihood that touches it at the current parameters, and the refit climbs that bound, so the log likelihood cannot fall. That makes EM stable, but a likelihood that has stopped rising only shows that the algorithm reached a stationary point. Running several starting values and comparing the results is standard practice.

Example With natural logarithms, the log likelihood of the four observations is about −7.425142-7.425142 under the starting model and −5.735514-5.735514 after the iteration above. A second iteration brings the means to about −1.5-1.5 and 2.52.5 and the variances to about 0.25, with log likelihood about −5.675754-5.675754, and later iterations change these values only in far decimal places. This limit is a local maximum. It is not the largest possible value of the likelihood, because, as the next card shows, this likelihood has no largest value at all. Starting instead from the means 2 and −1-1 reaches the same fit with the two labels exchanged.

Covariance collapse

Definition Covariance collapse is the failure in which a component’s covariance matrix approaches a singular matrix while the component concentrates on very few observations. In one dimension its variance tends to 0 while its mean sits on one observation. In pp dimensions it can happen whenever a component’s weight concentrates on pp or fewer points, since such points lie in a lower-dimensional flat. For a mixture with K≥2K \ge 2, set μ1=x1\mu_1 = x_1 with π1>0\pi_1 > 0, and hold the other components fixed with π2>0\pi_2 > 0. As σ12→0\sigma_1^2 \to 0,

π1 N(x1∣x1,σ12)=π12πσ12→∞,\begin{aligned} &\pi_1\, \mathcal{N}(x_1 \mid x_1, \sigma_1^2) \\[0.4em] &\quad = \frac{\pi_1}{\sqrt{2\pi \sigma_1^2}} \to \infty , \end{aligned}

while the mixture density at every other observation stays above the positive value π2 N2(xi)\pi_2\, \mathcal{N}_2(x_i). So the log likelihood grows without bound, an unconstrained maximum likelihood estimate does not exist, and EM can drift toward such a degenerate fit. The usual safeguards are a floor σk2≥c\sigma_k^2 \ge c with a stated c>0c > 0, or a small positive amount added to the diagonal of every covariance matrix, a prior on the covariances, several starting values, a rule for components whose effective count NkN_k falls near 1, and a comparison of fits by log likelihood on validation data. With a floor, every one-dimensional component density is at most 1/2πc1/\sqrt{2\pi c}, so the likelihood is bounded. A fit with a safeguard maximizes a constrained or penalized objective, and a report should say which.

In plain words The likelihood rewards a component for placing high density where observations are. A component that wraps itself around a single observation can make that density as large as it likes, so the likelihood can grow without limit while the model becomes useless for new data. A record-high likelihood is then a symptom, not a better model. A tiny component that claims one observation with almost no spread is nearly always this failure rather than a discovered group.

Example For the four observations, give each component weight 12\tfrac12, put component 2 at mean 0.50.5 with variance 4, and put component 1 on the observation −2-2 with variance s2s^2. The log likelihood is about −5.264-5.264 for s2=10−4s^2 = 10^{-4}, about −0.661-0.661 for s2=10−8s^2 = 10^{-8} and about 8.5498.549 for s2=10−16s^2 = 10^{-16}. Dividing s2s^2 by 100 raises it by about ln⁡10≈2.303\ln 10 \approx 2.303, and already at s2=10−4s^2 = 10^{-4} it exceeds the −5.676-5.676 of the sensible fit from the previous card. With a floor c=0.01c = 0.01 on the variances, every component density is at most 1/0.02π≈3.9891/\sqrt{0.02\pi} \approx 3.989, so the log likelihood of four observations cannot exceed 4ln⁡3.989≈5.5354 \ln 3.989 \approx 5.535.

Check yourself

  1. A two-component mixture has weights 0.2 and 0.8. At an observation xx, the two component densities are 2.0 and 0.1. Find both responsibilities and the hard assignment. A classmate says the value 2.0 shows an error, since a probability cannot exceed 1. Is that right?
Show the answer

The weighted densities are 0.2×2.0=0.40.2 \times 2.0 = 0.4 and 0.8×0.1=0.080.8 \times 0.1 = 0.08, with total 0.480.48, so

r1=0.40.48=56≈0.833,r2=0.080.48=16≈0.167.\begin{aligned} r_1 &= \frac{0.4}{0.48} = \frac{5}{6} \approx 0.833, \\[0.4em] r_2 &= \frac{0.08}{0.48} = \frac{1}{6} \approx 0.167 . \end{aligned}

The shares add to 1 and the hard assignment is component 1. The classmate is wrong. The value 2.0 is a density, a probability per unit of xx, not a probability. A normal density reaches 1/(σ2π)1/(\sigma\sqrt{2\pi}) at its mean, which is above 2 whenever σ<1/(22π)≈0.1995\sigma < 1/(2\sqrt{2\pi}) \approx 0.1995. For example, with σ=0.15\sigma = 0.15 the peak is about 2.660. Probabilities come from areas under the density, and the responsibilities, which are probabilities, stay in [0,1][0, 1].

  1. Four observations 00, 22, 44 and 1010 have responsibilities 11, 0.750.75, 0.250.25 and 00 for component 1 of a two-component mixture. Carry out the M step for component 1, then compare its mean and variance with those from a hard assignment to the component with the larger responsibility.
Show the answer

The effective count is N1=1+0.75+0.25+0=2N_1 = 1 + 0.75 + 0.25 + 0 = 2, so π1=2/4=0.5\pi_1 = 2/4 = 0.5. The weighted mean is

μ1=0+1.5+1+02=1.25.\mu_1 = \frac{0 + 1.5 + 1 + 0}{2} = 1.25 .

The squared deviations from 1.251.25 are 1.56251.5625, 0.56250.5625, 7.56257.5625 and 76.562576.5625, and weighting them by the responsibilities gives 1.56251.5625, 0.4218750.421875, 1.8906251.890625 and 00. Their sum is 3.8753.875, so σ12=3.875/2=1.9375\sigma_1^2 = 3.875/2 = 1.9375.

A hard assignment gives component 1 the observations 0 and 2, whose responsibilities are at least 0.75, and gives 4 and 10 to component 2. The hard mean is 1 and the hard variance, dividing by 2, is 1. The soft update lets the observation 4 pull the mean up with a quarter of its weight and widen the spread, and it counts 2 as only three quarters of a member. For component 2 the soft update gives N2=2N_2 = 2, mean 6.756.75 and variance 10.937510.9375, against the hard mean 7 and variance 9.

  1. A three-component mixture is fitted to 200 one-dimensional training observations from several random starts. One run reports a component with weight 0.005, a mean equal to the observation 7.31 and variance 10−1010^{-10}, and its training log likelihood is far above that of every other run. What has happened, and what should be done?
Show the answer

The effective count of that component is 0.005×200=10.005 \times 200 = 1, so it describes a single observation. Its standard deviation is 10−510^{-5}, and its density at its own mean is

12π×10−10≈39,894.\frac{1}{\sqrt{2\pi \times 10^{-10}}} \approx 39{,}894 .

Weighted by 0.005 this is about 199.5, so the mixture density at 7.31 is at least 199.5, and that single observation contributes at least ln⁡199.5≈5.30\ln 199.5 \approx 5.30 to the log likelihood. Each further halving of the variance would multiply the spike’s height by 2\sqrt{2} and add about ln⁡2≈0.347\ln \sqrt{2} \approx 0.347, so this direction has no maximum to find. This is covariance collapse. The high training likelihood is an artifact, not evidence of a real group of identical values, and on new observations the spike contributes almost nothing, since a new value almost never falls within a few multiples of 10−510^{-5} of 7.31.

The run should be discarded or refitted with a safeguard, such as a stated floor on the variances or a small amount added to each variance, and the remaining runs and choices of KK compared by log likelihood on validation data rather than on the training data. If collapse keeps recurring, the model may simply have more components than the data support.

How this lesson connects