Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Bayesian statistics

Variational Inference, the ELBO and the Mean-Field Approximation

Instead of sampling the posterior, approximate it: choose the member of a simple family (independent Gaussians, say) closest to it in KL divergence. Minimizing KL(q‖p) is equivalent to maximizing the evidence lower bound (ELBO), turning inference into optimization that scales to huge models and datasets. You will derive the ELBO, run coordinate-ascent mean-field VI, use reparameterized stochastic gradients (black-box VI, the engine of VAEs), and learn VI's systematic biases: it is mode-seeking and underestimates uncertainty, especially when parameters are correlated.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Tracing a complicated shape with a simple stencil

Suppose the answer to your problem is a complicated, lumpy shape that’s expensive to describe. You only have a few stencils: circles and ovals of any size. Variational inference picks the stencil that best matches the shape, by sliding and stretching it until it overlaps as well as possible.

It’s much faster than exploring the whole shape point by point (as MCMC does), so it scales to models with millions of parameters. The price: a simple stencil can’t capture everything, so it may hide in one lump or make the shape look tighter than it really is.

Approximate the posterior by the closest simple distribution
reverse KL(q‖p) (VI): N(-1.73, 0.61²) · forward KL(p‖q) (moment matching): N(-0.40, 1.84²)
Shaded: the target. VI minimizes KL(q‖p), which punishes q for putting mass where p has none, so q hides inside one mode (mode-seeking) and its variance is too small. Forward KL punishes q for missing mass, so it spreads over both modes (mass-covering), even across the empty valley between them.
Why engineers care

Variational autoencoders, Bayesian deep learning, large-scale topic models and fast approximate filters all use variational inference. Knowing its typical errors (overconfidence and missed modes) tells you when to trust it on a robot or spacecraft, and when to double-check with sampling.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Variational inference turns posterior computation into…
  2. 2. Compared with MCMC, VI is usually…
  3. 3. A common failure of simple VI approximations is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The ELBO

For any distribution q(z)q(z) over the latent variables or parameters,

log⁡p(x)=Eq[log⁡p(x,z)]−Eq[log⁡q(z)]⏟ELBO(q)+KL⁡(q(z) ∥ p(z∣x))\log p(x) = \underbrace{\mathbb E_q[\log p(x, z)] - \mathbb E_q[\log q(z)]}_{\text{ELBO}(q)} + \operatorname{KL}\big(q(z)\,\|\,p(z\mid x)\big)

Since KL ≥ 0, the ELBO is a lower bound on the log evidence. Because log⁡p(x)\log p(x) doesn’t depend on qq, maximizing the ELBO = minimizing KL(q‖posterior), and we never need the intractable p(x)p(x). The ELBO is “expected fit” + “entropy of qq”.

Mean-field

The most common family is q(z)=∏jqj(zj)q(z) = \prod_jq_j(z_j): independent factors. The coordinate-ascent update (CAVI) for each factor is

log⁡qj∗(zj)=Eq−j[log⁡p(x,z)]+const\log q_j^*(z_j) = \mathbb E_{q_{-j}}[\log p(x, z)] + \text{const}

For conjugate exponential-family models this has closed form, and CAVI is like Gibbs with expectations instead of samples. The ELBO never decreases.

📝 Quick check (Level 1)
0/3 answered
  1. 1. log p(x) − ELBO =
  2. 2. Mean-field assumes the latent variables are…
  3. 3. Maximizing the ELBO requires knowing p(x)?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Black-box / stochastic VI

For non-conjugate models, maximize the ELBO by stochastic gradient ascent. With qϕ(z)=N(m,diag⁡(s2))q_\phi(z) = \mathcal N(m, \operatorname{diag}(s^2)), reparameterize z=m+s⊙εz = m + s\odot\varepsilon with ε∼N(0,I)\varepsilon\sim\mathcal N(0, I) (A4.3-2):

∇mELBO=Eε[∇zlog⁡p(x,z)],∇log⁡sELBO=Eε[∇zlog⁡p(x,z)⊙ε⊙s]+1\nabla_m\text{ELBO} = \mathbb E_\varepsilon[\nabla_z\log p(x, z)], \qquad \nabla_{\log s}\text{ELBO} = \mathbb E_\varepsilon[\nabla_z\log p(x, z)\odot\varepsilon\odot s] + 1

Estimate it with a few samples per step and use Adam. Minibatches of data make it scale to millions of points (stochastic VI; Hoffman et al., 2013). A VAE amortizes this: an encoder network outputs (m,s)(m, s) for each data point (Kingma and Welling, 2014).

What VI gets wrong

  • Variance underestimation. For a Gaussian target with precision Λ\Lambda, mean-field gives marginal variances 1/Λii1/\Lambda_{ii} instead of (Λ−1)ii(\Lambda^{-1})_{ii}. With correlation 0.9, that’s 0.19 instead of 1.
  • Mode-seeking. KL(q‖p) is infinite if qq puts mass where pp has none, so qq retreats into one mode.
  • Better families help: full-covariance or low-rank Gaussians, normalizing flows (A4.3-2), mixtures. Always validate VI against MCMC on a subproblem, or use diagnostics such as PSIS-k^\hat k (Yao et al., 2018).
🐍 Exercise 1: mean-field CAVI, the ELBO and the KL gap
Run CAVI for a correlated 2-D Gaussian target. Show that it recovers the means but reports variances 1 − ρ², then check by Monte Carlo that log Z − ELBO equals the analytic KL(q ‖ p).
📝 Quick check (Level 2)
0/3 answered
  1. 1. The reparameterization trick writes z = …
  2. 2. Mean-field variance for a Gaussian target is…
  3. 3. A VAE's encoder outputs…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Deriving the ELBO decomposition

log⁡p(x)=Eq[log⁡p(x,z)p(z∣x)]=Eq[log⁡p(x,z)q(z)]+Eq[log⁡q(z)p(z∣x)]\log p(x) = \mathbb E_q\Big[\log\frac{p(x, z)}{p(z\mid x)}\Big] = \mathbb E_q\Big[\log\frac{p(x, z)}{q(z)}\Big] + \mathbb E_q\Big[\log\frac{q(z)}{p(z\mid x)}\Big]

The first term is the ELBO and the second is KL⁡(q ∥ p(⋅∣x))≥0\operatorname{KL}(q\,\|\,p(\cdot\mid x))\ge0. Equivalently, Jensen: log⁡Eq[p(x,z)/q]≥Eqlog⁡[p(x,z)/q]\log\mathbb E_q[p(x, z)/q] \ge\mathbb E_q\log[p(x, z)/q]. ∎ The same bound gives EM (A5.2-10).

Deriving the CAVI update

Holding q−jq_{-j} fixed, the ELBO as a functional of qjq_j is Eqj[Eq−jlog⁡p(x,z)]−Eqjlog⁡qj+c=−KL⁡(qj ∥ p~j)+c′\mathbb E_{q_j}[\mathbb E_{q_{-j}}\log p(x, z)] - \mathbb E_{q_j}\log q_j + c = -\operatorname{KL}(q_j\,\|\,\tilde p_j) + c', where p~j∝exp⁡Eq−jlog⁡p(x,z)\tilde p_j\propto\exp\mathbb E_{q_{-j}}\log p(x, z). It’s maximized by qj=p~jq_j = \tilde p_j. ∎ For a Gaussian target, this gives qj=N(mj,1/Λjj)q_j = \mathcal N(m_j, 1/\Lambda_{jj}) with mj=μj−Λjj−1∑k≠jΛjk(mk−μk)m_j = \mu_j - \Lambda_{jj}^{-1}\sum_{k\ne j}\Lambda_{jk}(m_k - \mu_k).

Why reverse KL is mode-seeking

KL⁡(q ∥ p)=∫qlog⁡qp\operatorname{KL}(q\,\|\,p) = \int q\log\frac qp blows up wherever q>0q > 0 and p≈0p\approx0, so qq avoids the valley between modes and fits under one. KL⁡(p ∥ q)\operatorname{KL}(p\,\|\,q) blows up wherever p>0p > 0 and q≈0q\approx0, so qq must cover all of pp’s mass. Its optimum within Gaussians is moment matching. Expectation propagation uses the forward direction locally (Minka, 2001).

Exercise 3.1: Compute KL(q ‖ p) for mean-field vs a 2-D Gaussian with correlation ρ

With q=N(μ,(1−ρ2)I)q = \mathcal N(\mu, (1 - \rho^2)I) and unit-variance pp: tr⁡(ΛΣq)=2\operatorname{tr}(\Lambda\Sigma_q) = 2. So KL⁡=12[2−2+log⁡det⁡Σp−log⁡det⁡Σq]=12[log⁡(1−ρ2)−2log⁡(1−ρ2)]=−12log⁡(1−ρ2)\operatorname{KL} = \frac12[2 - 2 + \log\det\Sigma_p - \log\det\Sigma_q] = \frac12[\log(1 - \rho^2) - 2\log(1 - \rho^2)] = -\frac12\log(1 - \rho^2). That’s 0.83 nats at ρ = 0.9.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The ELBO is a lower bound by…
  2. 2. The optimal mean-field factor is ∝ …
  3. 3. Minimizing forward KL(p‖q) over Gaussians gives…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

VI vs MCMC for grasp-success regression

Black-box VI with a mean-field Gaussian on the same logistic-regression posterior as A5.2-7 converges in a few hundred Adam steps. Its means match MCMC’s, but its posterior standard deviations are smaller (here by about 6%). The parameters are correlated, and mean-field can’t represent that. For decisions near a safety threshold, that overconfidence matters. Practical recipes: use full-rank or flow-based qq, check against MCMC on a subsample, or use VI only for the initial fit and then run NUTS.

🐍 Exercise 2 (research): black-box VI for Bayesian logistic regression
Implement reparameterized-gradient VI (mean-field Gaussian, Adam) for the grasp-success logistic regression. Check that the ELBO increases, that the VI means match an MCMC reference, and how the VI posterior sds compare.

Variational inference in AI, robotics and aerospace

  • Variational autoencoders and deep generative models maximize the ELBO with amortized encoders (Kingma and Welling, 2014; Rezende, Mohamed and Wierstra, 2014). World models for model-based RL use sequential VAEs (Hafner et al., 2019).
  • Bayesian neural networks: Bayes by Backprop (Blundell et al., 2015) and MC dropout (Gal and Ghahramani, 2016) give approximate weight uncertainty for perception and control.
  • Variational filters and smoothers: variational Bayes Kalman filters that adapt unknown noise covariances online (Särkkä and Nummenmaa, 2009).
  • Topic and mixture models at scale: stochastic VI over millions of documents or log lines (Hoffman et al., 2013).
  • Control as inference: maximum-entropy RL and MPPI can be derived as variational inference on trajectories (Levine, 2018).

References

  1. M. Jordan, Z. Ghahramani, T. Jaakkola, L. Saul (1999). “An introduction to variational methods for graphical models.” Machine Learning, 37.
  2. D. Blei, A. Kucukelbir, J. McAuliffe (2017). “Variational inference: a review for statisticians.” JASA, 112(518).
  3. M. Hoffman, D. Blei, C. Wang, J. Paisley (2013). “Stochastic variational inference.” JMLR, 14.
  4. D. Kingma, M. Welling (2014). “Auto-encoding variational Bayes.” ICLR.
  5. R. Ranganath, S. Gerrish, D. Blei (2014). “Black box variational inference.” AISTATS.
  6. T. Minka (2001). “Expectation propagation for approximate Bayesian inference.” UAI.
  7. Y. Yao, A. Vehtari, D. Simpson, A. Gelman (2018). “Yes, but did it work? Evaluating variational inference.” ICML.
  8. C. Blundell, J. Cornebise, K. Kavukcuoglu, D. Wierstra (2015). “Weight uncertainty in neural networks.” ICML.
  9. S. Särkkä, A. Nummenmaa (2009). “Recursive noise adaptive Kalman filtering by variational Bayesian approximations.” IEEE Transactions on Automatic Control, 54(3).
  10. S. Levine (2018). “Reinforcement learning and control as probabilistic inference: tutorial and review.” arXiv:1805.00909.