Variational Inference - Overview
Summary
Variational inference (VI) approximates an intractable posterior by optimization rather than sampling: posit a family of densities and find the member closest to the posterior in Kullback-Leibler divergence, (Blei, Kucukelbir & McAuliffe 2017, Eq. 1). Because the KL involves the unknown evidence , one instead maximizes the evidence lower bound (ELBO). The classical recipe is the mean-field family with coordinate ascent (CAVI); the modern recipe is Monte Carlo gradients of the ELBO, which yields ADVI in Stan/PyMC and, with an amortized neural encoder, the variational autoencoder. VI is fast and scalable but comes with few guarantees and systematically distorted uncertainty, so it must be diagnosed, not trusted.
Overview
Blei et al. frame VI and MCMC as “different approaches to solving the same problem” (Sec. 1, p. 3). MCMC constructs an ergodic Markov chain whose stationary distribution is the posterior and approximates the posterior with samples; VI solves an optimization problem and approximates the posterior with the optimizer. The trade is explicit:
| MCMC | Variational inference | |
|---|---|---|
| Output | (Asymptotically exact) samples | Best member of a family |
| Guarantee | Converges to the target as chain length | “Can only find a density close to the target” (p. 3); even the global optimum is misspecified if |
| Cost | Heavier; hard to subsample data | Lighter; inherits stochastic and distributed optimization |
| Known bias | Monte Carlo error only | ”Generally underestimates the variance of the posterior density; this is a consequence of its objective function” (p. 3) |
| Suited to | Small/expensive data, a trusted model, precise inference | Large data, rapid exploration of many models |
Blei et al. add that dataset size is not the only consideration; posterior geometry matters too. For mixture models with label-switching multimodality, VI may outperform a general-purpose sampler like HMC even on small data, because it commits to one mode (p. 3; compare Monsters and Mixtures).
The problem has three moving parts, and each note in this cluster isolates one:
- The objective. Reverse KL, its equivalence to maximizing the ELBO, and why this direction of KL is mode-seeking and variance-shrinking. See The ELBO and KL Divergence Minimization.
- The family . Fully factorized mean-field; mean-field or full-rank Gaussians in unconstrained space (ADVI); amortized conditional Gaussians parameterized by a neural network (VAE); and invertible transformations of a simple base density (normalizing flows).
- The optimizer. Closed-form coordinate ascent for conditionally conjugate models; natural-gradient stochastic VI for massive data; score-function (“black-box”) and reparameterization gradients for arbitrary differentiable models. See Stochastic and Black-Box Variational Inference.
A fourth part, added by Yao, Vehtari, Simpson & Gelman (2018), is verification: “While it’s always possible to compute a variational approximation to a posterior distribution, it can be difficult to discover problems with this approximation” (abstract). See Diagnosing Variational Inference (PSIS k-hat and VSBC).
Main Content
The variational inference problem ^def-vi-problem
Let be observations and latent variables (in a Bayesian model, all unknowns, including parameters) with joint density . The posterior is where the evidence is typically unavailable in closed form or exponential-time to compute (Blei et al., Eqs. 2-3). VI specifies a family of densities over and solves
“The complexity of the family determines the complexity of this optimization” (Sec. 2.2).
Why the evidence is intractable: Bayesian mixture of Gaussians ^ex-gmm-evidence
With unit-variance components, , and , the evidence is
Each appears in all factors, so the integral does not factor; rewriting it as a sum over assignment configurations gives conjugate integrals (Blei et al., Eqs. 7-9). Exponential in , hence approximate inference.
Four generations of VI ^def-generations
- Model-specific mean-field VI (1990s-2000s): hand-derived closed-form coordinate updates for conditionally conjugate exponential-family models. CAVI.
- Stochastic VI (Hoffman et al. 2013): noisy natural gradients from data subsamples; scales CAVI to millions of documents.
- Black-box / automatic VI (Ranganath et al. 2014; Kingma & Welling 2013; Kucukelbir et al. 2017): Monte Carlo gradients of the ELBO requiring only (and its gradient). ADVI makes VI a button in Stan.
- Amortized and flow-based VI: a neural network outputs the variational parameters for each datum (VAE), and the family is enriched by normalizing flows.
What VI gets wrong, and why it matters to this vault. SBC Case Studies documents ADVI “drastically underestimat[ing] the posterior for the slope ” on a simple linear regression. The mechanism is fully explained by two facts developed here: (i) the mean-field Gaussian family cannot represent posterior correlation (here between intercept and slope), and (ii) reverse KL penalizes for placing mass where has little, so the best factorized shrinks to fit inside the correlated posterior rather than covering it. For a Gaussian target with precision matrix the optimal mean-field variance is ; see the worked example in the mean-field note. Kucukelbir et al. measure exactly this: true marginal variances versus mean-field ADVI (their Fig. 4).
VI can also be over-dispersed. In the eight-schools hierarchical model, Yao et al. find centered-parameterization ADVI over-estimates (the posterior mode is at but “the entropy penalization keeps VI estimation away from it”) and consequently over-estimates the posterior variance of every (Sec. 4.3). The honest summary is that VI uncertainty is unreliable in an unknown direction unless checked.
Theory is thin. Blei et al. Sec. 5.2 surveys what is known: results are model-by-model, mostly showing that VI posterior means are consistent point estimates (Bayesian linear model, Poisson mixed models, stochastic block models, Gaussian mixtures), while Wang & Titterington (2005) show the asymptotic variational posterior covariance is “too small”, differing from the inverse Fisher information by a positive-definite matrix.
Relevance to marketing measurement and applied work ^ex-applied-relevance
- Media mix models. A Bayesian MMM has strongly correlated posteriors: adstock decay vs. saturation parameters, channel coefficients for collinear spend, and baseline/trend vs. media effects. These are precisely the correlations a mean-field family deletes. ADVI (
pm.fitin PyMC,variationalin Stan) will return ROI intervals that look tight and are wrong. Use VI for fast iteration on model structure and for initializing NUTS; report intervals from MCMC, or from VI only after a passing check.- Hierarchical geo models. Geo-level effects with a shared scale produce the funnel geometry of Hierarchical Models; Yao et al. show ADVI fails the joint diagnostic () on the centered eight-schools model and only becomes usable () after non-centering.
- Scale. Where VI is genuinely the right tool: user-level or store-level models with millions of rows where minibatch ADVI/SVI is the only thing that fits in time, and the deliverable is a point prediction rather than a calibrated interval. Kucukelbir et al. recommend mean-field for prediction, full-rank when “posterior variances and covariances” are of interest (Sec. 3.1).
- Bayesian experimental design. The variational EIG estimators reuse the same machinery (an amortized trained by stochastic gradients on a bound) to estimate expected information gain for geo-experiment design.
- ABMs and simulators. When the likelihood itself is unavailable, amortized VI ideas reappear as neural posterior estimation; see Neural Simulation-Based Inference - Overview and Simulation-Based and Amortized Inference.
Examples
A minimal decision procedure, synthesizing the recommendations in the four papers:
- Fit with mean-field ADVI while the model is still changing. Treat the output as a rough posterior and a source of initial values (the use the Bayesian Workflow book endorses).
- Tighten the convergence tolerance. Yao et al. show that on a linear regression the default relative-ELBO tolerance gives while gives (Sec. 4.1): the “failure” was premature stopping.
- Draw samples from , compute importance ratios , and fit the Pareto shape . If , use PSIS-reweighted estimates; otherwise reparameterize, go full-rank, or switch to MCMC.
- If only point estimates are needed, check them with VSBC (a VI-specific variant of SBC).
Connections
- The ELBO and KL Divergence Minimization - the objective and what reverse KL does to .
- Mean-Field Family and Coordinate Ascent VI (CAVI) - the classical family and algorithm; the variance-underestimation result.
- Stochastic and Black-Box Variational Inference - SVI, the score-function estimator, variance reduction.
- Automatic Differentiation Variational Inference (ADVI) - the Stan/PyMC algorithm the vault’s case studies actually run.
- Reparameterization Trick and Variational Autoencoders - pathwise gradients, amortization, the bridge to deep generative models.
- Normalizing Flows for Variational Inference - enriching beyond Gaussians.
- Diagnosing Variational Inference (PSIS k-hat and VSBC) - how to tell whether it worked.
- Variational Inference and Pathfinder - the Bayesian Workflow book’s assessment of VI and the Pathfinder alternative.
- Approximation Methods - BDA3 Ch. 13: modal/Laplace approximation, EM, variational Bayes and expectation propagation.
- Approximate Algorithms and Approximate Models - the workflow-level framing of when an approximate computation is acceptable.
See Also
- SBC Case Studies - the ADVI-on-linear-regression failure that motivated this cluster.
- Efficient MCMC and HMC and Stan in Practice - the exact-but-slower alternative VI is measured against.
- Approximations Based on Joint and Conditional Posterior Modes - Laplace-type approximations, which Kucukelbir et al. stress are not equivalent to Gaussian VI.
- Variational Posterior Estimator (Barber-Agakov) and Variational Marginal Estimator - variational bounds used for expected information gain.
- Normalizing Flows as Conditional Density Estimators - the flow architecture used as an amortized posterior in simulation-based inference.
- Factor Analysis and PPCA - the linear-Gaussian latent variable model that the VAE generalizes.
- Hilbert Space Gaussian Processes - a model-side approximation that often removes the need for an inference-side one.