Variational inference
When the posterior cannot be written down, approximate it: maximize the evidence lower bound over a convenient family. The two KL terms of the ELBO, mean field and the general factor update, coordinate ascent on a Normal–Gamma model checked against the exact answer, underestimated variance and the mode-seeking reverse KL.
From the E-step to optimization
The topic EM and Bayesian PCA split the log-likelihood into a lower bound and a gap, and its E-step closed the gap by setting . That step quietly assumed we can write the exact posterior over the latent variables. Often we cannot. A non-conjugate piece or a coupling between latent variables is enough to make the normalizing integral intractable. And once the parameters get priors too, they join the unknowns, and a posterior over everything at once is rarely available in closed form.
Variational inference keeps the decomposition and gives up exactness. Let now stand for all unknowns: latent variables and parameters alike. For any distribution ,
The left side does not depend on . So raising the ELBO lowers the KL to the posterior by exactly the same amount. We pick a variational family of distributions we can handle and solve
An integral we cannot take becomes an optimization we can run. If happens to contain the true posterior, the optimum is the posterior itself, and we are back at the exact E-step of EM.
All of this is useful only if can be evaluated while the posterior cannot. Read its definition once more with that question in mind; the check below asks exactly this.
If some parameters stay point estimates, we can alternate: a variational E-step over , then an M-step over . This is variational EM. Each step raises the ELBO, but unlike exact EM the log-likelihood itself is no longer guaranteed to grow.
Two KL terms in one bound
Substitute into the ELBO and regroup:
The first term rewards for putting mass where the unknowns explain the data well. The second keeps close to the prior. This is the Bayesian trade-off in one line, and it is exactly the VAE loss of the topic Black-box variational inference and the VAE, with the sign flipped.
So this topic now has two KL divergences: one in the decomposition of from the previous block, and one inside the bound. Their second arguments differ, and only one of them can be computed. Mixing them up is a classic exam mistake, so keep track of which is which.
A tiny example makes the gap concrete. Take one point and the two-component mixture from the topic EM and Bayesian PCA, with label . Suppose the joint values are and . Then and the posterior is . For a discrete the bound is a sum:
With equal to the posterior, every ratio equals and : no gap. With the hard assignment , as in -means, , and the gap is nats. That number is the KL from to .
Mean field and the general update
The choice of is the whole game. A fixed-form family names the shape of , for example a Gaussian with free mean and covariance; we return to it in the topic Black-box variational inference and the VAE. A free-form family names only which unknowns are independent and lets the bound decide the shape. The classic free-form family is mean field: split into blocks and require
Correlations between blocks are lost by design; within a block anything goes.
Now optimize one factor while the others are fixed. Write for the expectation over all factors except . The terms of that involve are
where . A KL is smallest, zero, when its arguments coincide. So the optimal factor is
The constant is fixed by normalization. Terms of that do not contain fall into it, so only the complete conditional matters. When that conditional lies in an exponential family whose natural parameters are linear in the statistics of the other blocks (conditional conjugacy), stays in the same family, and an update only recomputes its parameters.
Cycling through the blocks is coordinate ascent variational inference (CAVI). Every update maximizes exactly over one factor, so the ELBO never decreases. It converges to a local optimum, which may depend on the starting point.
Example: the mean and precision of a Normal
Time for a worked example. The topic Conjugate priors and the exponential family estimated a Normal mean with known variance. Now both the mean and the precision are unknown, with the Normal–Gamma prior:
The exact posterior is again Normal–Gamma, so we do not need VI here. That is the point: we can grade its answer. The data are four numbers , so and ; the prior is , , .
The mean-field family is . Write the log joint, dropping constants:
Update of . Take of the terms with . They are linear in , so only enters:
This is a quadratic in , so with
Here and . Notice that does not depend on at all: precision scales both terms of the bracket equally.
Update of . Take of the whole log joint. Every term is either a multiple of or a multiple of , and that is the signature of a Gamma density . Collect the coefficients of to get the shape ; the rate needs of the squares and comes in the next block.
Coordinate ascent in numbers
For the rate, use and the same for :
With our data, and . The shape is . One CAVI sweep is: set and from the current , then from the new , then . The ELBO is also in closed form, since every expectation it needs is a Gaussian or Gamma moment, with ( is the digamma function).
Start from the guess :
| sweep | ELBO | |||
|---|---|---|---|---|
| 1 | 10.000 | 3.850 | 0.9091 | −7.8443 |
| 2 | 4.545 | 4.150 | 0.8434 | −7.7127 |
| 3 | 4.217 | 4.193 | 0.8348 | −7.7112 |
| 4 | 4.174 | 4.199 | 0.8335 | −7.7111 |
| limit | 4.167 | 4.200 | 0.8333 | −7.7111 |
Three things to read off the table:
- The ELBO grows at every sweep, as CAVI promises. A drop would mean a bug in an update or in the ELBO formula, not a feature of the data.
- Convergence takes a handful of sweeps. Only really moves, because was right from the first update.
- The limit is , , so and .
Run the same loop in your head from a different start and you land on the same limit: in this model the optimum is unique.
What mean field gets wrong
Now grade the answer against the exact Normal–Gamma posterior: , , , .
- Means are right. The exact and , the same as under . This is no coincidence: at the fixed point and , which solve to .
- The spread of is too small. Under the variance is . The exact marginal of is a Student with variance . Standard deviation instead of .
- The gap is small but not zero. The exact , so nats, and no number of sweeps will close it.
Where did the spread go? In the exact posterior and are dependent: the precision of is , so a small goes with a wide . Averaging over fattens the tails of . Mean field cuts that link and plugs in a single .
The same effect, in its purest form: let the target be a Gaussian with precision matrix . The update rule gives at the optimum, and , with equality only for uncorrelated coordinates. With unit variances and correlation the factor variance is : for , instead of . For a Gaussian target the means still come out exact; for other targets mean field guarantees neither the means nor the variances. The safe rule: mean-field credible intervals tend to be too narrow.
The reverse KL seeks a mode
Why too narrow and not too wide? Write the divergence VI minimizes:
Wherever is close to zero but is not, is huge and the penalty explodes. So must vanish where vanishes. This is zero-forcing, and it makes the reverse KL mode-seeking: a that cannot cover everything retreats into one high-density region. The forward , which expectation propagation works with, behaves the other way and spreads over all the mass.
Take and a Gaussian . The reverse-KL optimum is essentially (or its mirror image), with . The moment-matched , which covers both bumps and sits on the valley between them, scores worse, . Under the forward KL the ranking flips: for the wide Gaussian against for the single bump.
The number is not an accident. A that matches one of two equal, separated modes misses exactly half the mass. Mixtures produce such modes for free. Put priors on the parameters of the two-component mixture from the topic EM and Bayesian PCA. Swapping the labels of the components changes nothing in the likelihood or in a symmetric prior, so the posterior over the component parameters has two mirror-image modes. A mean-field settles in one of them, and its bound sits below by about from the label symmetry alone. Since this part of the gap is the same for every mode, it is often added back by hand when the ELBO is used to compare models.
Variational inference is fast and deterministic, and its ELBO is a usable stand-in for the evidence. Its error, though, is built into the family: the gap of nats in our Normal–Gamma example and the missing modes of a mixture stay however long we optimize. The alternative is to give up a closed-form and draw samples whose distribution approaches the exact posterior, so the error shrinks as the run grows. That is the topic Monte Carlo and Markov chains.
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.