from pyndamics3 import Simulation
MCMC (using emcee package)
Useful Functions
pyndamics3 version 0.0.32
dicttable
dicttable (D)
time2str
time2str (tm)
timeit
timeit (reset=False)
corner
corner (samples, labels)
histogram
histogram (y, bins=50, plot=True)
Distributions – Defined for Speed
loglognormalpdf
loglognormalpdf (x, mn, sig)
logbetapdf
logbetapdf (theta, h, N)
lognormalpdf
lognormalpdf (x, mn, sig, all_positive=False)
loghalfnormalpdf
loghalfnormalpdf (x, sig)
loghalfcauchypdf
loghalfcauchypdf (x, x0, scale)
logcauchypdf
logcauchypdf (x, x0, scale)
logexponpdf
logexponpdf (x, _lambda)
logjeffreyspdf
logjeffreyspdf (x)
loguniformpdf
loguniformpdf (x, mn, mx)
lognchoosek
lognchoosek (N, k)
Distribution Classes
Beta
Beta (h=100, N=100)
Initialize self. See help(type(self)) for accurate signature.
Cauchy
Cauchy (x0=0, scale=1)
Initialize self. See help(type(self)) for accurate signature.
LogNormal
LogNormal (mean=0, std=1)
Initialize self. See help(type(self)) for accurate signature.
HalfNormal
HalfNormal (sigma=1)
Initialize self. See help(type(self)) for accurate signature.
HalfCauchy
HalfCauchy (x0=0, scale=1)
Initialize self. See help(type(self)) for accurate signature.
Jeffreys
Jeffreys ()
Initialize self. See help(type(self)) for accurate signature.
Uniform
Uniform (min=0, max=1)
Initialize self. See help(type(self)) for accurate signature.
Exponential
Exponential (_lambda=1)
Initialize self. See help(type(self)) for accurate signature.
Normal
Normal (mean=0, std=1, all_positive=False)
Initialize self. See help(type(self)) for accurate signature.
Emcee functions
lnprior_function
lnprior_function (model)
MCMCModel
MCMCModel (sim, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
MCMCModelReg
MCMCModelReg (sim, verbose=True, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
A linear growth example
Data from http://www.seattlecentral.edu/qelp/sets/009/009.html
=np.array([7,14,21,28,35,42,49,56,63,70,77,84],float)
t=np.array([17.93,36.36,67.76,98.10,131,169.5,205.5,228.3,247.1,250.5,253.8,254.5])
h
'-o')
py.plot(t,h,'Days')
py.xlabel('Height [cm]') py.ylabel(
Text(0, 0.5, 'Height [cm]')
Run an initial (and bad) simulation
=Simulation()
sim"h'=a",1,plot=True)
sim.add(=t,h=h,plot=True)
sim.add_data(t=1)
sim.params(a0,90) sim.run(
<Figure size 864x576 with 0 Axes>
Fitting \(a\)
model=MCMCModel(sim,a=Uniform(-10,10))
=model.run_mcmc(500)
result model.plot_chains()
Sampling Prior...
Done.
0.39 s
Running MCMC 1/1...
Done.
40.76 s
<Figure size 864x576 with 0 Axes>
Although this looked converged, you might have situations where you want to repeat the mcmc-resample loop (i.e. resample parameters from the 95% CI of the current samples)
result
Priors
name |
prior | |||||
a |
Uniform |
|
||||
_sigma_h |
Jeffreys |
|
Fit Statistics
data points | 12 |
variables | 2 |
number of walkers | 100 |
number of samples | 37500 |
Bayesian info crit. (BIC) | 112.50038192512125 |
Posteriors
name |
value |
2.5% |
97.5% |
a |
3.5405 |
2.9562 |
3.825 |
_sigma_h |
22.942 |
12.544 |
37.921 |
100,repeat=3)
model.run_mcmc( model.plot_chains()
Running MCMC 1/3...
Done.
8.47 s
Running MCMC 2/3...
Done.
8.21 s
Running MCMC 3/3...
Done.
8.20 s
<Figure size 864x576 with 0 Axes>
model.summary()
Priors
name |
prior | |||||
a |
Uniform |
|
||||
_sigma_h |
Jeffreys |
|
Fit Statistics
data points | 12 |
variables | 2 |
number of walkers | 100 |
number of samples | 7500 |
Bayesian info crit. (BIC) | 112.50477437641784 |
Posteriors
name |
value |
2.5% |
97.5% |
a |
3.5456 |
3.0126 |
3.7934 |
_sigma_h |
22.97 |
12.659 |
39.585 |
model.best_estimates()
{'a': array([3.41276037, 3.5509136 , 3.6829332 ]),
'_sigma_h': array([18.50769675, 22.69773539, 28.29713438])}
0,90) sim.run(
<Figure size 864x576 with 0 Axes>
model.plot_distributions()
Fitting with \(a\) and the initial \(h\)
=MCMCModel(sim,
model=Uniform(-10,10),
a=Uniform(0,180),
initial_h )
500,repeat=2)
model.run_mcmc( model.plot_chains()
Sampling Prior...
Done.
0.43 s
Running MCMC 1/2...
Done.
29.34 s
Running MCMC 2/2...
Done.
29.19 s
<Figure size 864x576 with 0 Axes>
0,90) sim.run(
<Figure size 864x576 with 0 Axes>
model.plot_distributions()
0,90,'h') model.plot_many(
Logistic with the Same Data
=np.array([7,14,21,28,35,42,49,56,63,70,77,84],float)
t=np.array([17.93,36.36,67.76,98.10,131,169.5,205.5,228.3,247.1,250.5,253.8,254.5])
h
=Simulation()
sim"h'=a*h*(1-h/K)",1,plot=True)
sim.add(=t,h=h,plot=True)
sim.add_data(t=1,K=500)
sim.params(a0,90) sim.run(
<Figure size 864x576 with 0 Axes>
=MCMCModel(sim,
model=Uniform(.001,5),
a=Uniform(100,500),
K=Uniform(0,100),
initial_h )
500,repeat=3)
model.run_mcmc( model.plot_chains()
Sampling Prior...
Done.
0.43 s
Running MCMC 1/3...
Done.
38.74 s
Running MCMC 2/3...
Done.
48.56 s
Running MCMC 3/3...
Done.
52.50 s
<Figure size 864x576 with 0 Axes>
model.best_estimates()
{'a': array([0.08452244, 0.08754994, 0.09062923]),
'K': array([258.46068768, 261.11871483, 263.83119905]),
'initial_h': array([11.2367951 , 12.3431459 , 13.55099098]),
'_sigma_h': array([3.12526592, 3.88416891, 5.06139448])}
0,90) sim.run(
<Figure size 864x576 with 0 Axes>
model.plot_distributions()
model.triangle_plot()
0,90,'h') model.plot_many(
=True # turn off the simulation plots
sim.noplots=[]
saved_hfor i in range(500):
model.draw()0,150)
sim.run('g-',alpha=.05)
py.plot(sim.t,sim.h,
saved_h.append(sim.h)=False # gotta love a double-negative
sim.noplots'bo') # plot the data
py.plot(t,h,=np.array(saved_h) saved_h
=np.percentile(saved_h,50,axis=0)
med=np.percentile(saved_h,2.5,axis=0)
lower=np.percentile(saved_h,97.5,axis=0)
upper'r-')
py.plot(sim.t,med,'r:')
py.plot(sim.t,lower,'r:')
py.plot(sim.t,upper,
'bo') # plot the data py.plot(t,h,