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
2.3 The D-Dimensional Case
In
Normalization constant:
Shift again with
Hence:
Setting
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
- 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
First pick a cluster (a discrete choice):
or, in compact (one-hot) form:
Explanation: only one $ k^
z_{nk^}=1 $, so: 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
which brings us back to the mixture form.
3.5 The Posterior (Responsibility)
By Bayes’ rule:
Substituting the GMM parameters:
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,
Introduce the latent variables
The idea of EM: use the “soft label” $\mathbb{E}[z{nk}]
4.2 Definition of the Q Function
At iteration
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
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
→
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
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
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
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
Therefore, 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:
- All components share the same spherical covariance:
- 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
K-means: minimizes the within-cluster sum of squared distances;
- a geometric model with no probabilistic structure;
- hard (0/1) assignments.
From distance to Gaussian:
- exponential energy model
; - normalization ⇒ Gaussian distribution.
- exponential energy model
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.
- introduce the one-hot latent variable
EM algorithm:
- E-step: compute
- M-step: maximize Q to get closed-form updates for
.
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.
- construct the variational lower bound
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.

