ML Systems Lab Open interactive version →
Advanced 60 min read variational inferenceELBOKL divergencemean fieldCAVI

Variational Inference

ELBO, KL divergence, mean field VI, CAVI, stochastic VI — when to use over MCMC

You want to fit a topic model (LDA) to 100,000 documents. The exact posterior P(topics | documents) requires summing over all possible topic assignments for every word in every document — exponential in the number of words. You cannot compute it. MCMC could sample from it, but running chains long enough to converge on 100,000 documents takes hours to days. You need a tractable alternative that scales with the data. This is the problem variational inference solves.

Variational inference reframes posterior computation as optimization. Instead of computing the true posterior p(z|x), you pick a simpler family of distributions q(z; φ) — typically factorized Gaussians or Dirichlet distributions — and find the member of that family closest to the true posterior. "Closest" is measured by KL divergence. You minimize KL(q ‖ p) by maximizing the ELBO (Evidence Lower Bound): ELBO = E_q[log p(x, z)] - E_q[log q(z)]. The ELBO is a lower bound on log p(x). Maximizing it pushes q as close to p as possible while keeping q tractable.

The mechanism: ELBO = log p(x) - KL[q(z) ‖ p(z|x)]. Since KL ≥ 0, ELBO ≤ log p(x) always. Maximizing ELBO is exactly equivalent to minimizing KL[q ‖ p(z|x)] — they are the same objective. Once you can compute and differentiate the ELBO, the posterior approximation reduces to gradient descent. Stochastic VI uses minibatch gradient estimates, reducing cost to O(batch_size) per update. This is how VI scales to millions of examples — making LDA at scale, SVGP, and VAE training all tractable.

NOT this. "Variational inference always gives accurate posteriors." The approximation quality depends entirely on the variational family. Mean-field VI — the fully factorized assumption q(z) = ∏ᵢ qᵢ(zᵢ) — assumes all latent variables are independent. Real posteriors almost always have correlations. Mean-field ignores all of them. The result: predictions are overconfident because the approximate posterior is too narrow. The forward KL[q ‖ p] is mode-seeking — it concentrates on one posterior mode and ignores others, systematically underestimating posterior variance. When posterior accuracy matters more than speed (clinical decision making, scientific inference), use richer variational families (normalizing flows) or MCMC. Mean-field VI is not a universal approximate Bayesian method; it is a fast approximation with known failure modes.

Key points

Takeaway

VI is biased by construction: it minimises KL[q ‖ p], which is mode-seeking, so it concentrates on one posterior mode and systematically underestimates uncertainty. Tight VI posteriors do not mean the true posterior is tight — they may mean VI found one mode and ignored the rest. The practical choice is: VI when scalability matters and approximate uncertainty is acceptable; MCMC when exact uncertainty is the deliverable and you can afford the runtime.

Recap

Check your understanding

Q1. Explain why the ELBO is a lower bound on log p(x) and why maximising it is equivalent to minimising KL[q ‖ p(z|x)].

Q2. Your mean field VI model gives very tight posteriors (narrow q distributions) but makes poor predictions. What is likely happening and how do you diagnose it?

Q3. Select the two correct statements about CAVI vs black-box variational inference (BBVI).

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →