EM and Bayesian PCA
Latent variables and the EM algorithm: the ELBO + KL decomposition of the log-likelihood, E- and M-steps with old and new parameters, Gaussian mixture updates on a seven-point example, why the likelihood never decreases, probabilistic PCA in closed form and by EM, and Bayesian PCA, which switches off unneeded columns.
Latent variables and mixtures
In the topic The Laplace approximation and Bayesian classification we found the mode of a posterior by Newton's method and put a Gaussian there. That worked because the log-posterior was a sum of simple terms. Now each object gets a hidden part that we never observe, and even finding the mode becomes hard.
A latent variable is a quantity attached to the object that the model uses but the data do not contain. The classic example is a mixture of Gaussians. Each point first picks a component with probability , then is drawn from that component:
The parameters are . We observe only , so the likelihood sums the latent variable out:
The logarithm sits outside the inner sum. Setting the gradient to zero couples all components at once, and there is no closed-form solution.
Compare this with the complete-data log-likelihood, the one we would have if the labels were known:
Here the logarithm acts on single terms, and the problem splits into one small Gaussian fit per component. EM exploits exactly this: we do not know , but for given we know its posterior, and we can average the easy complete-data log-likelihood over it.
Running example. Seven points on a line,
and a mixture of two components with unit variances:
Here is the weight of the first component, not . We start from , where . Later topics of the course return to this mixture.
Log-likelihood = ELBO + KL
Take any distribution over the latent variables. For every value of the product rule gives , so
The left side does not depend on . Average both sides over , then add and subtract :
The first part is the evidence lower bound (ELBO). It equals the expected complete-data log-likelihood plus the entropy of :
The second part is a KL divergence, which is never negative. Hence for every and every , and the gap is exactly . The gap vanishes only when is the exact posterior over .
In a mixture the points are independent given , so the posterior factorizes over points: . If factorizes the same way, the gap is a sum of per-point KL divergences.
A small case on the running example. At the point has posterior by Bayes' rule. Suppose we assign it to the second component with certainty, . The KL from a point mass is minus the log-probability of the chosen value, so this point alone opens a gap of nats.
E-step and M-step
The decomposition turns one hard maximization into two easy ones. EM is coordinate ascent on : it alternates between and . Write for the current parameters.
E-step. Fix and maximize over . Since does not depend on , this is the same as minimizing the KL term, and the answer is the posterior:
After this step the bound touches the likelihood at .
M-step. Fix and maximize over . The entropy is now a constant, so only the expected complete-data log-likelihood matters. It is called the Q-function:
Keep the two arguments apart. The distribution we average over is computed once, with the old parameters, and stays frozen. The logarithm inside contains the new parameters, the ones we optimize. That separation is what makes the M-step easy: we maximize a weighted complete-data log-likelihood, not a log of a sum.
Two remarks. The E-step does not sample : it computes a distribution, or just the expectations that needs. And the M-step need not have a closed form: in some models it is a numerical optimization, for example a few gradient steps of a logistic regression.
EM for a Gaussian mixture
For a mixture, is a sum of terms , where is 1 when and 0 otherwise. The expectation of the indicator is the posterior probability of that component.
E-step: responsibilities. With the old parameters,
Each row sums to one: . These are soft assignments: a point may belong to one component with probability and to the other with . Then
M-step: weighted ML estimates. Let be the effective number of points in component . Maximizing (with a Lagrange multiplier for ) gives
These are the ML estimates from the case of known labels, with every point counted with weight . The variance divides by , not : it is an ML estimate. In dimensions the last formula becomes a weighted covariance .
First iteration on the running example. At the responsibilities of the first component are
and . So and . The new weight is , and the new first mean is . The variances stay fixed at one in this model.
Why the likelihood never decreases
Why does EM never make the likelihood worse? Three facts, chained:
The first inequality holds because the ELBO is a lower bound at any . The second is the M-step: it does not decrease in . The equality is the E-step: with the exact posterior the gap is zero at the old parameters.
On the running example the log-likelihood goes , , , , , … and settles at . After the first step ; at convergence : one component near zero, one near .
What the chain does not promise:
- A global maximum. EM climbs to a stationary point, and different starts may reach different ones. Restarting from several initializations is standard.
- Convergence of the parameters. Monotone and bounded values converge; the parameters themselves need extra conditions. Near a flat optimum EM can be very slow.
- A finite maximum. With free variances, put one component on a single data point and let : its density at that point grows without bound, and so does . In practice the variances are floored or given a prior.
Three useful relaxations keep part of the guarantee:
- Generalized EM. If the E-step is exact, the M-step only has to increase , for example by a few gradient steps. The chain still holds.
- MAP-EM. Add to . The same argument shows that , the unnormalized log-posterior, never decreases.
- Variational EM. If the exact posterior is out of reach, the E-step picks the best in a restricted family. The gap no longer closes, so only the ELBO is guaranteed to grow; the likelihood itself may go down.
EM does not require the complete-data distribution to be in the exponential family. That family only makes the M-step convenient.
Probabilistic PCA
Mixtures have a discrete latent variable. Now let it be continuous. Probabilistic PCA (PPCA) describes data through a few hidden coordinates , :
The loadings matrix has one column per latent direction; is isotropic noise. Everything is Gaussian and linear, so integrates out in closed form. Means add, and covariances of independent terms add:
The ML solution. The mean is . Centre the data, , and diagonalize the sample covariance with . Let hold the first eigenvectors and . Then
where is any orthogonal matrix. Along a kept eigenvector the model variance is , and ML matches it to . In the other directions only noise is left, so is the average variance that the latent coordinates do not explain. The rotation is free because : rotating the latent coordinates does not change the distribution of . The column space is the PCA subspace. If all discarded eigenvalues are zero, no maximum with exists.
Example. Ten-dimensional data from the owner's note, , drawn with a few strong latent directions and noise variance . The eigenvalues of are
With : , and the columns have lengths . The maximized log-likelihood for each :
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|
The gains are large up to , then small, but never negative. At the extra columns soak up noise and drops to , well below the true . Maximum likelihood cannot choose .
EM for probabilistic PCA
The closed form needs the matrix and its eigenvectors. EM avoids both: it costs per iteration, handles missing coordinates by averaging over them, and is the base for Bayesian PCA. Work with centred data , so is fixed and .
E-step. All parameters on the right are old. Collect the terms in in the exponent of :
This is a Gaussian in with precision . With , a matrix,
The M-step needs two moments: and .
M-step. The expected complete-data log-likelihood is
Setting the derivatives to zero gives
The first is a least-squares regression of on , with the inputs known only up to their posterior. The moments , come from the old parameters; the update already uses the new . Why here and in the closed form? Here we average expected squared residuals over all coordinates; the closed form appears only after is eliminated at the joint optimum. At that optimum the two agree.
Bayesian PCA
PPCA by maximum likelihood cannot pick . The topic The relevance vector machine already solved a similar problem for regression weights: give each one its own precision and let the evidence switch the useless ones off. Bayesian PCA does the same for whole columns of :
Start with columns; that is enough to represent any covariance. A large pulls column to zero and deletes latent direction .
Training alternates two steps.
Step 1: MAP-EM for and with fixed. The E-step is unchanged. Write , and . The prior adds to , and the M-step for becomes
With this is EM for PPCA.
Step 2: precisions with fixed. Update each as in the RVM, now for a column of numbers: .
The update plugs in a point estimate of instead of integrating it out, so it only approximates evidence maximization.
Which directions survive. Take a column along an eigenvector of with eigenvalue , and let . At a fixed point of both steps,
A positive root exists only when the discriminant is non-negative and the linear coefficient has the right sign, which works out to
Maximum likelihood keeps a direction whenever . The prior demands extra evidence, about in relative terms when . Below the threshold the only fixed point is with .
On the ten-dimensional example (, , nine columns at the start) the threshold is . EM settles at , close to the true , so directions with are pruned. Three columns survive, with lengths and precisions . The ML lengths were : the prior also shrinks what it keeps.
Every step of this topic leaned on one thing: the exact posterior in the E-step. A fully Bayesian PCA would also put a posterior on , and then and enter the likelihood as a product : the joint posterior has no closed form. When the E-step cannot be exact, we choose from a tractable family and maximize the ELBO over it. That is the next topic, Variational inference.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.