An example using PyMC3
Here we show a standalone example of using PyMC3 to
estimate the parameters of a straight line model in data with Gaussian noise. The
data and model used in this example are defined in createdata.py
, which can be downloaded
from here. The
script shown below can be downloaded from here.
Example code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Example of running PyMC3 to fit the parameters of a straight line.
"""
from __future__ import print_function, division
import os
import sys
import numpy as np
import matplotlib as mpl
mpl.use("Agg") # force Matplotlib backend to Agg
# import PyMC3
import pymc3 as pm
# import model and data
from createdata import *
# set the PyMC3 model
linear_model = pm.Model()
with linear_model:
# set prior parameters
cmin = -10. # lower range of uniform distribution on c
cmax = 10. # upper range of uniform distribution on c
mmu = 0. # mean of Gaussian distribution on m
msigma = 10. # standard deviation of Gaussian distribution on m
# set priors for unknown parameters
cmodel = pm.Uniform('c', lower=cmin, upper=cmax) # uniform prior on y-intercept
mmodel = pm.Normal('m', mu=mmu, sd=msigma) # Gaussian prior on gradient
sigmamodel = sigma # set a single standard deviation
# Expected value of outcome, aka "the model"
mu = mmodel*x + cmodel
# Gaussian likelihood (sampling distribution) of observations, "data"
Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigmamodel, observed=data)
Nsamples = 1000 # final number of samples
Ntune = 1000 # number of tuning samples
# perform sampling
with linear_model:
trace = pm.sample(Nsamples, tune=Ntune, discard_tuned_samples=True) # perform sampling
# extract the samples
postsamples = np.vstack((trace['m'], trace['c'])).T
# plot posterior samples (if corner.py is installed)
try:
import corner # import corner.py
except ImportError:
sys.exit(1)
print('Number of posterior samples is {}'.format(postsamples.shape[0]))
fig = corner.corner(postsamples, labels=[r"$m$", r"$c$"], truths=[m, c])
fig.savefig('PyMC3.png')
Running the code
A description of installing PyMC3 is given here. If you have downloaded the createdata.py
and test_PyMC3.py
scripts into the directory ${HOME}
, then you can run it using:
python test_PyMC3.py
If you have Matplotlib installed then the script will produce a plot of the posterior distributions on the straight line parameters $m$ and $c$.
A Python 3 Docker image with PyMC3 installed is available, which can be used with:
docker run -it -v ${HOME}:/work mattpitkin/samplers:python3
to enter an interactive container, and then within the container the test script can be run with:
python test_PyMC3.py