Automatic Guide Generation for Stan via NumPyro

Guillaume Baudart, Louis MandelarXiv 2021

TL;DR

Researchers have developed a compiler that translates Stan models into NumPyro, giving Stan users access to a much wider variety of Variational Inference (VI) algorithms. By leveraging NumPyro’s "autoguides"—including advanced normalizing flows—users can now approximate complex posterior distributions that traditional Stan tools often struggle to handle.

What Problem Does This Paper Address?

In the world of probabilistic programming, Stan is a titan, beloved for its high-quality Hamiltonian Monte Carlo (HMC) sampler. However, HMC can be computationally expensive. When speed is a priority, researchers turn to Variational Inference (VI), which treats inference as an optimization problem by finding a simpler "guide" distribution that approximates the true posterior.

The problem? Stan’s native VI tool, ADVI, is limited. It primarily uses simple Gaussian approximations (mean-field or full-rank), which often fail to capture the nuances of complex, multi-modal distributions. Meanwhile, generative languages like Pyro and NumPyro have developed a "zoo" of sophisticated "autoguides" that can automatically synthesize complex approximations. Until now, these two worlds were largely separated.

Methods

The authors bridge this gap using a recently developed compiler backend for stanc3 that translates Stan code directly into NumPyro.

Once a model is compiled into NumPyro, the authors utilize Stochastic Variational Inference (SVI). The core contribution is the evaluation of eight different NumPyro autoguides—heuristics that automatically generate the approximating distribution (the "guide") based on the model's structure. These include:

  • Normalizing Flows (AutoBNAFNormal, AutoIAFNormal): These use neural networks to "warp" simple distributions into complex shapes, allowing them to capture multi-modality.
  • MAP Estimates (AutoDelta): Finding the single most likely point (Maximum A Posteriori).
  • Laplace Approximation: A quadratic approximation around the MAP.
  • Low-Rank Approximations: A middle ground between simple mean-field and expensive full-rank Gaussians.

Datasets

To prove the effectiveness of this approach, the authors used PosteriorDB, a standard benchmark database for probabilistic programs.

  • Scope: The database contains 49 pairs of models and datasets, complete with "gold standard" reference posterior samples.
  • Coverage: The authors tested 43 of these models. Six were excluded because the current compiler does not yet support certain complex Stan library functions, such as ODE solvers.

Key Insights and Ideas

  • The Power of Compilers: This paper demonstrates that compiling one probabilistic language into another isn't just a technical exercise—it’s a way to "teleport" features. Stan gets better VI, and NumPyro developers get a massive library of real-world Stan models to stress-test their algorithms.
  • Normalizing Flows are the MVP: The results highlight that "Normalizing Flows" (like BNAF and IAF) are significantly more flexible than standard Gaussian approximations. They were the only methods capable of identifying multiple modes in challenging "mixture" models where Stan’s native NUTS and ADVI struggled.
  • No "Silver Bullet": The "zoo" approach is valuable because different models require different trade-offs. Some autoguides are fast but crude (Delta), while others are accurate but complex (BNAF). Having a menu of options allows researchers to pick the right tool for the job.

Key Results

The authors compared the NumPyro autoguides against Stan’s native ADVI (Mean-Field and Full-Rank) using a relative error metric.

  • Accuracy: The normalizing flow autoguides (AutoBNAFNormal and AutoIAFNormal) consistently outperformed other methods.
  • Success Rates: Using a threshold of 0.3 relative error (a standard Stan regression test metric), AutoBNAFNormal succeeded on 25 models, whereas Stan’s native Mean-Field ADVI only succeeded on 4.
  • Robustness: While complex guides sometimes failed to converge, simpler guides like AutoDelta and AutoLaplaceApproximation ran on every single model without runtime errors, providing a reliable (if sometimes less accurate) baseline.

Limitations and Future Work

While the results are promising, there are a few hurdles remaining:

  • Library Support: The compiler is not yet "feature-complete." It lacks support for some of Stan's more specialized functions (like the aforementioned ODE solvers), which limited the evaluation.
  • Optimization Tuning: In this study, NumPyro was run for a fixed 100,000 iterations, whereas Stan’s ADVI uses an adaptive optimization sequence. Future work could involve making the NumPyro inference more "plug-and-play" by implementing similar adaptive logic.
  • Integration: The authors suggest that this work paves the way for these advanced autoguides to eventually be integrated more natively into the Stan ecosystem, providing users the best of both worlds.

Summary updated

BibTeX
@misc{baudart2021automatic,
  author = {Guillaume Baudart and Louis Mandel},
  title = {{Automatic Guide Generation for Stan via NumPyro}},
  year = {2021},
  eprint = {2110.11790},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.11790},
}
Figure