Foundations to Frontier
Tests

Gaussian Mixture Models with EM

A Gaussian mixture model describes data as coming from k Gaussian components, each with its own mean, covariance and weight. Fitting it by expectation–maximisation alternates soft assignments (responsibilities: the posterior probability that each component generated each point) with weighted re-estimation of weights, means and covariances. Unlike k-means, clusters can be elliptical, tilted and of different sizes, assignments are probabilities, and the model is a density you can evaluate. This page covers the multivariate model, EM updates with full, diagonal, spherical and tied covariances, numerical safeguards (log-sum-exp, covariance regularisation, singularities), choosing k and the covariance type by BIC, and the k-means connection. In the exercises you implement multivariate EM and beat k-means on elongated clusters; then you use BIC to select both k and the covariance structure.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Clusters as overlapping bells of any shape

A Gaussian mixture model says each cluster is a bell-shaped cloud, and each cloud may be round, stretched or tilted, big or small. Every point gets a probability of belonging to each cloud rather than a single label. EM fits it by repeating two steps:

  • guess each point’s probabilities under the current clouds;
  • refit each cloud to the points, weighted by those probabilities.
Gaussian mixture + EM: soft clusters with shape
log-likelihood by iteration
log-likelihood -827.9 · BIC 1751.0 (lower is better)
Full covariances let each ellipse tilt and stretch to fit one elongated cluster. With spherical or diagonal covariances the shapes can't tilt, so the fit is worse and more components are needed to cover each cluster. Compare the BIC across settings: it trades fit against the number of parameters. The likelihood never drops from one iteration to the next.
Why engineers care

Sensor clusters are rarely round. Correlated channels produce tilted, elongated clouds that k-means cuts wrongly. A GMM captures that shape, gives calibrated soft memberships for borderline cases, and provides a density for anomaly detection (low likelihood means unusual). It underlies speaker recognition, background subtraction, and many fault-detection systems.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A GMM assigns each point…
  2. 2. Full covariances let clusters be…
  3. 3. EM's likelihood from one iteration to the next…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The pieces by hand

Responsibilities. Two 1-D components N(0, 1) and N(2, 1) with equal weights:

  • for the point x = 0, the first component’s responsibility is 1/(1 + e^(−2)) = 0.881;
  • for x = 1, halfway between, both responsibilities are 0.5.

Weighted M-step. Points 0 and 4 with responsibilities 3 and 1 (as unnormalised weights): mean (0·3 + 4·1)/4 = 1, variance (3·1² + 1·3²)/4 = 3.

Parameter counts (d = 2, k = 3). Weights take k − 1 = 2 and means take k·d = 6, plus the covariances:

covariance covariance parameters total
full k·d(d + 1)/2 = 9 17
diagonal k·d = 6 14
spherical k = 3 11

A tied (shared full) covariance with k = 4 has 3 + 8 + 3 = 14.

BIC. BIC = −2·log L + p·ln n, lower is better. With log L = −100, p = 5 and n = e², BIC = 200 + 10 = 210.

The steps in plain English:

  1. Initialise k means (k-means++ or k-means), covariances (data covariance) and equal weights.
  2. E-step: for every point and component, compute the weighted density, in logs, and normalise to responsibilities.
  3. M-step: weights = mean responsibility; means = responsibility-weighted averages; covariances = weighted scatter, plus a small ridge.
  4. Repeat until the log-likelihood stops improving. Use several starts, and choose k and the covariance type by BIC.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Parameters of a full-covariance GMM with k = 2, d = 2?
  2. 2. Responsibility at a point equidistant from two equal components?
  3. 3. Lower BIC means…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Covariance structure, singularities, and k-means

Why GMMs beat k-means on elongated clusters. k-Means assigns by Euclidean distance to centres, so its boundaries are perpendicular bisectors. It cuts long parallel clusters across their length. A full-covariance GMM assigns by Mahalanobis distance plus log-determinant terms, so it follows each cluster’s shape. Exercise 1, three long thin parallel clusters: the GMM reached ARI 0.921, while k-means reached 0.361 even when started from one point inside each true cluster.

Covariance types.

  • Full: most flexible, d(d + 1)/2 parameters per component.
  • Diagonal: axis-aligned ellipses.
  • Spherical: round, one variance per component.
  • Tied: one shared full covariance, which gives linear boundaries.

Restricted types need fewer parameters, but they approximate tilted clusters with several components.

Singularities. If a component collapses onto one point, its variance goes to 0 and the likelihood to +∞. The maximum-likelihood problem is unbounded. Guard against this with a ridge (reg_covar, here 10⁻⁶·I), a minimum variance, priors (MAP-EM), or restarting tiny components.

k-means as a limit. With equal weights and shared spherical covariance σ²I, as σ → 0 the responsibilities become hard nearest-centre assignments, and EM becomes Lloyd’s algorithm.

Choosing k and the shape. BIC penalises parameters by ½·ln n each. Exercise 2: on three tilted, elongated clusters, BIC over k = 1–5 and three covariance types picked (full, k = 3) in every dataset, even at n = 60. If only spherical components were allowed, BIC wanted 5 components (the grid maximum), because elongated clusters need several round blobs.

from sklearn.mixture import GaussianMixture
best = min((GaussianMixture(k, covariance_type=t, n_init=3, random_state=0).fit(X) for t in ["spherical", "diag", "full"]
            for k in range(1, 7)), key=lambda g: g.bic(X))
best.predict_proba(X), best.score_samples(X)     # responsibilities, log-density
🐍 Exercise 1: multivariate EM
Implement the log-space E-step with Cholesky-based Gaussian log-densities, the M-step with a covariance ridge, and EM with restarts; check monotone likelihood and beat k-means on long, thin parallel clusters.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why is the GMM likelihood unbounded?
  2. 2. A tied covariance gives decision boundaries that are…
  3. 3. k-Means is the limit of EM for a GMM with…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

EM as coordinate ascent. For any distribution q over the component labels, log L ≥ E_q[log p(x, z)] + H(q). The E-step sets q to the posterior, making the bound tight. The M-step maximises the bound over the parameters. Hence log L never decreases (Dempster, Laird and Rubin, 1977). Convergence is to a stationary point, usually a local maximum, and it is linear, slow when clusters overlap heavily.

Identifiability. Mixtures are identifiable only up to relabelling of components. The likelihood has k! symmetric maxima. That matters for interpreting components, not for clustering.

BIC consistency. For mixtures, BIC is not a regular-model criterion, but it is consistent for the number of components under mild conditions (Keribin, 2000). It tends to prefer fewer components than AIC. With model misspecification (non-Gaussian clusters), BIC adds components to approximate each cluster’s shape. That is a density-estimation answer, not a clustering answer, and ICL (BIC plus an entropy penalty) was designed to fix it.

Interview questions.

  • GMM or k-means? Use a GMM when clusters differ in shape, size or orientation, when you need probabilities or a density, or for anomaly scoring. k-Means is faster and adequate for round, similar clusters.
  • How do you make EM robust? Use several initialisations (k-means++), a covariance ridge, log-space computation, BIC or ICL for model choice, and inspect tiny or degenerate components.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The E-step makes the EM lower bound…
  2. 2. How many equivalent maxima from label switching for k = 4?
  3. 3. ICL differs from BIC by adding…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Can BIC pick both k and the covariance shape?

History.

  • K. Pearson (1894) fitted a two-component normal mixture by the method of moments.
  • A. Dempster, N. Laird and D. Rubin formalised EM (1977).
  • C. Fraley and A. Raftery developed model-based clustering with BIC over covariance parameterisations (mclust, 1998–2002).
  • C. Biernacki, G. Celeux and G. Govaert proposed ICL (2000).

Exercise 2. Three rotated, elongated clusters (variances 2 and 0.12); BIC over k = 1–5 × spherical/diagonal/full, best of 2 starts, 3 datasets per n.

n BIC’s overall choice best k, spherical only best k, diagonal only best k, full only
60 full, k = 3 (3/3) 4.67 3.67 3.00
150 full, k = 3 (3/3) 5.00 4.67 3.00
600 full, k = 3 (3/3) 5.00 5.00 3.00
  • BIC chose the true model every time, even at n = 60.
  • Restricted shapes inflated k. Their best k reached the grid maximum (5), and more so as n grew. With more data, BIC can afford more components to approximate each tilted cluster better.
  • The takeaway: if you fix a restrictive covariance type, BIC’s k counts density components, not clusters.
🐍 Exercise 2 (research): BIC over k and covariance type
On three elongated, rotated clusters, compute BIC for k = 1–5 with spherical, diagonal and full covariances at n = 60, 150, 600; record the overall choice and the best k within each covariance type.

GMMs in AI, robotics and aerospace

  • Anomaly detection: low GMM log-density flags unusual flight or engine states.
  • Background subtraction and segmentation: per-pixel mixtures (Stauffer–Grimson) on surveillance and drone cameras.
  • Multi-modal sensor models: mixtures of range-measurement modes in lidar or sonar, and mixture priors in tracking (Gaussian-sum filters).

References

  1. A. P. Dempster, N. M. Laird, D. B. Rubin (1977). “Maximum likelihood from incomplete data via the EM algorithm.” JRSS B 39(1).
  2. C. Fraley, A. E. Raftery (2002). “Model-based clustering, discriminant analysis, and density estimation.” JASA 97(458).
  3. C. Biernacki, G. Celeux, G. Govaert (2000). “Assessing a mixture model for clustering with the integrated completed likelihood.” IEEE TPAMI 22(7).
  4. G. McLachlan, D. Peel (2000). Finite Mixture Models. Wiley.
  5. C. Stauffer, W. E. L. Grimson (1999). “Adaptive background mixture models for real-time tracking.” Proc. CVPR.