This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.

7.7. Fitting a Bayesian model by sampling from a posterior distribution with a Markov Chain Monte Carlo method

You can find the instructions to install PyMC on the package's website. (

You also need the Storms dataset from the book's website. (

  1. Let's import the standard packages and PyMC.
In [ ]:
import numpy as np
import pandas as pd
import pymc
import matplotlib.pyplot as plt
%matplotlib inline
  1. Let's import the data with Pandas.
In [ ]:
df = pd.read_csv("data/Allstorms.ibtracs_wmo.v03r05.csv",
  1. With Pandas, it only takes a single line of code to get the annual number of storms in the North Atlantic Ocean. We first select the storms in that basin (NA), then we group the rows by year (Season), and we take the number of unique storm (Serial_Num) as each storm can span several days (nunique method).
In [ ]:
cnt = df[df['Basin'] == ' NA'].groupby('Season') \
years = cnt.index
y0, y1 = years[0], years[-1]
arr = cnt.values
plt.plot(years, arr, '-ok');
plt.xlim(y0, y1);
plt.ylabel("Number of storms");
  1. Now, we define our probabilistic model. We assume that storms arise following a time-dependent Poisson process with a deterministic rate. We assume this rate is a piecewise-constant function that takes a first value early_mean before a certain switch point, and a second value late_mean after that point. These three unknown parameters are treated as random variables (we will describe them more in the How it works... section).

A Poisson process is a particular point process, that is, a stochastic process describing the random occurence of instantaneous events. The Poisson process is fully random: the events occur independently at a given rate.

In [ ]:
switchpoint = pymc.DiscreteUniform('switchpoint',
                                   lower=0, upper=len(arr))
early_mean = pymc.Exponential('early_mean', beta=1)
late_mean = pymc.Exponential('late_mean', beta=1)
  1. We define the piecewise-constant rate as a Python function.
In [ ]:
def rate(s=switchpoint, e=early_mean, l=late_mean):
    out = np.empty(len(arr))
    out[:s] = e
    out[s:] = l
    return out
  1. Finally, the observed variable is the annual number of storms. It follows a Poisson variable with a random mean (the rate of the underlying Poisson process). This fact is a known mathematical property of Poisson processes.
In [ ]:
storms = pymc.Poisson('storms', mu=rate, value=arr, observed=True)
  1. Now, we use the MCMC method to sample from the posterior distribution, given the observed data. The sample method launches the fitting iterative procedure.
In [ ]:
model = pymc.Model([switchpoint, early_mean, late_mean, rate, storms])
In [ ]:
mcmc = pymc.MCMC(model)
mcmc.sample(iter=10000, burn=1000, thin=10)
  1. Let's plot the sampled Markov chains. Their stationary distribution corresponds to the posterior distribution we want to characterize.
In [ ]:
plt.ylabel("Switch point"); 
plt.ylabel("Early mean");
plt.ylabel("Late mean");
  1. We also plot the distribution of the samples: they correspond to the posterior distributions of our parameters, after the data points have been taken into account.
In [ ]:
plt.hist(mcmc.trace('switchpoint')[:] + y0, 15);
plt.xlabel("Switch point")
plt.hist(mcmc.trace('early_mean')[:], 15);
plt.xlabel("Early mean");
plt.hist(mcmc.trace('late_mean')[:], 15);
plt.xlabel("Late mean");
  1. Taking the sample mean of these distributions, we get posterior estimates for the three unknown parameters, including the year where the frequency of storms suddenly increased.
In [ ]:
yp = y0 + mcmc.trace('switchpoint')[:].mean()
em = mcmc.trace('early_mean')[:].mean()
lm = mcmc.trace('late_mean')[:].mean()
print((yp, em, lm))
  1. Now we can plot the estimated rate on top of the observations.
In [ ]:
plt.plot(years, arr, '-ok');
plt.axvline(yp, color='k', ls='--');
plt.plot([y0, yp], [em, em], '-b', lw=3);
plt.plot([yp, y1], [lm, lm], '-r', lw=3);
plt.xlim(y0, y1);
plt.ylabel("Number of storms");

For a possible scientific interpretation of the data considered here, see

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).