Skip to content

Regression and Other Stories translated into Python!

Ravin Kumar writes in with some great news:

As readers of this blog likely know Andrew Gelman, Jennifer Hill, and Aki Vehtari have recently published a new book, Regression and Other Stories. What readers likely don’t know is that there is an active effort to translate the code examples written in R and the rstanarm library to Python and the bambi library. The core ideas that are presented in the book transcend any specific programming language, and more so insightful ideas in how to think about statistical problems, frame them in a structured way, and use mathematical concepts to solve them. Regardless of R or Python, the fundamentals of Bayesian modeling are the same, and if you’re fluent in Python that shouldn’t be a barrier to extensive knowledge that is present in this book.

Reading this book you’ll also learn that the rstanarm library is quite good! It’s so good actually that there are many features we’d like to port to bambi. So if you’re fluent in Python we invite you to first read the book, and then come help out, both with translating the book and with adding features to bambi! Contributions are very welcome.

The repository for the library and Python port are in the repositories below:


  1. The core ideas indeed transcend programming language. So it’s no surprise to me that Bambi’s built on PyMC3.

    My contention remains that the only way Stan can be competitive in Python for general Bayesian modeling (as opposed to canned models like rstanarm) is to build a graphical modeling API like PyMC3’s. There’s no reason at all that couldn’t be done and backed by Stan. PyMC3 itself is being translated to a TensorFlow back end for PyMC4.

    Whenever I bring it up, the Stan community’s generally negative for a number of reasonable reasons, most notably having to do with blurring our focus and lack of resources.

    • Andrew says:


      This is an interesting issue; thanks for bringing it up. It’s not really a concern for Regression and Other Stories, as there we’re only working with pre-compiled models (with one exception in the very last chapter). But it will come up with more general Bayesian modeling.

    • Ricardo says:

      That would indeed be great to have!

    • Tal Yarkoni says:

      Just to clarify, when Jake Westfall and I wrote the original release of Bambi, it did actually have support for both PyMC3 and Stan backends (the latter via PyStan). But we both moved onto other things, and the PyMC3 folks were kind enough to take over maintenance of the package. I think the decision to drop Stan support was unfortunate from an end user perspective (particularly because estimation was faster in Stan than PyMC3 for most models), but totally understandable from a development perspective. Having to funnel everything through a common high-level abstraction was a pain in the ass, plus the fact that PyMC3 is natively written in Python made it way easier for us to support certain features than in Stan (not necessarily because it *couldn’t* be done in Stan, but more because I didn’t know Stan well, and writing a lot of Stan boilerplate seemed much less fun than writing a few lines of Python code).

      I agree 100% with the sentiment that if you were to implement a comprehensive Python API for Stan (PyStan is great, but it’s really just a thin wrapper around Stan), you’d probably gain a lot of users in the Python community who currently appreciate Stan at a distance but find it easier to do what they need to in PyMC3 or TensorFlow Probability than to learn a new modeling language and/or work outside Python. Though whether the adoption boost would be sufficient to justify the likely large effort required, I don’t know. It seems like a basic API that maps Python distributions onto built-in Stan distributions might not be terribly difficult (in a sense, I already had to do this when building the old Stan backend). But the real power of PyMC3 is that users can, e.g., write custom log likelihood functions in native Python, and doing *that* in a way that seamlessly transpiles Python code to Stan code seems rather difficult.

Leave a Reply