release 0.1.0.dev2 he jia and uros seljak

29
bayesfast documentation Release 0.1.0.dev2 He Jia and Uros Seljak Oct 06, 2021

Upload: others

Post on 15-Oct-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentationRelease 0.1.0.dev2

He Jia and Uros Seljak

Oct 06, 2021

Page 2: Release 0.1.0.dev2 He Jia and Uros Seljak
Page 3: Release 0.1.0.dev2 He Jia and Uros Seljak

BayesFast

1 Links 3

2 Installation 5

3 Dependencies 7

4 License 9

5 Citing BayesFast 115.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115.2 API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Index 25

i

Page 4: Release 0.1.0.dev2 He Jia and Uros Seljak

ii

Page 5: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

BayesFast is a Python package for efficient Bayesian analysis developed by He Jia and Uros Seljak, which can beorders of magnitude faster than traditional methods, on both posterior sampling and evidence estimation.

For cosmologists, we have an add-on package CosmoFast, which provides several frequently-used cosmological mod-ules.

Both packages are in live development, so the API may be changed at any time. Note that some parts of the codeare still experimental. If you find a bug or have useful suggestions, please feel free to open an issue / pull request,or email He Jia. We also have a roadmap for features to implement in the future. Your contributions would be greatlyappreciated!

BayesFast 1

Page 6: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

2 BayesFast

Page 7: Release 0.1.0.dev2 He Jia and Uros Seljak

CHAPTER 1

Links

• Website: https://www.bayesfast.org/

• Documentation: https://bayesfast.readthedocs.io/en/latest/

• Source Code: https://github.com/HerculesJack/bayesfast

• Bug Reports: https://github.com/HerculesJack/bayesfast/issues

3

Page 8: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

4 Chapter 1. Links

Page 9: Release 0.1.0.dev2 He Jia and Uros Seljak

CHAPTER 2

Installation

We plan to add pypi and conda-forge support later. For now, please install BayesFast from source with:

git clone https://github.com/HerculesJack/bayesfastcd bayesfastpip install -e .# you can drop the -e option if you don't want to use editable mode# but note that pytest may not work correctly in this case

To check if BayesFast is built correctly, you can do:

pytest # for this you will need to have pytest installed

5

Page 10: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

6 Chapter 2. Installation

Page 11: Release 0.1.0.dev2 He Jia and Uros Seljak

CHAPTER 3

Dependencies

BayesFast requires python>=3.6, cython, extension-helpers, matplotlib, multiprocess, numdifftools, numpy>=1.17,scikit-learn, scipy>=1.0 and threadpoolctl. Currently, it has been tested on Ubuntu and MacOS, with python 3.6-3.8.

7

Page 12: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

8 Chapter 3. Dependencies

Page 13: Release 0.1.0.dev2 He Jia and Uros Seljak

CHAPTER 4

License

BayesFast is distributed under the Apache License, Version 2.0.

9

Page 14: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

10 Chapter 4. License

Page 15: Release 0.1.0.dev2 He Jia and Uros Seljak

CHAPTER 5

Citing BayesFast

If you find BayesFast useful for your research, please consider citing our papers accordingly:

• He Jia and Uros Seljak, BayesFast: A Fast and Scalable Method for Cosmological Bayesian Inference, in prep(for posterior sampling)

• He Jia and Uros Seljak, Normalizing Constant Estimation with Gaussianized Bridge Sampling, AABI 2019Proceedings, PMLR 118:1-14 (for evidence estimation)

5.1 Getting Started

5.2 API Reference

5.2.1 core

bayesfast.ModuleBase([input_vars, . . . ]) Base class for Module.bayesfast.Module([fun, jac, fun_and_jac]) Basic wrapper for user-definied functions.bayesfast.Surrogate([input_size, . . . ]) Base class for surrogate modules.bayesfast.Pipeline([module_list, . . . ]) Constructing composite functions from basic modules.bayesfast.Density([density_name, . . . ]) Specialized Pipeline for probability densities.bayesfast.DensityLite([logp, grad, . . . ]) Directly defines probability densities with logp, grad

and/or logp_and_grad.bayesfast.sample(density[, sample_trace, . . . ]) Sampling a probability density.bayesfast.OptimizeStep([surrogate_list, . . . ]) Configuring a step for optimization.bayesfast.SampleStep([surrogate_list, . . . ]) Configuring a step for sampling.bayesfast.PostStep([n_is, k_trunc, . . . ]) Configuring a step for post-processing.bayesfast.StaticSample([sample_steps, . . . ]) Configuring a static multi-step sample strategy.bayesfast.RecipeTrace([optimize, sample,. . . ])

Recording the process of running a Recipe.

Continued on next page

11

Page 16: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

Table 1 – continued from previous pagebayesfast.Recipe(density[, . . . ]) Unified recipe to run the whole BayesFast surrogate

sampling process.

class bayesfast.ModuleBase(input_vars=’__var__’, output_vars=’__var__’, delete_vars=(),input_shapes=None, output_shapes=None, in-put_scales=None, label=None, fun_args=(), fun_kwargs=None,jac_args=(), jac_kwargs=None, fun_and_jac_args=(),fun_and_jac_kwargs=None)

Base class for Module.

Notes

ModuleBase differs from Module in that, you should use it as a base class and define _fun (not fun) etcin your own derived class, instead of passing those functions to the initializer. It is more convenient when youwant to write some pre-defined modules; see e.g. the camb and planck_18 modules in cosmofast. Seethe docstring of Module for more information about its usage. As mentioned above, you don’t need to specifyfun, jac and fun_and_jac in the initializer.

class bayesfast.Module(fun=None, jac=None, fun_and_jac=None, **kwargs)Bases: bayesfast.core.module.ModuleBase

Basic wrapper for user-definied functions.

Parameters

• fun (callable or None, optional) – Callable returning the value of function, or None if un-defined. Set to None by default.

• jac (callable or None, optional) – Callable returning the value of Jacobian, or None if un-defined. Set to None by default.

• fun_and_jac (callable or None, optional) – Callable returning the function and Jacobian atthe same time, or None if undefined. Set to None by default.

• input_vars (str or 1-d array_like of str, optional) – Name(s) of input variable(s). Set to'__var__' by default.

• output_vars (str or 1-d array_like of str, optional) – Name(s) of output variable(s). Set to'__var__' by default.

• delete_vars (str or 1-d array_like of str, optional) – Name(s) of variable(s) to be deletedfrom the dict during runtime. Set to () by default.

• input_shapes (None or 1-d array_like of int, optional) – Controlling the reshaping of inputvariables. If None, the input variable(s) will be directly fed to fun etc. Otherwise, the inputvariable(s) will first be concatenated as a 1-d array, and then if the size of input_shapesis larger than 1, the input variables will be splitted accordingly. Set to None by default.

• output_shapes (None or 1-d array_like of int, optional) – Controlling the reshaping ofoutput variables. If None, the output variable(s) will be directly fetched from fun etc.Otherwise, the output variable(s) will first be concatenated as a 1-d array, and then if thesize of output_shapes is larger than 1, the output variables will be splitted accordingly.Set to None by default.

• input_scales (None or array_like, optional) – Controlling the scaling of input variables. Setto None by default.

• label (str or None, optional) – The label of the module used in print_summary. Set toNone by default.

12 Chapter 5. Citing BayesFast

Page 17: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• fun_args, jac_args, fun_and_jac_args (array_like, optional) – Additional arguments to bepassed to fun, jac and fun_and_jac. Set to () by default.

• fun_kwargs, jac_kwargs, fun_and_jac_kwargs (dict, optional) – Additional keyword ar-guments to be passed to fun, jac and fun_and_jac. Set to {} by default.

funWrapper of the callable to evaluate the function.

fun_and_jacWrapper of the callable to evaluate the function and Jacobian.

jacWrapper of the callable to evaluate the Jacobian.

class bayesfast.Surrogate(input_size=None, output_size=None, scope=(0, 1), fit_options=None,**kwargs)

Bases: bayesfast.core.module.ModuleBase

Base class for surrogate modules.

Parameters

• input_size (int or None, optional) – The size of input variables. If None, will be inferredfrom input_shapes. Set to None by default.

• output_size (int or None, optional) – The size of output variables. If None, will be inferredfrom output_shapes. Set to None by default.

• scope (array_like of 2 ints, optional) – Will be unpacked as (i_step, n_step), wherei_step represents the index where the true module should start to be replaced by the surrogatemodule, and n_step represents the number of module(s) to be replaced. Set to (0, 1) bydefault.

• fit_options (dict, optional) – Additional keyword arguments for fitting the surrogate module.Set to {} by default.

• kwargs (dict, optional) – Additional keyword arguments to be passed to Module.__init__.

Notes

Unlike Module, the default value of input_shapes will be -1.

fit(*args, **kwargs)Base method for fitting the surrogate module.

class bayesfast.Pipeline(module_list=(), surrogate_list=(), input_vars=’__var__’, in-put_shapes=None, input_scales=None, hard_bounds=False,copy_input=False, module_start=None, module_stop=None, origi-nal_space=True, use_surrogate=False)

Bases: bayesfast.core.density._PipelineBase

Constructing composite functions from basic modules.

Parameters

• module_list (Module or 1-d array_like of Module, optional) – Each element should be asubclass object derived from ModuleBase. Set to () by default.

• surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element shouldbe a subclass object derived from Surrogate. Set to () by default.

5.2. API Reference 13

Page 18: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• input_vars (str or 1-d array_like of str, optional) – Name(s) of input variable(s). Set to'__var__' by default.

• input_shapes (1-d array_like of int, or None, optional) – Used to divide and extract thevariable(s) from the input. If 1-d array_like, should have the same shape as input_vars.If None, will be interpreted as there is only one input variable. Set to None by default.

• input_scales (None or array_like of float, optional) – Controlling the scaling of input vari-ables. Set to None by default.

• hard_bounds (bool or array_like, optional) – Controlling whether input_scalesshould be interpreted as hard bounds, or just used to rescale the variables. If bool, willbe applied to all the variables. If array_like, should have shape of (input_size,) or(input_size, 2). Set to False by default.

• copy_input (bool, optional) – Whether to make a copy of the input before evaluating thepipeline. Set to False by default.

• module_start (int or None, optional) – The index of the module in module_list atwhich to start the evaluation. If None, will be interpreted as 0, i.e. the first module. Set toNone by default.

• module_stop (int or None, optional) – The index of the module in module_list afterwhich to end the evaluation. If None, will be interpreted as n_module - 1, i.e. the lastmodule. Set to None by default.

• original_space (bool, optional) – Whether the input variables are in the original, untrans-formed space. Will be overwritten if the original_space argument of fun, jac andfun_and_jac is not None. Set to True by default.

• use_surrogate (bool, optional) – Whether to use surrogate modules during the evaluation.Will be overwritten if the use_surrogate argument of fun, jac and fun_and_jacis not None. Set to False by default.

Notes

See the tutorial for more information of usage.

fun(x, original_space=None, use_surrogate=None)Evaluate the function of the pipeline.

fun_and_jac(x, original_space=None, use_surrogate=None)Evaluate the function and Jacobian of the pipeline.

jac(x, original_space=None, use_surrogate=None)Evaluate the Jacobian of the pipeline.

class bayesfast.Density(density_name=’__var__’, decay_options=None, return_dict=False,**kwargs)

Bases: bayesfast.core.density.Pipeline, bayesfast.core.density._DensityBase

Specialized Pipeline for probability densities.

Parameters

• density_name (str, optional) – The name of the variable that stands for the logarithmicprobability density. Set to __var__ by default.

• decay_options (dict, optional) – Keyword arguments to be passed to self.set_decay_options during initialization. Set to {} by default.

14 Chapter 5. Citing BayesFast

Page 19: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• return_dict (bool, optional) – Whether to also return the full VariableDict. Will beoverwritten if the return_dict argument of logp, grad and logp_and_grad is notNone. Set to False by default.

• kwargs (dict, optional) – Additional keyword arguments to be passed to Pipeline.__init__.

Notes

See the docstring of Pipeline.

fit(var_dicts)Fit all the surrogate modules.

grad(x, original_space=None, use_surrogate=None, return_dict=None)Evaluate the gradient of logp.

logp(x, original_space=None, use_surrogate=None, return_dict=None)Evaluate the logarithmic probability density.

logp_and_grad(x, original_space=None, use_surrogate=None, return_dict=None)Evaluate the logp and grad at the same time.

set_decay_options(use_decay=False, alpha=None, alpha_p=150.0, gamma=0.1)Set the decay options when far away from current samples.

class bayesfast.DensityLite(logp=None, grad=None, logp_and_grad=None, input_size=None,input_scales=None, hard_bounds=False, copy_input=False,vectorized=False, original_space=True, logp_args=(),logp_kwargs=None, grad_args=(), grad_kwargs=None,logp_and_grad_args=(), logp_and_grad_kwargs=None)

Bases: bayesfast.core.density._PipelineBase, bayesfast.core.density._DensityBase

Directly defines probability densities with logp, grad and/or logp_and_grad.

Parameters

• logp (callable or None, optional) – Callable returning the logarithmic probability density,or None if undefined. Set to None by default.

• grad (callable or None, optional) – Callable returning the gradient of logp, or None ifundefined. Set to None bu default.

• logp_and_grad (callable or None, optional) – Callable returning the logp and grad at thesame time, or None if undefined. Set to None by default.

• input_size (None or positive int, optional) – The size of input variables. Only used togenerate starting points for sampling, when no x_0 is given. Set to None by default.

• input_scales (None or array_like of float, optional) – Controlling the scaling of input vari-ables. Set to None by default.

• hard_bounds (bool or array_like, optional) – Controlling whether input_scalesshould be interpreted as hard bounds, or just used to rescale the variables. If bool, willbe applied to all the variables. If array_like, should have shape of (input_size,) or(input_size, 2). Set to False by default.

• copy_input (bool, optional) – Whether to make a copy of the input before evaluating thepipeline. Set to False by default.

5.2. API Reference 15

Page 20: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• vectorized (bool, optional) – Whether the original definitions of logp, grad andlogp_and_grad are vectorized. If not, a wrapper will be used to enable vectorization.Set to False by default.

• original_space (bool, optional) – Whether the input variables are in the original, untrans-formed space. Will be overwritten if the original_space argument of logp, gradand logp_and_grad is not None. Set to True by default.

• logp_args, grad_args, logp_and_grad_args (array_like, optional) – Additional argumentsto be passed to logp, grad and logp_and_grad. Set to () by default.

• logp_kwargs, grad_kwargs, logp_and_grad_kwargs (dict, optional) – Additional key-word arguments to be passed to logp, grad and logp_and_grad. Set to {} by default.

gradWrapper of the callable to evaluate the gradient of logp.

logpWrapper of the callable to evaluate the logarithmic probability density.

logp_and_gradWrapper of the callable to evaluate the logp and grad at the same time.

bayesfast.sample(density, sample_trace=None, sampler=’NUTS’, n_run=None, paral-lel_backend=None, verbose=True)

Sampling a probability density.

Parameters

• density (Density or DensityLite) – The probability density to sample.

• sample_trace (SampleTrace or dict, optional) – Configuring the sampler parameters. If dict,will be passed to initialize a SampleTrace object corresponding to the type indicated bysampler (i.e. will get a NTrace if sampler='NUTS'). Set to {} by default.

• sampler (str, optional) – Specifying the type of sampler to use. Should be one of 'NUTS','HMC', 'TNUTS', 'THMC' and 'Ensemble' (not implemented yet). Will be ignored ifsample_trace is a subclass of SampleTrace. Set to 'NUTS' by default.

• n_run (int or None, optional) – The number of steps to run. If None, will do the numberindicated in sample_trace. Set to None by default.

• parallel_backend (None, ParallelBackend, int, Pool, Client or MapReduce, optional) – IfNone, will use the global bayesfast parallel backend. Otherwise, will be passed to constructa ParallelBackend for parallelization.

• verbose (bool or array_like, optional) – Whether to print the progress messages during thesampling. Set to True by default.

Returns tt – The parallel sampling results.

Return type TraceTuple

Notes

See the tutorial for more information of usage.

class bayesfast.OptimizeStep(surrogate_list=(), alpha_n=2.0, laplace=None, eps_pp=0.1,eps_pq=0.1, max_iter=5, x_0=None, fitted=False,run_sampling=True, sample_trace=None, reuse_metric=True)

Bases: bayesfast.core.recipe._BaseStep

Configuring a step for optimization.

16 Chapter 5. Citing BayesFast

Page 21: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

Parameters

• surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element shouldbe a subclass object derived from Surrogate. Set to () by default.

• alpha_n (int, optional) – Controlling the number of samples used to fit the surrogate models,so that for a surrogate model with n parameters, we will evaluate the true model at alpha_n* n points during each iteration. If negative, will use all the samples available. Set to 2 bydefault.

• laplace (Laplace or dict, optional) – Configuring the Laplace sampler. Set to {'beta':100.} by default.

• eps_pp (positive float, optional) – The convergence threshold for |logp_i - logp_i-1|. Set to0.1 by default.

• eps_pq (positive float, optional) – The convergence threshold for |logp_i - logq_i|. Set to0.1 by default.

• max_iter (positive int, optional) – The maximum number of iterations allowed. Set to 5 bydefault.

• x_0 (2-d array of float or None, optional) – The starting points to fit the first surrogatemodel. If None, will draw from standard multivariate Gaussian via Sobol sequence. Set toNone by default.

• fitted (bool, optional) – If True, will assume that the surrogate models have already beenfitted. Set to False by default.

• run_sampling (bool, optional) – Whether to do a real MCMC sampling in the end. Thiscan be beneficial since it can provide a better starting point for the subsequent SampleStep.Set to True by default.

• sample_trace (SampleTrace or dict, optional) – Configuring the sampler parameters. Onlyused if run_sampling is True. If dict, will be used to initialize an NTrace. Set to {}by default.

• reuse_metric (bool, optional) – If True, will use the cov information of previous (Laplace)samples to set up the MCMC metric, or its starting point if the metric is adaptive. Set toTrue by default.

class bayesfast.SampleStep(surrogate_list=(), alpha_n=2.0, sample_trace=None, re-sampler=None, reuse_samples=0, reuse_step_size=True,reuse_metric=True, logp_cutoff=True, alpha_min=0.75, al-pha_supp=1.25, x_0=None, fitted=False)

Bases: bayesfast.core.recipe._BaseStep

Configuring a step for sampling.

Parameters

• surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element shouldbe a subclass object derived from Surrogate. Set to () by default.

• alpha_n (int, optional) – Controlling the number of samples used to fit the surrogate models,so that for a surrogate model with n parameters, we will evaluate the true model at alpha_n* n points during each iteration. If negative, will use all the samples available. See the notesbelow for more details about the case where logp_cutoff is True. Set to 2 by default.

• sample_trace (SampleTrace or dict, optional) – Configuring the sampler parameters. Ifdict, will be used to initialize a NTrace. Set to {} by default.

5.2. API Reference 17

Page 22: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• resampler (callable or dict, optional) – Given the previous surrogate samples, decid-ing where to evaluate the true model for the next iteration. If dict, will be used toinitilize a SystematicResampler. If callable, should have the same signature asSystematicResampler.run. Set to {} by default.

• reuse_samples (int, optional) – If positive, will also use the existing (adequate) samplesfrom this number of previous SampleStep(s) to fit the surrogate models. If negative, willuse all the previous SampleStep(s). Set to 0 by default.

• reuse_step_size (bool, optional) – If True, will use the previous SampleStep to set up theMCMC step size, or its starting point if the step size is adaptive. Set to True by default.

• reuse_metric (bool, optional) – If True, will use the cov information of previous samplesto set up the MCMC metric, or its starting point if the metric is adaptive. Set to True bydefault.

• logp_cutoff (bool, optional) – Whether to abandon the samples with too small logp. Seethe notes below for more details.

• alpha_min (float, optional) – Only used when logp_cutoff is True. See the notes belowfor more details.

• alpha_supp (float, optional) – Only used when logp_cutoff is True. See the notesbelow for more details.

• x_0 (2-d array of float or None, optional) – The starting points to fit the first surrogatemodel. If None, will first try to get from previous steps; if failed, will then draw fromstandard multivariate Gaussian via Sobol sequence. Set to None by default.

• fitted (bool, optional) – If True, will assume that the surrogate models have already beenfitted. Set to False by default.

Notes

If logp_cutoff is False, we will just evaluate the true model at alpha_n * n points and use them to fitthe surrogate model. If logp_cutoff is True, we will compare the logp and (previous) logq values of thesealpha_n * n points, and abandon the points whose logp is smaller than the smallest logq. Then, we requirethat the number of remaining adequate samples is larger than alpha_n * alpha_min * n, otherwisewe will continue to draw more samples until this requirement is satisfied. alpha_supp controls how manysupplemental samples to draw at each time. It might be useful to use some value larger than 1, since some ofthe new samples may also be rejected.

class bayesfast.PostStep(n_is=0, k_trunc=0.25, evidence_method=None)Configuring a step for post-processing.

Parameters

• n_is (int, optional) – The number of samples to do Importance Sampling (IS). If negative,will use all the samples available. Set to 0 by default.

• k_trunc (float, optional) – Truncating the IS weights w at <w> * n_IS**k_trunc. Setto 0.25 by default.

• evidence_method (str, None, or specified object, optional) – If None, will not compute theevidence. If str, should be one of GBS, GIS and GHM. If dict, will be used to initialize a GBSobject. Otherwise, should have a valid run method, with the same signature as GBS.run.Set to None by default.

class bayesfast.StaticSample(sample_steps=None, repeat=None, verbose=True)Bases: bayesfast.core.recipe._SampleStrategy

18 Chapter 5. Citing BayesFast

Page 23: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

Configuring a static multi-step sample strategy.

Parameters

• sample_steps (SampleStep, 1-d array-like of SampleStep, or None, optional) – Specifyinghow to perform surrogate sampling in each step. If None, will be interpreted as (), i.e. nosteps.

• repeat (1-d array-like of positive int, or None, optional) – If not None, will be interpretedas the number of times to repeat each element of sample_steps. Set to None by default.

• verbose (bool, optional) – Whether to print a message before each iteration. Set to Trueby default.

class bayesfast.RecipeTrace(optimize=None, sample=None, post=None, sample_repeat=None)Recording the process of running a Recipe.

Parameters

• optimize (OptimizeStep, dict or None, optional) – If None, no OptimizeStep will be per-formed. If dict, will be used to initialize an OptimizeStep. Set to None by default.

• sample (StaticSample, SampleStep, 1-d array-like of SampleStep, or None, optional) – Ifnot StaticSample, will be used to initialize a StaticSample. Set to None by default.

• post (PostStep or dict, optional) – If dict, will be used to initialize a PostStep. Set to {} bydefault.

• sample_repeat (1-d array-like of positive int, or None, optional) – If sample is not aStaticSample, will be used to initialize a StaticSample (as the repeat argument). Set toNone by default.

Notes

The default behavior of SampleStrategy initialization may change later.

class bayesfast.Recipe(density, parallel_backend=None, recipe_trace=None, optimize=None, sam-ple=None, post=None, sample_repeat=None, copy_density=True)

Unified recipe to run the whole BayesFast surrogate sampling process.

Parameters

• density (Density or DensityLite) – The probability density to sample.

• parallel_backend (None, ParallelBackend, int, Pool, Client or MapReduce, optional) – IfNone, will use the global bayesfast parallel backend. Otherwise, will be passed to constructa ParallelBackend for parallelization.

• recipe_trace (RecipeTrace, dict or None, optional) – If dict, will be used to initialize aRecipeTrace. If None, will use the arguments below to initialize a RecipeTrace. Set toNone by default.

• optimize (OptimizeStep, dict or None, optional) – The optimize parameter to initialize aRecipeTrace. Only used if recipe_trace is None. Set to None by default.

• sample (StaticSample, SampleStep, 1-d array-like of SampleStep, or None, optional) – Thesample parameter to initialize a RecipeTrace. Only used if recipe_trace is None. Setto None by default.

• post (PostStep or dict, optional) – The post parameter to initialize a RecipeTrace. Onlyused if recipe_trace is None. Set to None by default.

5.2. API Reference 19

Page 24: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• sample_repeat (1-d array-like of positive int, or None, optional) – The sample_repeatparameter to initialize a RecipeTrace. Only used if recipe_trace is None. Set to Noneby default.

• copy_density (bool, optional) – Whether to make a deepcopy of density. Set to Trueby default.

get()Getting the results of the Recipe.

Returns result – The results of the Recipe.

Return type PostResult

run()Running the Recipe.

5.2.2 modules

bayesfast.modules.PolyConfig(order[, . . . ]) Configuring the PolyModel.bayesfast.modules.PolyModel(configs[, . . . ]) Polynomial surrogate model, currently up to cubic or-

der.bayesfast.modules.Sum(input_vars, out-put_vars)

Computing the sum of input vars.

bayesfast.modules.Gaussian(mean, cov[,. . . ])

A univariate or multivariate Gaussian distribution.

class bayesfast.modules.PolyConfig(order, input_mask=None, output_mask=None)Configuring the PolyModel.

Parameters

• order (str) – Specifying the order of the polynomial model. Should be one of 'linear','quadratic', 'cubic-2' and 'cubic-3'.

• input_mask (None or 1-d array_like of int, optional) – The indice of input variables thatare activated. If None, will be interpreted as np.arange(input_size), i.e. all the variables areactivated. Set to None by default.

• output_mask (None or 1-d array_like of int, optional) – The indice of output variables thatare activated. If None, will be interpreted as np.arange(output_size), i.e. all the variablesare activated. Set to None by default.

class bayesfast.modules.PolyModel(configs, bound_options=None, *args, **kwargs)Bases: bayesfast.core.module.Surrogate

Polynomial surrogate model, currently up to cubic order.

Parameters

• configs (str, PolyConfig, or 1-d array_like of them) – Determining the configurationof the model. If str, should be one of 'linear', 'quadratic', 'cubic-2'and 'cubic-3'. Note that 'quadratic' will be interpreted as ['linear','quadratic'], i.e. up to quadratic order; similar for 'cubic-2' and 'cubic-3'.

• bound_options (dict, optional) – Keyword arguments to be passed to self.set_bound_options. Set to {} by default.

20 Chapter 5. Citing BayesFast

Page 25: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• args (array_like, optional) – Additional arguments to be passed to Surrogate.__init__.

• kwargs (dict, optional) – Additional keyword arguments to be passed to Surrogate.__init__.

fit(x, y, logp=None, w=None)Fit the polynomial model.

set_bound_options(use_bound=True, alpha=None, alpha_p=100.0, center_max=True)Set the linear extrapolation options when far away from current samples.

class bayesfast.modules.Sum(input_vars, output_vars, delete_vars=(), b=None, label=None)Bases: bayesfast.core.module.ModuleBase

Computing the sum of input vars.

Parameters

• input_vars (str or 1-d array_like of str) – Name(s) of input variable(s). Will first be con-catenated as one single variable.

• output_vars (str or 1-d array_like of str) – Name of output variable. Should contain only 1variable here.

• delete_vars (str or 1-d array_like of str, optional) – Name(s) of variable(s) to be deletedfrom the dict during runtime. Set to () by default.

• b (1-d array_like of float, or None, optional) – If not None, should match the shape of (con-catenated) input_vars, and then the summation of b * x_input will be computed.Set to None by default.

• label (str or None, optional) – The label of the module used in print_summary. Set toNone by default.

class bayesfast.modules.Gaussian(mean, cov, input_vars=’__var__’, output_vars=’__var__’,delete_vars=(), lower=None, upper=None, label=None)

Bases: bayesfast.core.module.ModuleBase

A univariate or multivariate Gaussian distribution.

Parameters

• mean (1-d array_like of float) – The mean of the Gaussian.

• cov (1-d or 2-d array_like of float) – If 1-d, will be interpreted as the diagonal of the covari-ance matrix. If 2-d, will be interpreted as the full covariance matrix.

• input_vars (str or 1-d array_like of str, optional) – Name(s) of input variable(s). Will firstbe concatenated as one single variable. Set to '__var__' by default.

• output_vars (str or 1-d array_like of str, optional) – Name of output variable. Shouldcontain only 1 variable here. Set to '__var__' by default.

• delete_vars (str or 1-d array_like of str, optional) – Name(s) of variable(s) to be deletedfrom the dict during runtime. Set to () by default.

• lower (1-d array_like of float, or None, optional) – If not None, will be used to compute thecorrect normalization of a truncated Gaussian. Set to None by default.

• upper (1-d array_like of float, or None, optional) – If not None, will be used to computethe correct normalization of a truncated Gaussian. Set to None by default.

• label (str or None, optional) – The label of the module used in print_summary. Set toNone by default.

5.2. API Reference 21

Page 26: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

5.2.3 utils

bayesfast.utils.Laplace([optimize_method,. . . ])

Evaluating and sampling the Laplace approximation forthe target density.

bayesfast.utils.SystematicResampler([nodes, . . . ])

Systematically resamples the input array.

class bayesfast.utils.Laplace(optimize_method=’Newton-CG’, optimize_tol=1e-05, opti-mize_options=None, max_cond=100000.0, n_sample=2000,beta=1.0, mvn_generator=None, grad_options=None,hess_options=None)

Evaluating and sampling the Laplace approximation for the target density.

Parameters

• optimize_method (str or callable, optional) – The method parameter for scipy.optimize.minimize. Set to 'Newton-CG' by default.

• optimize_tol (float, optional) – The tol parameter for scipy.optimize.minimize.Set to 1e-5 by default.

• optimize_options (dict, optional) – The options parameter for scipy.optimize.minimize. Set to {} by default.

• max_cond (positive float, optional) – The maximum conditional number allowed forthe Hessian matrix. All the eigenvalues that are smaller than max_eigen_value /max_cond will be truncated at this value. Set to 1e5 by default.

• n_sample (positive int or None, optional) – The number of samples to draw from the ap-proximated Gaussian distribution. If None, will be determined by min(1000, x_0.shape[-1] * 10) during runtime. Set to None by default.

• beta (positive float, optional) – Scaling the approximate distribution logq, i.e. the finalsamples will come from beta * logq. Set to 1. by default.

• mvn_generator (None or callable, optional) – Random number generator for themultivairate normal distribution. Should have signature (mean, cov, size) ->samples. If None, will use bayesfast.utils.sobol.multivariate_normal.Set to None by default.

• grad_options (dict, optional) – Additional keyword arguments for numdifftools tocompute the gradient. Will be ignored if direct expression for the gradient is provided inrun. Set to {} by default.

• hess_options (dict, optional) – Additional keyword arguments for numdifftools tocompute the Hessian. Will be ignored if direct expression for the Hessian is provided inrun. Set to {} by default.

run(logp, x_0, grad=None, hess=None)Running optimization and Laplace approximate sampling.

Parameters

• logp (callable) – The logarithmic probability density to sample.

• x_0 (1-d array_like of float) – The starting point for optimization.

• grad (callable or None, optional) – The gradient of the target density. If not callable, willuse finite difference methods in numdifftools. Set to None by default.

22 Chapter 5. Citing BayesFast

Page 27: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

• hess (callable or None, optional) – The Hessian of the target density. If not callable,will use finite difference methods in numdifftools (by computing the Jacobian ofgradient). Set to None by default.

static untemper_laplace_samples(laplace_result)Retrieve untempered (beta=1) Laplace results.

Parameters laplace_result (LaplaceResult) – The results returned by a previous run.

Returns x – The untempered Laplace samples.

Return type 2-d numpy.ndarray of float

class bayesfast.utils.SystematicResampler(nodes=(1.0, 100.0), weights=None, re-quire_unique=True)

Systematically resamples the input array.

Parameters

• nodes (1-d array-like of float, optional) – Percentiles dividing the different weights. Set to(1., 100.) by default.

• weights (1-d array-like of float or None, optional) – Relative weights of different intervals.If None, will use equal weights for the various intervals. Set to None by default.

• require_unique (bool, optional) – Whether to require that the returned indices are allunique. Set to True by default.

run(a, n)Running systematic resampling.

Parameters

• a (1-d array-like of float) – The input array to resample.

• n (positive int) – The number of samples to draw.

Returns i – The indices of resampled elements.

Return type 1-d numpy.ndarray of int

5.2. API Reference 23

Page 28: Release 0.1.0.dev2 He Jia and Uros Seljak

bayesfast documentation, Release 0.1.0.dev2

24 Chapter 5. Citing BayesFast

Page 29: Release 0.1.0.dev2 He Jia and Uros Seljak

Index

DDensity (class in bayesfast), 14DensityLite (class in bayesfast), 15

Ffit() (bayesfast.Density method), 15fit() (bayesfast.modules.PolyModel method), 21fit() (bayesfast.Surrogate method), 13fun (bayesfast.Module attribute), 13fun() (bayesfast.Pipeline method), 14fun_and_jac (bayesfast.Module attribute), 13fun_and_jac() (bayesfast.Pipeline method), 14

GGaussian (class in bayesfast.modules), 21get() (bayesfast.Recipe method), 20grad (bayesfast.DensityLite attribute), 16grad() (bayesfast.Density method), 15

Jjac (bayesfast.Module attribute), 13jac() (bayesfast.Pipeline method), 14

LLaplace (class in bayesfast.utils), 22logp (bayesfast.DensityLite attribute), 16logp() (bayesfast.Density method), 15logp_and_grad (bayesfast.DensityLite attribute), 16logp_and_grad() (bayesfast.Density method), 15

MModule (class in bayesfast), 12ModuleBase (class in bayesfast), 12

OOptimizeStep (class in bayesfast), 16

PPipeline (class in bayesfast), 13

PolyConfig (class in bayesfast.modules), 20PolyModel (class in bayesfast.modules), 20PostStep (class in bayesfast), 18

RRecipe (class in bayesfast), 19RecipeTrace (class in bayesfast), 19run() (bayesfast.Recipe method), 20run() (bayesfast.utils.Laplace method), 22run() (bayesfast.utils.SystematicResampler method), 23

Ssample() (in module bayesfast), 16SampleStep (class in bayesfast), 17set_bound_options()

(bayesfast.modules.PolyModel method),21

set_decay_options() (bayesfast.Density method),15

StaticSample (class in bayesfast), 18Sum (class in bayesfast.modules), 21Surrogate (class in bayesfast), 13SystematicResampler (class in bayesfast.utils), 23

Uuntemper_laplace_samples()

(bayesfast.utils.Laplace static method), 23

25