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()
00:06:52 WARNING The naima package is not available. Models that depend on it will not be functions.py:47 available
WARNING The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it functions.py:68 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()
00:06:55 INFO sampler set to emcee bayesian_analysis.py:202
00:06:57 INFO Mean acceptance fraction: 0.7088 emcee_sampler.py:157
00:06:58 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 | (9.93 +/- 0.13) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.959 -0.034 +0.031) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.747515 |
total | -8.747515 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.200913 |
BIC | 23.486495 |
DIC | 21.720799 |
PDIC | 2.112838 |
[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()
00:06:59 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
*****************************************************
MultiNest Warning!
Parameter 1 of mode 5 is converging towards the edge of the prior.
analysing data from chains/fit-.txt ln(ev)= -18.851350892197942 +/- 0.15093621567797696
Total Likelihood Evaluations: 5623
Sampling finished. Exiting MultiNest
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 | (9.94 -0.12 +0.10) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.956 -0.031 +0.030) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.751447 |
total | -8.751447 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.208776 |
BIC | 23.494359 |
DIC | 21.289796 |
PDIC | 1.896275 |
log(Z) | -8.187038 |
INFO deleting the chain directory chains multinest_sampler.py:255
WARNING:root:Too few points to create valid contours
[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()
00:07:00 INFO sampler set to dynesty_nested bayesian_analysis.py:202
4464it [00:04, 995.55it/s, +400 | bound: 9 | nc: 1 | ncall: 20360 | eff(%): 24.369 | loglstar: -inf < -8.754 < inf | logz: -18.863 +/- 0.151 | dlogz: 0.001 > 0.409]
00:07:05 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 | (9.94 +/- 0.12) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.958 -0.032 +0.031) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.747634 |
total | -8.747634 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.201151 |
BIC | 23.486733 |
DIC | 21.437391 |
PDIC | 1.970579 |
log(Z) | -8.192202 |
[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()
00:07:06 INFO sampler set to dynesty_dynamic bayesian_analysis.py:202
7398it [00:07, 1358.23it/s, batch: 0 | bound: 13 | nc: 4 | ncall: 27530 | eff(%): 26.393 | loglstar: -inf < -8.765 < inf | logz: -18.939 +/- 0.135 | dlogz: 0.010 > 0.010]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
8705it [00:08, 1169.33it/s, batch: 1 | bound: 3 | nc: 1 | ncall: 29275 | eff(%): 29.304 | loglstar: -10.646 < -9.149 < -9.213 | logz: -18.929 +/- 0.139 | stop: 1.410]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
9558it [00:10, 1006.93it/s, batch: 2 | bound: 2 | nc: 1 | ncall: 30159 | eff(%): 31.499 | loglstar: -11.128 < -9.472 < -10.639 | logz: -18.930 +/- 0.113 | stop: 1.199]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
9783it [00:11, 544.93it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 30395 | eff(%): 31.646 | loglstar: -11.558 < -11.341 < -11.127 | logz: -18.931 +/- 0.106 | stop: 1.022]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases
10322it [00:11, 869.09it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 30959 | eff(%): 33.341 | loglstar: -11.558 < -8.779 < -11.127 | logz: -18.931 +/- 0.106 | stop: 0.850]
00:07:18 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 | (9.93 +/- 0.12) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.958 +/- 0.032) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.747237 |
total | -8.747237 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.200356 |
BIC | 23.485938 |
DIC | 21.549585 |
PDIC | 2.027583 |
log(Z) | -8.220186 |
[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()
00:07:19 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:09<00:00, 66.27it/s]
00:07:29 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: 35
Scale Factor: 1.164591
Mean Integrated Autocorrelation Time: 2.99
Effective Sample Size: 4180.1
Number of Log Probability Evaluations: 65810
Effective Samples per Log Probability Evaluation: 0.063518
None
Maximum a posteriori probability (MAP) point:
result | unit | |
---|---|---|
parameter | ||
demo.spectrum.main.Sin.K | (9.93 +/- 0.12) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.958 +/- 0.031) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.747222 |
total | -8.747222 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.200326 |
BIC | 23.485908 |
DIC | 21.447816 |
PDIC | 1.976284 |
[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()
00:07:30 INFO sampler set to ultranest bayesian_analysis.py:202
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-9
[ultranest] Likelihood function evaluations: 8559
[ultranest] logZ = -18.66 +- 0.1348
[ultranest] Effective samples strategy satisfied (ESS = 977.9, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-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.15 bs:0.13 tail:0.41 total:0.43 required:<0.50
[ultranest] done iterating.
00:07:38 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 | (9.94 +/- 0.12) x 10^-1 | 1 / (cm2 keV s) |
demo.spectrum.main.Sin.f | (9.960 -0.033 +0.029) x 10^-2 | rad / keV |
Values of -log(posterior) at the minimum:
-log(posterior) | |
---|---|
demo | -8.750222 |
total | -8.750222 |
Values of statistical measures:
statistical measures | |
---|---|
AIC | 22.206326 |
BIC | 23.491908 |
DIC | 21.483093 |
PDIC | 1.992386 |
log(Z) | -8.100086 |
[10]:


