Bayesian Sampler Examples
Examples of running each sampler avaiable in 3ML.
Before, that, let’s discuss setting up configuration default sampler with default parameters. We can set in our configuration a default algorithm and default setup parameters for the samplers. This can ease fitting when we are doing exploratory data analysis.
With any of the samplers, you can pass keywords to access their setups. Read each pacakges documentation for more details.
[1]:
from threeML import *
from threeML.plugins.XYLike import XYLike
import numpy as np
import dynesty
from jupyterthemes import jtplot
%matplotlib inline
jtplot.style(context="talk", fscale=1, ticks=True, grid=False)
silence_warnings()
set_threeML_style()
22:17:12 WARNING The naima package is not available. Models that depend on it will not be functions.py:48 available
WARNING The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it functions.py:69 will not be available.
WARNING The ebltable package is not available. Models that depend on it will not be absorption.py:33 available
[2]:
threeML_config.bayesian.default_sampler
[2]:
<Sampler.emcee: 'emcee'>
[3]:
threeML_config.bayesian.emcee_setup
[3]:
{'n_burnin': None, 'n_iterations': 500, 'n_walkers': 50, 'seed': 5123}
If you simply run bayes_analysis.sample()
the default sampler and its default parameters will be used.
Let’s make some data to fit.
[4]:
sin = Sin(K=1, f=0.1)
sin.phi.fix = True
sin.K.prior = Log_uniform_prior(lower_bound=0.5, upper_bound=1.5)
sin.f.prior = Uniform_prior(lower_bound=0, upper_bound=0.5)
model = Model(PointSource("demo", 0, 0, spectral_shape=sin))
x = np.linspace(-2 * np.pi, 4 * np.pi, 20)
yerr = np.random.uniform(0.01, 0.2, 20)
xyl = XYLike.from_function("demo", sin, x, yerr)
xyl.plot()
bayes_analysis = BayesianAnalysis(model, DataList(xyl))
emcee
[5]:
bayes_analysis.set_sampler("emcee")
bayes_analysis.sampler.setup(n_walkers=20, n_iterations=500)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
INFO sampler set to emcee bayesian_analysis.py:202
22:17:18 INFO Mean acceptance fraction: 0.7206999999999999 emcee_sampler.py:157
22:17:19 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.006 -0.013 +0.012 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 +/- 0.07) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.924711 |
total | -10.924711 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.555304 |
BIC | 27.840886 |
DIC | 25.729262 |
PDIC | 1.938592 |
[5]:
multinest
[6]:
bayes_analysis.set_sampler("multinest")
bayes_analysis.sampler.setup(n_live_points=400, resume=False, auto_clean=True)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
22:17:20 INFO sampler set to multinest bayesian_analysis.py:202
*****************************************************
MultiNest v3.10
Copyright Farhan Feroz & Mike Hobson
Release Jul 2015
no. of live points = 400
dimensionality = 2
*****************************************************
analysing data from chains/fit-.txt ln(ev)= -20.219864601307840 +/- 0.14375594527396279
Total Likelihood Evaluations: 5347
Sampling finished. Exiting MultiNest
22:17:21 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.006 -0.013 +0.014 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 -0.07 +0.06) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.924957 |
total | -10.924957 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.555797 |
BIC | 27.841379 |
DIC | 26.013032 |
PDIC | 2.079833 |
log(Z) | -8.781376 |
INFO deleting the chain directory chains multinest_sampler.py:255
[6]:
dynesty
[7]:
bayes_analysis.set_sampler("dynesty_nested")
bayes_analysis.sampler.setup(n_live_points=400)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
INFO sampler set to dynesty_nested bayesian_analysis.py:202
4099it [00:04, 880.07it/s, +400 | bound: 8 | nc: 1 | ncall: 19640 | eff(%): 23.384 | loglstar: -inf < -10.919 < inf | logz: -20.115 +/- 0.143 | dlogz: 0.001 > 0.409]
22:17:26 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.006 -0.012 +0.013 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 -0.06 +0.07) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.925352 |
total | -10.925352 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.556587 |
BIC | 27.842170 |
DIC | 25.773575 |
PDIC | 1.962053 |
log(Z) | -8.735692 |
[7]:
[8]:
bayes_analysis.set_sampler("dynesty_dynamic")
bayes_analysis.sampler.setup(
stop_function=dynesty.utils.old_stopping_function, n_effective=None
)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
22:17:27 INFO sampler set to dynesty_dynamic bayesian_analysis.py:202
7162it [00:07, 1772.40it/s, batch: 0 | bound: 12 | nc: 1 | ncall: 25789 | eff(%): 27.559 | loglstar: -inf < -10.922 < inf | logz: -20.019 +/- 0.127 | dlogz: 0.004 > 0.010]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
8223it [00:08, 1248.81it/s, batch: 1 | bound: 3 | nc: 1 | ncall: 27270 | eff(%): 29.758 | loglstar: -12.773 < -11.260 < -11.400 | logz: -20.015 +/- 0.131 | stop: 1.516]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
8858it [00:10, 663.61it/s, batch: 2 | bound: 2 | nc: 1 | ncall: 27940 | eff(%): 31.310 | loglstar: -13.203 < -12.125 < -12.769 | logz: -20.028 +/- 0.107 | stop: 1.071]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
9554it [00:11, 700.79it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 28708 | eff(%): 32.987 | loglstar: -13.630 < -11.977 < -13.194 | logz: -20.034 +/- 0.100 | stop: 1.002]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
9779it [00:11, 816.36it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 28963 | eff(%): 33.764 | loglstar: -13.630 < -10.919 < -13.194 | logz: -20.034 +/- 0.100 | stop: 0.897]
22:17:40 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.006 +/- 0.013 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 +/- 0.07) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.924779 |
total | -10.924779 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.555440 |
BIC | 27.841022 |
DIC | 25.839795 |
PDIC | 1.995218 |
log(Z) | -8.702847 |
[8]:
zeus
[9]:
bayes_analysis.set_sampler("zeus")
bayes_analysis.sampler.setup(n_walkers=20, n_iterations=500)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
INFO sampler set to zeus bayesian_analysis.py:202
WARNING:root:The sampler class has been deprecated. Please use the new EnsembleSampler class.
The run method has been deprecated and it will be removed. Please use the new run_mcmc method.
Initialising ensemble of 20 walkers...
Sampling progress : 100%|██████████| 625/625 [00:10<00:00, 60.35it/s]
22:17:51 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Summary
-------
Number of Generations: 625
Number of Parameters: 2
Number of Walkers: 20
Number of Tuning Generations: 25
Scale Factor: 1.142734
Mean Integrated Autocorrelation Time: 3.13
Effective Sample Size: 3988.22
Number of Log Probability Evaluations: 65897
Effective Samples per Log Probability Evaluation: 0.060522
None
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.007 -0.013 +0.014 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 +/- 0.07) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.924719 |
total | -10.924719 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.555321 |
BIC | 27.840903 |
DIC | 26.042977 |
PDIC | 2.096679 |
[9]:
ultranest
[10]:
bayes_analysis.set_sampler("ultranest")
bayes_analysis.sampler.setup(
min_num_live_points=400, frac_remain=0.5, use_mlfriends=False
)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
22:17:52 INFO sampler set to ultranest bayesian_analysis.py:202
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-1e+01
[ultranest] Likelihood function evaluations: 6121
[ultranest] logZ = -20.23 +- 0.1392
[ultranest] Effective samples strategy satisfied (ESS = 971.5, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.07 nat, need <0.50 nat)
[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.43, need <0.5)
[ultranest] logZ error budget: single: 0.14 bs:0.14 tail:0.41 total:0.43 required:<0.50
[ultranest] done iterating.
22:17:59 INFO fit restored to maximum of posterior sampler_base.py:178
INFO fit restored to maximum of posterior sampler_base.py:178
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | 1.007 -0.014 +0.012 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.88 -0.06 +0.07) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -10.925473 |
total | -10.925473 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 26.556829 |
BIC | 27.842411 |
DIC | 25.916953 |
PDIC | 2.032554 |
log(Z) | -8.790191 |
[10]: