Mixtures and the EM algorithm
Clustering as a probability model — the Gaussian mixture, latent variables and responsibilities, a lower bound on the log-likelihood and the EM algorithm as coordinate ascent on it, the updates for a Gaussian mixture, k-means as the hard limit, local optima, collapsing components and choosing the number of components with BIC.
From k-means to a mixture
The topic Clustering ended with k-means and its two blind spots. Every point goes to exactly one cluster, and nothing says how sure that assignment is. And the objective counts plain distances, so k-means behaves as if every cluster were a round ball of the same width. This topic turns clustering into a probability model and fits it by maximum likelihood.
A Gaussian mixture describes the data by a two-step story. First pick a component with probabilities . Then draw from that component's Gaussian:
Here is the number of components, the clusters of the previous topic. The weights satisfy and . Each component has its own mean and covariance , and collects all the parameters. The second argument of is a covariance, a variance in one dimension.
The component is a latent variable: the model uses it, the data never show it. So the density of alone sums the joint over , by the law of total probability:
Each component is a multivariate Gaussian with elliptical contours set by (the topic The multivariate Gaussian in Probability Theory). So a cluster may be tilted, elongated, small or wide, and each has its own weight.
Example. In one dimension take , and standard deviations and . At the component densities are and , so
Responsibilities
Given the parameters, which component produced the point ? Bayes' formula answers it (the topic Conditional probability and Bayes' formula). The prior is , the likelihood is , and the evidence is :
These numbers are the responsibilities. Each row is a distribution over components: and . They are the soft version of the k-means assignment .
In the example above, the point belongs to the first component with probability and to the second with . k-means with centres and would put the same point exactly on the boundary, because it compares distances only. The mixture also weighs how wide each component is and how often it occurs.
Why the likelihood is hard
For independent points the log-likelihood is
The logarithm of a sum does not split. Setting the gradient to zero couples every parameter with every other, and there is no closed-form solution.
Now pretend the components were observed. The complete-data log-likelihood is
This is a sum of logarithms. It splits into one Gaussian fit per component, on the points with that label, and one fit of the weights. Each has the closed form from the topic Maximum likelihood: the share of points, the mean, and the covariance with the factor . All the difficulty is in not knowing .
Three properties of matter before we fit it.
- It is not concave and has several local maxima.
- Relabelling the components does not change it, so every maximum comes in copies.
- It is unbounded. Centre a component on one data point and shrink its variance to zero: the density at that point, and with it , grows without limit. The topic Maximum likelihood met this case in its block on where the method breaks.
So the global maximum of a Gaussian mixture likelihood does not exist. What we look for is a good local maximum away from such collapses.
The lower bound
The trick is a lower bound on that is easy to maximize. Take one point with latent (for a dataset, sum over points). Let be any distribution over the values of , positive wherever the posterior is. The product rule gives , so for every
The left side does not depend on . Averaging both sides over leaves it unchanged:
The second term is the Kullback–Leibler divergence from to the posterior. As the topic Maximum likelihood recalled, it is never negative and is zero only when the two distributions coincide. Hence
with equality exactly when is the posterior . is the evidence lower bound, the ELBO. Jensen's inequality for the concave logarithm gives the same bound in one line (the topic Expectation and variance). The identity says more: the gap is exactly a KL divergence, so we know when the bound is tight.
Why is this bound useful? Its first term is the complete-data log-likelihood averaged over guessed labels, a sum of logarithms again. Its second term does not involve at all.
EM: coordinate ascent
has two blocks of variables: the distributions , one per point, and the parameters . The EM algorithm maximizes it over one block at a time, from a start .
- E-step. Fix and maximize over . The left side does not depend on , so this means making the KL term zero: . For a mixture these are the responsibilities . After the E-step the bound touches at .
- M-step. Fix and maximize over . The entropy is now a constant, so we maximize the expected complete-data log-likelihood:
Why the log-likelihood never decreases takes one line. The bound holds at any ; the M-step does not lower ; the E-step made the bound tight at :
This is coordinate ascent, the mirror image of Lloyd's coordinate descent on . Where the bound touches the two also share a gradient, so EM stops only at stationary points of . That can be a local maximum or a saddle, and which one depends on the start.
A toy run. Seven points and the model , where only the mean of the second component is unknown. At the responsibilities of the second component are
The M-step for one mean is a weighted average, . Then , and EM converges to . The log-likelihood goes , , , and settles at . Soon each step is about of the previous one: EM converges linearly, fast at first and slowly at the end.
The updates for a Gaussian mixture
For a full Gaussian mixture the M-step objective is
It is the complete-data log-likelihood with each indicator replaced by its probability . It splits by parameter. Write for the effective number of points in component .
- Weights. Maximize subject to . A Lagrange multiplier gives (the topic Lagrange multipliers and KKT conditions).
- Means. The gradient in is . It vanishes at the weighted mean.
- Covariances. The derivatives of and of a trace (the topic Matrix calculus) give the weighted covariance.
Together the M-step is
These are the Gaussian maximum likelihood estimates with every point counted with weight , and uses the new . One iteration costs for the responsibilities plus for the inverses and determinants. The E-step is computed in logarithms, with evaluated stably, because densities underflow in high dimension.
On 300 points. Take 300 points in the plane from three Gaussians: one tilted and elongated, one small, one wide. Start EM badly on purpose, with all three means at points of the first cloud. The average log-likelihood is at the start, after one iteration, after five, after 25 and after 50, and it never falls. scikit-learn's GaussianMixture from the same start reaches the same . Against the true clusters, the fitted mixture scores an adjusted Rand index (ARI, the topic Clustering) of , k-means only . k-means draws straight boundaries halfway between centres and hands 8 points of the elongated tilted cloud to the small cluster.
k-means as the hard limit
Where is k-means in all this? Fix every covariance at with a known and update only the means. The responsibilities become
As , the term of the nearest mean beats every other by a factor that grows without bound. So for the nearest centre and for the rest: the E-step becomes the assignment step of k-means. The M-step for the means becomes the mean of the assigned points, the update step. And becomes plus terms that do not involve the means. Lloyd's algorithm is EM for identical spherical Gaussians in the limit of vanishing variance. On the 300 points with , every responsibility is or , and the means coincide with the k-means centres from the same start.
Replacing the posterior by its most probable value is called hard EM. It no longer maximizes the likelihood. A point in the overlap of two clusters goes entirely to one of them, and that pushes the means apart. In the toy run, hard EM from ends at , the mean of . Soft EM ends at , the maximum of the likelihood: there , at the hard answer .
Starts, collapses and the number of components
Starts. Monotonicity promises progress, not the destination. On the 300 points, 30 runs started from random data points end at six different values of the average log-likelihood, from to , and 83% reach the best one. All 30 runs started from k-means++ centres reach it. Hence the recipe: start from k-means or k-means++ (scikit-learn runs k-means first by default), make several starts, keep the highest likelihood. EM is also slow when components overlap heavily, because the responsibilities, and with them the steps, change little.
Collapses. A component that captures one or two points can shrink its covariance towards a singular matrix while grows. Three defences: add a small constant to the diagonal of every (reg_covar in scikit-learn, by default); put a prior on the covariances and fit the MAP estimate, a regularized M-step in the sense of the topic Regularization; or restrict the covariances to be shared, diagonal or spherical, which also cuts the parameter count.
How many components. The likelihood cannot choose , for the same reason cannot. On the 300 points it is , , , , , for , and it keeps rising. The Bayesian information criterion penalizes the number of free parameters :
and smaller is better. The three parts of are the weights minus one constraint, the means, and the symmetric covariances. For and , . On the 300 points the BIC is , , , , , , smallest at . BIC is a large-sample approximation. The alternative without asymptotics is the log-likelihood on held-out data, as in the topic Validation and overfitting.
Further. Nothing in the derivation used Gaussians. EM works whenever the complete-data likelihood is easy and the posterior of the latent variables is computable: missing values, mixtures of other distributions, hidden Markov models. Bayesian Methods in Machine Learning, in the topic EM and Bayesian PCA, treats EM in general and what to do when the exact E-step is out of reach. Generative Models, in the topic Latent variables and the ELBO, keeps the same bound for a continuous and a neural decoder: the posterior is no longer computable, so an encoder learns to approximate it, and that is the variational autoencoder.
A mixture explains each point by a discrete latent variable, the cluster it came from. Clusters summarize objects. The next topic, Dimensionality reduction, summarizes features instead: it finds a few continuous directions along which the data vary. Its probabilistic version, probabilistic PCA, is again a latent-variable model fitted by maximum likelihood, this time with a continuous .
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.