Introduction
In the landscape of Probabilistic Programming Languages (PPLs), a significant divide exists between declarative systems like Stan and generative systems like Pyro or NumPyro. Stan is the gold standard for statistical modeling, favored for its high-level syntax and robust Hamiltonian Monte Carlo (HMC) inference. However, generative PPLs offer greater flexibility, particularly in their integration with deep learning frameworks like PyTorch and JAX.
This paper introduces a formal compilation scheme to bridge these worlds, allowing Stan models to be translated into generative PPLs. This work culminates in DeepStan, an extension that enables users to integrate deep neural networks and custom variational inference guides directly into familiar Stan code, often achieving significant performance gains.
The Challenge of Non-Generative Features
A simple 1:1 translation from Stan to a generative framework is impossible because Stan allows several "non-generative" features that do not fit the standard stochastic procedure model. These include:
- Left expressions: Sampling into a function of a variable (e.g.,
sum(x) ~ normal(...)). - Multiple updates: Updating the log-density of the same variable multiple times.
- Implicit priors: Variables that exist without an explicit distribution assigned to them.
As shown in the table above, these features are prevalent in real-world Stan models, with implicit priors appearing in 58% of the surveyed codebase. A robust compiler must handle these cases while maintaining mathematical rigor.
The Comprehensive Compilation Scheme
To reconcile Stan’s log-density approach with the generative "sample-then-factor" approach, the researchers developed a Comprehensive Compilation Scheme. The core innovation—the "Aha!" moment—is to initialize every parameter by sampling it from a flat, uniform distribution (or an "improper uniform" for unbounded variables).
In this scheme, every sampling statement (~) in the original Stan code is converted into an observe statement (a likelihood factor). The authors provided a formal proof showing that the measure produced by this compiled generative program is proportional to the original Stan model's density, ensuring correctness.
Empirical Evaluation and Speed
The compiler was evaluated against 531 real-world models from the Stan example set and 98 models from PosteriorDB. The results were compelling:
- Generality: The compiler successfully handled 522 out of 531 models.
- Accuracy: On the
PosteriorDBbenchmarks, the compiled models produced posterior distributions that matched reference samples perfectly. - Speed: By targeting the NumPyro backend (which utilizes JAX and XLA compilation), the compiled models achieved a 2.3x geometric mean speedup over the original Stan engine. In specific cases, such as the
nesmodels, speedups exceeded 10x-20x.
DeepStan: Extending Stan for Deep Learning
Beyond mere translation, the authors introduced DeepStan, which adds three new blocks to the Stan language: networks (for neural network signatures), guide parameters (for learnable VI parameters), and guide (for defining variational distributions). This allows statisticians to use state-of-the-art deep learning tools without leaving the Stan ecosystem. The authors successfully demonstrated this by implementing Variational Auto-Encoders (VAEs) and Bayesian Neural Networks that performed as well as hand-written Pyro code.
Conclusion
By proving that Stan’s expressive power is a subset of generative PPLs, this research opens new avenues for statistical modeling. It allows the massive library of existing Stan models to benefit from the performance of JAX and the flexibility of deep learning frameworks, effectively lowering the barrier between traditional statistics and modern machine learning.
Summary updated