2.5.7Unsupervised Learning

Gaussian Mixture Models and EM algorithm

3,377 words15 min readdifficulty · medium2 backlinks

Overview

Gaussian Mixture Models (GMM) are probabilistic models that assume data comes from a mixture of several Gaussian distributions, each representing a cluster. The Expectation-Maximization (EM) algorithm is an iterative method to find the parameters of these Gaussians when we don't know which data point belongs to which cluster.

Think of it like genres: a movie isn't purely "action" or "drama" — it's 60% action, 40% drama. GMMs capture this blend naturally.

The Mathematical Foundation

p(x)=k=1KπkN(xμk,Σk)p(\mathbf{x}) = \sum_{k=1}^{K} \pi_k \mathcal{N}(\mathbf{x} | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)

where:

  • KK = number of mixture components (clusters)
  • πk\pi_k = mixing coefficient for component kk (satisfies k=1Kπk=1\sum_{k=1}^{K} \pi_k = 1, πk0\pi_k \geq 0)
  • N(xμk,Σk)\mathcal{N}(\mathbf{x} | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k) = Gaussian distribution with mean μk\boldsymbol{\mu}_k and covariance Σk\boldsymbol{\Sigma}_k

WHY this form? We're saying: "Pick cluster kk with probability πk\pi_k, then generate a point from that cluster's Gaussian." The sum marginalizes over all possible cluster choices.

Deriving the Gaussian Component

The multivariate Gaussian distribution is:

N(xμ,Σ)=1(2π)d/2Σ1/2exp(12(xμ)TΣ1(xμ))\mathcal{N}(\mathbf{x} | \boldsymbol{\mu}, \boldsymbol{\Sigma}) = \frac{1}{(2\pi)^{d/2} |\boldsymbol{\Sigma}|^{1/2}} \exp\left(-\frac{1}{2}(\mathbf{x} - \boldsymbol{\mu})^T \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu})\right)

WHY this formula?

  • The exponent 12(xμ)TΣ1(xμ)-\frac{1}{2}(\mathbf{x} - \boldsymbol{\mu})^T \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}) is the Mahalanobis distance squared — it measures how far x\mathbf{x} is from μ\boldsymbol{\mu} accounting for correlations (captured by Σ\boldsymbol{\Sigma})
  • The normalizing constant ensures p(x)dx=1\int p(\mathbf{x}) d\mathbf{x} = 1
  • (2π)d/2(2\pi)^{d/2} comes from integrating the Gaussian over dd dimensions
  • Σ1/2|\boldsymbol{\Sigma}|^{1/2} accounts for the "volume" of the covariance ellipsoid

The EM Algorithm: From First Principles

THE PROBLEM: We have data X={x1,,xN}\mathbf{X} = \{\mathbf{x}_1, \ldots, \mathbf{x}_N\} but we don't know which cluster generated each point. We need to estimate θ={πk,μk,Σk}k=1K\boldsymbol{\theta} = \{\pi_k, \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k\}_{k=1}^{K}.

WHY is this hard? If we knew cluster assignments, estimating parameters would be easy (just compute means and covariances). If we knew parameters, computing assignments would be easy (just use Bayes' rule). We know neither! This is a chicken-and-egg problem.

Introducing Latent Variables

Let zi{1,,K}z_i \in \{1, \ldots, K\} be the hidden cluster label for xi\mathbf{x}_i. The complete-data log-likelihood would be:

logp(X,Zθ)=i=1Nk=1K1(zi=k)[logπk+logN(xiμk,Σk)]\log p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\theta}) = \sum_{i=1}^{N} \sum_{k=1}^{K} \mathbb{1}(z_i = k) \left[\log \pi_k + \log \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\right]

WHY this form? The indicator 1(zi=k)\mathbb{1}(z_i = k) picks out which cluster ii belongs to, and we log the probability of that choice (πk\pi_k) times the probability of xi\mathbf{x}_i under that Gaussian.

But we don't observe Z\mathbf{Z}! So we maximize the incomplete-data log-likelihood:

logp(Xθ)=i=1Nlog(k=1KπkN(xiμk,Σk))\log p(\mathbf{X} | \boldsymbol{\theta}) = \sum_{i=1}^{N} \log \left(\sum_{k=1}^{K} \pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\right)

This is hard to maximize directly because the log of a sum has no closed form.

By Jensen's inequality, for any distribution q(Z)q(\mathbf{Z}):

logp(Xθ)=logZp(X,Zθ)\log p(\mathbf{X} | \boldsymbol{\theta}) = \log \sum_{\mathbf{Z}} p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\theta}) =logZq(Z)p(X,Zθ)q(Z)= \log \sum_{\mathbf{Z}} q(\mathbf{Z}) \frac{p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\theta})}{q(\mathbf{Z})} Zq(Z)logp(X,Zθ)q(Z)=L(q,θ)\geq \sum_{\mathbf{Z}} q(\mathbf{Z}) \log \frac{p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\theta})}{q(\mathbf{Z})} = \mathcal{L}(q, \boldsymbol{\theta})

WHY does this help? The lower bound L(q,θ)\mathcal{L}(q, \boldsymbol{\theta}) is easier to maximize. We alternate:

  1. E-step: Fix θ\boldsymbol{\theta}, choose qq to make the bound tight: q(Z)=p(ZX,θold)q(\mathbf{Z}) = p(\mathbf{Z} | \mathbf{X}, \boldsymbol{\theta}^{\text{old}})
  2. M-step: Fix qq, maximize L\mathcal{L} with respect to θ\boldsymbol{\theta} to get θnew\boldsymbol{\theta}^{\text{new}}

E-Step: Computing Responsibilities

We need p(zi=kxi,θ)p(z_i = k | \mathbf{x}_i, \boldsymbol{\theta}), called the responsibility γik\gamma_{ik}.

By Bayes' rule:

γik=p(zi=kxi,θ)=p(zi=k)p(xizi=k,θ)j=1Kp(zi=j)p(xizi=j,θ)\gamma_{ik} = p(z_i = k | \mathbf{x}_i, \boldsymbol{\theta}) = \frac{p(z_i = k) p(\mathbf{x}_i | z_i = k, \boldsymbol{\theta})}{\sum_{j=1}^{K} p(z_i = j) p(\mathbf{x}_i | z_i = j, \boldsymbol{\theta})}

WHY this step? Numerator: prior probability of cluster kk times likelihood of xi\mathbf{x}_i under cluster kk. Denominator: total probability over all clusters (normalization).

γik=πkN(xiμk,Σk)j=1KπjN(xiμj,Σj)\gamma_{ik} = \frac{\pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)}{\sum_{j=1}^{K} \pi_j \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_j, \boldsymbol{\Sigma}_j)}

WHAT does γik\gamma_{ik} mean? The probability that point ii was generated by cluster kk. If γi3=0.8\gamma_{i3} = 0.8, point ii is 80% likely from cluster 3.

M-Step: Updating Parameters

We maximize the expected complete-data log-likelihood:

Q(θ,θold)=EZX,θold[logp(X,Zθ)]Q(\boldsymbol{\theta}, \boldsymbol{\theta}^{\text{old}}) = \mathbb{E}_{\mathbf{Z} | \mathbf{X}, \boldsymbol{\theta}^{\text{old}}}[\log p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\theta})]

Substituting responsibilities:

Q=i=1Nk=1Kγik[logπk+logN(xiμk,Σk)]Q = \sum_{i=1}^{N} \sum_{k=1}^{K} \gamma_{ik} \left[\log \pi_k + \log \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\right]

Deriving μk\boldsymbol{\mu}_k update:

Qμk=i=1Nγikμk[12(xiμk)TΣk1(xiμk)]\frac{\partial Q}{\partial \boldsymbol{\mu}_k} = \sum_{i=1}^{N} \gamma_{ik} \frac{\partial}{\partial \boldsymbol{\mu}_k} \left[-\frac{1}{2}(\mathbf{x}_i - \boldsymbol{\mu}_k)^T \boldsymbol{\Sigma}_k^{-1} (\mathbf{x}_i - \boldsymbol{\mu}_k)\right]

WHY this derivative? Only the quadratic term in the Gaussian depends on μk\boldsymbol{\mu}_k.

=i=1NγikΣk1(xiμk)= \sum_{i=1}^{N} \gamma_{ik} \boldsymbol{\Sigma}_k^{-1} (\mathbf{x}_i - \boldsymbol{\mu}_k)

Setting to zero:

i=1NγikΣk1xi=i=1NγikΣk1μk\sum_{i=1}^{N} \gamma_{ik} \boldsymbol{\Sigma}_k^{-1} \mathbf{x}_i = \sum_{i=1}^{N} \gamma_{ik} \boldsymbol{\Sigma}_k^{-1} \boldsymbol{\mu}_k

μk=i=1Nγikxii=1Nγik=1Nki=1Nγikxi\boldsymbol{\mu}_k = \frac{\sum_{i=1}^{N} \gamma_{ik} \mathbf{x}_i}{\sum_{i=1}^{N} \gamma_{ik}} = \frac{1}{N_k} \sum_{i=1}^{N} \gamma_{ik} \mathbf{x}_i

where Nk=i=1NγikN_k = \sum_{i=1}^{N} \gamma_{ik} is the effective number of points in cluster kk.

WHAT does this mean? The new mean is the weighted average of all points, weighted by their responsibility to cluster kk. Points strongly in cluster kk (γik1\gamma_{ik} \approx 1) pull the mean toward them; points weakly in kk have little effect.

Deriving Σk\boldsymbol{\Sigma}_k update:

Similarly (taking derivative of log-determinant and quadratic terms):

Σk=1Nki=1Nγik(xiμk)(xiμk)T\boldsymbol{\Sigma}_k = \frac{1}{N_k} \sum_{i=1}^{N} \gamma_{ik} (\mathbf{x}_i - \boldsymbol{\mu}_k)(\mathbf{x}_i - \boldsymbol{\mu}_k)^T

WHY? Weighted average of squared deviations from the mean — the definition of covariance, responsibility-weighted.

Deriving πk\pi_k update:

We maximize k=1KNklogπk\sum_{k=1}^{K} N_k \log \pi_k subject to k=1Kπk=1\sum_{k=1}^{K} \pi_k = 1 using a Lagrange multiplier λ\lambda:

L=k=1KNklogπk+λ(1k=1Kπk)\mathcal{L} = \sum_{k=1}^{K} N_k \log \pi_k + \lambda\left(1 - \sum_{k=1}^{K} \pi_k\right)

Lπk=Nkπkλ=0    πk=Nkλ\frac{\partial \mathcal{L}}{\partial \pi_k} = \frac{N_k}{\pi_k} - \lambda = 0 \implies \pi_k = \frac{N_k}{\lambda}

Suming over kk: k=1Kπk=k=1KNkλ=Nλ=1    λ=N\sum_{k=1}^{K} \pi_k = \frac{\sum_{k=1}^{K} N_k}{\lambda} = \frac{N}{\lambda} = 1 \implies \lambda = N

πk=NkN=1Ni=1Nγik\pi_k = \frac{N_k}{N} = \frac{1}{N}\sum_{i=1}^{N} \gamma_{ik}

WHAT does this mean? The mixing coefficient is the fraction of all responsibility assigned to cluster kk.

Initialize: Choose KK, initialize πk\pi_k, μk\boldsymbol{\mu}_k, Σk\boldsymbol{\Sigma}_k randomly or via K-means

Repeat until convergence:

E-step: Compute responsibilities for all i,ki, k: γik=πkN(xiμk,Σk)j=1KπjN(xiμj,Σj)\gamma_{ik} = \frac{\pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)}{\sum_{j=1}^{K} \pi_j \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_j, \boldsymbol{\Sigma}_j)}

M-step: Update parameters for all kk: Nk=i=1NγikN_k = \sum_{i=1}^{N} \gamma_{ik} μk=1Nki=1Nγikxi\boldsymbol{\mu}_k = \frac{1}{N_k} \sum_{i=1}^{N} \gamma_{ik} \mathbf{x}_i Σk=1Nki=1Nγik(xiμk)(xiμk)T\boldsymbol{\Sigma}_k = \frac{1}{N_k} \sum_{i=1}^{N} \gamma_{ik} (\mathbf{x}_i - \boldsymbol{\mu}_k)(\mathbf{x}_i - \boldsymbol{\mu}_k)^T πk=NkN\pi_k = \frac{N_k}{N}

Convergence: Stop when log-likelihood change < threshold or max iterations reached.

Worked Examples

Data: X={1,2,9,10}\mathbf{X} = \{1, 2, 9, 10\}, K=2K=2

Initialize:

  • π1=0.5,π2=0.5\pi_1 = 0.5, \pi_2 = 0.5
  • μ1=1.5,μ2=9.5\mu_1 = 1.5, \mu_2 = 9.5 (from data endpoints)
  • σ12=1,σ22=1\sigma_1^2 = 1, \sigma_2^2 = 1 (arbitrary small variance)

E-step iteration 1:

For x1=1x_1 = 1: N(11.5,1)=12πe0.1250.352\mathcal{N}(1 | 1.5, 1) = \frac{1}{\sqrt{2\pi}} e^{-0.125} \approx 0.352 N(19.5,1)=12πe36.1250\mathcal{N}(1 | 9.5, 1) = \frac{1}{\sqrt{2\pi}} e^{-36.125} \approx 0

WHY these values? Plug into the Gaussian formula. Point 1 is very close to μ1=1.5\mu_1 = 1.5 (distance 0.5) but far from μ2=9.5\mu_2 = 9.5 (distance 8.5).

γ11=0.5×0.3520.5×0.352+0.5×01.0\gamma_{11} = \frac{0.5 \times 0.352}{0.5 \times 0.352 + 0.5 \times 0} \approx 1.0

WHAT does this mean? Point 1 is essentially100% assigned to cluster 1.

Similarly:

  • γ211.0\gamma_{21} \approx 1.0 (point 2 → cluster 1)
  • γ310.0\gamma_{31} \approx 0.0 (point 9 → cluster 2)
  • γ410.0\gamma_{41} \approx 0.0 (point 10 → cluster 2)

M-step iteration 1:

N1=γ11+γ21+γ31+γ412.0N_1 = \gamma_{11} + \gamma_{21} + \gamma_{31} + \gamma_{41} \approx 2.0 N22.0N_2 \approx 2.0

WHY? Two points strongly in cluster 1, two in cluster 2.

μ1=1×1+1×2+0×9+0×102=32=1.5\mu_1 = \frac{1 \times 1 + 1 \times 2 + 0 \times 9 + 0 \times 10}{2} = \frac{3}{2} = 1.5

WHY this step? Weighted average of all points, but only points 1 and 2 have non-zero weight for cluster 1.

μ2=0×1+0×2+1×9+1×102=9.5\mu_2 = \frac{0 \times 1 + 0 \times 2 + 1 \times 9 + 1 \times 10}{2} = 9.5

σ12=1×(11.5)2+1×(21.5)22=0.25+0.252=0\sigma_1^2 = \frac{1 \times (1-1.5)^2 + 1 \times (2-1.5)^2}{2} = \frac{0.25 + 0.25}{2} = 0

WHY? Variance is the weighted average of squared deviations. Both points are0.5 away from mean 1.5.

σ22=1×(99.5)2+1×(109.5)22=0.25\sigma_2^2 = \frac{1 \times (9-9.5)^2 + 1 \times (10-9.5)^2}{2} = 0.25

π1=π2=24=0.5\pi_1 = \pi_2 = \frac{2}{4} = 0.5

Result: After one iteration, the clusters have tightened (σ2\sigma^2 decreased from 1 to 0.25), means are stable. The algorithm would converge quickly since the data is well-separated.

Data:

  • Cluster 1: (0,0),(1,1),(0.5,0.5)(0,0), (1,1), (0.5, 0.5)
  • Cluster 2: (4,4),(5,5),(4.5,4.5)(4,4), (5,5), (4.5, 4.5)
  • Ambiguous point: (2.5,2.5)(2.5, 2.5)

After convergence:

  • μ1(0.5,0.5)\boldsymbol{\mu}_1 \approx (0.5, 0.5)
  • μ2(4.5,4.5)\boldsymbol{\mu}_2 \approx (4.5, 4.5)
  • For the ambiguous point (2.5,2.5)(2.5, 2.5): γ710.5,γ720.5\gamma_{71} \approx 0.5, \gamma_{72} \approx 0.5

WHY 50/50? The point is equidistant from both cluster centers, so it has equal responsibility to both. This is the key advantage of GMM: soft assignments capture uncertainty.

If we forced hard assignment (like K-means), we'd arbitrarily put it in one cluster, losing information. GMM says "this point is genuinely ambiguous — it's half from each cluster."

For convergence checking, we compute:

logp(Xθ)=i=1Nlog(k=1KπkN(xiμk,Σk))\log p(\mathbf{X} | \boldsymbol{\theta}) = \sum_{i=1}^{N} \log \left(\sum_{k=1}^{K} \pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\right)

For point x1=(1,1)\mathbf{x}_1 = (1, 1) with K=2K=2:

p(x1)=π1N(x1μ1,Σ1)+π2N(x1μ2,Σ2)p(\mathbf{x}_1) = \pi_1 \mathcal{N}(\mathbf{x}_1 | \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1) + \pi_2 \mathcal{N}(\mathbf{x}_1 | \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2)

If π1=0.6\pi_1=0.6, π2=0.4\pi_2=0.4, and the Gaussians evaluate to 0.20.2 and $0.001:

p(x1)=0.6×0.2+0.4×0.001=0.12+0.004=0.1204p(\mathbf{x}_1) = 0.6 \times 0.2 + 0.4 \times 0.001 = 0.12 + 0.004 = 0.1204

WHY sum? Total probability is the mixture of both components.

logp(x1)=log(0.1204)2.116\log p(\mathbf{x}_1) = \log(0.1204) \approx -2.116

Sum over all points to get total log-likelihood. If it changes by less than 10610^{-6} between iterations, we've converged.

Common Mistakes and Fixes

Wrong: Compute γik=πkN(xiμk,Σk)\gamma_{ik} = \pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k) and use directly.

Why it feels right: We computed the probability of xi\mathbf{x}_i under cluster kk, that should be the responsibility!

Why it's wrong: Without the denominator, k=1Kγik1\sum_{k=1}^{K} \gamma_{ik} \neq 1. Responsibilities are conditional probabilities and must sum to 1 for each point.

Fix: Always include the normalizing sum: γik=πkN(xiμk,Σk)j=1KπjN(xiμj,Σj)\gamma_{ik} = \frac{\pi_k \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)}{\sum_{j=1}^{K} \pi_j \mathcal{N}(\mathbf{x}_i | \boldsymbol{\mu}_j, \boldsymbol{\Sigma}_j)}

Wrong: A cluster colapses to a single point, making Σk=0|\boldsymbol{\Sigma}_k| = 0, which breaks the Gaussian evaluation (divide by zero).

Why it happens: If all points in a cluster have identical coordinates (or nearly identical due to numerical issues), the covariance becomes singular.

Why it's dangerous: The algorithm crashes or produces NaN values.

Fix: Add regularization by adding a small diagonal term: Σk=1Nki=1Nγik(xiμk)(xiμk)T+ϵI\boldsymbol{\Sigma}_k = \frac{1}{N_k} \sum_{i=1}^{N} \gamma_{ik} (\mathbf{x}_i - \boldsymbol{\mu}_k)(\mathbf{x}_i - \boldsymbol{\mu}_k)^T + \epsilon\mathbf{I} where ϵ=106\epsilon = 10^{-6} is a small constant. This ensures Σk\boldsymbol{\Sigma}_k is always positive definite.

Steel-man: "I don't want to add artificial variance — it's not in the data!" True, but with zero variance, the model is saying "this cluster generates only one exact point with infinite precision," which is physically unrealistic. The regularization represents measurement noise or model uncertainty.

Wrong: Initialize all μk\boldsymbol{\mu}_k randomly from a uniform distribution without looking at the data.

Why it feels right: Random initialization works for gradient descent on neural networks, so it should work here too!

Why it's wrong: EM is sensitive to initialization. If two means start very close together, they may stay close (both trying to model the same cluster) while another cluster is missed entirely.

Fix: Use K-means++ for initialization:

  1. Run K-means to convergence (fast, simple)
  2. Use K-means cluster centers as μk\boldsymbol{\mu}_k
  3. Use K-means cluster covariances as Σk\boldsymbol{\Sigma}_k
  4. Use K-means cluster sizes to initialize πk\pi_k

Why this works: K-means gives a good rough clustering, and EM refines it with soft assignments.

Wrong: Run EM once, get result, assume it's the best possible clustering.

Why it feels right: EM is guaranteed to increase the likelihood each iteration, so it must converge to the global maximum!

Why it's wrong: EM is guaranteed to converge to a local maximum, not necessarily the global one. Different initializations lead to different local maxima.

Fix: Run EM multiple times (5-10) with different random initializations, pick the result with highest final log-likelihood.

Steel-man: "But that's computationally expensive!" Yes, but it's necessary. The log-likelihood landscape is non-convex with many local maxima. Think of it like training neural networks: you try different random seeds and pick the best.

Choosing the Number of Clusters K

Unlike K-means, GMM is probabilistic, so we can use model selection criteria:

Akaike Information Criterion (AIC): AIC=2k2ln(L^)\text{AIC} = 2k - 2\ln(\hat{L})

where kk is the number of parameters, L^\hat{L} is the maximum likelihood.

Bayesian Information Criterion (BIC): BIC=kln(N)2ln(L^)\text{BIC} = k \ln(N) - 2\ln(\hat{L})

WHY BIC over AIC? BIC penalizes model complexity more heavily (factor of ln(N)\ln(N) vs. 2). For GM, BIC often performs better at avoiding overfitting.

HOW to use: Fit GMMs with K=1,2,3,K = 1, 2, 3, \ldots and plot BIC. Choose KK with the lowest BIC.

Parameter count for GMM:

  • K1K-1 mixing coefficients (constraint: sum to 1)
  • K×dK \times d means
  • K×d(d+1)/2K \times d(d+1)/2 covariance parameters (symmetric matrices)

Total: k=(K1)+Kd+Kd(d+1)2k = (K-1) + Kd + K \frac{d(d+1)}{2}

Connections

  • K-Means Clustering: GM is a soft version of K-means; K-means is GM with spherical covariances and hard assignments
  • Maximum Likelihood Estimation: EM maximizes the likelihood of observed data under the mixture model
  • Bayes Theorem: Responsibilities are computed via Bayes' rule
  • Expectation Maximization Algorithm: GM is the most common application of the general EM framework
  • Principal Component Analysis: GMM covariances capture data spread like PCA, but per-cluster
  • Hidden Markov Models: HMMs useEM with similar E-step (forward-backward) and M-step updates
  • Variational Inference: Modern Bayesian GMs use variational EM (more robust, automatic KK selection)
  • Anomaly Detection: Points with low probability under all mixture components are outliers
  • Density Estimation: GMs model the full probability density, useful for generative modeling

Or think: Evaluate, then Modify. You evaluate where points belong, then modify the cluster parameters accordingly.

Recall Explain to a 12-year-old

Imagine you have a bag of red and blue marbles mixed together, but someone painted them all gray so you can't tell which is which. You know there are about half red and half blue, and you want to figure out which gray marble was originally which color.

You start by guessing: "I think the smaller marbles were red and the bigger ones were blue."

Then you look at each marble and say: "This is medium-sized... it's probably 60% likely to be red and 40% likely to be blue." You're not sure, so you hedge your bets.

Now you update your guess: "Okay, if I treat this marble as 60% red, then the average size of red marbles is probably this much..." You recalculate the average size of reds and blues based on these partial assignments.

Then you go back and look at each marble again with your new averages: "Hmm, with this new average, maybe this marble is actually 70% red, not 60%."

You keep going back and forth — updating your guess about which marbles are which color, then updating your guess about what the average red and blue sizes are — until your guesses stop changing. That's the EM algorithm! You're alternating between estimating assignments (E-step) and updating parameters (M-step).

GMs do this with data points and clusters instead of marbles and colors.


Flashcards

#flashcards/ai-ml

What does GMM stand for and what is its key advantage over K-means? :: Gaussian Mixture Model. Key advantage: soft assignments — each point has a probability of belonging to each cluster, capturing uncertainty, rather than hard assignment to one cluster.

What are the three parameters of a GMM component?
Mixing coefficient π_k (weight), mean μ_k, and covariance matrix Σ_k.
What is the formula for the responsibility γ_ik?
γ_ik = [π_k · N(x_i | μ_k, Σ_k)] / [Σ_j π_j · N(x_i | μ_j, Σ_j)] — the probability that point i belongs to cluster k.
What does the E-step compute inEM for GMM?
The responsibilities γ_ik — the posterior probability that each point belongs to each cluster, given current parameters.
What does the M-step update in EM for GMM?
Updates all parameters: means μ_k (weighted average of points), covariances Σ_k (weighted covariance), and mixing coefficients π_k (fraction of total responsibility).
What is N_k in the EM M-step?
N_k = Σ_i γ_ik, the effective number of points assigned to cluster k (sum of all responsibilities for that cluster).
Why does EM converge to a local maximum, not global?
The log-likelihood landscape is non-convex — there are multiple local maxima. EM is guaranteed to increase likelihood each iteration, but can get stuck in local optima depending on initialization.
What is the update formula for the GM mean μ_k?
μ_k = (1/N_k) Σ_i γ_ik · x_i — the responsibility-weighted average of all data points.
What is the update formula for GM mixing coefficient π_k?
π_k = N_k / N = (1/N) Σ_i γ_ik — the fraction of total responsibility assigned to cluster k.
How do you prevent singular covariance matrices in GMM?
Add regularization: Σ_k = [...computed covariance...] + ε·I where ε ≈ 10^-6. This ensures positive definiteness.

What

Concept Map

assumes data from

each represents

weights via

constraint

vs K-means

gives

distance metric

params estimated by

solves

introduces

iterates

iterates

uses

estimates

Gaussian Mixture Model

Mixture of Gaussians

Cluster

Mixing Coefficient pi_k

Sum pi_k equals 1

Soft Assignment

Probability per cluster

Mahalanobis Distance

EM Algorithm

Chicken-and-Egg Problem

Latent Variables z_i

E-Step: compute assignments

M-Step: update params

Bayes Rule

Means Covariances Weights

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo, is concept ko simple tarike se samajhte hain. Socho tumhare paas kaafi saara data hai, jaise students ke marks ya movies ki ratings, aur tumhe pata hai ki ye alag-alag groups ya clusters mein aata hai. K-means jaisa method har point ko sirf ek hi cluster mein daal deta hai — "hard assignment". Lekin real life mein cheezein itni clear-cut nahi hoti. Ek movie 60% action aur 40% drama ho sakti hai, na? Yahi pe Gaussian Mixture Models (GMM) kaam aate hain. Ye maan lete hain ki data kai saare Gaussian distributions (bell curves) ka mixture hai, aur har point ko probability deta hai ki wo kis cluster ka hai — yani "soft assignment". Isse real-world ki uncertainty naturally capture ho jaati hai.

Ab problem ye hai ki humein data toh dikhta hai, par ye nahi pata ki kaunsa point kis Gaussian se aaya. Ye ek chicken-and-egg problem hai — agar cluster assignments pata hote toh parameters (mean, covariance) nikalna easy hota, aur agar parameters pata hote toh assignments nikalna easy hota. Par dono mein se kuch bhi pata nahi! Isko solve karne ke liye EM (Expectation-Maximization) algorithm use hota hai. Ye iterative tarike se kaam karta hai: pehle E-step mein current parameters use karke guess karta hai ki har point kis cluster ka kitna hai (probabilities), phir M-step mein un guesses ke basis pe parameters update karta hai. Ye process repeat hoti rehti hai jab tak cheezein stable na ho jaayein.

Ye concept matter isliye karta hai kyunki real-world data aksar simple, clear boundaries wala nahi hota — overlap hota hai, uncertainty hoti hai. GMM tumhe flexibility deta hai clusters ke shapes aur sizes ke saath (covariance matrix ki wajah se ellipse-shaped clusters bhi ban sakte hain, sirf round nahi). Aur EM algorithm ek powerful general technique hai jo tab kaam aati hai jab tumhare paas hidden ya missing information ho — isliye ye sirf clustering mein nahi, balki bahut saare probabilistic models mein use hoti hai. Toh yaad rakhna: GMM = soft, probabilistic clustering, aur EM = wo smart trick jo hidden variables ke saath parameters seekhne mein madad karti hai.

Test yourself — Unsupervised Learning

Connections