Mixtures as Latent-Variable Models
A Gaussian mixture can be read as a story with a hidden step: first nature secretly picks a component , then draws from that component's Gaussian. Making the hidden variable explicit explains the responsibilities, gives EM its general form, and leads to the latent-variable models of modern ML, from hidden Markov models to variational autoencoders.
The generative story
Let be a one-hot vector (exactly one ). Then
The joint is , and the sum rule recovers the mixture:
Bayes' theorem gives the posterior over the hidden label, which is exactly the responsibility:
If we knew , it would be easy
With observed labels, the complete-data log-likelihood is a sum of separate Gaussian fits: each component's mean is the average of its own points, its weight the fraction of points. EM fills in the unknown labels with their expected values (the responsibilities) and then does exactly this. That is the general recipe of EM for any latent-variable model.
import numpy as np
rng = np.random.default_rng(0)
pi = np.array([0.6, 0.4]); mu = np.array([0.0, 3.0]); sd = np.array([1.0, 0.7])
z = rng.choice(2, size=1000, p=pi)
x = rng.normal(mu[z], sd[z])
# complete data: labels known -> closed-form ML
mu_hat = np.array([x[z == k].mean() for k in range(2)])
pi_hat = np.array([np.mean(z == k) for k in range(2)])
print("with labels: means", np.round(mu_hat, 3), " weights", np.round(pi_hat, 3))
# posterior over the hidden label for a few points (true parameters)
for xn in (-1.0, 1.5, 3.0):
w = pi * np.exp(-(xn - mu)**2 / (2 * sd**2)) / sd
print(f"p(z | x={xn}) = {np.round(w / w.sum(), 4)}")with labels: means [-0.016 2.995] weights [0.581 0.419]
p(z | x=-1.0) = [1. 0.]
p(z | x=1.5) = [0.772 0.228]
p(z | x=3.0) = [0.0115 0.9885]
Soft versus hard assignments: k-means
If all components share the covariance and , the responsibilities become 0 or 1 (each point belongs to its nearest mean) and EM becomes k-means: assign points to the nearest centre, then move each centre to the mean of its points. GMMs keep the soft assignments and also learn shapes and weights.
import numpy as np
mu = np.array([0.0, 3.0]); x = 1.4
for s in (2.0, 1.0, 0.5, 0.1):
w = np.exp(-(x - mu)**2 / (2 * s**2))
print(f"sigma={s}: responsibilities {np.round(w / w.sum(), 4)}")sigma=2.0: responsibilities [0.5187 0.4813]
sigma=1.0: responsibilities [0.5744 0.4256]
sigma=0.5: responsibilities [0.7685 0.2315]
sigma=0.1: responsibilities [1. 0.]
Beyond mixtures
The same pattern (hidden variable, generative story, posterior inference, EM or a variational bound) underlies many models:
| Model | Hidden variable |
|---|---|
| hidden Markov model | a sequence of discrete states |
| factor analysis, probabilistic PCA | a continuous low-dimensional |
| topic models (LDA) | topic of each word |
| variational autoencoder | continuous decoded by a neural network |
When the posterior over is intractable (as in a VAE), the E-step is replaced by an approximate posterior trained with the same evidence lower bound.
In software
GaussianMixture.predictreturns the most probable hidden label,predict_probathe full posterior.hmmlearnfits hidden Markov models with EM (Baum-Welch).- VAEs in PyTorch optimize the ELBO with the reparameterization trick.
Exercises
Exercise 1: Joint probability
With , and at some , find and .
Answer
, .
Solution
; posterior . The rarer component wins because it explains much better.
Exercise 2: k-means as a limit
Why does EM with shared covariance turn into k-means as ?
Answer
Responsibilities become hard 0/1 assignments to the nearest mean.
Solution
. As , the smallest distance dominates exponentially, so for the nearest mean and 0 otherwise; the M-step mean update becomes the plain average of the assigned points.
Exercise 3: Sample the story (code)
Simulate 10,000 draws from the generative story with and count how many come from each component.
Answer
About 2,000 and 8,000.
Solution
import numpy as np
rng = np.random.default_rng(5)
z = rng.choice(2, size=10000, p=[0.2, 0.8])
print(np.bincount(z))[2033 7967]
Test yourself
In the latent-variable view of a GMM, the responsibility is…
- the prior
- the posterior
- the likelihood
- a fixed constant
k-means is the limit of EM for a GMM when…
- all components share covariance with
- the data is one-dimensional
- the weights are unequal
Which model uses a continuous latent variable decoded by a neural network?
- Hidden Markov model
- Variational autoencoder
- k-means
- Naive Bayes