TExcelTAD
Home › Math for Machine Learning › Gaussian Mixture Models

Gaussian Mixture Models

One Gaussian has one bump. Real data often has several clusters: heights of adults and children, sensor readings in different operating modes, customer segments. A Gaussian mixture model (GMM) is a weighted sum of KK Gaussians, flexible enough to approximate almost any density and simple enough to fit with the EM algorithm.

The model

p(x∣θ)=∑k=1Kπk N(x∣μk,Σk),πk≥0, ∑kπk=1p(\mathbf{x} \mid \boldsymbol{\theta}) = \sum_{k=1}^{K}\pi_k\,\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k), \qquad \pi_k \ge 0,\ \sum_k\pi_k = 1

The parameters are θ={πk,μk,Σk}k=1K\boldsymbol{\theta} = \{\pi_k, \boldsymbol{\mu}_k, \Sigma_k\}_{k=1}^K: mixture weights, means and covariances. With enough components a GMM can model skewed, multi-modal and heavy-ish tails.

Why maximum likelihood is hard here

The log-likelihood of NN points has a log of a sum:

log⁡p(X∣θ)=∑n=1Nlog⁡∑k=1Kπk N(xn∣μk,Σk)\log p(X \mid \boldsymbol{\theta}) = \sum_{n=1}^{N}\log\sum_{k=1}^{K}\pi_k\,\mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_k, \Sigma_k)

The log cannot move inside the sum, so setting the gradient to zero gives no closed form. The gradient condition for μk\boldsymbol{\mu}_k is still illuminating:

μk=∑nrnk xn∑nrnk,rnk=πk N(xn∣μk,Σk)∑jπj N(xn∣μj,Σj)\boldsymbol{\mu}_k = \frac{\sum_n r_{nk}\,\mathbf{x}_n}{\sum_n r_{nk}}, \qquad r_{nk} = \frac{\pi_k\,\mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_k, \Sigma_k)}{\sum_j \pi_j\,\mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_j, \Sigma_j)}

The responsibility rnkr_{nk} is the probability that point nn came from component kk. The mean is a responsibility-weighted average, but the responsibilities depend on the means: a chicken-and-egg problem that the EM algorithm solves by alternating.

python · try it yourself
import numpy as np

def gauss(x, mu, var): return np.exp(-(x - mu)**2 / (2 * var)) / np.sqrt(2 * np.pi * var)
pi = np.array([0.3, 0.7]); mu = np.array([-2.0, 1.5]); var = np.array([0.5, 1.0])
x = np.linspace(-6, 6, 12001); dx = x[1] - x[0]
p = sum(pi[k] * gauss(x, mu[k], var[k]) for k in range(2))
print("integrates to:", round(float(p.sum() * dx), 6))
print("mixture mean:", round(float((x * p).sum() * dx), 4), " = sum pi_k mu_k =", pi @ mu)
for xn in (-2.0, 0.0, 1.5):
    w = pi * gauss(xn, mu, var)
    print(f"x = {xn:4}: responsibilities {np.round(w / w.sum(), 4)}")
integrates to: 0.999998
mixture mean: 0.45  = sum pi_k mu_k = 0.44999999999999984
x = -2.0: responsibilities [0.9964 0.0036]
x =  0.0: responsibilities [0.0331 0.9669]
x =  1.5: responsibilities [0. 1.]

Points near −2-2 belong almost surely to component 1; points in between are shared. That softness is the difference between a GMM and k-means.

Sampling from a mixture

To draw from a GMM: pick a component kk with probability πk\pi_k, then draw from N(μk,Σk)\mathcal{N}(\boldsymbol{\mu}_k, \Sigma_k). This two-step story is the latent-variable view (Mixtures as Latent-Variable Models).

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
pi = np.array([0.3, 0.7]); mu = np.array([-2.0, 1.5]); sd = np.sqrt([0.5, 1.0])
k = rng.choice(2, size=100000, p=pi)
x = rng.normal(mu[k], sd[k])
print("fraction from component 1:", round(np.mean(k == 0), 3))
print("sample mean:", round(x.mean(), 3), " sample var:", round(x.var(), 3))
mix_var = pi @ (sd**2 + mu**2) - (pi @ mu)**2
print("theory var:", round(mix_var, 3))
fraction from component 1: 0.299
sample mean: 0.455  sample var: 3.435
theory var: 3.423

In software

  • sklearn.mixture.GaussianMixture(n_components, covariance_type) fits GMMs with EM; predict_proba returns the responsibilities.
  • GMMs are used for soft clustering, anomaly detection (low density means unusual) and as flexible density models in speech recognition.
  • Mixture density networks output the parameters of a GMM from a neural network.

Exercises

Exercise 1: Mixture mean

A mixture has weights (0.2,0.5,0.3)(0.2, 0.5, 0.3) and means (0,4,10)(0, 4, 10). What is its mean?

Answer
Solution

E[x]=∑kπkμk=0+2+3=5\mathbb{E}[x] = \sum_k\pi_k\mu_k = 0 + 2 + 3 = 5 by the law of total expectation.

Exercise 2: Responsibility by hand

Two 1-D components with equal weights and equal variance 1, means 0 and 2. What is the responsibility of component 2 for x=1x = 1? For x=2x = 2?

Answer

0.50.5; about 0.8810.881.

Solution

At x=1x = 1 both densities are equal. At x=2x = 2 the ratio of densities is e−0/e−2=e2e^{-0}/e^{-2} = e^2, so r=e21+e2=σ(2)≈0.881r = \frac{e^2}{1 + e^2} = \sigma(2) \approx 0.881.

Exercise 3: Bimodal check (code)

For an equal mixture of N(−1,1)\mathcal{N}(-1, 1) and N(1,1)\mathcal{N}(1, 1), is the density at 0 a local minimum? Repeat with means ±1.5\pm 1.5.

Answer

With means ±1\pm 1 the density has a single peak at 0; with ±1.5\pm 1.5 it has a dip at 0 (two modes).

Solution

An equal mixture of two unit-variance Gaussians is bimodal only when the means are more than 2 apart.

python · try it yourself
import numpy as np
g = lambda x, m: np.exp(-(x - m)**2 / 2)
for m in (1.0, 1.5):
    p = lambda x: g(x, -m) + g(x, m)
    print(m, "dip at 0" if p(0) < p(0.1) else "peak at 0")
1.0 peak at 0
1.5 dip at 0

Test yourself

The mixture weights πk\pi_k of a GMM must…

  1. be equal
  2. be non-negative and sum to 1
  3. be integers
  4. sum to KK

Why does maximum likelihood for a GMM have no closed-form solution?

  1. Gaussians have no derivatives
  2. The log-likelihood contains a log of a sum, and responsibilities depend on the parameters
  3. The likelihood is always zero
  4. There are too many data points

The responsibility rnkr_{nk} is…

  1. the weight πk\pi_k
  2. the posterior probability that point nn came from component kk
  3. the distance from xn\mathbf{x}_n to μk\boldsymbol{\mu}_k
  4. the variance of component kk