Variance describes one feature
Higher variance means values tend to lie farther from their mean.
Clustering · Part 2
Build a Gaussian mixture, calculate soft memberships, and learn the means, covariances, and priors.
Each point goes to exactly one cluster, the one with the nearest centroid.
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.
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.
Drag the white diamond across a boundary to see its cluster change. Move a centroid to redraw the cells.
Distance from the point
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.
As in classification, we can estimate a probability of belonging to each group. In clustering, the groups are clusters.
is the unknown cluster index and is one possible cluster. is the random feature vector and is the feature vector we observed. For two clusters, probabilities of 0.60 and 0.40 show how uncertain the assignment is.
As in classification, multiply the likelihood by the prior and divide by the evidence to get the posterior.
Chance that this point came from cluster after we see its features .
How densely points from cluster fall near .
Chance that a random point came from cluster before we see its features.
Density of the whole mixture at . Add each cluster’s density weighted by its prior.
is the observed feature vector and is a possible cluster label. We learn the cluster distributions and priors from unlabeled data.
For each observed point , we want the probability that it came from cluster . Over the next few slides, we’ll see how Bayes’ rule gives this formula for a Gaussian mixture.
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.
Two of the three quantities we will optimize during training are the mean , which locates the cluster’s center, and the variance-covariance matrix , which describes its spread and tilt.
Mean fixed at 0. The blue marker shows the curve’s height at x.
The first line says that points from cluster follow a Gaussian. The blue term is the probability density at given cluster . With one feature, it’s the height of that cluster’s Gaussian curve at . The density is highest at the cluster center. Bayes’ theorem uses it with the prior to compute the membership probability.
Compare two curves with the same mean, then three point clouds drawn on the same scales.
Higher variance means values tend to lie farther from their mean.
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 and for . Near-zero covariance means the cloud has little upward or downward tilt.
The diagonal stores the variance of each feature. The other entries store the covariance of each pair of features within cluster .
With features, and . The indices and identify features. Covariance matrices are positive semidefinite. A Gaussian density in the full feature space needs a positive definite covariance matrix.
The prior tells us how common cluster is. If we draw a point at random from the mixture, it is the chance the point came from cluster before we see its features. Training learns this probability alongside and .
Imagine that about 70 out of every 100 points come from cluster 1 and the other 30 come from cluster 2. Then and . We use the priors and Gaussian densities to find the evidence at , then apply Bayes’ theorem to get the posterior.
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?
60 of the 100 songs
50% of these run under three minutes
40 of the 100 songs
25% of these run under three minutes
Multiply each genre’s share by its short-song rate, then add
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 , weight each cluster’s density at by its prior and add the results.
The law of total probability tells us to add the contributions from all clusters. Each contribution is the cluster’s Gaussian density at multiplied by its prior.
The blue term is the probability density at given cluster . With one feature, it’s the height of that Gaussian curve at . Multiplying by the prior gives the joint term , which considers the point and cluster together. Here is the random feature vector, the observed vector, the hidden cluster label, and one possible label. The sum is the mixture density, which can have several peaks.
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.
For an observed point , take cluster ’s weighted density and divide it by the total density at that point. This gives its probability of belonging to cluster .
is the cluster we are asking about. Keep it fixed in the numerator.
runs over all clusters in the denominator, including .
is the random feature vector, is the observed vector, and is the hidden cluster label.
is the probability that belongs to cluster .
is the hard label. Argmax picks the cluster with the largest posterior.
, , and are cluster ’s prior, mean, and covariance matrix. Use the same meanings for subscript .
gives the Gaussian density at . The denominator adds the weighted densities to give the evidence .
Move the point 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.
Prior × Gaussian density (likelihood)
Add the two weighted densities
Weighted density ÷ evidence
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.
We write the dataset as in the likelihood. The three bold symbols collect the weights, means, and covariance matrices for all clusters. Argmax returns the parameter values that give the highest score. The hats mark those estimates.
Start with the likelihood of the whole dataset. Rewrite it so each observation contributes one term, then add the rules for valid priors.
Treat the observations as fixed. Change the cluster parameters and compare the likelihood assigned to the same data.
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.
We model the observations as independent draws from the same mixture. For fixed parameters, the joint likelihood is the product of their individual densities.
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.
Replace from the previous step with the mixture density at . Since the cluster is hidden, add the Gaussian densities from all clusters, each weighted by its prior. Take the log of that total for each observation, then add the results.
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.
You have €100 to spend on projects A and B. How do you choose the amounts for A and for B to get the most benefit while spending 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.
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.
The cluster priors are shares of a 100% budget. We use to bring that rule into the calculation for the mixture’s log likelihood.
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.
The parentheses add up the priors and subtract 1. With priors of 0.6 and 0.4, that gives . The multiplier makes the term we subtract , 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 .
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 aside. We’ll check that the resulting priors are nonnegative when we derive their update.
The estimate of is a weighted mean of the observations. Each weight is the posterior probability that observation belongs to cluster .
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.
The estimate of gives the weighted variances and covariances of the features. It uses the same weights as the mean.
Here is the newly estimated mean from the previous slide. The outer product is . With one feature, it gives a squared deviation. With several features, it also gives the cross-products for the covariances.
The estimate of is the average of all posterior memberships for cluster . It estimates the share of the dataset that belongs to that cluster.
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 points out of 3. That’s half the points, so the updated prior is 50%.
Every membership probability is nonnegative, so each updated prior is nonnegative. Now check that the updated priors add up to 1.
Add the posterior memberships for a single cluster and divide by . Every is a probability, so the result is nonnegative.
Substitute the update for each prior. The divisor is the same for all of them, so take it outside both sums.
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.
Add the row totals of one, then divide by . The new priors therefore add up to one.
Each observation contributes one unit of membership across all clusters. Adding across all observations and dividing by gives a total of 1.
We have a problem. To estimate , we need the priors , means , and covariances . The three parameter updates, in turn, need .
The priors, means, and covariances go into Bayes’ formula.
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 for and . Then estimate using those posteriors. Repeat until convergence.
Start with initial means , covariances , and priors . Hold them fixed while using Bayes’ theorem to find the membership probabilities.
Compute one probability for each pair of observation and cluster . Every row contains soft memberships, which add up to 1.
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.
Move the slider to change the probability that the point at belongs to cluster . See how its fitted Gaussian changes.
The curve shows this cluster’s contribution to the mixture.
The curve shows this cluster’s contribution to the mixture.
The curve shows this cluster’s contribution to the mixture.
The curve shows this cluster’s contribution to the mixture.
The curve shows this cluster’s contribution to the mixture.
Step through the fit and watch the density contours move. Select a point to see its membership probabilities.
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.
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.
The priors, means, and covariances go into Bayes’ formula.
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.