ADVI (Kucukelbir, Tran, Ranganath, Gelman & Blei 2017) turns VI into a generic algorithm for any differentiable probability model: the user writes the model (e.g. in Stan) and nothing else. The recipe has three ingredients: (1) automatically transform constrained latent variables to RK, adding a log-Jacobian term, so that one variational family serves every model; (2) posit a Gaussian in the unconstrained space, either mean-field or full-rank, which implies a non-Gaussian approximation in the original space; (3) standardize the Gaussian (the reparameterization trick) so the gradient moves inside the expectation, compute it by automatic differentiation with a single Monte Carlo draw, and run stochastic gradient ascent with an adaptive step size. It is fast and automatic; its accuracy is limited by the Gaussian family and, in mean-field form, by ignored posterior correlation.
Overview
Before ADVI, “each step requires expert thought and analysis in the service of a single algorithm for a single model” (Sec. 2.2): choose a family satisfying the support constraint, derive expectations, derive updates, implement, debug. ADVI removes every model-specific step for the class of models that Stan already supports for HMC, those with continuous latent variables and a gradient ∇θlogp(x,θ) on the support of the prior. Discrete latents must be marginalized out, exactly as for HMC in Stan. No conjugacy of any kind is assumed; the running example is a Poisson likelihood with a Weibull prior on the rate.
This is the algorithm behind Stan’s variational method and PyMC’s ADVI/FullRankADVI, and therefore the algorithm that SBC Case Studies finds miscalibrated on a simple linear regression.
Main Content
Differentiable probability model ^def-differentiable-model
A joint density p(x,θ) with continuous latent variables θ∈supp(p(θ))⊆RK whose log-joint gradient ∇θlogp(x,θ) exists on the support of the prior. Includes GLMs, mixtures and HMMs/topic models with discrete variables marginalized, state-space models, Gaussian processes, deep exponential families (Table 1). Excludes models where marginalization is intractable (Ising, sigmoid belief nets, untruncated Bayesian nonparametrics).
Step 1: transform to real coordinate space
Reverse KL requires supp(q)⊆supp(p(θ∣x)) (support constraint); ADVI additionally assumes the posterior support equals the prior support. Rather than pick a family per constraint type, define a one-to-one differentiable T:supp(p(θ))→RK and set ζ=T(θ). The transformed joint is
p(x,ζ)=p(x,T−1(ζ))detJT−1(ζ).
For a positive rate, T=log and the Jacobian factor is eζ; e.g. p(x,ζ)=Poisson(x∣eζ)Weibull(eζ;1.5,1)eζ (Sec. 2.3). Stan supplies the library of transforms (bounds, simplexes, ordered vectors, covariance and Cholesky factors), the same ones it uses for HMC.
Step 2: a Gaussian family in the unconstrained space
Mean-field and full-rank Gaussian families ^def-advi-families
Mean-field:q(ζ;ϕ)=N(ζ;μ,diag(σ2)) with ω=logσ, so ϕ=(μ,ω)∈R2K is unconstrained.
Full-rank:q(ζ;ϕ)=N(ζ;μ,LL⊤) with L lower-triangular (diagonal not constrained positive), so ϕ=(μ,L)∈RK+K(K+1)/2.
The implied density on the original space, q(T(θ);ϕ)∣detJT(θ)∣, is non-Gaussian and automatically respects the support (Sec. 2.4).
Two remarks from the paper. A Gaussian variational approximation “is not equivalent to the Laplace approximation”: Laplace expands around the MAP (Approximations Based on Joint and Conditional Posterior Modes); ADVI minimizes an average discrepancy. And full-rank is “a form of structured mean-field variational inference” whose off-diagonal terms “capture posterior correlations,” at O(K2) parameter cost.
Step 3: the objective, standardization and gradients
(Eq. 5). The optimization ϕ∗=argmaxϕL(ϕ) is now unconstrained. The Gaussian entropy is analytic: implemented once, reused for all models.
Automatic differentiation cannot differentiate an expectation whose measure depends on ϕ. Elliptical standardization fixes this: η=Sϕ(ζ)=diag(exp(ω))−1(ζ−μ) (mean-field) or L−1(ζ−μ) (full-rank), so η∼N(0,I) regardless of ϕ. The paper notes this is the same device “also known as… the ‘re-parameterization trick’ (Kingma and Welling, 2014)” (fn. 6); see Reparameterization Trick and Variational Autoencoders. With ζ=Sϕ−1(η) and θ=T−1(ζ):
and for full-rank the same bracket times η⊤ plus (L−1)⊤ (Eqs. 7-9). Everything inside the expectations is an autodiff call; the expectation is a Monte Carlo average over M draws and “in practice a single sample suffices.”
ADVI (Kucukelbir et al., Algorithm 1) ^alg-advi
Input: data x1:N, model p(x,θ).
Initialize μ(1)=0 and ω(1)=0 (mean-field) or L(1)=I (full-rank): a standard Gaussian in unconstrained space.
Choose the step-size scale η∈{0.01,0.1,1,10,100} by a short search on a data subset.
While the change in ELBO exceeds a threshold:
draw ηm∼N(0,I), m=1,…,M;
estimate ∇μL and ∇ωL (or ∇LL) by Monte Carlo;
compute step sizes ρ(i) and update μ←μ+diag(ρ(i))∇μL, similarly ω or L.
Returnμ∗,ω∗ (or L∗).
Step size (Eqs. 10-11): ρk(i)=η⋅i−1/2+ϵ⋅(τ+sk(i))−1 with sk(i)=α(gk(i))2+(1−α)sk(i−1), using ϵ=10−16, α=0.1, τ=1. The decaying factor satisfies Robbins-Monro; the last factor is RMSProp-like finite-memory curvature adaptation.
Cost:O(NMK) per iteration, or O(BMK) with minibatches of size B and the likelihood scaled by N/B.
Properties (Sec. 3)
Accuracy: what mean-field loses ^ex-advi-accuracy
Correlated 2-D Gaussian (1000 data points, analytic posterior): both variants recover the mean. Marginal variances: analytic (0.28,0.31), full-rank (0.28,0.31), mean-field (0.13,0.14) (Fig. 4). “ADVI minimizes the KL divergence from the approximation to the exact posterior; this leads to a systemic underestimation of marginal variances.”
Logistic regression (10 coefficients, 1000 points): posterior means agree with NUTS; mean-field “underestimates marginal posterior variances on most of the coefficients,” full-rank matches (Fig. 5).
Stochastic volatility (500 time steps, AR(1) log-volatility): here mean-field gets even the mean wrong, “particularly when the log volatility drifts far away from μ,” because neighbouring ht are strongly correlated; full-rank matches sampling, and its covariance matrix shows the banded structure that mean-field cannot (Figs. 6-7).
Recommendation: “Scientists interested in posterior variances and covariances should use the full-rank approximation… Scientists interested in prediction should initially rely on the mean-field approximation,” because “accurate posterior mean estimates dominate predictive accuracy; underestimating marginal variances matters less.”
The stochastic volatility case is the warning for time-series work: latent states in a media mix model with time-varying baselines or coefficients have exactly this local correlation structure.
Sensitivity to the transformation and the optimal T
The choice of T changes the implied family on the original space. For Gamma posteriors on R>0, T2(θ)=log(eθ−1) beats T1(θ)=logθ: KL(q∥p) is 1.6×10−2 vs 8.1×10−2 for Gamma(1,2), and 7.7×10−4 vs 8.5×10−3 for Gamma(10,10) (Table 2), because T2 is nearly linear for large θ and both Gamma and Gaussian are light-tailed. The optimal transformation is
T∗=Φ−1∘P(θ∣x),
posterior CDF followed by the standard-normal quantile function, under which a Gaussian is exact. But estimating P(θ∣x) “is just as hard as the original goal” (Sec. 3.3). Learning an approximation to T∗ is what normalizing flows do.
Gradient variance. The ADVI (pathwise) gradient has lower variance than the BBVI score-function gradient, with or without control variates, on both a univariate and a 100-dimensional model (Fig. 8); see Stochastic and Black-Box Variational Inference.
Speed (Sec. 4). Measured by held-out predictive likelihood against time: parity with NUTS on ARD linear regression (250 regressors, 10,000 points) and hierarchical logistic regression (145 regressors); “an order of magnitude” faster on non-negative matrix factorization of the Frey faces; on a 30-component Gaussian mixture over 250,000 images, minibatch ADVI converges in about two hours where “NUTS cannot handle such large datasets,” with minibatches below B=500 giving worse optima; clustering 1.7 million taxi trajectories after ADVI-fitted PPCA with an ARD prior selected an 11-dimensional subspace. All of these comparisons are predictive, the regime in which the paper itself says variance errors matter least.
Examples
Running and checking ADVI ^ex-advi-usage
# PyMC (sketch): mean-field, then full-rank, then draw for diagnosticswith model: mf = pm.fit(n=50_000, method="advi") # mean-field Gaussian fr = pm.fit(n=50_000, method="fullrank_advi") # full-rank Gaussian idata_vi = fr.sample(2_000)
Center and scale predictors, and non-center hierarchies. Mean-field can only be right if the posterior is nearly factorized in the unconstrained coordinates. The linear-regression calculation in the mean-field note shows an uncentered predictor alone can halve the slope’s posterior sd; hierarchical funnels need the non-centered form (see Computational Troubleshooting).
Tighten the tolerance. Yao et al. (2018) show the default relative-ELBO tolerance of 10−2 can stop far too early (k^=4.4 vs 0.61 at 10−5 on a 100-regressor linear model).
Open issues named by the authors (Sec. 5): sensitivity to T; first-order optimization only; initialization at a standard Gaussian and the finite search for η are heuristics; no discrete latents without falling back on the high-variance score-function estimator.