ML Systems Lab Open interactive version →
Advanced 38 min read GMMEM algorithmprobabilistic clusteringsoft assignment

Gaussian Mixture Models

EM algorithm, soft assignments, model selection with BIC

You have per-transaction purchase amounts x. Some transactions are 5 dollars (app purchases), some are 50 dollars (subscriptions), some are 500 dollars (enterprise). The distribution of purchase amounts is trimodal — three distinct subpopulations. K-means would split them by which of 3 centroids is nearest. But a single $26 transaction sits between the $5 and $50 clusters — does it belong to the 5-dollar cluster or the 50-dollar cluster? GMM computes a responsibility for each component, worked out below.

Gaussian Mixture Model:

$P(x) = Σₖ πₖ N(x | μₖ, Σₖ)$

where πₖ is the mixing weight (Σπₖ = 1), N(x | μₖ, Σₖ) is the k-th Gaussian component evaluated at a single scalar transaction amount x — not an aggregate over a customer's several purchases. Responsibility for component k at a point x is:

$r_k(x) = πₖN(x|μₖ,Σₖ) / Σⱼ πⱼN(x|μⱼ,Σⱼ)$

Worked example: take the $5 cluster as (π=0.45, μ=5, σ=8) and the $50 cluster as (π=0.35, μ=50, σ=12). At x=26:

$N(26|5,8) ≈ 0.00159 and N(26|50,12) ≈ 0.00450$

so πₖN(x|μₖ,Σₖ) gives:

$0.45×0.00159 ≈ 0.00072 (the $5 cluster) and 0.35×0.00450 ≈ 0.00157 (the $50 cluster)$

Normalizing those two:

$0.00072/(0.00072+0.00157) ≈ 31% and 0.00157/(0.00072+0.00157) ≈ 69%$

— gives responsibilities of about 31% for the $5 cluster and 69% for the $50 cluster for that one $26 transaction. A customer with several transactions gets one responsibility vector per transaction, not a single blended number for the customer.

Fitted via EM, which alternates two steps until log-likelihood stops improving: the E-step computes every point's responsibility r_k(x) for every component using the formula above; the M-step then re-estimates πₖ, μₖ, Σₖ as the responsibility-weighted mixing fraction, mean, and covariance over all points. Each E/M pair provably does not decrease the log-likelihood, but the landscape is multimodal, so EM only hill-climbs to whichever local optimum is nearest its starting point.

Covariance types control cluster shape: full (each component has a different Σₖ — arbitrary ellipsoids), tied (all components share one Σ), diag (Σₖ is diagonal — axis-aligned ellipsoids, fewer parameters), spherical (Σₖ = σₖ²I — each component is K-means-like). Full covariance is most expressive but requires the most parameters. Spherical alone is not enough to equal soft K-means — it also has to be tied (one shared σ² across every component, not a separate σₖ² per component); spherical-and-tied covariance is equivalent to soft K-means, and as σ→0 it reduces further to hard K-means's plain Euclidean nearest-centroid rule. Untied (per-component) spherical covariance still lets each cluster use its own scale, so it does not collapse to that rule.

Bayesian Information Criterion for model selection: BIC = k·log(n) - 2·log(L̂). Lower is better. Penalizes model complexity. Fit GMMs with K=1 to 20, plot BIC, take the minimum.

NOT-this: "GMM is just soft K-means." Soft K-means is GMM with spherical, tied covariance (one shared σ² across every component, not per-component σₖ²) and equal mixing weights. Full GMM with full covariance can fit elliptical clusters of any orientation and size — qualitatively different from K-means geometry. The covariance matrix is the key structural difference.

Key points

Takeaway

GMM lifts three K-means restrictions at once — soft assignments, elliptical clusters, log-likelihood as the fit criterion — but EM only finds a local optimum, so run multiple restarts and use BIC to choose K.

Recap

Check your understanding

Q1. EM for GMM is guaranteed to not decrease log-likelihood at each step, yet it often converges to a poor local optimum. Which two of the following correctly explain this?

Q2. You fit a GMM with K=5 and diagonal covariance on 10,000 points in 50 dimensions. BIC keeps decreasing as you increase K from 1 to 20. What does this suggest and what do you do?

Q3. A customer segmentation GMM has a component with mixture weight π_k = 0.001 that collapses to a tiny covariance (singular matrix). What happened and how do you fix it?

Q4. How do you use GMM for density estimation and anomaly detection? What determines the anomaly threshold?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →