S14 · Lesson 35 · Approximate inference
When the posterior has no closed form, the Laplace approximation and its evidence estimate, variational inference with the evidence lower bound and mean-field factors, which way the KL divergence leans, and the variational autoencoder
Conjugate priors and Gaussian processes gave exact posteriors, but most models don't. Logistic regression's posterior over its weights, a mixture model's posterior over its assignments, a neural network's posterior over anything: none has a closed form, and the integrals for predictions and evidence are out of reach. Two families of approximation fill the gap. Laplace's method fits a Gaussian at the peak; variational inference picks the best member of a tractable family by optimization, and the variational autoencoder turns that into a way of training generative networks.
- The solid curve is a gamma posterior for a count rate, with shape a (from a − 1 events under a flat prior), scaled to peak at 1.
- The dashed curve is its Laplace approximation: a Gaussian at the same peak with the same curvature there.
- With few events the posterior is skewed and the Gaussian misplaces its mass; as a grows, the two converge, as the central limit theorem suggests.
Predict
A two-dimensional Gaussian posterior has unit variances and correlation 0.9. A mean-field variational approximation (a product of independent Gaussians) is fitted by minimizing KL(q‖p). What variance does it give each variable?
The best factorized q matches the conditional precision, not the marginal variance: each factor's variance is = 0.19, a standard deviation of 0.44 against the true 1. Minimizing KL(q‖p) heavily penalizes q for putting mass where p has little, so a factorized q shrinks to fit inside the correlated ellipse. Variational posteriors tend to be overconfident.
Name it
The Laplace approximation
Find the posterior's mode and the curvature of its logarithm there, . Then
For M parameters it also estimates the evidence:
Since |A| grows about as , dropping the terms that don't grow with N leaves BIC (lesson 12). The approximation is good when the posterior is nearly Gaussian, which large data sets usually make it, and poor for skewed or multimodal posteriors.
Variational inference
For any distribution q over the hidden variables Z,
The KL term is never negative, so , the evidence lower bound (ELBO), is a lower bound on the log evidence, and maximizing it over a tractable family of q minimizes the divergence from the true posterior. In the mean-field family , each factor's best form is
updated in turn, a coordinate ascent that never lowers the bound. EM (lesson 36) is the special case where q is the exact posterior over the hidden assignments.
Which way KL leans
KL(q‖p) is large wherever q has mass and p doesn't, so minimizing it makes q zero-forcing: it sits inside p, under one mode if p has several, and underestimates spread. KL(p‖q), used by expectation propagation, is zero-avoiding: q covers all of p's mass, spreading over every mode.
The variational autoencoder
An encoder network outputs a Gaussian ; a decoder network gives ; the prior is . Both are trained to maximize the ELBO, a reconstruction term minus
Samples are drawn as with , the reparameterization that lets gradients flow through the sampling to μ and σ.
Worked example
A pass rate has a uniform prior, and 7 of 10 trials pass. Compare the Laplace approximation with the exact beta(8, 4) posterior.
Show the solution
- Mode. The log posterior is , with its mode at 0.7.
- Curvature. At the mode it is so the Laplace approximation is N(0.7, 0.021), a standard deviation of 0.145.
- Exact posterior. Beta(8, 4) has mean 0.667 and standard deviation 0.131.
- Compare. The Laplace version is centered at the mode rather than the mean and is a little too wide, because the true posterior is skewed toward 0.5 by the boundary at 1; with 70 of 100 the two would agree to a few percent.
Answer the Laplace approximation is N(0.7, 0.021), with standard deviation 0.145, against the exact posterior's mean 0.667 and standard deviation 0.131.
Worked example
Work out the mean-field approximation in the predict question and its KL divergence from the true posterior.
Show the solution
- True precision matrix. so each mean-field factor is N(0, 0.19).
- KL divergence. For two zero-mean Gaussians:
- What suffers. The means are right; only the spread suffers, and predictions that need the posterior's width will be overconfident.
Answer each mean-field factor is N(0, 0.19), and the KL divergence from the true posterior is 0.83 nats.
Worked example
A VAE's encoder gives one latent variable μ = 1 and σ = 0.5. Find its KL term and the sample drawn with ε = −1.2. What would the total KL be for 32 such latent dimensions?
Show the solution
- KL term.
- Sample.
- 32 dimensions. The KL cost is 26.2 nats per example, which the reconstruction must repay.
- Unused dimensions. Dimensions the decoder doesn't use are pushed to μ = 0, σ = 1, where their KL is zero, so a VAE switches off latent dimensions it doesn't need.
Answer the KL term is 0.818 nats, the sample is z = 0.4, and 32 such dimensions cost 26.2 nats.
Check
A success probability has a uniform prior, and 15 of 50 trials succeed.
(a)
Where is the Laplace approximation centered?
(b)
What standard deviation does it give?
Check
A rate's posterior is gamma with shape 10 and rate 0.5.
(a)
What standard deviation does its Laplace approximation have?
(b)
By how many exact standard deviations is the approximation's center below the true mean?
Check
A Gaussian posterior over two variables has equal standard deviations 1 and correlation 0.5. It's approximated by minimizing KL(q‖p) over factorized Gaussians.
(a)
What standard deviation does each factor get?
(b)
What is the KL divergence of the approximation from the posterior?
Check
A VAE encoder outputs μ = 0.5 and σ = 0.2 for each of 64 latent dimensions.
(a)
What is the total KL term?
(b)
What sample does ε = 0.7 give in one dimension?
Check
A model with 5 parameters has a peak log-likelihood of −150 and log prior −3 at its mode, where the log posterior's Hessian has determinant 2000 (in magnitude).
(a)
What log evidence does Laplace's method estimate?
Check
A posterior has two well-separated modes of equal height. What does a single Gaussian fitted by minimizing KL(q‖p) do?
Check
A probability's posterior is beta(a, b).
Step 1
Where is its mode?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What variance does the Laplace approximation give?
Step 3
What is the exact variance?
Explain why
Why does a variational approximation found by minimizing KL(q‖p) tend to underestimate the posterior's uncertainty?
The divergence KL(q‖p) averages the log ratio of q to p over q's own mass. Wherever q places probability and p has little, that ratio is huge, so the optimization strongly avoids spreading q into regions p considers unlikely, while it pays little for missing parts of p that q doesn't cover. If the approximating family can't match p's shape, for example because it assumes independence where p is correlated, or has one mode where p has several, the best compromise is a q that fits inside p, narrower than p's true spread. Predictions built on it inherit that overconfidence.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.