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 Gaussians, flexible enough to approximate almost any density and simple enough to fit with the EM algorithm.
The model
The parameters are : 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 points has a log of a sum:
The log cannot move inside the sum, so setting the gradient to zero gives no closed form. The gradient condition for is still illuminating:
The responsibility is the probability that point came from component . 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.
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 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 with probability , then draw from . This two-step story is the latent-variable view (Mixtures as Latent-Variable Models).
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_probareturns 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 and means . What is its mean?
Answer
Solution
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 ? For ?
Answer
; about .
Solution
At both densities are equal. At the ratio of densities is , so .
Exercise 3: Bimodal check (code)
For an equal mixture of and , is the density at 0 a local minimum? Repeat with means .
Answer
With means the density has a single peak at 0; with 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.
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 of a GMM must…
- be equal
- be non-negative and sum to 1
- be integers
- sum to
Why does maximum likelihood for a GMM have no closed-form solution?
- Gaussians have no derivatives
- The log-likelihood contains a log of a sum, and responsibilities depend on the parameters
- The likelihood is always zero
- There are too many data points
The responsibility is…
- the weight
- the posterior probability that point came from component
- the distance from to
- the variance of component