Course home Clustering · Part 2Open slides

Lecture 17 of 20

Clustering · Part 2

Gaussian Mixture Clustering

Build a Gaussian mixture, calculate soft memberships, and learn the means, covariances, and priors.

7.3 · Gaussian Mixture Clustering

K-means makes a hard assignment

Each point goes to exactly one cluster, the one with the nearest centroid.

Halfway between the centroidsShorter distanceLonger distanceCentroid 1Selected pointCentroid 2

This point is closer to centroid 1, so its membership is 1 for cluster 1 and 0 for cluster 2. Even near a boundary, K-means makes a single choice.

7.3 · Gaussian Mixture Clustering

K-means divides the space into Voronoi cells

For a fixed set of centroids, K-means divides the feature space into colored regions called Voronoi cells. Each cell contains the locations closest to its centroid.

Feature 1Feature 20246810246123

Drag the white diamond across a boundary to see its cluster change. Move a centroid to redraw the cells.

Distance from the point

Centroid 1
Centroid 2
Centroid 3

A shared boundary is where two or more centroids tie for the shortest distance. K-means still makes a hard assignment there. This demo breaks a tie by choosing the lowest cluster number.

7.3 · Gaussian Mixture Clustering

A soft assignment gives each cluster a probability

As in classification, we can estimate a probability of belonging to each group. In clustering, the groups are clusters.

ℙ(Y=k∣X=x)=pY∣X(k∣x)

Y is the unknown cluster index and k is one possible cluster. X is the random feature vector and x is the feature vector we observed. For two clusters, probabilities of 0.60 and 0.40 show how uncertain the assignment is.

7.3 · Gaussian Mixture Clustering

Use Bayes’ theorem to find those probabilities

As in classification, multiply the likelihood by the prior and divide by the evidence to get the posterior.

In symbolspY∣X(y∣x)=pX∣Y(x∣y)pY(y)pX(x)
In wordsPosterior=Likelihood×PriorEvidence

Posterior

pY∣X(y∣x)

Chance that this point came from cluster y after we see its features x.

Likelihood

pX∣Y(x∣y)

How densely points from cluster y fall near x.

Prior

pY(y)

Chance that a random point came from cluster y before we see its features.

Evidence

pX(x)

Density of the whole mixture at x. Add each cluster’s density weighted by its prior.

x is the observed feature vector and y is a possible cluster label. We learn the cluster distributions and priors from unlabeled data.

7.3 · Gaussian Mixture Clustering

Our goal is the posterior

For each observed point x, we want the probability that it came from cluster k. Over the next few slides, we’ll see how Bayes’ rule gives this formula for a Gaussian mixture.

Bayes’ rulepY∣X(k∣x)=pX∣Y(x∣k)pY(k)pX(x)
pY∣X(k∣x)=πk𝒩(x;μk,Σk)∑y=1Kπy𝒩(x;μy,Σy)

We’ll find the Gaussian density for each cluster, weight it by its prior, and add those weighted densities to get the evidence in the denominator.

7.3 · Gaussian Mixture Clustering

Model each cluster with a Gaussian

Two of the three quantities we will optimize during training are the mean μk, which locates the cluster’s center, and the variance-covariance matrix Σk, which describes its spread and tilt.

Bayes’ rulepY∣X(k∣x)=pX∣Y(x∣k)pY(k)pX(x)
X∣Y=k∼𝒩(μk,Σk)pX∣Y(x∣k)=𝒩(x;μk,Σk)
pX∣Y(x∣k) = likelihood
Density at the selected point
Density0.00.20.40.6-4-2024Feature value x

Mean fixed at 0. The blue marker shows the curve’s height at x.

The first line says that points from cluster k follow a Gaussian. The blue term 𝒩(x;μk,Σk) is the probability density at x given cluster k. With one feature, it’s the height of that cluster’s Gaussian curve at x. The density is highest at the cluster center. Bayes’ theorem uses it with the prior to compute the membership probability.

7.3 · Gaussian Mixture Clustering

What do variance and covariance tell us?

Compare two curves with the same mean, then three point clouds drawn on the same scales.

Lower varianceHigher varianceSame meanFeature value

Variance describes one feature

Higher variance means values tend to lie farther from their mean.

X₂X₁PositiveX₂X₁Near zeroX₂X₁Negative

Covariance describes a pair of features

Positive means high values tend to go together. Negative means that a higher value of one feature tends to come with a lower value of the other.

Each dot on the right has a value for X1 and for X2. Near-zero covariance means the cloud has little upward or downward tilt.

7.3 · Gaussian Mixture Clustering

Variance-Covariance matrix

The diagonal stores the variance of each feature. The other entries store the covariance of each pair of features within cluster k.

(Σk)jj′={Var(Xj∣Y=k)if j=j′Cov(Xj,Xj′∣Y=k)otherwise

With m features, μk∈ℝm and Σk∈ℝm×m. The indices j and j′ identify features. Covariance matrices are positive semidefinite. A Gaussian density in the full feature space needs a positive definite covariance matrix.

7.3 · Gaussian Mixture Clustering

The prior is the third quantity we learn

The prior πk tells us how common cluster k is. If we draw a point at random from the mixture, it is the chance the point came from cluster k before we see its features. Training learns this probability alongside μk and Σk.

Bayes’ rulepY∣X(k∣x)=pX∣Y(x∣k)pY(k)pX(x)
pY(k)=πk(k=1,…,K),πk≥0,∑k=1Kπk=1
pY(k)=πk = prior

Imagine that about 70 out of every 100 points come from cluster 1 and the other 30 come from cluster 2. Then π1=0.70 and π2=0.30. We use the priors and Gaussian densities to find the evidence at x, then apply Bayes’ theorem to get the posterior.

7.3 · Gaussian Mixture Clustering

Law of total probability

It tells you that you can find the overall chance of something happening by looking at each group separately and adding what each group contributes.

Pick a song at random from a 100-song playlist. Each is labeled pop or rock. What’s the chance it runs under three minutes?

Pop

60 of the 100 songs

50% of these run under three minutes

60×0.5=30

Rock

40 of the 100 songs

25% of these run under three minutes

40×0.25=10

Chance a random song is under three minutes

ℙ(E)=30+10100=40100=40%

Multiply each genre’s share by its short-song rate, then add

ℙ(E)=∑k=1Kℙ(Y=k)ℙ(E∣Y=k)

E means the song is under three minutes. Y is its genre.

On the next slide, clusters take the place of genres. To find the evidence at x, weight each cluster’s density at x by its prior and add the results.

7.3 · Gaussian Mixture Clustering

Add the weighted densities to find the evidence

The law of total probability tells us to add the contributions from all clusters. Each contribution is the cluster’s Gaussian density at x multiplied by its prior.

Bayes’ rulepY∣X(k∣x)=pX∣Y(x∣k)pY(k)pX(x)
pX(x)=∑y=1KpX,Y(x,y)=∑y=1KpX∣Y(x∣y)pY(y)=∑y=1Kπy𝒩(x;μy,Σy)
pX(x) = evidence

The blue term 𝒩(x;μy,Σy) is the probability density at x given cluster y. With one feature, it’s the height of that Gaussian curve at x. Multiplying by the prior πy gives the joint term pX,Y(x,y), which considers the point and cluster together. Here X is the random feature vector, x the observed vector, Y the hidden cluster label, and y one possible label. The sum is the mixture density, which can have several peaks.

7.3 · Gaussian Mixture Clustering

What does a Gaussian mixture look like?

Move the means to slide the curves left or right. Change the prior to shift how much each curve contributes to the green sum.

Both variances are fixed at 1.

Density−6−3036Feature value x
  • Cluster 1 weighted density
  • Cluster 2 weighted density
  • Evidence (sum)

A component is a source in the model. Strong overlap can make two components appear as one peak in the summed curve.

7.3 · Gaussian Mixture Clustering

Use the evidence to find the posterior

For an observed point x, take cluster k’s weighted density and divide it by the total density at that point. This gives its probability of belonging to cluster k.

Bayes’ rulepY∣X(k∣x)=pX∣Y(x∣k)pY(k)pX(x)
pY∣X(k∣x)=πk𝒩(x;μk,Σk)∑y=1Kπy𝒩(x;μy,Σy)c=argmaxk∈{1,…,K}pY∣X(k∣x)
pY∣X(k∣x) = posterior

Cluster indices

k is the cluster we are asking about. Keep it fixed in the numerator.

y runs over all K clusters in the denominator, including k.

Point and membership

X is the random feature vector, x is the observed vector, and Y is the hidden cluster label.

pY∣X(k∣x) is the probability that x belongs to cluster k.

c is the hard label. Argmax picks the cluster with the largest posterior.

Gaussian contributions

πk, μk, and Σk are cluster k’s prior, mean, and covariance matrix. Use the same meanings for subscript y.

𝒩 gives the Gaussian density at x. The denominator adds the weighted densities to give the evidence pX(x).

7.3 · Gaussian Mixture Clustering

Calculate the posteriors at one point

Move the point x or change the mixture. At the dashed line, divide each cluster’s weighted density by the total to get its posterior.

Both variances are fixed at 1.

Weighted densities at

Prior × Gaussian density (likelihood)

Cluster 1
Cluster 2

Evidence

Add the two weighted densities

Posterior probabilities

Weighted density ÷ evidence

Cluster 1
Cluster 2
Density−6−3036Feature value x
  • Cluster 1 weighted density
  • Cluster 2 weighted density
  • Evidence (sum)

If the weighted densities are 0.12 and 0.08, then the evidence is 0.20 and the posterior of component 1 is 0.12/0.20=0.60. The two posteriors sum to 1.

7.3.1 · Training

Which mixture best fits the observations?

Imagine trying different centers, shapes, and mixture weights. Each candidate gives a likelihood for the whole observed dataset. We want the combination that gives the highest likelihood.

(𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮p(𝑿∣𝝅,𝝁,𝜮)

We write the dataset 𝒟={xi}i=1n as 𝑿 in the likelihood. The three bold symbols collect the weights, means, and covariance matrices for all K clusters. Argmax returns the parameter values that give the highest score. The hats mark those estimates.

7.3.1 · Training

Build the Gaussian mixture optimization problem

Start with the likelihood of the whole dataset. Rewrite it so each observation contributes one term, then add the rules for valid priors.

  1. Start with the whole dataset
    (𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮p(𝑿∣𝝅,𝝁,𝜮)

    Treat the observations as fixed. Change the cluster parameters and compare the likelihood assigned to the same data.

  2. Take the natural log
    (𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮ln(p(𝑿∣𝝅,𝝁,𝜮))

    The natural log is strictly increasing, so a candidate with a higher likelihood still scores higher after taking the log. The same parameters win. This sets up the next simplification.

  3. Write a product over observations
    (𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮ln(∏i=1np(xi∣𝝅,𝝁,𝜮))

    We model the observations as independent draws from the same mixture. For fixed parameters, the joint likelihood is the product of their individual densities.

  4. Turn the product into a sum
    (𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮∑i=1nln(p(xi∣𝝅,𝝁,𝜮))

    The log of a product is the sum of the logs. Now every observation contributes one term. We also avoid having to multiply many tiny density values together.

  5. Substitute the mixture density inside the log
    (𝝅^,𝝁^,𝜮^)=argmax𝝅,𝝁,𝜮∑i=1nln(∑k=1Kπk𝒩(xi;μk,Σk))

    Replace p(xi∣𝝅,𝝁,𝜮) from the previous step with the mixture density at xi. Since the cluster is hidden, add the Gaussian densities from all K clusters, each weighted by its prior. Take the log of that total for each observation, then add the results.

  6. Require valid probabilities
    subject to πk≥0(k=1,…,K),∑k=1Kπk=1

    Maximize the objective subject to these constraints. Each prior is a probability, and the clusters together must account for all the membership before seeing a point.

The hats on the left mark the same estimates throughout the derivation. In the full objective, the inner sum is over possible clusters for one row and the outer sum is over observed rows.

7.3.1 · Training

Why use a Lagrange multiplier?

You have €100 to spend on projects A and B. How do you choose the amounts x for A and y for B to get the most benefit while spending exactly €100?

ℒ(x,y,λ)=total benefit(x,y)−λ(x+y−100)

Spend exactly €100

Giving either project one more euro means taking a euro from the other. Setting the derivative with respect to λ to zero gives x + y = 100.

Find the best split

If one more euro brings €3 of benefit in A and €5 in B, moving a euro from A to B gains about €2. If both projects receive funding at the best split, their benefit from one more euro is the same. The derivatives with respect to x and y set both of these benefits equal to λ.

The extra term is zero when we respect the budget. It still changes the slope because increasing just one amount changes the total spent. In a Gaussian mixture, the budget is 100% of the probability and the amounts are the cluster priors.

7.3.1 · Training

Put the sum-to-one rule into the Lagrangian

The cluster priors are shares of a 100% budget. We use λ to bring that rule into the calculation for the mixture’s log likelihood.

  1. Measure the gap from 100%
    ∑k=1Kπk−1=0

    The gap is zero for a valid set of priors. For example, priors of 0.6 and 0.4 satisfy the rule. Two priors of 0.6 exceed the total by 0.2.

  2. Put the rule into the objective
    ℒ=∑i=1nln(∑k=1Kπk𝒩(xi;μk,Σk))−λ(∑k=1Kπk−1)

    The parentheses add up the priors and subtract 1. With priors of 0.6 and 0.4, that gives 0.6+0.4−1=0. The multiplier λ makes the term we subtract λ×0=0, so the log likelihood keeps its original value.

The method of Lagrange multipliers lets us handle the equality constraint by solving the Lagrangian’s stationarity equations. We check nonnegativity separately in the update for πk.

7.3.1 · Training

From the Lagrangian to the parameter updates

To find candidate solutions, set the derivatives of the Lagrangian with respect to the priors 𝝅, means 𝝁, and covariance matrices 𝜮 to zero. Solving those equations gives the three updates on the following slides.

When we form the Lagrangian, we set the nonnegativity constraints πk≥0 aside. We’ll check that the resulting priors π^k are nonnegative when we derive their update.

7.3.1 · Training

Update 1 · Find the weighted mean

The estimate of μk is a weighted mean of the observations. Each weight pik is the posterior probability pY∣X(k∣xi) that observation xi belongs to cluster k.

μ^k=∑i=1npikxi∑i=1npik

Multiply each observation by its membership weight, add the results, and divide by the sum of those weights. Memberships of 0 or 1 give the K-means mean for a nonempty cluster.

7.3.1 · Training

Update 2 · Find the weighted variance-covariance matrix

The estimate of Σk gives the weighted variances and covariances of the features. It uses the same pik weights as the mean.

Σ^k=∑i=1npik(xi−μk)(xi−μk)⊤∑i=1npik

Here μk is the newly estimated mean from the previous slide. The outer product is (xi−μk)(xi−μk)⊤. With one feature, it gives a squared deviation. With several features, it also gives the cross-products for the covariances.

7.3.1 · Training

Update 3 · Average the memberships to find the prior

The estimate of πk is the average of all n posterior memberships for cluster k. It estimates the share of the dataset that belongs to that cluster.

π^k=∑i=1npikn

The posterior is the chance that this particular point came from a cluster, after we see where the point landed.
The prior is how common a cluster is overall.

Say three points have a 90%, 50%, and 10% chance of belonging to cluster 1. Together they count as 0.9+0.5+0.1=1.5 points out of 3. That’s half the points, so the updated prior is 50%.

7.3.1 · Training

Why are the updated priors valid?

Every membership probability is nonnegative, so each updated prior is nonnegative. Now check that the updated priors add up to 1.

  1. Check that each prior is nonnegative
    π^k=∑i=1npikn≥0

    Add the posterior memberships for a single cluster and divide by n. Every pik is a probability, so the result is nonnegative.

  2. Add the shares across clusters
    ∑k=1Kπ^k=∑k=1K∑i=1npikn=1n∑k=1K∑i=1npik

    Substitute the update for each prior. The divisor n is the same for all of them, so take it outside both sums.

  3. Count by observation instead
    1n∑k=1K∑i=1npik=1n∑i=1n∑k=1Kpik

    These are the same entries of the membership table. Read it row by row instead of column by column so we can use the fact that each row sums to one.

  4. Use one unit of membership per row
    1n∑i=1n∑k=1Kpik=1n∑i=1n1=1n·n=1

    Add the n row totals of one, then divide by n. The new priors therefore add up to one.

Each observation contributes one unit of membership across all clusters. Adding across all n observations and dividing by n gives a total of 1.

7.3.1 · Training

The parameters and posteriors depend on each other

We have a problem. To estimate pik, we need the priors 𝝅^, means 𝝁^, and covariances 𝜮^. The three parameter updates, in turn, need pik.

  1. To compute the posteriors

    pik=πk𝒩(xi;μk,Σk)∑y=1Kπy𝒩(xi;μy,Σy)

    The priors, means, and covariances go into Bayes’ formula.

  2. To update the parameters

    μ^k=∑i=1npikxi∑i=1npikΣ^k=∑i=1npik(xi−μk)(xi−μk)⊤∑i=1npikπ^k=∑i=1npikn

    All three updates use the posterior weights.

The updated parameters go back into the posterior formula.

To break this circular dependency, start from initial values for 𝝁,𝜮,𝝅 and alternate as in K-means. First compute pik for i=1,…,n and k=1,…,K. Then estimate 𝝁^,𝜮^,𝝅^ using those posteriors. Repeat until convergence.

7.3.1 · Training

Training step 1 · Compute all the posteriors

Start with initial means 𝝁, covariances 𝜮, and priors 𝝅. Hold them fixed while using Bayes’ theorem to find the membership probabilities.

pik=pY∣X(k∣xi),i=1,…,n,k=1,…,K

Compute one probability for each pair of observation i and cluster k. Every row contains K soft memberships, which add up to 1.

7.3.1 · Training

Training step 2 · Re-estimate the mixture parameters

Hold the posteriors from step 1 fixed. Use the three update formulas to estimate new means, covariances, and priors.

(𝝁,𝜮,𝝅)←(𝝁^,𝜮^,𝝅^)

Use the new estimates as the starting values for the next round. Repeat both steps until the fit stabilizes, for example when the log likelihood changes very little.

7.3.1 · Training

Follow one component’s three updates

Move the slider to change the probability that the point at x3=4 belongs to cluster k. See how its fitted Gaussian changes.

Weighted mean

μ^k=∑i=1npikxi∑i=1npik

Weighted variance

Σ^k=∑i=1npik(xi−μ^k)2∑i=1npik

Updated prior

π^k=∑i=1npikn

Fitted Gaussian for cluster k

The curve shows this cluster’s contribution to the mixture.

Mean
0.933
Blue line marks the center
Variance
1.529
Standard deviation = 1.236
Prior
50.0%
Area under the full weighted curve
0.00.10.2-4-268Weighted densityFeature value xMean ± 1 standard deviationx₁ = 0p₁ₖ = 90%x₂ = 2p₂ₖ = 50%x₃ = 4p₃ₖ = 10%

The mean locates the center. The variance sets the spread. The prior scales the entire Gaussian, so the area under the full weighted curve is π^k.

7.3.1 · Training

Watch a Gaussian mixture fit the same cloud

Step through the fit and watch the density contours move. Select a point to see its membership probabilities.

Open on its slideThree fitted Gaussian components, each with three nested density contours

Each ellipse joins locations with the same Gaussian density. The solid, dashed, and dotted lines are at Mahalanobis radii 1, 2, and 3, enclosing about 39%, 86%, and 99% of that component’s probability mass. A dot’s color shows its most probable component. The fit uses a small diagonal covariance regularizer.

7.3.1 · Training

Convergence depends on the starting mixture

Gaussian mixtures give soft assignments and often take more iterations than K-means. We can initialize the mixture from K-means by using its centroids as means and its assignments to estimate covariances and priors.

Different starts can lead to different local solutions. A full-covariance component can also collapse onto one observation and drive the likelihood arbitrarily high. The lab uses a small covariance regularizer.

7.3.1 · Training

A first look at Expectation–Maximization

The current means, covariances, and priors give us soft memberships. Those memberships then become the weights for updating the same parameters. K-means uses a similar loop with hard assignments.

  1. To compute the posteriors

    pik=πk𝒩(xi;μk,Σk)∑y=1Kπy𝒩(xi;μy,Σy)

    The priors, means, and covariances go into Bayes’ formula.

  2. To update the parameters

    μ^k=∑i=1npikxi∑i=1npikΣ^k=∑i=1npik(xi−μk)(xi−μk)⊤∑i=1npikπ^k=∑i=1npikn

    All three updates use the posterior weights.

The updated parameters go back into the posterior formula.

Part 3 generalizes this pattern as Expectation–Maximization (EM), a way to estimate parameters by maximum likelihood when some variables are hidden and direct optimization is difficult.

What to remember

  • Calculate a posterior from weighted component densities.
  • Interpret each parameter update as a weighted calculation.
  • Alternate between calculating soft memberships and updating the mixture.