18
Stan: A Probabilistic Programming Language Development Team: Andrew Gelman, Bob Carpenter, Matt Hoffman, Daniel Lee, Ben Goodrich, Michael Betancourt, Marcus Brubaker, Jiqiang Guo, Peter Li, Allen Riddell Ecological Forecasting mc-stan.org

Daniel Lee STAN

Embed Size (px)

Citation preview

Page 1: Daniel Lee STAN

Stan: A ProbabilisticProgramming Language

Development Team:

Andrew Gelman, Bob Carpenter, Matt Hoffman,Daniel Lee, Ben Goodrich, Michael Betancourt,Marcus Brubaker, Jiqiang Guo, Peter Li, Allen Riddell

Ecological Forecasting mc-stan.org

1

Page 2: Daniel Lee STAN

Motivation

• Fit rich Bayesian statistical models

• The Process

1. Create a statistical model

2. Perform inference on the model

3. Evaluate

• Difficulty with models of interest in existing tools

2

Page 3: Daniel Lee STAN

Motivation (cont.)

• Usability– general purpose, clear modeling language, integration

• Scalability– model complexity, number of parameters, data size

• Efficiency– high effective sample sizes, fast iterations, low memory

• Robustness– model structure (i.e. posterior geometry), numerical routines

3

Page 4: Daniel Lee STAN

What is Stan?

• Statistical model specification language– high level, probabilistic programming language

– derived from BUGS

– user specifies statistical model

– easy to create statistical models

• 4 cross-platform users interfaces– CmdStan - command line

– RStan - R integration

– PyStan - Python integration

– MStan - Matlab integration (user contributed)

4

Page 5: Daniel Lee STAN

Inference• Hamiltonian Monte Carlo (HMC)

– sample parameters on unconstrained space→ transform + Jacobian adjustment

– gradients of the model wrt parameters→ automatic differentiation

– sensitive to tuning parameters → No-U-Turn Sampler

• No-U-Turn Sampler (NUTS)– warmup: estimates mass matrix and step size– sampling: adapts number of steps– maintains detailed balance

• Optimization– BFGS, Newton’s method

5

Page 6: Daniel Lee STAN

Differences

• language

– declarative→ imperative

– untyped→ strong static typing

– language is growing

• Gibbs sampling→ NUTS (or optimization)

• interpreted→ compiled C++

• restrictive licensing→ open-source, BSD license

6

Page 7: Daniel Lee STAN

User Group

• 4 Interfaces: CmdStan, RStan, PyStan, MStan

• 650+ on stan-users mailing list

• Actual number of users unknown– User manual: 2200+ downloads since 2/14

– PyStan: 1600+ downloads in the last month

– CmdStan / RStan / MStan: ?

• 50+ citations over 2 years– stats, astrophysics, political science

– genetics, medical informatics

– ecological forecasting: phycology, fishery

7

Page 8: Daniel Lee STAN

Stan to Scientists• Flexible probabilistic language, language still growing

• Focus on science: the modeling and assumptions– access to multiple algorithms (default is pretty good)– faster and less error prone than implementing from scratch– efficient implementation

• Lots of (free) modeling help on users list

• Responsive developers, continued support for Stan

• Not just for inference– fast forward sampling; lots of distributions– gradients for arbitrary functions

8

Page 9: Daniel Lee STAN

Stan to Software Workflow Developers• A Stan model is:

– imperative declaration of joint model;

specifies the log probability distribution

• Translated to templated C++, specifying:– log probability function (normalized and unnormalized)

– gradients

– transforms (includes Jacobian adjustment)

• Addiitonal C++ APIs– automatic differentiation with math library

– inference algorithms

– can write a new Stan wrapper

9

Page 10: Daniel Lee STAN

Stan’s Near Future (2014)

• L-BFGS optimization

• User specified functions

• Differential equations

• Approximate inference– maximum marginal likelihood

– expectation propagation

• More efficient automatic differentiation

• Refactoring, testing, adding distributions, etc.

10

Page 11: Daniel Lee STAN

Stan’s Future

• Riemann Manifold HMC– needs efficient implementation of Hessians

– needs rewrite of current auto-diff implementation

• Variational Bayesian Inference– non-conjugate

– black-box

• Stocastic Variational Bayes– needs stats / machine learning research

– data partitioning

11

Page 12: Daniel Lee STAN

Limitations

• no discrete parameters (can marginalize)

• no implicit missing data (code as parameters)

• not parallelized within chains

• language limited relative to black boxes (cf., emcee)

• limited data types and constraints

• C++ template code is complex for user extension

• sampling slow, nonscalable; optimization brittle or approx

12

Page 13: Daniel Lee STAN

How Stan Got its Name• “Stan” is not an acronym; Gelman mashed up

1. Eminem song about a stalker fan, and

2. Stanislaw Ulam (1909–1984), co-inventor ofMonte Carlo method (and hydrogen bomb).

Ulam holding the Fermiac, Enrico Fermi’s physical Monte Carlosimulator for random neutron diffusion

13

Page 14: Daniel Lee STAN

NUTS vs. Gibbs and MetropolisThe No-U-Turn Sampler

Figure 7: Samples generated by random-walk Metropolis, Gibbs sampling, and NUTS. The plots

compare 1,000 independent draws from a highly correlated 250-dimensional distribu-

tion (right) with 1,000,000 samples (thinned to 1,000 samples for display) generated by

random-walk Metropolis (left), 1,000,000 samples (thinned to 1,000 samples for display)

generated by Gibbs sampling (second from left), and 1,000 samples generated by NUTS

(second from right). Only the first two dimensions are shown here.

4.4 Comparing the Efficiency of HMC and NUTS

Figure 6 compares the efficiency of HMC (with various simulation lengths λ ≈ �L) andNUTS (which chooses simulation lengths automatically). The x-axis in each plot is thetarget δ used by the dual averaging algorithm from section 3.2 to automatically tune the stepsize �. The y-axis is the effective sample size (ESS) generated by each sampler, normalized bythe number of gradient evaluations used in generating the samples. HMC’s best performanceseems to occur around δ = 0.65, suggesting that this is indeed a reasonable default valuefor a variety of problems. NUTS’s best performance seems to occur around δ = 0.6, butdoes not seem to depend strongly on δ within the range δ ∈ [0.45, 0.65]. δ = 0.6 thereforeseems like a reasonable default value for NUTS.

On the two logistic regression problems NUTS is able to produce effectively indepen-dent samples about as efficiently as HMC can. On the multivariate normal and stochasticvolatility problems, NUTS with δ = 0.6 outperforms HMC’s best ESS by about a factor ofthree.

As expected, HMC’s performance degrades if an inappropriate simulation length is cho-sen. Across the four target distributions we tested, the best simulation lengths λ for HMCvaried by about a factor of 100, with the longest optimal λ being 17.62 (for the multivari-ate normal) and the shortest optimal λ being 0.17 (for the simple logistic regression). Inpractice, finding a good simulation length for HMC will usually require some number ofpreliminary runs. The results in Figure 6 suggest that NUTS can generate samples at leastas efficiently as HMC, even discounting the cost of any preliminary runs needed to tuneHMC’s simulation length.

25

• Two dimensions of highly correlated 250-dim distribution

• 1M samples from Metropolis, 1M from Gibbs (thin to 1K)

• 1K samples from NUTS, 1K independent draws

14

Page 15: Daniel Lee STAN

NUTS vs. Basic HMCHoffman and Gelman

Figure 6: Effective sample size (ESS) as a function of δ and (for HMC) simulation length�L for the multivariate normal, logistic regression, hierarchical logistic regression,and stochastic volatility models. Each point shows the ESS divided by the numberof gradient evaluations for a separate experiment; lines denote the average of thepoints’ y-values for a particular δ. Leftmost plots are NUTS’s performance, eachother plot shows HMC’s performance for a different setting of �L.

The trajectory length (measured in number of states visited) grows as the acceptancerate target δ grows, which is to be expected since a higher δ will lead to a smaller stepsize �, which in turn will mean that more leapfrog steps are necessary before the trajectorydoubles back on itself and satisfies equation 9.

24

• 250-D normal and logistic regression models

• Vertical axis is effective sample size per sample (bigger better)

• Left) NUTS; Right) HMC with increasing t = εL

15

Page 16: Daniel Lee STAN

NUTS vs. Basic HMC II

Hoffman and Gelman

Figure 6: Effective sample size (ESS) as a function of δ and (for HMC) simulation length�L for the multivariate normal, logistic regression, hierarchical logistic regression,and stochastic volatility models. Each point shows the ESS divided by the numberof gradient evaluations for a separate experiment; lines denote the average of thepoints’ y-values for a particular δ. Leftmost plots are NUTS’s performance, eachother plot shows HMC’s performance for a different setting of �L.

The trajectory length (measured in number of states visited) grows as the acceptancerate target δ grows, which is to be expected since a higher δ will lead to a smaller stepsize �, which in turn will mean that more leapfrog steps are necessary before the trajectorydoubles back on itself and satisfies equation 9.

24

• Hierarchical logistic regression and stochastic volatility• Simulation time t is ε L, step size (ε) times number of steps (L)• NUTS can beat optimally tuned HMC (latter very expensive)

16

Page 17: Daniel Lee STAN

HMC Example TrajectoryHoffman and Gelman

−0.1 0 0.1 0.2 0.3 0.4 0.5−0.1

0

0.1

0.2

0.3

0.4

Figure 2: Example of a trajectory generated during one iteration of NUTS. The blue ellipseis a contour of the target distribution, the black open circles are the positions θtraced out by the leapfrog integrator and associated with elements of the set ofvisited states B, the black solid circle is the starting position, the red solid circlesare positions associated with states that must be excluded from the set C ofpossible next samples because their joint probability is below the slice variable u,and the positions with a red “x” through them correspond to states that must beexcluded from C to satisfy detailed balance. The blue arrow is the vector from thepositions associated with the leftmost to the rightmost leaf nodes in the rightmostheight-3 subtree, and the magenta arrow is the (normalized) momentum vectorat the final state in the trajectory. The doubling process stops here, since theblue and magenta arrows make an angle of more than 90 degrees. The crossed-out nodes with a red “x” are in the right half-tree, and must be ignored whenchoosing the next sample.

being more complicated, the analogous algorithm that eliminates the slice variable seemsempirically to be slightly less efficient than the algorithm presented in this paper.

6

• Blue ellipse is contour of target distribution

• Initial position at black solid circle

• Arrows indicate a U-turn in momentum

17

Page 18: Daniel Lee STAN

No-U-Turn Sampler (NUTS)• HMC highly sensitive to tuning parameters

– discretization step size ε– discretization number of steps L

• NUTS sets ε during burn-in by stochastic optimization(Nesterov-style dual averaging)

• NUTS chooses L online per-sample using no-U-turn idea:

keep simulating as long as position gets fur-ther away from initial position

• Number of steps just a bit of bookkeeping on top of HMC

(Hoffman and Gelman, 2011)

18