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()
22:46:08 WARNING   The naima package is not available. Models that depend on it will not be         functions.py:45
                  available                                                                                        
         WARNING   The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it  functions.py:67
                  will not be available.                                                                           
         WARNING   The ebltable package is not available. Models that depend on it will not be     absorption.py:34
                  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:46:10 INFO      Using Gaussian statistic (equivalent to chi^2) with the provided errors.            XYLike.py:90
         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:186
22:46:12 INFO      Mean acceptance fraction: 0.7125999999999999                                emcee_sampler.py:145
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.005 -0.018 +0.019 1 / (keV s cm2)
demo.spectrum.main.Sin.f (10.00 -0.07 +0.08) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.121609
BIC 19.407191
DIC 17.334324
PDIC 1.957055
[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:46:13 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)=  -15.550546591119319      +/-  0.14002755071674625
 Total Likelihood Evaluations:         4804
 Sampling finished. Exiting MultiNest
22:46:14 INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.004 -0.022 +0.020 1 / (keV s cm2)
demo.spectrum.main.Sin.f (1.000 -0.008 +0.005) x 10^-1 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.127722
BIC 19.413305
DIC 17.467584
PDIC 2.022813
log(Z) -6.753517
         INFO      deleting the chain directory chains                                     multinest_sampler.py:234
[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(nlive=400)
bayes_analysis.sample()

xyl.plot()
bayes_analysis.results.corner_plot()
         INFO      sampler set to dynesty_nested                                           bayesian_analysis.py:186
3998it [00:04, 939.22it/s, +400 | bound: 10 | nc: 1 | ncall: 18703 | eff(%): 24.029 | loglstar:   -inf < -6.703 <    inf | logz: -15.649 +/-  0.141 | dlogz:  0.001 >  0.409]
22:46:18 INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Maximum a posteriori probability (MAP) point:

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

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

statistical measures
AIC 18.122402
BIC 19.407984
DIC 17.505967
PDIC 2.045229
log(Z) -6.796479
[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()

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()
22:46:19 INFO      sampler set to dynesty_dynamic                                          bayesian_analysis.py:186
15879it [00:16, 964.11it/s, batch: 8 | bound: 4 | nc: 1 | ncall: 37184 | eff(%): 42.682 | loglstar: -11.479 < -6.704 < -7.164 | logz: -15.706 +/-  0.074 | stop:  0.908]
22:46:36 INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.004 +/- 0.021 1 / (keV s cm2)
demo.spectrum.main.Sin.f (10.00 +/- 0.07) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.121338
BIC 19.406921
DIC 17.492308
PDIC 2.038490
log(Z) -6.819061
[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:186
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:05<00:00, 122.04it/s]
22:46:41 INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Summary
-------
Number of Generations: 625
Number of Parameters: 2
Number of Walkers: 20
Number of Tuning Generations: 33
Scale Factor: 1.085172
Mean Integrated Autocorrelation Time: 3.3
Effective Sample Size: 3791.1
Number of Log Probability Evaluations: 65733
Effective Samples per Log Probability Evaluation: 0.057674
None
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.004 -0.021 +0.020 1 / (keV s cm2)
demo.spectrum.main.Sin.f (10.00 +/- 0.07) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.121293
BIC 19.406875
DIC 17.494795
PDIC 2.039342
[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:46:42 INFO      sampler set to ultranest                                                bayesian_analysis.py:186
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-7
[ultranest] Likelihood function evaluations: 8770
[ultranest]   logZ = -15.3 +- 0.09622
[ultranest] Effective samples strategy satisfied (ESS = 975.3, 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.14 bs:0.10 tail:0.41 total:0.42 required:<0.50
[ultranest] done iterating.
22:46:46 INFO      fit restored to maximum of posterior                                         sampler_base.py:167
         INFO      fit restored to maximum of posterior                                         sampler_base.py:167
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.004 +/- 0.021 1 / (keV s cm2)
demo.spectrum.main.Sin.f (10.00 -0.07 +0.08) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.122170
BIC 19.407752
DIC 17.517283
PDIC 2.050911
log(Z) -6.651643
[10]:
../_images/notebooks_sampler_docs_16_12.png
../_images/notebooks_sampler_docs_16_13.png
../_images/notebooks_sampler_docs_16_14.png