From K-means to GMM

From K-means to GMM

K-means to GMM: From Distance Minimization to Probabilistic Models and the EM Derivation

Goal: starting from the geometric distance in K-means, derive step by step the Gaussian Mixture Model (GMM) and its EM algorithm, stringing everything together with detailed math:
distance → exponential-form likelihood → Gaussian distribution → mixture model → latent variables → joint distribution and marginalization → E-step posterior → M-step closed-form solution → EM monotonicity → K-means as a limiting case.


1. K-means: Distance-Based Hard Clustering

1.1 Model Setup

Dataset:

Cluster centers (“prototypes”):

Hard assignment variables (the implicit cluster assignment):

1.2 The K-means Objective

K-means minimizes the within-cluster sum of squared distances:

The algorithm alternates between two steps:

  • Assignment step (analogous to the E-step): given the centers , assign each sample to its nearest cluster:

  • Update step (analogous to the M-step): given the assignments , update each center to the mean of the samples in its cluster:

At this point K-means is purely a geometric optimization problem, with no explicit probabilistic structure yet.


2. From “Distance” to “Probability”: Exponential Form and the Gaussian Distribution

We want to formalize “closer distance → higher probability” as a probabilistic model.

2.1 The Energy View: Distance as “Energy”

Intuitive assumption:

The closer a point is to the center , the more likely it “belongs to this cluster”.

Construct an unnormalized likelihood/density:

  • Non-negative;
  • Large distance ⇒ exponential decay;
  • But it does not yet “integrate to 1”.

2.2 Normalization: The 1-D Case

Start with one dimension:

Normalization constant:

Shift the variable, :

Use the Gaussian integral (a classic result):

Therefore:

Setting gives the standard 1-D Gaussian:

2.3 The D-Dimensional Case

In -dimensional space:

Normalization constant:

Shift again with :

Hence:

Setting , we get:

i.e. the isotropic multivariate Gaussian with covariance .

Conclusion:
“Exponential of the squared distance + normalization” ⇒ Gaussian distribution.


3. From a Single Gaussian to the Gaussian Mixture Model (GMM)

Real data is often multimodal ⇒ superimpose several Gaussians.

3.1 Definition of the GMM

Suppose there are Gaussian components, each with parameters:

  • Weight: , with
  • Mean:
  • Covariance matrix: , symmetric positive definite

Then the marginal distribution of the GMM is:

where

and

3.2 The Generative Process (Introducing Latent Variables)

To describe the process of “which Gaussian component a sample comes from”, introduce a latent variable :

  1. First pick a cluster (a discrete choice):

    or, in compact (one-hot) form:

    Explanation: only one $ k^ z_{nk^}=1 $, so:

  2. Once the component is chosen, sample from the corresponding Gaussian:

    or, in compact form:

3.3 The Joint Distribution

By the chain rule:

3.4 The Marginal Distribution : Summing over the Latent Variable

Since can only take the K one-hot values , we have:

which brings us back to the mixture form.

3.5 The Posterior (Responsibility)

By Bayes’ rule:

Substituting the GMM parameters:

is the responsibility, which expresses “how much component k explains sample ”, and implements soft clustering.


4. Maximum Likelihood for the GMM and the EM Algorithm

4.1 Log-Likelihood and Latent Variables

The log-likelihood of the observed data:

Problem: there is a sum inside the log, , which makes it hard to differentiate with respect to and maximize directly.

Introduce the latent variables and define the complete-data log-likelihood:

The idea of EM: use the “soft label” $\mathbb{E}[z{nk}] z{nk} $.

4.2 Definition of the Q Function

At iteration , with the current parameters known, define:

Since

we get:

4.3 E-Step: Compute the Responsibilities

Given the current parameters :

4.4 M-Step: Maximize Q to Obtain Closed-Form Updates

Let

4.4.1 Updating the Mixing Coefficients

Take the part that depends on :

Constraint:

Form the Lagrangian:

Differentiate with respect to :

Apply the constraint:

Also, since for every n, , we have:

Hence:

4.4.2 Updating the Means

Take only the part of Q that depends on :

Expand the Gaussian log:

where does not depend on .

Therefore

Differentiate with respect to :

So

Set the derivative to 0:

Expanding:

so:

4.4.3 Updating the Covariances

Again start from:

and differentiate with respect to .

Using the matrix-derivative formulas (for symmetric matrices):

we obtain:

Set the derivative to 0:

Multiply by on the left and on the right:

Therefore:


5. EM Monotonicity: Why Does Each Step Never Decrease the Log-Likelihood?

5.1 Constructing the Variational Lower Bound (ELBO)

For any distribution :

By Jensen’s inequality:

Define:

where is the entropy.

5.2 KL Decomposition: The Relation between the Log-Likelihood and the Lower Bound

One can show:

where the KL divergence is always non-negative.

Derivation:

Rearranging gives the result.

5.3 E-Step: Fix , Find the Optimal

With fixed, is a constant.

We want to maximize ,
which is equivalent to minimizing the KL:

The minimum is 0, attained if and only if:

At that point:

That is: the E-step “pulls” the lower bound up to the current log-likelihood, making it tangent to it.

5.4 M-Step: Fix , Update

In the M-step, by definition:

so:

On the other hand, for the new parameters the decomposition still holds:

Combining the two:

Therefore:

i.e. no EM iteration ever decreases the log-likelihood.

5.5 The Relation between the Q Function and

After the E-step chooses :

where does not depend on .

Therefore, maximizing in the M-step is equivalent to maximizing:

which is exactly the Q function used in the M-step derivation above.


6. K-means as a Limiting Case of GMM-EM

Simplify the GMM with the following constraints:

  1. All components share the same spherical covariance:
  2. Uniform weights:

Then the responsibilities:

are a “softmax over squared distances”.

As :

  • The exponential term of the nearest cluster dominates all the others by far;
  • The softmax degenerates into the one-hot argmin assignment.

That is:

At this point:

  • E-step ⇒ hard assignment to the nearest center (the K-means assignment step);
  • M-step:i.e. the K-means update.

Therefore:

K-means = the Hard-EM special case of GMM-EM in the limit of “covariance → 0, uniform weights”.


7. Summary

  1. K-means: minimizes the within-cluster sum of squared distances;

    • a geometric model with no probabilistic structure;
    • hard (0/1) assignments.
  2. From distance to Gaussian:

    • exponential energy model ;
    • normalization ⇒ Gaussian distribution.
  3. GMM: a weighted sum of several Gaussians;

    • introduce the one-hot latent variable to build the joint ;
    • marginalize ⇒ the mixture form ;
    • the posterior ⇒ soft responsibilities.
  4. EM algorithm:

    • E-step: compute
    • M-step: maximize Q to get closed-form updates for .
  5. EM monotonicity:

    • construct the variational lower bound and use
    • the E-step makes KL=0, so the lower bound equals the current log-likelihood;
    • the M-step raises the lower bound ⇒ the log-likelihood does not decrease.
  6. The relation between K-means and GMM:

    • GMM is the probabilistic generalization of K-means;
    • K-means is the Hard-EM limit of GMM-EM with shared spherical covariance and .

Translated from the Chinese original.

中文