- Introduced by
- Kucukelbir, Tran, Ranganath, Gelman, Blei
- Year
- 2017
- Type
- Variational inference algorithm
What does it do?
Automatic Differentiation Variational Inference (ADVI) is a black-box algorithm for approximate Bayesian posterior inference, introduced by Kucukelbir, Tran, Ranganath, Gelman, and Blei (2017, JMLR).
Given a probabilistic model with a differentiable log-joint density, it transforms constrained latent variables to unconstrained real space, fits a Gaussian mean-field variational family by maximising the evidence lower bound (ELBO) with stochastic gradient ascent, and returns an approximate posterior without model-specific derivations. Stan provides it as its built-in variational method, and PyMC also offers it.
The intuition behind the method
Classical variational inference requires a practitioner to choose a variational family and then manually derive the coordinate-update equations for each new model, a process that is error-prone and must be repeated from scratch for every model. ADVI eliminates this manual step. It notices that every constrained latent variable (a standard deviation bounded below by zero, a probability bounded between 0 and 1, etc.) can be mapped to the real line with a simple invertible transformation such as a log or logit. Once all variables live on the real line, a single Gaussian mean-field family is placed over them, and automatic differentiation computes the gradient of the ELBO with respect to the variational parameters. Stochastic gradient ascent then moves those parameters uphill until convergence, delivering an approximate posterior mean and variance for every latent variable on the transformed (unconstrained) scale. The practitioner writes only the model; ADVI takes care of the rest.
History
Classical variational inference required model-specific coordinate-update derivations, which limited its use. Black-box variational methods, which estimate gradients by Monte Carlo sampling and so reduce the need for manual derivations, were subsequently developed. Kucukelbir and colleagues (2017) combined the reparameterisation gradient estimator, automatic differentiation, and automatic constraint-handling transformations into a single algorithm that requires only the log-joint density, and the approach was adopted as Stan's built-in variational method. An arXiv preprint with the same title and authors appeared in 2016; the JMLR article of 2017 is the version to cite. Later work has extended the variational family beyond mean-field Gaussians, for example with normalising flows and full-rank Gaussians, to recover posterior correlations.
When to use it
ADVI is appropriate when the posterior cannot be sampled with MCMC in reasonable time — typically because the model has many parameters, the data are large, or fast approximate inference is needed for iterative model development. It assumes all latent variables are continuous and that the log-joint density is differentiable; discrete latent variables require marginalisation or a separate approach. It is well-suited to hierarchical models, latent Gaussian models, and any model already implemented in Stan or PyMC. When exact or highly accurate posterior uncertainty is required, full MCMC (e.g. NUTS) should be preferred, as the mean-field Gaussian family can underestimate posterior variance and miss multimodal or strongly correlated posteriors.
Limitations and considerations
- The mean-field Gaussian variational family cannot capture posterior correlations between parameters; this leads to underestimated posterior variance in models with strong parameter dependencies. Richer families, such as full-rank Gaussians or normalising flows, can represent correlations at additional computational cost (see history).
- Convergence is to a local optimum of the ELBO, not the global one; results can depend on initialisation.
- Does not apply to models with discrete latent variables unless those variables are analytically marginalised.
- Does not come with an MCMC-style R-hat check that certifies the approximation is close to the true posterior; the ELBO is a lower bound, but its absolute value is not directly interpretable as a measure of fit.
- The Gaussian approximation in transformed space may be a poor fit when the true posterior is heavy-tailed, multimodal, or highly skewed.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Requires no model-specific derivations beyond the log-joint density, because gradients are obtained by automatic differentiation of that density.
- The stochastic-gradient formulation permits minibatch gradient estimates in implementations that support them, which can reduce the cost of each update on large datasets.
- Often much faster than MCMC for complex hierarchical models, which makes it practical for iterative model development.
- Available as Stan's built-in variational method and in PyMC, so it can be run without custom inference code.
- The transformation step applies the same Gaussian family to constrained and unconstrained parameters alike.
Applications
- Rapid prototyping and model checking during hierarchical Bayesian model development before committing to full MCMC.
- Large-scale Bayesian inference where MCMC run times are prohibitive for the size of the model or dataset.
- Latent Gaussian and latent Gaussian process models, where ADVI can give a computationally tractable approximate posterior; compare its means and standard deviations with a short MCMC run before relying on them.
- Approximate Bayesian inference in Stan-based models using the built-in variational algorithm (method=variational).
- Sensitivity analysis and prior exploration across many candidate priors using fast approximate posteriors.
How it works
How it works
The objective is the Evidence Lower Bound (ELBO), which equals the expected log joint density of data and parameters under the variational distribution minus the entropy of that distribution. Maximising the ELBO is equivalent to minimising the Kullback–Leibler divergence from the variational approximation to the true posterior. The ELBO is intractable in general because the expectation cannot be evaluated analytically, but it can be estimated by Monte Carlo sampling.
Formula
\mathcal{L}(\phi) = \mathbb{E}_{q(\theta;\phi)}[\log p(x,\theta)] - \mathbb{E}_{q(\theta;\phi)}[\log q(\theta;\phi)]Every constrained parameter θ is mapped bijectively to an unconstrained counterpart ζ = T(θ) ∈ ℝ, for example by a log transform for a positive parameter. The density of the transformed variable includes the log-absolute Jacobian determinant term shown in the formula, which preserves the probability mass of the original model. After transformation all latent variables are real-valued, so the same variational family applies across model types.
Formula
\zeta = T(\theta) \in \mathbb{R}^K, \quad \log q(\theta;\phi) = \log q(\zeta;\phi) + \log\left|\det J_T(\theta)\right|ADVI places an independent Gaussian distribution over each unconstrained variable ζ_k. The variational parameters are the mean μ_k and the standard deviation σ_k of each dimension, collected into φ = (μ, σ) as in the mechanics formula. Because the family factorises across dimensions, it cannot represent correlations between parameters, which is the main source of the underestimated posterior spread discussed under limitations. Its parameters are directly readable as approximate posterior means and standard deviations in the transformed space. Summaries reported on the scale of a constrained parameter are obtained by drawing from q(ζ; φ), applying the inverse transform T^{-1}, and summarising the transformed draws, because μ and σ describe ζ rather than θ.
Formula
q(\zeta;\phi) = \prod_{k=1}^{K} \mathcal{N}(\zeta_k;\mu_k,\sigma_k^2), \quad \phi = (\mu,\sigma)The gradient of the ELBO with respect to φ is computed via the reparameterisation trick: each Monte Carlo sample from q(ζ; φ) is written as ζ = μ + σ ⊙ η where η ~ N(0, I). This re-expression makes the sampling path differentiable with respect to φ, so the gradient can be evaluated by automatic differentiation through the log-joint of the model. Averaging over a small number of Monte Carlo samples (often one) gives a noisy but unbiased gradient estimate.
Formula
\nabla_\phi \mathcal{L}(\phi) = \mathbb{E}_{\mathcal{N}(\eta;0,I)}\left[\nabla_\phi \log p(x, T^{-1}(\mu + \sigma \odot \eta)) + \nabla_\phi \log\left|\det J_{T^{-1}}\right|\right]The noisy gradient estimate is passed to an adaptive stochastic gradient update with per-dimension step sizes ρ^(t). These step sizes are typically scaled by the accumulated gradient magnitudes, which damps oscillation and helps the iterations settle. The variational parameters are updated iteratively; convergence is monitored by tracking the ELBO or its rate of change over iterations.
Formula
\phi^{(t+1)} = \phi^{(t)} + \rho^{(t)} \odot \hat{\nabla}_\phi \mathcal{L}(\phi^{(t)})Common pitfalls
- Treating the variational posterior variance as an accurate measure of uncertainty without comparing to MCMC on a simpler version of the model.
- Stopping optimisation too early because the ELBO appears flat, when the approximation has not yet converged.
- Applying ADVI to a model containing discrete latent variables without marginalising them out first.
- Using the approximate posterior for critical decisions without acknowledging that mean-field ADVI can systematically underestimate posterior spread.
- Ignoring the sensitivity of results to the learning-rate schedule; poorly tuned step sizes can prevent convergence or cause oscillation.
Frequently asked questions
How does ADVI differ from standard (coordinate-ascent) variational inference?
Coordinate-ascent variational inference (CAVI) requires manually deriving closed-form update equations for each parameter conditional on the others, which is practical for conditionally conjugate exponential-family models. ADVI replaces all of this with a single stochastic gradient step computed by automatic differentiation, making it applicable to any differentiable probabilistic model without model-specific derivations.
Why does ADVI transform parameters to unconstrained space?
The Gaussian variational family is defined on all of ℝ, but many model parameters are constrained (e.g. a variance must be positive). By applying a bijection such as the log transform, ADVI maps each constrained parameter to the real line, where a Gaussian approximation is valid. The Jacobian of the transformation corrects the density so that the approximation remains probabilistically consistent.
Can I trust ADVI's posterior uncertainty estimates?
The mean-field Gaussian family approximates each parameter independently, so posterior correlations between parameters are ignored. This typically causes ADVI to underestimate posterior variance, sometimes substantially. For critical inferences, results should be validated against MCMC on a smaller dataset or a simplified model version.
When should I prefer MCMC (NUTS) over ADVI?
Prefer NUTS when exact posterior uncertainty is important, when the model has strong parameter correlations, when the posterior may be multimodal, or when the dataset is small enough that MCMC finishes quickly. ADVI is the practical choice when iteration speed matters (e.g. exploring many models) or when MCMC mixing times are too long for a large or complex model.
Related methods
Sources
Kucukelbir, A., Tran, D., Ranganath, R., Gelman, A. & Blei, D. M. (2017). Automatic differentiation variational inference. Journal of Machine Learning Research, 18(14), 1–45.
Open publication referenceKucukelbir, A., Tran, D., Ranganath, R., Gelman, A. & Blei, D. M. (2016). Automatic differentiation variational inference. arXiv:1603.00788.
Open publication referenceGelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A. & Rubin, D. B. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.
ISBN: 978-1439840955