2013-10-07

Stan is an application framework for building Monte Carlo samplers. Superficially, it looks a lot like BUGS (or JAGS): you define a model using an R-like syntax and then...magic!...samples appear. But that's where the similarities end.

For starters, with Stan, there's an intermediate step in which the Stan "compiler" spits out a C++ file based on your model; you must then compile this source file and link it to the Stan libraries. (The documentation is quite clear about how to do this.) So Stan's output is another program—your program—which you can then run to produce (magically) samples.

Another difference between Stan and BUGS lies in how the samples are generated. BUGS relies on Gibbs sampling, whereas Stan uses self-tuning Hamiltonian Monte Carlo sampling (the so-called no U-turn sampler of Hoffman and Gelman). Hamiltonian MC is similar to another technique I've used—Riemann manifold Langevin MC—in that it uses the gradient of the log posterior density to "aim" the sampler to higher density regions. Samples tend to take more computation time to generate, but make up for this loss with lower autocorrelation.

Stan uses automatic differentiation to calculate the value of the gradient for any exact values of the model parameters—the same approach I took in my job market paper. This isn't an easy thing to code up by hand, in part because the best templated library for matrix operations (eigen) doesn't work very will with automatic differentiation (yet). The Stan team decided to work around this problem by implementing their own automatic differentiation library. Basically, Stan rolls all of my favorite tools into one nicely integrated package.

I've been using Stan to prototype a model comprising a finite mixture of Poisson distributions. So far it's working really nicely. I can make rather drastic changes to the model in a short amount of time, and because the sampler runs rather quickly with the small amount of data I'm using for the prototype, I can see how these changes impact fit. I'm eager to see how far I can push this tool as I add more data...

One last point—Stan claims to provide an API so that you can access the log-probability functions (and their gradients) directly from your own code. So far these features do not appear to have been documented, and looking through the code, I can see that such integration will require an investment in understanding how the whole shebang works. Hopefully the upcoming release of Stan 2.0 will address this shortcoming.

comments powered by Disqus