Gaussian mixtures and the EM algorithm
Model data as a mix of Gaussians: soft, elliptical clusters fitted by expectation–maximisation.
Animated explanation
The idea
k-means gives each point exactly one cluster and assumes clusters are round. A Gaussian mixture model says the data was generated by first picking one of Gaussian "sources" (with probabilities ) and then drawing a point from it. Each source has its own mean and covariance, so clusters can be stretched and tilted, and each point gets a probability of belonging to each cluster.
We do not know which source produced which point, which makes direct maximum likelihood hard. EM breaks the chicken-and-egg problem: guess the parameters; compute soft assignments (E-step); refit the parameters from those soft assignments (M-step); repeat. k-means is the special case with hard assignments and identical round covariances.
EM in general
EM works for any model with hidden variables : the E-step computes ; the M-step maximises the expected complete-data log-likelihood . Each iteration provably never decreases , because the E-step makes a lower bound (the ELBO) tight and the M-step raises it. It converges to a local maximum, so use several initialisations (k-means results are a good start).
Practical choices
- Covariance type: full, diagonal, tied or spherical, trading flexibility for parameters.
- Degeneracy: a component can collapse onto one point with zero variance and infinite likelihood; add a small regulariser to the diagonal.
- Choosing K: the Bayesian information criterion penalises extra parameters; pick the K with the lowest BIC.
- Uses: soft clustering, density estimation, anomaly scores (low ), and as building blocks (speaker models, HMM emissions).
The algorithm, step by step
- 1Initialise means (e.g. from k-means), covariances (sample covariance) and weights (uniform).
- 2E-step: compute responsibilities for every point and component.
- 3M-step: update , , from responsibility-weighted statistics.
- 4Compute the log-likelihood; stop when its improvement is below a tolerance.
- 5Assign each point to if a hard clustering is needed.
The maths
Mixture density
Log-likelihood
Responsibilities (E-step)
M-step
BIC
Strengths and weaknesses
Strengths
- Soft, probabilistic memberships
- Elliptical clusters of different sizes and orientations
- A full density model: likelihoods and anomaly scores
Weaknesses
- Local maxima; sensitive to initialisation
- Must choose K and covariance type
- Can degenerate without regularisation; assumes Gaussian shapes
Reach for it when…
Check your understanding
Lambdian Lab
EM for a Gaussian mixture
Fit a K-component GMM with full covariances.
- gaussian_pdf(X, mu, cov): densities of the rows of X.
- e_step(X, pi, mus, covs) → responsibilities R (n × K) and the total log-likelihood.
- m_step(X, R, reg) → (pi, mus, covs); add reg·I to each covariance.
- fit_gmm(X, K, iters, seed): initialise means from K random rows, covariances to the data covariance, weights uniform; return (pi, mus, covs, log_likelihoods).