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()
08:43:43 WARNING   The naima package is not available. Models that depend on it will not be         functions.py:50
                  available                                                                                        
         WARNING   The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it  functions.py:71
                  will not be available.                                                                           
08:43:44 WARNING   The ebltable package is not available. Models that depend on it will not be     absorption.py:36
                  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))
08:43:46 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:90
08:43:47 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:90
../_images/notebooks_sampler_docs_5_2.png

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:197
08:43:52 INFO      Mean acceptance fraction: 0.7109                                            emcee_sampler.py:157
08:43:53 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.17) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.978 +/- 0.022) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.665994
total -11.665994
Values of statistical measures:

statistical measures
AIC 28.037870
BIC 29.323452
DIC 27.297868
PDIC 1.983000
[5]:
../_images/notebooks_sampler_docs_7_12.png
../_images/notebooks_sampler_docs_7_13.png
../_images/notebooks_sampler_docs_7_14.png

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()
08:43:54 INFO      sampler set to multinest                                                bayesian_analysis.py:197
 *****************************************************
 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.620956748817559      +/-  0.14958920328776884
 Total Likelihood Evaluations:         6137
 Sampling finished. Exiting MultiNest

08:43:56 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.95 -0.18 +0.17) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.978 -0.022 +0.021) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.666902
total -11.666902
Values of statistical measures:

statistical measures
AIC 28.039687
BIC 29.325270
DIC 27.341229
PDIC 2.004385
log(Z) -9.389862
         INFO      deleting the chain directory chains                                     multinest_sampler.py:255
[6]:
../_images/notebooks_sampler_docs_9_11.png
../_images/notebooks_sampler_docs_9_12.png
../_images/notebooks_sampler_docs_9_13.png

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()
08:43:57 INFO      sampler set to dynesty_nested                                           bayesian_analysis.py:197
4303it [00:07, 575.00it/s, +400 | bound: 9 | nc: 1 | ncall: 19380 | eff(%): 24.779 | loglstar:   -inf < -11.672 <    inf | logz: -21.379 +/-  0.148 | dlogz:  0.001 >  0.409]
08:44: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.17) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.979 +/- 0.023) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.665899
total -11.665899
Values of statistical measures:

statistical measures
AIC 28.037681
BIC 29.323263
DIC 27.378708
PDIC 2.023335
log(Z) -9.284989
[7]:
../_images/notebooks_sampler_docs_11_10.png
../_images/notebooks_sampler_docs_11_11.png
../_images/notebooks_sampler_docs_11_12.png
[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()
08:44:06 INFO      sampler set to dynesty_dynamic                                          bayesian_analysis.py:197
7668it [00:12, 1155.50it/s, batch: 0 | bound: 13 | nc: 1 | ncall: 27925 | eff(%): 27.134 | loglstar:   -inf < -11.679 <    inf | logz: -22.058 +/-  0.136 | dlogz:  0.007 >  0.010]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9046it [00:14, 1263.04it/s, batch: 1 | bound: 3 | nc: 1 | ncall: 29712 | eff(%): 30.243 | loglstar: -13.617 < -11.848 < -12.144 | logz: -22.052 +/-  0.140 | stop:  1.396]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9805it [00:15, 863.56it/s, batch: 2 | bound: 2 | nc: 1 | ncall: 30511 | eff(%): 32.092 | loglstar: -14.158 < -11.817 < -13.616 | logz: -22.047 +/-  0.114 | stop:  1.213]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

10030it [00:17, 397.99it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 30762 | eff(%): 32.163 | loglstar: -14.559 < -13.584 < -14.157 | logz: -22.025 +/-  0.106 | stop:  1.029]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

10412it [00:18, 577.47it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 31185 | eff(%): 33.388 | loglstar: -14.559 < -11.676 < -14.157 | logz: -22.025 +/-  0.106 | stop:  0.876]
08:44:25 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.17) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.978 -0.022 +0.023) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.665923
total -11.665923
Values of statistical measures:

statistical measures
AIC 28.037729
BIC 29.323311
DIC 27.430420
PDIC 2.049181
log(Z) -9.558822
[8]:
../_images/notebooks_sampler_docs_12_10.png
../_images/notebooks_sampler_docs_12_11.png
../_images/notebooks_sampler_docs_12_12.png

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:197
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:18<00:00, 34.34it/s]
08:44:45 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: 37
Scale Factor: 1.171758
Mean Integrated Autocorrelation Time: 3.13
Effective Sample Size: 3993.7
Number of Log Probability Evaluations: 65432
Effective Samples per Log Probability Evaluation: 0.061036
None
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K (9.94 -0.18 +0.16) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.979 +/- 0.023) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.665884
total -11.665884
Values of statistical measures:

statistical measures
AIC 28.037651
BIC 29.323233
DIC 27.269658
PDIC 1.968023
[9]:
../_images/notebooks_sampler_docs_14_12.png
../_images/notebooks_sampler_docs_14_13.png
../_images/notebooks_sampler_docs_14_14.png

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()
08:44:46 INFO      sampler set to ultranest                                                bayesian_analysis.py:197
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-1e+01
[ultranest] Likelihood function evaluations: 9540
[ultranest]   logZ = -22.03 +- 0.1199
[ultranest] Effective samples strategy satisfied (ESS = 979.8, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-0.06 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.12 tail:0.41 total:0.42 required:<0.50
[ultranest] done iterating.
08:44: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.94 -0.16 +0.17) x 10^-1 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.978 -0.021 +0.024) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

-log(posterior)
demo -11.666404
total -11.666404
Values of statistical measures:

statistical measures
AIC 28.038691
BIC 29.324273
DIC 27.270813
PDIC 1.969264
log(Z) -9.570107
[10]:
../_images/notebooks_sampler_docs_16_12.png
../_images/notebooks_sampler_docs_16_13.png
../_images/notebooks_sampler_docs_16_14.png