Bayesian analysis with Python


Aside from my previous post on carrying out Bayesian analysis with R, here is an illustration on a Hierarchical Poisson failure rates from Clark and Gelfand,(1) using Python and the PyMC package.

The Python code is as simple as the R code, although it is obviously more object-oriented. The main part are highlighted below. This is in fact where we specify the prior distributions

alpha0 = Exponential(‘alpha0’, 1.0, value=1.)
beta0 = Gamma(‘beta0’, alpha=0.1, beta=1.0, value=1.)
theta = Gamma(‘theta’, alpha=alpha0, beta=beta0, value=ones(k))

Running the model is as simple as:

var_list = [alpha0, beta0, theta, y]
M.use_step_method(AdaptiveMetropolis, [alpha0, beta0])

Installing PyMC

As of June, the 27th, there are problem with the gp package ( isn’t in the source distribution, nor does it commes from the Universal installer through pypi). This is discussed on one Google group thread. I happened to setup a working version of PyMC by downloading the source from the Github repository and compiling from scratch, with the following commands:

$ export LDFLAGS=”-Wall -lgfortran -undefined dynamic_lookup -bundle -arch x86_64”
$ python config_fc —fcompiler gnu95 build
$ sudo python install


Clark, J.S. and Gelfand, A. (2006). Hierarchical modelling for the environmental sciences: statistical methods. Oxford university Press.


Articles with the same tag(s):

Academic teaching
Data cleaning techniques
Writing a book
Bad Data
Light Table and interactive live coding
Twenty canonical questions in machine learning
Do a large amount of consulting
Python for interactive scientific data visualization
Audit trails in statistical project
Dose finding studies and cross-over trials