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:55:43 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.                                                                           
22:55:44 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))
22:55:46 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:93
22:55:47 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:93
../_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:202
22:55:52 INFO      Mean acceptance fraction: 0.7249000000000001                                emcee_sampler.py:157
         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.74 -0.18 +0.17) x 10^-1 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.005 +/- 0.006) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 16.664804
BIC 17.950386
DIC 15.784568
PDIC 1.912365
[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()
22:55:53 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)=  -14.990971507685240      +/-  0.14143843416375856
 Total Likelihood Evaluations:         5359
 Sampling finished. Exiting MultiNest

22:55:54 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.74 +/- 0.17) x 10^-1 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.005 +/- 0.006) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 16.665120
BIC 17.950702
DIC 15.803547
PDIC 1.922119
log(Z) -6.510496
         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()
22:55:55 INFO      sampler set to dynesty_nested                                           bayesian_analysis.py:202
3998it [00:05, 760.08it/s, +400 | bound: 8 | nc: 1 | ncall: 19278 | eff(%): 23.297 | loglstar:   -inf < -6.006 <    inf | logz: -14.953 +/-  0.141 | dlogz:  0.001 >  0.409]
22:56:01 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.74 -0.18 +0.17) x 10^-1 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.004 -0.006 +0.007) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 16.665438
BIC 17.951020
DIC 15.917192
PDIC 1.978146
log(Z) -6.493893
[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()
22:56:02 INFO      sampler set to dynesty_dynamic                                          bayesian_analysis.py:202
6874it [00:07, 1203.70it/s, batch: 0 | bound: 12 | nc: 1 | ncall: 27077 | eff(%): 24.927 | loglstar:   -inf < -6.016 <    inf | logz: -15.184 +/-  0.128 | dlogz:  0.010 >  0.010]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

8205it [00:09, 910.94it/s, batch: 1 | bound: 2 | nc: 1 | ncall: 28872 | eff(%): 28.052 | loglstar: -7.809 < -6.333 < -6.464 | logz: -15.174 +/-  0.132 | stop:  1.484]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

8822it [00:10, 814.12it/s, batch: 2 | bound: 1 | nc: 1 | ncall: 29533 | eff(%): 29.492 | loglstar: -8.342 < -7.316 < -7.800 | logz: -15.177 +/-  0.108 | stop:  1.136]
WARNING DeprecationWarning: This an old stopping function that will be removed in future releases

9188it [00:11, 800.32it/s, batch: 2 | bound: 1 | nc: 1 | ncall: 29899 | eff(%): 30.716 | loglstar: -8.342 < -6.006 < -7.800 | logz: -15.177 +/-  0.108 | stop:  0.918]
22:56:14 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.74 -0.18 +0.17) x 10^-1 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.005 +/- 0.006) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 16.664854
BIC 17.950436
DIC 15.878479
PDIC 1.958974
log(Z) -6.589475
[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: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:13<00:00, 48.04it/s]
22:56:28 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: 23
Scale Factor: 1.203297
Mean Integrated Autocorrelation Time: 2.88
Effective Sample Size: 4347.45
Number of Log Probability Evaluations: 65233
Effective Samples per Log Probability Evaluation: 0.066645
None
Maximum a posteriori probability (MAP) point:

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

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

statistical measures
AIC 16.665009
BIC 17.950591
DIC 16.075614
PDIC 2.058331
[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()
22:56:29 INFO      sampler set to ultranest                                                bayesian_analysis.py:202
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-6
[ultranest] Likelihood function evaluations: 7608
[ultranest]   logZ = -15.09 +- 0.09011
[ultranest] Effective samples strategy satisfied (ESS = 975.1, 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.14 bs:0.09 tail:0.41 total:0.42 required:<0.50
[ultranest] done iterating.
22:56: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.73 -0.17 +0.18) x 10^-1 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.005 -0.007 +0.005) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 16.674272
BIC 17.959854
DIC 15.909811
PDIC 1.975243
log(Z) -6.552318
[10]:
../_images/notebooks_sampler_docs_16_12.png
../_images/notebooks_sampler_docs_16_13.png
../_images/notebooks_sampler_docs_16_14.png