Automatic Variational Inference in Stan

Alp Kucukelbir, Rajesh Ranganath, Andrew Gelman, David M. Blei

Bayesian inference is a powerful tool for data analysis, but practitioners often face a stark trade-off between usability and scalability. Markov chain Monte Carlo (MCMC) methods, like the No-U-Turn Sampler (NUTS), are highly accurate and "automatic"—users simply write the model and the sampler does the rest. However, MCMC is notoriously slow on large datasets. Conversely, Variational Inference (VI) scales beautifully to "big data" by framing inference as an optimization problem. The catch? Traditional VI is highly manual, requiring researchers to spend days deriving model-specific equations and selecting approximating distributions.

This paper introduces Automatic Differentiation Variational Inference (ADVI) to bridge this gap, offering a method that is both automatic (like MCMC) and scalable (like VI) within the popular Stan probabilistic programming ecosystem.

The ADVI Approach

The core innovation of ADVI is its three-step automated recipe that eliminates the need for manual mathematical derivations:

  1. Automatic Transformation: Many model parameters have strict constraints (e.g., standard deviations must be strictly positive). ADVI automatically transforms these constrained variables into an unconstrained, real-coordinate space.
  2. The Variational Family: In this unconstrained space, ADVI posits a simple Gaussian distribution to approximate the posterior. When mapped back through the inverse transformation, this simple Gaussian corresponds to a complex, non-Gaussian approximation in the original model space.
  3. Stochastic Optimization: To find the optimal approximation, ADVI maximizes the Evidence Lower Bound (ELBO). It leverages Stan's automatic differentiation to calculate gradients and employs stochastic gradient ascent to handle large datasets via minibatches.

Crucially, the algorithm uses a "reparameterization trick" (elliptical standardization) to ensure gradients remain unbiased with low variance, stabilizing the optimization process. Because ADVI is built directly into Stan, users only need to provide the probabilistic model and the dataset.

Stan code and graphical model for a simple Poisson model

Experiments and Results

The authors evaluated ADVI against state-of-the-art MCMC methods across several benchmarks, including hierarchical generalized linear models, non-negative matrix factorization on the Frey Face dataset, and Gaussian Mixture Models (GMM) on the ImageCLEF dataset.

The results highlight ADVI's massive advantages in speed and scalability. On smaller models, ADVI achieved the same predictive accuracy as MCMC but was orders of magnitude faster. Furthermore, ADVI successfully processed a massive 250,000-image dataset in under two hours—a scale where traditional MCMC techniques simply cannot complete the analysis in a reasonable timeframe. ADVI also proved more robust than MCMC in models prone to "label switching," such as mixture models, where MCMC often struggles to converge.

ADVI vs NUTS performance on a 1000-image subset and a 250,000-image full dataset

Limitations and Future Directions

While powerful, ADVI has some limitations. The current implementation relies on a "mean-field" assumption, using a diagonal covariance Gaussian. This assumes all latent variables are independent in the transformed space, potentially ignoring important correlations.

Additionally, because ADVI relies on automatic differentiation, the model must be differentiable; discrete latent variables must be analytically marginalized out before applying the algorithm. Finally, as an optimization-based method, ADVI can get stuck in local optima, whereas MCMC theoretically explores the entire posterior given infinite time.

Future work may explore "full-rank" ADVI to capture variable correlations and further improve approximation quality, continuing to push the boundaries of accessible, scalable Bayesian inference.

Summary updated

BibTeX
@inproceedings{kucukelbir2015automatic,
  author = {Alp Kucukelbir and Rajesh Ranganath and Andrew Gelman and David M. Blei},
  title = {{Automatic Variational Inference in Stan}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2015},
  url = {https://openreview.net/forum?id=kG-nw63J2Q},
}
Figure