TExcelTAD
Home › Math for Machine Learning › Mixtures as Latent-Variable Models

Mixtures as Latent-Variable Models

A Gaussian mixture can be read as a story with a hidden step: first nature secretly picks a component zz, then draws x\mathbf{x} 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 z∈{0,1}K\mathbf{z} \in \{0, 1\}^K be a one-hot vector (exactly one zk=1z_k = 1). Then

p(zk=1)=πk,p(x∣zk=1)=N(x∣μk,Σk)p(z_k = 1) = \pi_k, \qquad p(\mathbf{x} \mid z_k = 1) = \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k)

The joint is p(x,zk=1)=πk N(x∣μk,Σk)p(\mathbf{x}, z_k = 1) = \pi_k\,\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k), and the sum rule recovers the mixture:

p(x)=∑kp(x,zk=1)=∑kπk N(x∣μk,Σk)p(\mathbf{x}) = \sum_{k}p(\mathbf{x}, z_k = 1) = \sum_k\pi_k\,\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k)

Bayes' theorem gives the posterior over the hidden label, which is exactly the responsibility:

p(zk=1∣x)=πk N(x∣μk,Σk)∑jπj N(x∣μj,Σj)=rnkp(z_k = 1 \mid \mathbf{x}) = \frac{\pi_k\,\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \Sigma_k)}{\sum_j\pi_j\,\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_j, \Sigma_j)} = r_{nk}

If we knew zz, 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.

python · try it yourself
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 σ2I\sigma^2I and σ→0\sigma \to 0, 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.

python · try it yourself
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 z\mathbf{z}
topic models (LDA) topic of each word
variational autoencoder continuous z\mathbf{z} decoded by a neural network

When the posterior over z\mathbf{z} 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.predict returns the most probable hidden label, predict_proba the full posterior.
  • hmmlearn fits hidden Markov models with EM (Baum-Welch).
  • VAEs in PyTorch optimize the ELBO with the reparameterization trick.

Exercises

Exercise 1: Joint probability

With π=(0.25,0.75)\pi = (0.25, 0.75), p(x∣z1=1)=0.4p(x \mid z_1 = 1) = 0.4 and p(x∣z2=1)=0.1p(x \mid z_2 = 1) = 0.1 at some xx, find p(x)p(x) and p(z1=1∣x)p(z_1 = 1 \mid x).

Answer

p(x)=0.175p(x) = 0.175, p(z1=1∣x)=47≈0.571p(z_1 = 1 \mid x) = \frac{4}{7} \approx 0.571.

Solution

p(x)=0.25⋅0.4+0.75⋅0.1=0.1+0.075=0.175p(x) = 0.25 \cdot 0.4 + 0.75 \cdot 0.1 = 0.1 + 0.075 = 0.175; posterior 0.1/0.175=4/70.1/0.175 = 4/7. The rarer component wins because it explains xx much better.

Exercise 2: k-means as a limit

Why does EM with shared covariance σ2I\sigma^2I turn into k-means as σ→0\sigma \to 0?

Answer

Responsibilities become hard 0/1 assignments to the nearest mean.

Solution

rnk∝πke−∥xn−μk∥2/(2σ2)r_{nk} \propto \pi_k e^{-\lVert \mathbf{x}_n - \boldsymbol{\mu}_k \rVert^2/(2\sigma^2)}. As σ→0\sigma \to 0, the smallest distance dominates exponentially, so rnk→1r_{nk} \to 1 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 π=(0.2,0.8)\pi = (0.2, 0.8) and count how many come from each component.

Answer

About 2,000 and 8,000.

Solution
python · try it yourself
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 rnkr_{nk} is…

  1. the prior p(zk=1)p(z_k = 1)
  2. the posterior p(zk=1∣xn)p(z_k = 1 \mid \mathbf{x}_n)
  3. the likelihood p(xn)p(\mathbf{x}_n)
  4. a fixed constant

k-means is the limit of EM for a GMM when…

  1. K→∞K \to \infty
  2. all components share covariance σ2I\sigma^2I with σ→0\sigma \to 0
  3. the data is one-dimensional
  4. the weights are unequal

Which model uses a continuous latent variable decoded by a neural network?

  1. Hidden Markov model
  2. Variational autoencoder
  3. k-means
  4. Naive Bayes