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 *

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()
Welcome to JupyROOT 6.22/02
09:58:00 WARNING   The naima package is not available. Models that depend on it will not be         functions.py:51
                  available                                                                                        
         WARNING   The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it  functions.py:72
                  will not be available.                                                                           
09:58:01 WARNING   The ebltable package is not available. Models that depend on it will not be     absorption.py:37
                  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))
09:58:04 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:62
09:58:05 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:62
../_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:233
09:58:10 INFO      Mean acceptance fraction: 0.7218                                            emcee_sampler.py:157
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.020 -0.012 +0.011 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.081735
total -8.081735

Values of statistical measures:

statistical measures
AIC 20.869352
BIC 22.154934
DIC 20.161992
PDIC 1.997216
[5]:
../_images/notebooks_sampler_docs_7_10.png
../_images/notebooks_sampler_docs_7_11.png
../_images/notebooks_sampler_docs_7_12.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()
09:58:12 INFO      sampler set to multinest                                                bayesian_analysis.py:233
  analysing data from chains/fit-.txt
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.019 -0.010 +0.011 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.96 +/- 0.05) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.085015
total -8.085015

Values of statistical measures:

statistical measures
AIC 20.875913
BIC 22.161495
DIC 19.825971
PDIC 1.831060
log(Z) -7.623362
09:58:13 INFO      deleting the chain directory chains                                     multinest_sampler.py:261
WARNING:root:Too few points to create valid contours
[6]:
../_images/notebooks_sampler_docs_9_9.png
 *****************************************************
 MultiNest v3.10
 Copyright Farhan Feroz & Mike Hobson
 Release Jul 2015

 no. of live points =  400
 dimensionality =    2
 *****************************************************
 ln(ev)=  -17.553440424512324      +/-  0.14584873865067560
 Total Likelihood Evaluations:         5454
 Sampling finished. Exiting MultiNest
../_images/notebooks_sampler_docs_9_11.png
../_images/notebooks_sampler_docs_9_12.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()
09:58:14 INFO      sampler set to dynesty_nested                                           bayesian_analysis.py:233
4176it [00:11, 377.25it/s, +400 | bound: 9 | nc: 1 | ncall: 19764 | eff(%): 23.631 | loglstar:   -inf < -8.063 <    inf | logz: -17.447 +/-  0.144 | dlogz:  0.001 >  0.409]
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.019 -0.011 +0.010 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.97 -0.05 +0.06) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.0825
total -8.0825

Values of statistical measures:

statistical measures
AIC 20.870882
BIC 22.156464
DIC 19.896201
PDIC 1.865633
log(Z) -7.577113
[7]:
../_images/notebooks_sampler_docs_11_8.png
../_images/notebooks_sampler_docs_11_9.png
../_images/notebooks_sampler_docs_11_10.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()
09:58:27 INFO      sampler set to dynesty_dynamic                                          bayesian_analysis.py:233
7407it [00:16, 1216.21it/s, batch: 0 | bound: 13 | nc: 1 | ncall: 26653 | eff(%): 27.614 | loglstar:   -inf < -8.066 <    inf | logz: -17.594 +/-  0.130 | dlogz:  0.003 >  0.010]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

8701it [00:19, 1318.75it/s, batch: 1 | bound: 3 | nc: 1 | ncall: 28339 | eff(%): 30.686 | loglstar: -9.880 < -8.079 < -8.560 | logz: -17.590 +/-  0.134 | stop:  1.280]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9310it [00:21, 622.68it/s, batch: 2 | bound: 2 | nc: 1 | ncall: 29010 | eff(%): 32.084 | loglstar: -10.360 < -8.110 < -9.878 | logz: -17.591 +/-  0.110 | stop:  1.091]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9530it [00:22, 324.15it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 29251 | eff(%): 32.124 | loglstar: -10.771 < -9.792 < -10.358 | logz: -17.590 +/-  0.103 | stop:  1.009]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9909it [00:23, 423.10it/s, batch: 3 | bound: 2 | nc: 1 | ncall: 29666 | eff(%): 33.402 | loglstar: -10.771 < -8.068 < -10.358 | logz: -17.590 +/-  0.103 | stop:  0.773]
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.019 +/- 0.011 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.96 +/- 0.06) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.081914
total -8.081914

Values of statistical measures:

statistical measures
AIC 20.869710
BIC 22.155292
DIC 20.210208
PDIC 2.023137
log(Z) -7.640340
[8]:
../_images/notebooks_sampler_docs_12_8.png
../_images/notebooks_sampler_docs_12_9.png
../_images/notebooks_sampler_docs_12_10.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()
09:58:53 INFO      sampler set to zeus                                                     bayesian_analysis.py:233
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:16<00:00, 38.95it/s]
Summary
-------
Number of Generations: 625
Number of Parameters: 2
Number of Walkers: 20
Number of Tuning Generations: 29
Scale Factor: 1.326089
Mean Integrated Autocorrelation Time: 3.27
Effective Sample Size: 3826.83
Number of Log Probability Evaluations: 65008
Effective Samples per Log Probability Evaluation: 0.058867
None
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.019 +/- 0.011 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.97 +/- 0.06) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.081847
total -8.081847

Values of statistical measures:

statistical measures
AIC 20.869575
BIC 22.155158
DIC 20.116406
PDIC 1.976342
[9]:
../_images/notebooks_sampler_docs_14_8.png
../_images/notebooks_sampler_docs_14_9.png
../_images/notebooks_sampler_docs_14_10.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()
09:59:11 INFO      sampler set to ultranest                                                bayesian_analysis.py:233
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-8
[ultranest] Likelihood function evaluations: 7209
[ultranest]   logZ = -17.67 +- 0.1081
[ultranest] Effective samples strategy satisfied (ESS = 987.6, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.45+-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.11 tail:0.41 total:0.42 required:<0.50
[ultranest] done iterating.
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.019 +/- 0.011 1 / (cm2 keV s)
demo.spectrum.main.Sin.f (9.96 -0.06 +0.05) x 10^-2 rad / keV

Values of -log(posterior) at the minimum:

-log(posterior)
demo -8.082356
total -8.082356

Values of statistical measures:

statistical measures
AIC 20.870594
BIC 22.156176
DIC 20.170178
PDIC 2.003036
log(Z) -7.675569
[10]:
../_images/notebooks_sampler_docs_16_9.png
../_images/notebooks_sampler_docs_16_10.png
../_images/notebooks_sampler_docs_16_11.png