Stan: A Probabilistic Programming Language

Bob Carpenter, Andrew Gelman, Matthew D. Hoffman, Daniel C. Lee, Ben Goodrich, Michael Betancourt, Marcus A. Brubaker, Jiqiang Guo, Peter Li, Allen Riddell

Journal of Statistical Software Publication Header

Abstract

Stan is a probabilistic programming language for specifying statistical models. A Stan program imperatively defines a log probability function over parameters conditioned on specified data and constants. As of version 2.14.0, Stan provides full Bayesian inference for continuous-variable models through Markov chain Monte Carlo methods such as the No-U-Turn sampler, an adaptive form of Hamiltonian Monte Carlo sampling. Penalized maximum likelihood estimates are calculated using optimization methods such as the limited memory Broyden-Fletcher-Goldfarb-Shanno algorithm. Stan is also a platform for computing log densities and their gradients and Hessians, which can be used in alternative algorithms such as variational Bayes, expectation propagation, and marginal inference using approximate integration. Stan can be called from the command line using the cmdstan package, through R using the rstan package, and through Python using the pystan package. All three interfaces support sampling and optimization-based inference with diagnostics and posterior analysis.

Authors and Affiliations

The Stan Development Team and their respective institutions. <i>Stan</i>: A Probabilistic Programming Language, page 1

The Stan Development Team and their respective institutions.

Introduction

For decades, Bayesian modeling relied on tools like BUGS and JAGS, which use declarative graphical models and sampling techniques like Gibbs or Metropolis-Hastings. While revolutionary, these methods often struggle with "random walk" behavior, becoming painfully slow or failing to converge as models grow in complexity or dimensionality.

Stan was introduced to bridge the gap between flexible model specification and efficient computation. It addresses the need for a language that is both expressive enough for modern research and robust enough to handle the geometry of high-dimensional posterior distributions without requiring manual tuning from the user.

Methods: The Architecture of Stan

Stan’s power comes from several key architectural choices:

  • Imperative Language: Unlike its predecessors, Stan uses an imperative syntax similar to C++ or Java. This allows for local variables, loops, and conditional logic, making it more natural for programmers to specify complex data transformations.
  • Hamiltonian Monte Carlo (HMC): Stan uses HMC, which leverages the physical analogy of a puck sliding on a frictionless surface to explore the probability space. This allows the sampler to take much larger, more directed steps than traditional MCMC.
  • The No-U-Turn Sampler (NUTS): A major innovation in Stan is NUTS, an adaptive version of HMC that automatically determines how long to run each "leapfrog" step. This eliminates the need for users to manually tune the most difficult parameters of the algorithm.
  • Automatic Differentiation: Under the hood, Stan translates its high-level code into C++ and uses a custom library for reverse-mode automatic differentiation. This allows Stan to calculate the exact gradients of the log-posterior, which are essential for HMC and gradient-based optimization.
  • Modular Blocks: Models are organized into clear blocks: data (inputs), parameters (what you want to estimate), model (the likelihood and priors), and generated quantities (for predictions and diagnostics).

Illustrative Examples

The paper focuses on demonstrating Stan's functionality through illustrative examples:

  • Bernoulli Model: A simple coin-flipping example used to introduce the basic syntax and the workflow of compiling and running a model.
  • Hierarchical Binomial Model: A more complex example involving tumor incidence in rats across multiple trials. This demonstrates Stan’s ability to handle hierarchical (multi-level) structures, hyperpriors, and the calculation of posterior predictive quantities.

Key Insights: Why Stan Works

  • Gradients are Everything: The "secret sauce" of Stan is its ability to automatically compute gradients. By knowing the "slope" of the probability landscape, Stan can navigate complex distributions far more efficiently than "blind" samplers.
  • Constraints and Transformations: Users can declare parameters with physical constraints (e.g., a variance must be positive). Stan automatically transforms these to an unconstrained space for efficient sampling and then transforms them back for the final output.
  • Statically Typed for Speed: By requiring variable types to be declared at compile-time, Stan can generate highly optimized C++ code, leading to significant execution speedups over interpreted languages.
  • Broad Ecosystem: Stan integrates directly into the workflows of data scientists through interfaces like rstan (R) and pystan (Python).

Results and Diagnostics

Stan provides a robust, "it just works" experience for a wide range of models. Key quantitative diagnostics provided by the tool include:

  • Effective Sample Size ($N_{eff}$): Stan often produces a much higher number of independent samples per second than BUGS or JAGS, especially in high dimensions.
  • R-hat ($\hat{R}$): A convergence diagnostic that ensures multiple chains have reached the same stationary distribution.
  • Optimization: Beyond Bayesian sampling, Stan’s L-BFGS implementation provides fast penalized maximum likelihood estimates (MLE) and maximum a posteriori (MAP) estimates.

Limitations and Future Work

  • Continuous Parameters Only: Stan’s current HMC/NUTS engine requires gradients, meaning it cannot directly sample discrete parameters. These must be mathematically marginalized out by the user.
  • Compilation Overhead: Because Stan translates code to C++ and compiles it, there is a "wait time" (often 20-60 seconds) before a model starts running.
  • Future Directions: Ongoing work includes Variational Bayes for faster approximate inference and expanding the library of built-in functions to support specialized statistical distributions and ODE solvers.

Summary updated

BibTeX
@article{carpenter2017i,
  author = {Bob Carpenter and Andrew Gelman and Matthew D. Hoffman and Daniel C. Lee and Ben Goodrich and Michael Betancourt and Marcus A. Brubaker and Jiqiang Guo and Peter Li and Allen Riddell},
  title = {{<i>Stan</i>: A Probabilistic Programming Language}},
  journal = {Journal of Statistical Software},
  year = {2017},
  doi = {10.18637/jss.v076.i01},
}
Figure