Skip to content
Snippets Groups Projects

Add PyMC3 sampler

Merged Matthew David Pitkin requested to merge matthew-pitkin/bilby:pymc3 into master
All threads resolved!
Compare and
4 files
+ 463
0
Compare changes
  • Side-by-side
  • Inline
Files
4
#!/bin/python
"""
An example of how to use tupak to perform paramater estimation for
non-gravitational wave data. In this case, fitting a linear function to
data with background Gaussian noise
"""
from __future__ import division
import tupak
import numpy as np
import matplotlib.pyplot as plt
import inspect
from tupak.core.likelihood import GaussianLikelihood
# A few simple setup steps
label = 'linear_regression_pymc3'
outdir = 'outdir'
tupak.utils.check_directory_exists_and_if_not_mkdir(outdir)
# First, we define our "signal model", in this case a simple linear function
def model(time, m, c):
return time * m + c
# Now we define the injection parameters which we make simulated data with
injection_parameters = dict(m=0.5, c=0.2)
# For this example, we'll use standard Gaussian noise
sigma = 1
# These lines of code generate the fake data. Note the ** just unpacks the
# contents of the injection_paramsters when calling the model function.
sampling_frequency = 10
time_duration = 10
time = np.arange(0, time_duration, 1/sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
ax.plot(time, data, 'o', label='data')
ax.plot(time, model(time, **injection_parameters), '--r', label='signal')
ax.set_xlabel('time')
ax.set_ylabel('y')
ax.legend()
fig.savefig('{}/{}_data.png'.format(outdir, label))
# Now lets instantiate a version of our GaussianLikelihood, giving it
# the time, data and signal model
likelihood = GaussianLikelihood(time, data, model, sigma=sigma)
# From hereon, the syntax is exactly equivalent to other tupak examples
# We make a prior
priors = {}
priors['m'] = tupak.core.prior.Uniform(0, 5, 'm')
priors['c'] = tupak.core.prior.Uniform(-2, 2, 'c')
# And run sampler
result = tupak.run_sampler(
likelihood=likelihood, priors=priors, sampler='pymc3',
injection_parameters=injection_parameters, outdir=outdir,
draws=2000, label=label)
result.plot_corner()
Loading