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()
[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))
../_images/notebooks_sampler_docs_5_0.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()
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.035 -0.027 +0.032 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 -0.04 +0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.002062
BIC 19.287644
DIC 17.502876
PDIC 2.096617
[5]:
../_images/notebooks_sampler_docs_7_8.png
../_images/notebooks_sampler_docs_7_9.png
../_images/notebooks_sampler_docs_7_10.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()
 *****************************************************
 MultiNest v3.10
 Copyright Farhan Feroz & Mike Hobson
 Release Jul 2015

 no. of live points =  400
 dimensionality =    2
 *****************************************************
 Starting MultiNest
 generating live points
 live points generated, starting sampling
Acceptance Rate:                        0.993377
Replacements:                                450
Total Samples:                               453
Nested Sampling ln(Z):              -1948.451433
Acceptance Rate:                        0.980392
Replacements:                                500
Total Samples:                               510
Nested Sampling ln(Z):              -1656.354501
Acceptance Rate:                        0.940171
Replacements:                                550
Total Samples:                               585
Nested Sampling ln(Z):              -1428.232552
Acceptance Rate:                        0.909091
Replacements:                                600
Total Samples:                               660
Nested Sampling ln(Z):              -1244.526444
Acceptance Rate:                        0.844156
Replacements:                                650
Total Samples:                               770
Nested Sampling ln(Z):              -1133.334442
Acceptance Rate:                        0.824499
Replacements:                                700
Total Samples:                               849
Nested Sampling ln(Z):              -1093.257666
Acceptance Rate:                        0.815217
Replacements:                                750
Total Samples:                               920
Nested Sampling ln(Z):              -1022.016563
Acceptance Rate:                        0.809717
Replacements:                                800
Total Samples:                               988
Nested Sampling ln(Z):               -970.889752
Acceptance Rate:                        0.812620
Replacements:                                850
Total Samples:                              1046
Nested Sampling ln(Z):               -927.704411
Acceptance Rate:                        0.802855
Replacements:                                900
Total Samples:                              1121
Nested Sampling ln(Z):               -884.252661
Acceptance Rate:                        0.798991
Replacements:                                950
Total Samples:                              1189
Nested Sampling ln(Z):               -830.245349
Acceptance Rate:                        0.798085
Replacements:                               1000
Total Samples:                              1253
Nested Sampling ln(Z):               -767.768844
Acceptance Rate:                        0.796662
Replacements:                               1050
Total Samples:                              1318
Nested Sampling ln(Z):               -711.723588
Acceptance Rate:                        0.790798
Replacements:                               1100
Total Samples:                              1391
Nested Sampling ln(Z):               -672.318228
Acceptance Rate:                        0.785666
Replacements:                               1151
Total Samples:                              1465
Nested Sampling ln(Z):               -645.628960
Acceptance Rate:                        0.764670
Replacements:                               1251
Total Samples:                              1636
Nested Sampling ln(Z):               -541.809710
Acceptance Rate:                        0.742716
Replacements:                               1351
Total Samples:                              1819
Nested Sampling ln(Z):               -408.809179
Acceptance Rate:                        0.736175
Replacements:                               1451
Total Samples:                              1971
Nested Sampling ln(Z):               -324.591193
Acceptance Rate:                        0.738220
Replacements:                               1551
Total Samples:                              2101
Nested Sampling ln(Z):               -268.867365
Acceptance Rate:                        0.737383
Replacements:                               1651
Total Samples:                              2239
Nested Sampling ln(Z):               -218.774652
Acceptance Rate:                        0.740381
Replacements:                               1751
Total Samples:                              2365
Nested Sampling ln(Z):               -188.254896
Acceptance Rate:                        0.739217
Replacements:                               1851
Total Samples:                              2504
Nested Sampling ln(Z):               -164.683600
Acceptance Rate:                        0.740980
Replacements:                               1951
Total Samples:                              2633
Nested Sampling ln(Z):               -143.620663
Acceptance Rate:                        0.740968
Replacements:                               2051
Total Samples:                              2768
Nested Sampling ln(Z):               -124.060099
Acceptance Rate:                        0.742236
Replacements:                               2151
Total Samples:                              2898
Nested Sampling ln(Z):               -108.309722
Acceptance Rate:                        0.741436
Replacements:                               2251
Total Samples:                              3036
Nested Sampling ln(Z):                -94.424976
Acceptance Rate:                        0.743517
Replacements:                               2351
Total Samples:                              3162
Nested Sampling ln(Z):                -83.191661
Acceptance Rate:                        0.738475
Replacements:                               2451
Total Samples:                              3319
Nested Sampling ln(Z):                -71.611882
Acceptance Rate:                        0.735795
Replacements:                               2551
Total Samples:                              3467
Nested Sampling ln(Z):                -63.251201
Acceptance Rate:                        0.733131
Replacements:                               2651
Total Samples:                              3616
Nested Sampling ln(Z):                -53.682256
Acceptance Rate:                        0.732003
Replacements:                               2715
Total Samples:                              3709
Nested Sampling ln(Z):                -45.842652
Acceptance Rate:                        0.726294
Replacements:                               2765
Total Samples:                              3807
Nested Sampling ln(Z):                -41.184104
Acceptance Rate:                        0.723464
Replacements:                               2815
Total Samples:                              3891
Nested Sampling ln(Z):                -37.468254
Acceptance Rate:                        0.717685
Replacements:                               2865
Total Samples:                              3992
Nested Sampling ln(Z):                -33.633668
Acceptance Rate:                        0.713586
Replacements:                               2915
Total Samples:                              4085
Nested Sampling ln(Z):                -30.834427
Acceptance Rate:                        0.709840
Replacements:                               2965
Total Samples:                              4177
Nested Sampling ln(Z):                -28.413274
Acceptance Rate:                        0.708412
Replacements:                               3015
Total Samples:                              4256
Nested Sampling ln(Z):                -26.724997
Acceptance Rate:                        0.707525
Replacements:                               3065
Total Samples:                              4332
Nested Sampling ln(Z):                -24.993423
Acceptance Rate:                        0.706990
Replacements:                               3115
Total Samples:                              4406
Nested Sampling ln(Z):                -23.670092
Acceptance Rate:                        0.706000
Replacements:                               3165
Total Samples:                              4483
Nested Sampling ln(Z):                -22.250088
Acceptance Rate:                        0.703655
Replacements:                               3215
Total Samples:                              4569
Nested Sampling ln(Z):                -21.006891
Acceptance Rate:                        0.702302
Replacements:                               3265
Total Samples:                              4649
Nested Sampling ln(Z):                -19.956385
Acceptance Rate:                        0.702777
Replacements:                               3315
Total Samples:                              4717
Nested Sampling ln(Z):                -19.168090
Acceptance Rate:                        0.701188
Replacements:                               3365
Total Samples:                              4799
Nested Sampling ln(Z):                -18.506460
Acceptance Rate:                        0.701664
Replacements:                               3415
Total Samples:                              4867
Nested Sampling ln(Z):                -17.997903
Acceptance Rate:                        0.700424
Replacements:                               3465
Total Samples:                              4947
Nested Sampling ln(Z):                -17.578489
Acceptance Rate:                        0.700060
Replacements:                               3515
Total Samples:                              5021
Nested Sampling ln(Z):                -17.222908
Acceptance Rate:                        0.698746
Replacements:                               3565
Total Samples:                              5102
Nested Sampling ln(Z):                -16.910023
Acceptance Rate:                        0.699091
Replacements:                               3615
Total Samples:                              5171
Nested Sampling ln(Z):                -16.639640
Acceptance Rate:                        0.697697
Replacements:                               3665
Total Samples:                              5253
Nested Sampling ln(Z):                -16.410522
Acceptance Rate:                        0.697129
Replacements:                               3715
Total Samples:                              5329
Nested Sampling ln(Z):                -16.222182
Acceptance Rate:                        0.697481
Replacements:                               3765
Total Samples:                              5398
Nested Sampling ln(Z):                -16.062304
Acceptance Rate:                        0.697951
Replacements:                               3815
Total Samples:                              5466
Nested Sampling ln(Z):                -15.927397
Acceptance Rate:                        0.698789
Replacements:                               3865
Total Samples:                              5531
Nested Sampling ln(Z):                -15.812683
Acceptance Rate:                        0.698733
Replacements:                               3915
Total Samples:                              5603
Nested Sampling ln(Z):                -15.716837
Acceptance Rate:                        0.698802
Replacements:                               3965
Total Samples:                              5674
Nested Sampling ln(Z):                -15.636027
Acceptance Rate:                        0.698869
Replacements:                               4015
Total Samples:                              5745
Nested Sampling ln(Z):                -15.568963
Acceptance Rate:                        0.698854
Replacements:                               4024
Total Samples:                              5758
Nested Sampling ln(Z):                -15.557998
  analysing data from chains/fit-.txt
 ln(ev)=  -15.212928382421730      +/-  0.13739045566475439
 Total Likelihood Evaluations:         5758
 Sampling finished. Exiting MultiNest
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.034 -0.030 +0.029 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.004385
BIC 19.289968
DIC 17.486319
PDIC 2.094097
log(Z) -6.606891
[6]:
../_images/notebooks_sampler_docs_9_7.png
../_images/notebooks_sampler_docs_9_8.png
../_images/notebooks_sampler_docs_9_9.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()
100%|█████████▉| 3937/3938 [00:05<00:00, 780.35it/s, +400 | bound: 10 | nc: 1 | ncall: 18836 | eff(%): 23.525 | loglstar: -6.613 | logz: -15.405 +/-  0.140 | dlogz:  0.001 >  0.409]
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.035 -0.033 +0.030 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.002302
BIC 19.287885
DIC 17.353010
PDIC 2.026555
log(Z) -6.690142
[7]:
../_images/notebooks_sampler_docs_11_7.png
../_images/notebooks_sampler_docs_11_8.png
../_images/notebooks_sampler_docs_11_9.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()
100%|█████████▉| 15851/15853 [00:17<00:00, 883.75it/s, batch: 8 | bound: 4 | nc: 1 | ncall: 36911 | eff(%): 42.903 | loglstar: -11.430 < -6.614 < -7.176 | logz: -15.368 +/-  0.072 | stop:  0.901]
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.035 -0.030 +0.029 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.001913
BIC 19.287495
DIC 17.302029
PDIC 2.002796
log(Z) -6.668518
[8]:
../_images/notebooks_sampler_docs_12_7.png
../_images/notebooks_sampler_docs_12_8.png
../_images/notebooks_sampler_docs_12_9.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()
Initialising ensemble of 20 walkers...
Sampling progress : 100%|██████████| 625/625 [00:04<00:00, 125.33it/s]
fit restored to maximum of posterior
fit restored to maximum of posterior
Summary
-------
Number of Generations: 625
Number of Parameters: 2
Number of Walkers: 20
Number of Tuning Generations: 16
Scale Factor: 1.276222
Mean Integrated Autocorrelation Time: 3.02
Effective Sample Size: 4140.46
Number of Log Probability Evaluations: 65390
Effective Samples per Log Probability Evaluation: 0.063319
None
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.035 -0.029 +0.028 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.002583
BIC 19.288166
DIC 17.103151
PDIC 1.903519
[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()
sampler set to [blue]ultranest[/blue]
[ultranest] Sampling 400 live points from prior ...
[ultranest] Explored until L=-7
[ultranest] Likelihood function evaluations: 8219
[ultranest]   logZ = -15.35 +- 0.1336
[ultranest] Effective samples strategy satisfied (ESS = 980.6, need >400)
[ultranest] Posterior uncertainty strategy is satisfied (KL: 0.46+-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.14 bs:0.13 tail:0.41 total:0.43 required:<0.50
[ultranest] done iterating.
fit restored to maximum of posterior
fit restored to maximum of posterior
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.035 -0.029 +0.030 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 -0.05 +0.04) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.004896
BIC 19.290478
DIC 17.306719
PDIC 2.005192
log(Z) -6.667701
[10]:
../_images/notebooks_sampler_docs_16_11.png
../_images/notebooks_sampler_docs_16_12.png
../_images/notebooks_sampler_docs_16_13.png

nautilus

[11]:
bayes_analysis.set_sampler("nautilus")
bayes_analysis.sampler.setup(n_live=3000, verbose=False)
bayes_analysis.sample()

xyl.plot()
bayes_analysis.results.corner_plot()
sampler set to [blue]nautilus[/blue]
Starting the nautilus sampler...
Please report issues at github.com/johannesulf/nautilus.
Status    | Bounds | Ellipses | Networks | Calls    | f_live | N_eff | log Z
Finished  | 20     | 1        | 4        | 60700    | N/A    | 16471 | -15.39
fit restored to maximum of posterior
fit restored to maximum of posterior
Maximum a posteriori probability (MAP) point:

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

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

statistical measures
AIC 18.001934
BIC 19.287516
DIC 17.287258
PDIC 1.994807
log(Z) -15.390948
[11]:
../_images/notebooks_sampler_docs_18_9.png
../_images/notebooks_sampler_docs_18_10.png
../_images/notebooks_sampler_docs_18_11.png

autoemcee

[12]:
bayes_analysis.set_sampler("autoemcee")
bayes_analysis.sampler.setup(
    num_global_samples=10000,
    num_chains=4,
    num_walkers=None,
    max_ncalls=1000000,
    max_improvement_loops=4,
    num_initial_steps=100,
    min_autocorr_times=0,
)
bayes_analysis.sample()

xyl.plot()
bayes_analysis.results.corner_plot()
sampler set to [blue]autoemcee[/blue]
[autoemcee] finding starting points and running initial 100 MCMC steps
finding starting points and running initial 100 MCMC steps
global sampling for starting point ...
100%|██████████| 100/100 [00:00<00:00, 259.90it/s]
global sampling for starting point ...
100%|██████████| 100/100 [00:00<00:00, 259.38it/s]
global sampling for starting point ...
100%|██████████| 100/100 [00:00<00:00, 260.40it/s]
global sampling for starting point ...
100%|██████████| 100/100 [00:00<00:00, 257.48it/s]
checking convergence (iteration 1) ...
acceptance rates: [52 52 56 57 57 57 59 59]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [51 57 57 58 58 58 58 59]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [55 56 57 58 58 58 59 60]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [48 54 56 56 57 58 58 59]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
[autoemcee] rhat chain diagnostic: [1.02294326 1.02694039] (<1.010 is good)
rhat chain diagnostic: [1.02294326 1.02694039] (<1.010 is good)
[autoemcee] not converged yet at iteration 1 after 80400 evals
not converged yet at iteration 1 after 80400 evals
expected memory usage: 0.00 GiB
[autoemcee] Running 1000 MCMC steps ...
Running 1000 MCMC steps ...
[autoemcee] Starting points chosen: {np.int64(34)}, L=-7.1
Starting points chosen: {np.int64(34)}, L=-7.1
[autoemcee] Starting at [0.61446967 0.20094027] +- [1.24454209e-04 4.33515844e-06]
Starting at [0.61446967 0.20094027] +- [1.24454209e-04 4.33515844e-06]
100%|██████████| 100/100 [00:00<00:00, 262.97it/s]
100%|██████████| 1000/1000 [00:03<00:00, 261.30it/s]
[autoemcee] Starting points chosen: {np.int64(61)}, L=-7.1

Starting points chosen: {np.int64(61)}, L=-7.1
[autoemcee] Starting at [0.66334636 0.19838521] +- [1.26793550e-04 4.67471209e-06]
Starting at [0.66334636 0.19838521] +- [1.26793550e-04 4.67471209e-06]
100%|██████████| 100/100 [00:00<00:00, 265.32it/s]
100%|██████████| 1000/1000 [00:03<00:00, 260.50it/s]
[autoemcee] Starting points chosen: {np.int64(35)}, L=-7.1

Starting points chosen: {np.int64(35)}, L=-7.1
[autoemcee] Starting at [0.63873821 0.20080976] +- [1.36191742e-04 4.04145569e-06]
Starting at [0.63873821 0.20080976] +- [1.36191742e-04 4.04145569e-06]
100%|██████████| 100/100 [00:00<00:00, 261.03it/s]
100%|██████████| 1000/1000 [00:03<00:00, 261.71it/s]
[autoemcee] Starting points chosen: {np.int64(94)}, L=-7.1

Starting points chosen: {np.int64(94)}, L=-7.1
[autoemcee] Starting at [0.65475846 0.19937308] +- [1.41391620e-04 3.70338761e-06]
Starting at [0.65475846 0.19937308] +- [1.41391620e-04 3.70338761e-06]
100%|██████████| 100/100 [00:00<00:00, 263.34it/s]
100%|██████████| 1000/1000 [00:03<00:00, 263.29it/s]
[autoemcee] Used 440800 calls in last MCMC run

Used 440800 calls in last MCMC run
checking convergence (iteration 2) ...
acceptance rates: [67 68 68 68 68 68 68 68]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [66 67 67 68 68 69 69 69]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [67 68 68 68 69 69 69 69]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
acceptance rates: [68 68 69 69 69 69 69 69]% (worst few)
autocorrelation length: tau=inf -> 0x lengths
[autoemcee] rhat chain diagnostic: [1.00026679 1.00041241] (<1.010 is good)
rhat chain diagnostic: [1.00026679 1.00041241] (<1.010 is good)
[autoemcee] converged!!!
converged!!!
fit restored to maximum of posterior
fit restored to maximum of posterior
Maximum a posteriori probability (MAP) point:

result unit
parameter
demo.spectrum.main.Sin.K 1.036 -0.031 +0.028 1 / (keV s cm2)
demo.spectrum.main.Sin.f (9.97 +/- 0.05) x 10^-2 rad / keV
Values of -log(posterior) at the minimum:

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

statistical measures
AIC 18.002642
BIC 19.288224
DIC 17.130400
PDIC 1.916702
[12]:
../_images/notebooks_sampler_docs_20_37.png
../_images/notebooks_sampler_docs_20_38.png
../_images/notebooks_sampler_docs_20_39.png