Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Gaussian Mixture Models and the EM Algorithm

Gaussian Mixture Model

From K-Means to GMM

K-means limitations:

GMM provides a soft assignment approach via probabilistic modeling.

Mixture Model

Each sample xx is generated with a hidden variable z∈{1,…,K}z \in \{1, \dots, K\} indicating which component generated it:

p(x,z)=p(x∣z) p(z)p(x, z) = p(x \mid z) \, p(z)

The marginal distribution (since zz is unobserved):

p(x)=∑k=1Kp(x∣z=k) πkp(x) = \sum_{k=1}^K p(x \mid z = k) \, \pi_k

where πk=p(z=k)\pi_k = p(z = k) are mixing coefficients with πk≥0\pi_k \geq 0 and ∑kπk=1\sum_k \pi_k = 1.

Gaussian Mixture Model

In a GMM, each component is Gaussian:

p(x∣z=k)=N(x∣μk,Σk)p(x \mid z = k) = \mathcal{N}(x \mid \mu_k, \Sigma_k)

Therefore:

p(x)=∑k=1Kπk N(x∣μk,Σk)p(x) = \sum_{k=1}^K \pi_k \, \mathcal{N}(x \mid \mu_k, \Sigma_k)

With sufficiently many components, GMMs can approximate a wide class of densities.

Maximum Likelihood Estimation

Given data X={x(1),…,x(N)}\mathcal{X} = \{x^{(1)}, \dots, x^{(N)}\}, the log-likelihood is:

ℓ(θ)=∑n=1Nlog⁡(∑k=1Kπk N(x(n)∣μk,Σk))\ell(\theta) = \sum_{n=1}^N \log \left( \sum_{k=1}^K \pi_k \, \mathcal{N}(x^{(n)} \mid \mu_k, \Sigma_k) \right)

Difficulty: the logarithm is outside the summation, so no closed-form solution exists. This motivates the EM algorithm.


Expectation-Maximization Algorithm

Preliminaries

Jensen’s Inequality: If ff is convex, then f(E[X])≤E[f(X)]f(\mathbb{E}[X]) \leq \mathbb{E}[f(X)]. For concave ff (like log⁡\log), the inequality reverses.

KL Divergence: For distributions pp and qq:

KL(q∥p)=∑zq(z)log⁡q(z)p(z)≥0\text{KL}(q \parallel p) = \sum_z q(z) \log \frac{q(z)}{p(z)} \geq 0

with equality iff q(z)=p(z)q(z) = p(z). KL divergence is non-negative but not symmetric.

Derivation via Lower Bound

For one sample xx and any distribution q(z)q(z):

log⁡p(x∣θ)=log⁡∑zq(z)p(x,z∣θ)q(z)\log p(x \mid \theta) = \log \sum_z q(z) \frac{p(x, z \mid \theta)}{q(z)}

By Jensen’s inequality (since log⁡\log is concave):

log⁡p(x∣θ)≥∑zq(z)log⁡p(x,z∣θ)q(z):=F(q,θ)\log p(x \mid \theta) \geq \sum_z q(z) \log \frac{p(x, z \mid \theta)}{q(z)} := \mathcal{F}(q, \theta)

This decomposes as:

log⁡p(x∣θ)=F(q,θ)+KL(q(z)∥p(z∣x,θ))\log p(x \mid \theta) = \mathcal{F}(q, \theta) + \text{KL}(q(z) \parallel p(z \mid x, \theta))

For the full dataset with auxiliary distributions qn(z(n))q_n(z^{(n)}):

log⁡p(D∣θ)=∑n=1NFn(qn,θ)+∑n=1NKL(qn(z(n))∥p(z(n)∣x(n),θ))\log p(\mathcal{D} \mid \theta) = \sum_{n=1}^N \mathcal{F}_n(q_n, \theta) + \sum_{n=1}^N \text{KL}(q_n(z^{(n)}) \parallel p(z^{(n)} \mid x^{(n)}, \theta))

E-Step

Fix θold\theta^{\text{old}}. To maximize the lower bound, minimize the sum of KL divergences. Since KL ≥0\geq 0, the minimum is 0, achieved when:

qn∗(z(n))=p(z(n)∣x(n),θold)q_n^*(z^{(n)}) = p(z^{(n)} \mid x^{(n)}, \theta^{\text{old}})

With this choice, the lower bound becomes tight at θold\theta^{\text{old}}:

log⁡p(D∣θold)=F({qn∗},θold)\log p(\mathcal{D} \mid \theta^{\text{old}}) = \mathcal{F}(\{q_n^*\}, \theta^{\text{old}})

M-Step

Fix qnq_n. Update θ\theta by maximizing the lower bound:

θnew=arg⁡max⁡θ∑n=1N∑z(n)qn(z(n))log⁡p(x(n),z(n)∣θ)\theta^{\text{new}} = \arg\max_\theta \sum_{n=1}^N \sum_{z^{(n)}} q_n(z^{(n)}) \log p(x^{(n)}, z^{(n)} \mid \theta)

Define the Q-function:

Q(θ,θold)=∑n=1NEp(z(n)∣x(n),θold)[log⁡p(x(n),z(n)∣θ)]Q(\theta, \theta^{\text{old}}) = \sum_{n=1}^N \mathbb{E}_{p(z^{(n)} \mid x^{(n)}, \theta^{\text{old}})} \left[ \log p(x^{(n)}, z^{(n)} \mid \theta) \right]

Then θnew=arg⁡max⁡θQ(θ,θold)\theta^{\text{new}} = \arg\max_\theta Q(\theta, \theta^{\text{old}}).

Why EM Increases the Log-Likelihood

Let {qn}\{q_n\} be the distributions from the E-step.

  1. After E-step: log⁡p(D∣θold)=F({qn},θold)\log p(\mathcal{D} \mid \theta^{\text{old}}) = \mathcal{F}(\{q_n\}, \theta^{\text{old}}) (tight)

  2. After M-step: F({qn},θnew)≥F({qn},θold)\mathcal{F}(\{q_n\}, \theta^{\text{new}}) \geq \mathcal{F}(\{q_n\}, \theta^{\text{old}}) (by maximization)

  3. Since log-likelihood ≥\geq lower bound: log⁡p(D∣θnew)≥F({qn},θnew)\log p(\mathcal{D} \mid \theta^{\text{new}}) \geq \mathcal{F}(\{q_n\}, \theta^{\text{new}})

Therefore:

log⁡p(D∣θnew)≥log⁡p(D∣θold)\log p(\mathcal{D} \mid \theta^{\text{new}}) \geq \log p(\mathcal{D} \mid \theta^{\text{old}})

EM guarantees monotonic increase of the observed-data log-likelihood.


EM for GMM

E-step: Compute responsibilities (posterior probabilities):

γk(n):=qn(z(n)=k)=πk N(x(n)∣μk,Σk)∑j=1Kπj N(x(n)∣μj,Σj)\gamma_k^{(n)} := q_n(z^{(n)} = k) = \frac{\pi_k \, \mathcal{N}(x^{(n)} \mid \mu_k, \Sigma_k)}{\sum_{j=1}^K \pi_j \, \mathcal{N}(x^{(n)} \mid \mu_j, \Sigma_j)}

M-step: Update parameters using responsibilities as weights. Define effective sample size:

Nk=∑n=1Nγk(n)N_k = \sum_{n=1}^N \gamma_k^{(n)}

Then:

μknew=1Nk∑n=1Nγk(n)x(n)\mu_k^{\text{new}} = \frac{1}{N_k} \sum_{n=1}^N \gamma_k^{(n)} x^{(n)}
Σknew=1Nk∑n=1Nγk(n)(x(n)−μknew)(x(n)−μknew)⊤\Sigma_k^{\text{new}} = \frac{1}{N_k} \sum_{n=1}^N \gamma_k^{(n)} (x^{(n)} - \mu_k^{\text{new}})(x^{(n)} - \mu_k^{\text{new}})^\top
πknew=NkN\pi_k^{\text{new}} = \frac{N_k}{N}

Interpretation

AspectK-MeansGMM with EM
AssignmentHard (0 or 1)Soft (probabilistic responsibilities)
Cluster shapeSpherical (implicitly)Ellipsoidal (via covariance matrices)
UpdateMean of assigned pointsWeighted mean of all points
ObjectiveMinimize within-cluster varianceMaximize log-likelihood

If all covariances are fixed as Σk=I\Sigma_k = I, EM for GMM reduces to a soft version of K-means.

GMM Advantages and Disadvantages

Advantages:

Disadvantages: