Lecture 14: Expectation-Maximization and Factor Analysis
Stanford CS229 lecture 14 notes: Expectation-Maximization and Factor Analysis.
- cs229
- machine-learning
- course-notes
On this page
Unsupervised learning: clustering#
Now we only have with no labels. We want to find groups.
K-means#
- Pick random points as initial cluster centroids .
- Assign every point to its closest centroid.
- Update each centroid to be the mean of the points assigned to it.
- Repeat 2 and 3 until nothing changes.
Step 1: Step 2: Step 3:
o o x o o x o o x
o x x o x x o x x
X1 X2 (colors by (centroids
nearest) move)It's guaranteed to converge (the distortion function goes down every step), but only to a local optimum. So run it several times from different random starts and keep the best result.
K-means makes a hard assignment: each point belongs to exactly one cluster. What if a point is kind of in the middle?
Mixture of Gaussians#
A softer, probabilistic version. Suppose there's a hidden (latent) variable telling us which cluster each point came from:
We never observe . The log likelihood is:
That sum inside the log makes it hard to maximize in closed form. If we knew , it would be easy (it'd be GDA). So we use EM.
The EM algorithm#
Repeat until convergence:
E-step: guess the latent variables. For each , compute the posterior probability that point came from cluster :
(Think of this as a soft version of K-means' "assign" step.)
M-step: update the parameters, using the as soft counts:
Compare it with GDA's formulas and you'll see it's exactly the same, with replaced by the soft weight .
Why does EM work?#
Jensen's inequality#
If is concave (like ), then .
Using this, for any distribution over :
The right side is a lower bound on the log likelihood (the ELBO). Equality holds when the ratio is constant, which happens when .
- E-step: set to the posterior. This makes the lower bound touch the true log likelihood at the current .
- M-step: maximize the lower bound with respect to . This pushes the likelihood up.
So each EM iteration is guaranteed to never decrease the likelihood. Like K-means, it can only find local optima.
Factor Analysis#
A problem: what if (features) is bigger than (examples)? For example, 100 dimensional data with only 30 points. The MLE covariance matrix is singular (not invertible), and a Gaussian fit just breaks.
Factor analysis assumes the data actually lives near a low dimensional subspace. There is a latent with :
where is , and is diagonal.
The marginal of is Gaussian with covariance . This is a covariance with only about parameters instead of , and it's always invertible since is positive diagonal.
We fit with EM, where is the latent variable. Since the joint of is Gaussian, the E-step has a closed form.