Skip to content
Lambdian

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 KK Gaussian "sources" (with probabilities πk\pi_k) 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 zz: the E-step computes q(z)=p(z∣x,θold)q(z) = p(z\mid x, \theta_\text{old}); the M-step maximises the expected complete-data log-likelihood Eq[log⁡p(x,z∣θ)]\mathbb{E}_q[\log p(x, z\mid\theta)]. Each iteration provably never decreases log⁡p(x∣θ)\log p(x\mid\theta), 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 BIC=−2log⁡L+plog⁡n\text{BIC} = -2\log L + p\log n penalises extra parameters; pick the K with the lowest BIC.
  • Uses: soft clustering, density estimation, anomaly scores (low p(x)p(\mathbf{x})), and as building blocks (speaker models, HMM emissions).

The algorithm, step by step

  1. 1
    Initialise means (e.g. from k-means), covariances (sample covariance) and weights (uniform).
  2. 2
    E-step: compute responsibilities rikr_{ik} for every point and component.
  3. 3
    M-step: update πk\pi_k, μk\boldsymbol\mu_k, Σk\Sigma_k from responsibility-weighted statistics.
  4. 4
    Compute the log-likelihood; stop when its improvement is below a tolerance.
  5. 5
    Assign each point to arg⁡max⁡krik\arg\max_k r_{ik} if a hard clustering is needed.

The maths

Mixture density

p(x)=∑kπkN(x∣μk,Σk)p(\mathbf{x}) = \sum_k\pi_k\mathcal{N}(\mathbf{x}\mid\boldsymbol\mu_k,\Sigma_k)

Log-likelihood

log⁡L=∑ilog⁡∑kπkN(xi∣μk,Σk)\log L = \sum_i\log\sum_k\pi_k\mathcal{N}(\mathbf{x}_i\mid\boldsymbol\mu_k,\Sigma_k)

Responsibilities (E-step)

rik=πkN(xi∣μk,Σk)∑jπjN(xi∣μj,Σj)r_{ik} = \frac{\pi_k\mathcal{N}(\mathbf{x}_i\mid\boldsymbol\mu_k,\Sigma_k)}{\sum_j\pi_j\mathcal{N}(\mathbf{x}_i\mid\boldsymbol\mu_j,\Sigma_j)}

M-step

πk=Nkn, μk=∑irikxiNk, Σk=∑irik(xi−μk)(xi−μk)⊤Nk\pi_k = \frac{N_k}{n},\ \boldsymbol\mu_k = \frac{\sum_i r_{ik}\mathbf{x}_i}{N_k},\ \Sigma_k = \frac{\sum_i r_{ik}(\mathbf{x}_i-\boldsymbol\mu_k)(\mathbf{x}_i-\boldsymbol\mu_k)^\top}{N_k}

BIC

BIC=−2log⁡L+plog⁡n\mathrm{BIC} = -2\log L + p\log n

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…

Clusters overlap or are elongated, you need membership probabilities, or you need a density model of continuous data.

Check your understanding

Question 1

Q1What does the E-step compute?

Question 2

Q2k-means is a special case of GMM-EM with…

Question 3

Q3How can you choose the number of components K?

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).
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.