9 ms·
Stan: a probabilistic programming language
- svisser 11y agoIt might give the right answers.
- probdist 11y agoStan is best viewed in my mind as a successor to BUGS (Bayesian Inference Using Gibbs Sampling) which more people may have heard of. In fact, there are lots of players in the probabilistic programming space now, personally I like the model of "Infer.NET" [1] from Microsoft Research, as I find variational and approximate variational inference a good solution to my problems and I like coding models in nearly C# more than in Stan format. If you want to get started as fast as possible with computational Bayesian inference and don't need the performance and advanced features Stan, I'd recommend emcee [2] which is a lightweight python MCMC sampler. Final recommendation to the HN crowd: Bayesian Data Analysis 3rd Edition [3] should be on your desk if you are thinking about using any of this software. [1] http://research.microsoft.com/en-us/um/cambridge/projects/infernet/ http://research.microsoft.com/en-us/um/cambridge/projects/in...] [2] http://dan.iel.fm/emcee/current/ http://dan.iel.fm/emcee/current/ [3] https://www.crcpress.com/Bayesian-Data-Analysis-Third-Edition/Gelman-Carlin-Stern-Dunson-Vehtari-Rubin/9781439840955 https://www.crcpress.com/Bayesian-Data-Analysis-Third-Editio...
- tel 11y ago+1 on all of this (except I'm a fan of full MCMC, have you used ADVI?)
- probdist 11y agoHaven't tried ADVI yet, but I think it is (likely) a great idea. In general automatic differentiation is a wildly powerful technique and is underutilized in computational science. Even in pure optimization problems people rarely leverage it to get J or H information as often as they probably should. I'll admit to not being super familiar with all the machinery under the hood in the ADVI paper [1]. It wasn't out when I started working on my current project. [1] http://arxiv.org/abs/1506.03431 http://arxiv.org/abs/1506.03431
- proditus 11y agodo let me know how it goes if you do try ADVI. we're also working on making it more robust to initialization and step-sizes. stay tuned.
- mjw 11y agoAVDI is incredibly awesome. Thanks so much for this work. Really nice readable paper on it too. As someone working with large datasets I think automating variational inference + SGD to work with a broad class of models is really the way forwards and a big force multiplier for machine learning.
- kristjankalm 11y agosome useful books available as a PDF: David Barber "Bayesian Reasoning and Machine Learning" http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=Brml.HomePage http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=... -- Thorough and systematic, accompanied by a Matlab library Marc Steyvers "Computational Statistics with Matlab" http://psiexp.ss.uci.edu/research/teachingP205C/205C.pdf http://psiexp.ss.uci.edu/research/teachingP205C/205C.pdf -- Somewhat unfinished but very good hands-on guide for a total novice David Draper "Bayesian Modeling, Inference and Prediction" https://users.soe.ucsc.edu/~draper/draper-BMIP-dec2005.pdf https://users.soe.ucsc.edu/~draper/draper-BMIP-dec2005.pdf -- Quite good if you're already familiar with a field, might be slightly daunting for a beginner and the classic, David MacKay's "Information Theory, Inference, and Learning Algorithms" http://www.inference.phy.cam.ac.uk/itprnn/book.html http://www.inference.phy.cam.ac.uk/itprnn/book.html
- chmullig 11y agoStan is very much a successor to BUGS, although I'm not sure BUGS is better known outside certain social science research communities these days.
- lgas 11y agoIt's not.
- dewarrn1 11y agoAnother endorsement for Gelman's books — they're some of the best instructional statistics work that I've read.
- eruditely 11y agoYou really think they're instructional? I would say it's sort of complete, but no means introductory.
- dustintran 11y agoHi, stan dev here. I think viewing Stan as a better BUGS is helpful but limiting. The syntax is similar, but the class of models Stan fits is far more general. The class of algorithms we have available also goes beyond MCMC, e.g., variational inference, optimization, and interfaces to Stan exist on all primary programming languages. It's more helpful to think of Stan as its own probabilistic programming language, and arguably the biggest entity with the largest user base.
- thisisdave 11y ago>the class of models Stan fits is far more general. This is mostly true, but last time I checked, Stan still couldn't sample discrete variables like BUGS can. Stan can only fit models with discrete parameters (e.g. finite mixtures) if the programmer is smart enough to integrate them out.
- tel 11y agoStan is absolutely a world class general Bayesian sampling tool. It replaces things like BUGS and uses fantastic algorithms for fast convergence. One of the major people behind it, Andrew Gelman, is a leader in applied hierarchical modeling and uses it to great effect in social sciences. If you have even vague interest in applied statistics in fields with lesser ability to design experimental controls then you should investigate this tool.
- skimpycompiler 11y agoDoes anyone have the experience with speed of convergence for some advanced models? And, defining sequence models in these probabilistic programming languages? Seems most (if not all) probabilistic programming languages use sampling as a general algorithm for training models. So, I guess it would be a hard job to express something like HMMs or CRFs that would fit into the framework (I guess one can easily train HMMs/CRFs with a fixed length of a sequence, but not undefined length).
- nextos 11y agoYou can easily encode any generative sequence model in e.g. Church (Scheme-based), Anglican (Clojure-based) or PyMC (Python-based). These are Turing-complete, so they can model any sampleable (computable) distribution. See http://forestdb.org/ http://forestdb.org/ for some examples. E.g. an infinite HMM. I have no experience with Stan. There was some controversy on whether it is Turing-complete, but I cannot elaborate on that. The obvious disadvantage of using a language that is too expressive for something as simple as an HMM is that you loose the nice performance guarantees given by all specialized algorithms such as Viterbi. Theoretically, they can achieve good efficiency by performing program transformations (e.g. with abstract interpretation). But in practice, we are still a bit far from that. A nice trick is to implement your generative model in something very efficient (e.g. Probabilisitic C or something GPU-based). You can then forget the burden of having to encode your own sampling procedure, but at the same time inference might be tractable.
- mjn 11y agoThere's some research on automatically deriving efficient algorithms for classes of distributions, e.g. getting a specialized EM-like algorithm for anything where EM-like algorithms are applicable: http://machinelearning.wustl.edu/mlpapers/paper_files/AA24.pdf http://machinelearning.wustl.edu/mlpapers/paper_files/AA24.p... But obviously that doesn't cover classes as big as "anything expressible in Church", so there's a pretty big gap. It's possible that languages like Church could use techniques like that as part of an optimizing compiler that recognizes specializable patterns, the way regular compilers do loop transformations and auto-vectorization and such. But, like auto-vectorization, not that easy to do.
- tristanz 11y agoThe great thing about Stan is it is geared toward practitioners doing real work in science and social science, but still manages to push the boundary of stats research (NUTS, ADVI, penalized ML). Other probabilistic programming languages are more expressive, but are typically much harder to use for day-to-day work. That being said, there's still a huge gulf between practitioners in science and social science that are building parametric Bayesian models and practitioners in the deep learning / ML community that are focused on building more general, scalable, machine learning algorithms for tasks like machine translation and question answering. It would be amazing if somebody could reconcile these communities by showing deep learning models can be expressed and fit effectively side-by-side with more parametric models.
- mailshanx 11y agoHere is a well deserved plug for Cam Davidson Pilon's excellent book called Bayesian Methods for Hackers: https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers https://github.com/CamDavidsonPilon/Probabilistic-Programmin...
- dj-wonk 11y agoOne of my first questions was "What are the motivations for a language over a library?" After reading more, I see that Stan has both. There are integrations with R, Python, Julia, C++, Stata, the command line, and more. To ask an intentionally over-simplified question: if the key value add of Stan in the functionality and approach -- why bother with the overhead of an external DSL? Why not use an internal DSL -- e.g. borrow data structures and/or syntax of another language? Arguments for an external DSL are, hopefully: cross-language portability and domain-clarity for people. But how does this play out if you want to support multiple languages and interop? Let's say I'm using Python, for example, and want to build up a model programmatically. The example at https://github.com/stan-dev/pystan https://github.com/stan-dev/pystan shows using a heredoc. That's simple and easy to start. However, a big downside of a plain-text DSL is that it can be hard to manipulate programatically. To do that, each language implementation has to be 'business' of converting to and from text. This reminds me of the 'SQL' problem ... many language SQL wrappers spend considerable time munging text. Ug. Given my bias towards data-oriented programming, I see another approach: why not use a rich data structure language (other than plain text) as the foundation? Why not edn, for example? If the LISPy nature of that is not preferred, then why not something else? (Does the C-community have some kind of generic data description language? I wouldn't recommend JSON necessarily but that would be better than a hand-rolled DSL, in my opinion.) Here's why I recommend starting with 'rich' data as opposed to parsing text... I'm not saying human DSLs are bad. I'm just saying they don't have to be the foundation. If you want a human external DSL, great -- but why not convert the human DSL to a canonical data structure? Then build everything around the data, rather than build everything around the text format?
- chmullig 11y agoIn my mind, there's two points to Stan. The first is the actual math -- it can fit lots of models, much faster and more flexibly than other alternatives. The second is to go out and meet the practitioners where they are. That means making Stan models look like the equations in your paper. It means connecting to existing research software ecosystems. It means making it easy to adapt. In this case, if you end up tightly coupled to, say, specific R data structure then it becomes much, much harder to make it also work well for Python, and for Stata, etc. Additionally, it means that if I start on a model in Python then I cannot easily switch to using R (or vice-versa). Using a DSL also has the nice property of cleanly separating Stan world vs R world. That makes it easier for users to build an accurate mental model for what happens when/where, which hopefully then leads to better performance. Most importantly though, Stan code is incredibly concise and clear for specifying models.
- powera 11y agoCan anyone explain to me briefly what Stan actually does that's different from what I could do in Python without it? I don't want to have to read the entire research paper to get a description in layman's terms.
- proditus 11y agothe Stan manual [1] is like a textbook. while it's a bit long (and a fair bit longer than a research paper), i highly encourage that you take a look. it's very informative. [1] http://mc-stan.org/documentation/ http://mc-stan.org/documentation/
- bengHN 11y agoThe number one thing in my opinion is that Stan's algorithm(s) for drawing from a posterior distribution produce samples that have much less dependence among adjacent draws than other simpler MCMC algorithms like Metropolis-Hastings or Gibbs samplers. Since it is often difficult to answer the question "How much dependence is too much in practice?", it is prudent to use the algorithm that yields the least dependence because the effective sample size (from the posterior distribution) per unit of wall time will usually be greater. PyMC3 has started to incorporate some of Stan's algorithms, although their implementations are not as far along.
- wlievens 11y agoI know some of those words...
- zmmmmm 11y agoForget about the statistics and MCMC and all that. Imagine you have a function that is a black box and returns a floating point number. You can't see the implementation, but you want to find its maximum. How do you do it? If you're like me you'd probably start with some kind of "grid search" by giving it evenly spaced parameters. Then you would evaluate it at each one and do some kind of gradient descent / ascent type algorithm. This will work, but in a high dimensional space (ie. a function with many arguments) you have no chance of covering even a tiny fraction of the parameter space. The key is, you don't want to waste time evaluating a set of parameters if it doesn't tell you anything new. ie. if you get back a similar answer to what you already had. This is what the GP means (I think) by trying to avoid "dependence between adjacent draws". I could be wrong about all this, I'm trying to learn it too.
- proditus 11y agohi. i'm one of the Stan devs. (i work on variational inference: ADVI). happy to answer any questions here.
- chmullig 11y agoYay Stan! So what's the deal with BBVI? I heard it's integrated in the newest Stan, but can I use it? What for?
- proditus 11y agoglad to see the enthusiasm! ADVI [1] is a variant of BBVI [2] where we fully leverage all of the amazing things that Stan has to offer (like automatic differentiation and automatic transformations of constrained parameters). you can use ADVI to get an approximation to the Bayesian posterior. the advantage of using ADVI over sampling is that ADVI is typically faster for large models (both in terms of # of parameters and # of data observations). ADVI also a bit better at handling models with multi-modal posteriors, such as mixture models. ADVI is currently in cmdStan (but not in RStan or PyStan). we're continuing to make the algorithm more robust. [1] http://arxiv.org/abs/1506.03431 http://arxiv.org/abs/1506.03431 [2] http://www.jmlr.org/proceedings/papers/v33/ranganath14.pdf http://www.jmlr.org/proceedings/papers/v33/ranganath14.pdf
- marmaduke 11y agohi, funny this came up on HN; I'm just trying to get a handle on fitting a dynamical systems model of neural activity propagation with Stan. What in general are the disadvantages of variational inference vs full MCMC? Are there in general significant advantages besides the speed up?
- proditus 11y agothat's a really good question. some aspects of VI vs MCMC are areas of active research. so it's tough to respond succinctly, but i'll try. the key disadvantages of VI (particularly ADVI) are: 1. mean-field variational inference cannot model posterior correlations. so if you expect your model + dataset to give a "skewed" posterior, then mean-field variational inference will have a difficult time describing such a posterior. (it will under estimate marginal variances.) 2. full-rank variational inference can model posterior correlations. but it can become too expensive for big models. there is a lot of great research coming up in this vein, such as [1,2]. 3. in either case, the version of variational inference we have in Stan (ADVI) uses a normal approximation in a transformed parameter space. thus, there is an additional mismatch of the shape of the variational posterior to the full MCMC posterior. in terms of advantages: 1. variational inference is (in general) a non-convex optimization problem. so it's easy to know when we've converged to a local optimum. convergence in MCMC is a bit more tricky to assess. 2. if your model has a multi-modal posterior, then variational inference will focus on just one of the modes. this is sometimes desirable as MCMC techniques might end up jumping around all of the modes and producing poor samples. this is just the tip of the iceberg. but i hope it helps! [1] http://arxiv.org/pdf/1506.03159.pdf http://arxiv.org/pdf/1506.03159.pdf [2] http://arxiv.org/pdf/1502.07685.pdf http://arxiv.org/pdf/1502.07685.pdf
- chmullig 11y agoStan is a fantastic language/library/tool. (Disclosure, I've taken 2 classes with Andrew Gelman[1]. For those who haven't used it, the typical usage is to use Stan in combination with other languages (most commonly R). In the Stan language you define a model, specifying the data you'll get, potentially transformations to that data, then a set of parameters you want to fit, then finally a model saying how those parameters interact with each other and the data. You can also define priors for parameters. Then typically you save that model, and using R pass in the variables to a Stan call. The resulting fit object is returned to your original environment automatically. It actually fits the work style reasonably well. All data munging happens as before, but instead of having some complicated model expression in, say, a glm() call, you have it in Stan language. If you're interested in this, Gelman has two great books. Gelman & Hill's Hierarchical Models[2] which is applied and geared towards social science researchers, and Bayesian Data Analysis[3]. [1] http://andrewgelman.com/ http://andrewgelman.com/ [2] http://www.stat.columbia.edu/~gelman/arm/ http://www.stat.columbia.edu/~gelman/arm/ [3] http://www.stat.columbia.edu/~gelman/book/ http://www.stat.columbia.edu/~gelman/book/
- bengHN 11y agoAlso, the rstanarm[1] R package (disclaimer: that I co-wrote) will be released this month, which does not require the user to write any code in the Stan language. Instead, you specify the likelihood of the data (for a few popular regression-type models) using conventional R syntax and utilize Stan's algorithms and optional priors on the parameters to draw from the posterior distribution. In the demos/ directory of [1], we have replicated most of the first half of Gelman & Hill's textbook and are starting on the second half, which heavily utilizes our stan_glmer() function that is compatible with the syntax of the glmer() function in the lme4 R package. [1] https://github.com/stan-dev/rstanarm/ https://github.com/stan-dev/rstanarm/
- chmullig 11y agooh neat!
- ericnovik 11y agoI found this recent paper showing how to fit and diagnose a simple non-linear model in Stan helpful. It is written from a statistician's perspective. http://www.stat.columbia.edu/~gelman/research/published/stan_jebs_2.pdf http://www.stat.columbia.edu/~gelman/research/published/stan... This one has more of a CS feel to it: http://www.stat.columbia.edu/~gelman/research/published/stan-paper-revision-feb2015.pdf http://www.stat.columbia.edu/~gelman/research/published/stan... Lastly, here is my own turbulent experience with getting started with Stan: http://ericnovik.github.io/2015/08/14/Getting-Started-with-Stan.html http://ericnovik.github.io/2015/08/14/Getting-Started-with-S...