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
from packaging.version import Version
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()
17:47:45 WARNING The naima package is not available. Models that depend on it will not be functions.py:43 available
WARNING The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it functions.py:65 will not be available.
17:47:46 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:186
17:47:50 INFO Mean acceptance fraction: 0.6436999999999999 emcee_sampler.py:145
INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.007 -0.026 +0.017 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0004 -0.0021 +0.0027) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.181299 |
| total | -11.181299 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.068481 |
| BIC | 28.354063 |
| DIC | 91.851159 |
| PDIC | 0.389275 |
[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()
17:47:51 INFO sampler set to multinest bayesian_analysis.py:186
*****************************************************
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)= -21.214746953152403 +/- 0.15045954709225123
Total Likelihood Evaluations: 5723
Sampling finished. Exiting MultiNest
17:47:52 INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.007 -0.021 +0.020 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0003 -0.0022 +0.0021) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.181953 |
| total | -11.181953 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.069788 |
| BIC | 28.355370 |
| DIC | 26.298412 |
| PDIC | 1.966182 |
| log(Z) | -9.213448 |
INFO deleting the chain directory chains multinest_sampler.py:234
[6]:
dynesty
[7]:
bayes_analysis.set_sampler("dynesty_nested")
bayes_analysis.sampler.setup(nlive=400)
bayes_analysis.sample()
xyl.plot()
bayes_analysis.results.corner_plot()
INFO sampler set to dynesty_nested bayesian_analysis.py:186
4418it [00:05, 861.25it/s, +400 | bound: 13 | nc: 1 | ncall: 19518 | eff(%): 25.201 | loglstar: -inf < -11.175 < inf | logz: -21.165 +/- 0.150 | dlogz: 0.001 > 0.409]
17:47:57 INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.007 -0.021 +0.019 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0003 -0.0018 +0.0024) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.18151 |
| total | -11.18151 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.068902 |
| BIC | 28.354484 |
| DIC | 26.183630 |
| PDIC | 1.906263 |
| log(Z) | -9.191686 |
[7]:
[8]:
bayes_analysis.set_sampler("dynesty_dynamic")
bayes_analysis.sampler.setup()
if Version(dynesty.__version__) >= Version("3.0.0"):
bayes_analysis.sample(n_effective=None)
else:
bayes_analysis.sample(
stop_function=dynesty.utils.old_stopping_function, n_effective=None
)
xyl.plot()
bayes_analysis.results.corner_plot()
17:47:58 INFO sampler set to dynesty_dynamic bayesian_analysis.py:186
16304it [00:18, 899.73it/s, batch: 8 | bound: 4 | nc: 1 | ncall: 37916 | eff(%): 42.944 | loglstar: -15.916 < -11.174 < -11.707 | logz: -21.071 +/- 0.077 | stop: 0.912]
17:48:16 INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.007 +/- 0.021 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0004 -0.0022 +0.0023) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.181315 |
| total | -11.181315 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.068513 |
| BIC | 28.354095 |
| DIC | 26.333027 |
| PDIC | 1.985095 |
| log(Z) | -9.149370 |
[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()
17:48:17 INFO sampler set to zeus bayesian_analysis.py:186
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:05<00:00, 117.78it/s]
17:48:22 INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Summary
-------
Number of Generations: 625
Number of Parameters: 2
Number of Walkers: 20
Number of Tuning Generations: 39
Scale Factor: 1.02588
Mean Integrated Autocorrelation Time: 2.77
Effective Sample Size: 4515.08
Number of Log Probability Evaluations: 66639
Effective Samples per Log Probability Evaluation: 0.067754
None
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.007 -0.021 +0.020 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0004 -0.0022 +0.0023) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.181236 |
| total | -11.181236 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.068355 |
| BIC | 28.353937 |
| DIC | 26.405246 |
| PDIC | 2.020593 |
[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()
17:48:23 INFO sampler set to ultranest bayesian_analysis.py:186
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-1e+01
[ultranest] Likelihood function evaluations: 8503
[ultranest] logZ = -20.9 +- 0.09528
[ultranest] Effective samples strategy satisfied (ESS = 986.4, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-0.08 nat, need <0.50 nat)
[ultranest] Evidency uncertainty strategy is satisfied (dlogz=0.42, need <0.5)
[ultranest] logZ error budget: single: 0.15 bs:0.10 tail:0.41 total:0.42 required:<0.50
[ultranest] done iterating.
17:48:27 INFO fit restored to maximum of posterior sampler_base.py:168
INFO fit restored to maximum of posterior sampler_base.py:168
Maximum a posteriori probability (MAP) point:
| result | unit | |
|---|---|---|
| parameter | ||
| demo.spectrum.main.Sin.K | 1.008 -0.023 +0.019 | 1 / (keV s cm2) |
| demo.spectrum.main.Sin.f | (1.0005 -0.0021 +0.0022) x 10^-1 | rad / keV |
Values of -log(posterior) at the minimum:
| -log(posterior) | |
|---|---|
| demo | -11.183516 |
| total | -11.183516 |
Values of statistical measures:
| statistical measures | |
|---|---|
| AIC | 27.072915 |
| BIC | 28.358497 |
| DIC | 26.300029 |
| PDIC | 1.967939 |
| log(Z) | -9.065638 |
[10]: