Skip to content

Gaussian Mixtures & EM

Replace hard cluster assignments with soft responsibilities and fit overlapping bell curves by alternating E and M steps

Quick recap
  • A Gaussian mixture models data as a weighted sum of Gaussian components.
  • The E-step computes each point’s responsibility for each component.
  • The M-step updates weights, means, and variances from those responsibilities.

This lesson follows Clustering with k-Means and leads into Principal Component Analysis. The probability background is in Probability & Statistics. You can compare hard assignments with soft responsibilities in the k-Means Playground.

1. Hidden membership

k-means assigns each point to exactly one cluster. That is a hard decision. But a point between two clumps may be plausibly generated by either one. A Gaussian mixture keeps that ambiguity as a probability.

Imagine two overlapping bell curves. Each observed point has a hidden label saying which curve produced it. We do not see the label, so we treat it as a latent variable. EM alternates between guessing the labels softly and refitting the curves to those guesses.

Before reading the formulas, look at the animation. At the start the two curves are a guess. Predict whether the points near the overlap get a responsibility of 0.5 or something closer to one component.

2. The mixture model

A two-component Gaussian mixture says the data density is a weighted sum of two Gaussians:

Mixture density

π_k is the mixture weight, with the weights summing to 1. μ_k and σ_k² are the mean and variance of component k. Unlike k-means, the clusters can overlap and have different widths.

The model is generative: to sample a point, first choose a component with probability π, then draw from that Gaussian. The observed data do not tell us which component was chosen.

3. Expectation and maximization

The E-step uses the current parameters to compute the responsibility of component k for point i:

Responsibility

The responsibilities are posterior probabilities. They are non-negative and sum to 1 for each point. A point at the overlap can have 0.5 / 0.5; a point deep inside one component can have 0.99 / 0.01.

The M-step treats the responsibilities as soft counts and updates each component:

Update mean
Update variance

The weight is the average responsibility of the component. The mean is a responsibility-weighted average of the data. The variance is a responsibility-weighted squared deviation. EM repeats E and M until the log likelihood stops improving.

Log likelihood

4. Worked example

Take two components with equal weights 0.5, means −1 and 1, and variance 1. At x = −1, the component with mean −1 is much more likely; at x = 1 the other component wins.

Worked example: responsibilities at two points

pointdensity from mean −1density from mean 1responsibility for mean −1responsibility for mean 1
x = −10.39890.05400.8810.119
x = 10.05400.39890.1190.881

The mixture log likelihood at these two points is −2.970. The responsibilities sum to 1 at each point, but they are not the same as the mixture weights.

One EM iteration would update the means toward the points with high responsibility, update the variances from the weighted spread, and recompute the responsibilities. The likelihood is non-decreasing under exact EM.

In the animation, the bars show the responsibilities after each E-step. They are not hard labels; a point can contribute to both components.

5. Choosing components

More components can always fit the training data better. A mixture with one component per point can drive the likelihood toward infinity by shrinking a variance around a point. Real implementations add variance floors, regularization, or a prior.

Model selection needs held-out likelihood, BIC, AIC, or a task-specific criterion. BIC penalizes parameters and can prefer fewer components; it is still a model-based score, not a guarantee that the components correspond to real groups.

Try it

Why is a high training likelihood not enough to choose the number of components?

6. Where it fails

EM finds a local optimum. Different initializations can produce different means, weights, and labels. A component can collapse onto one point if its variance is allowed to approach zero.

A Gaussian may be the wrong shape: counts, categories, and multimodal clusters need different likelihoods. Correlated features need a full covariance matrix, not independent one-dimensional Gaussians. In high dimensions, covariance estimation needs regularization and more data.

The next lesson changes the task from grouping points to compressing their dimensions. PCA looks for the directions that carry the most variance.

Next lesson: Principal Component Analysis

7. Going deeper

EM is a general algorithm for latent-variable models, not just Gaussian mixtures. The E-step computes a posterior over the latent variables; the M-step maximizes the expected complete-data log likelihood. The observed-data likelihood increases at every iteration, but the guarantee is local.

A Gaussian mixture with shared spherical covariance is close to k-means. Full covariance gives elliptical clusters; tied covariance shares one shape across components; diagonal covariance assumes feature independence. The choice is a bias-variance trade-off.

Bayesian Gaussian mixtures place a prior on the weights and can prune unused components. They are slower, but they avoid some singularities and make the number of active components part of the inference.

8. Practice

Try it

If a point has responsibility 0.9 for component A, what is its responsibility for component B in a two-component model?

Try it

Why can a Gaussian mixture represent an elongated cluster better than k-means?

Try it

Does increasing the number of components always improve the held-out likelihood?

9. Resources

The scikit-learn mixture chapter covers EM, covariance types, initialization, and model selection. The Wikipedia article derives the E and M steps for a Gaussian mixture. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Probability & statistics.

All Machine Learning lessons

Site soundtrack