API Reference

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.
bayesfast.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, input_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) etc in your own derived class, instead of passing those functions to the initializer. It is more convenient when you want to write some pre-defined modules; see e.g. the camb and planck_18 modules in cosmofast. See the docstring of Module for more information about its usage. As mentioned above, you don’t need to specify fun, 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 undefined. Set to None by default.
  • jac (callable or None, optional) – Callable returning the value of Jacobian, or None if undefined. Set to None by default.
  • fun_and_jac (callable or None, optional) – Callable returning the function and Jacobian at the 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 deleted from the dict during runtime. Set to () by default.
  • input_shapes (None or 1-d array_like of int, optional) – Controlling the reshaping of input variables. If None, the input variable(s) will be directly fed to fun etc. Otherwise, the input variable(s) will first be concatenated as a 1-d array, and then if the size of input_shapes is 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 of output 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 the size 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. Set to None by default.
  • label (str or None, optional) – The label of the module used in print_summary. Set to None by default.
  • fun_args, jac_args, fun_and_jac_args (array_like, optional) – Additional arguments to be passed to fun, jac and fun_and_jac. Set to () by default.
  • fun_kwargs, jac_kwargs, fun_and_jac_kwargs (dict, optional) – Additional keyword arguments to be passed to fun, jac and fun_and_jac. Set to {} by default.
fun

Wrapper of the callable to evaluate the function.

fun_and_jac

Wrapper of the callable to evaluate the function and Jacobian.

jac

Wrapper 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 inferred from input_shapes. Set to None by default.
  • output_size (int or None, optional) – The size of output variables. If None, will be inferred from output_shapes. Set to None by default.
  • scope (array_like of 2 ints, optional) – Will be unpacked as (i_step, n_step), where i_step represents the index where the true module should start to be replaced by the surrogate module, and n_step represents the number of module(s) to be replaced. Set to (0, 1) by default.
  • 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__', input_shapes=None, input_scales=None, hard_bounds=False, copy_input=False, module_start=None, module_stop=None, original_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 a subclass object derived from ModuleBase. Set to () by default.
  • surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element should be a subclass object derived from Surrogate. Set to () by default.
  • 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 the variable(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 variables. Set to None by default.
  • hard_bounds (bool or array_like, optional) – Controlling whether input_scales should be interpreted as hard bounds, or just used to rescale the variables. If bool, will be 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 the pipeline. Set to False by default.
  • module_start (int or None, optional) – The index of the module in module_list at which to start the evaluation. If None, will be interpreted as 0, i.e. the first module. Set to None by default.
  • module_stop (int or None, optional) – The index of the module in module_list after which to end the evaluation. If None, will be interpreted as n_module - 1, i.e. the last module. Set to None by default.
  • original_space (bool, optional) – Whether the input variables are in the original, untransformed space. Will be overwritten if the original_space argument of fun, jac and fun_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_jac is 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 logarithmic probability 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.
  • return_dict (bool, optional) – Whether to also return the full VariableDict. Will be overwritten if the return_dict argument of logp, grad and logp_and_grad is not None. 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 if undefined. Set to None bu default.
  • logp_and_grad (callable or None, optional) – Callable returning the logp and grad at the same time, or None if undefined. Set to None by default.
  • input_size (None or positive int, optional) – The size of input variables. Only used to generate 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 variables. Set to None by default.
  • hard_bounds (bool or array_like, optional) – Controlling whether input_scales should be interpreted as hard bounds, or just used to rescale the variables. If bool, will be 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 the pipeline. Set to False by default.
  • vectorized (bool, optional) – Whether the original definitions of logp, grad and logp_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, untransformed space. Will be overwritten if the original_space argument of logp, grad and logp_and_grad is not None. Set to True by default.
  • logp_args, grad_args, logp_and_grad_args (array_like, optional) – Additional arguments to be passed to logp, grad and logp_and_grad. Set to () by default.
  • logp_kwargs, grad_kwargs, logp_and_grad_kwargs (dict, optional) – Additional keyword arguments to be passed to logp, grad and logp_and_grad. Set to {} by default.
grad

Wrapper of the callable to evaluate the gradient of logp.

logp

Wrapper of the callable to evaluate the logarithmic probability density.

logp_and_grad

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

bayesfast.sample(density, sample_trace=None, sampler='NUTS', n_run=None, parallel_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 by sampler (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 if sample_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 number indicated in sample_trace. Set to None by default.
  • parallel_backend (None, ParallelBackend, int, Pool, Client or MapReduce, optional) – If None, will use the global bayesfast parallel backend. Otherwise, will be passed to construct a ParallelBackend for parallelization.
  • verbose (bool or array_like, optional) – Whether to print the progress messages during the sampling. 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.

Parameters:
  • surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element should be 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 by default.
  • 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 to 0.1 by default.
  • eps_pq (positive float, optional) – The convergence threshold for |logp_i - logq_i|. Set to 0.1 by default.
  • max_iter (positive int, optional) – The maximum number of iterations allowed. Set to 5 by default.
  • x_0 (2-d array of float or None, optional) – The starting points to fit the first surrogate model. If None, will draw from standard multivariate Gaussian via Sobol sequence. Set to None by default.
  • fitted (bool, optional) – If True, will assume that the surrogate models have already been fitted. Set to False by default.
  • run_sampling (bool, optional) – Whether to do a real MCMC sampling in the end. This can 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. Only used 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 to True by default.
class bayesfast.SampleStep(surrogate_list=(), alpha_n=2.0, sample_trace=None, resampler=None, reuse_samples=0, reuse_step_size=True, reuse_metric=True, logp_cutoff=True, alpha_min=0.75, alpha_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 should be 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 notes below 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. If dict, will be used to initialize a NTrace. Set to {} by default.
  • resampler (callable or dict, optional) – Given the previous surrogate samples, deciding where to evaluate the true model for the next iteration. If dict, will be used to initilize a SystematicResampler. If callable, should have the same signature as SystematicResampler.run. Set to {} by default.
  • reuse_samples (int, optional) – If positive, will also use the existing (adequate) samples from this number of previous SampleStep(s) to fit the surrogate models. If negative, will use 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 the MCMC 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 samples to set up the MCMC metric, or its starting point if the metric is adaptive. Set to True by default.
  • logp_cutoff (bool, optional) – Whether to abandon the samples with too small logp. See the notes below for more details.
  • alpha_min (float, optional) – Only used when logp_cutoff is True. See the notes below for more details.
  • alpha_supp (float, optional) – Only used when logp_cutoff is True. See the notes below for more details.
  • x_0 (2-d array of float or None, optional) – The starting points to fit the first surrogate model. If None, will first try to get from previous steps; if failed, will then draw from standard multivariate Gaussian via Sobol sequence. Set to None by default.
  • fitted (bool, optional) – If True, will assume that the surrogate models have already been fitted. 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 fit the surrogate model. If logp_cutoff is True, we will compare the logp and (previous) logq values of these alpha_n * n points, and abandon the points whose logp is smaller than the smallest logq. Then, we require that the number of remaining adequate samples is larger than alpha_n * alpha_min * n, otherwise we will continue to draw more samples until this requirement is satisfied. alpha_supp controls how many supplemental samples to draw at each time. It might be useful to use some value larger than 1, since some of the 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. Set to 0.25 by default.
  • evidence_method (str, None, or specified object, optional) – If None, will not compute the evidence. If str, should be one of GBS, GIS and GHM. If dict, will be used to initialize a GBS object. 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

Configuring a static multi-step sample strategy.

Parameters:
  • sample_steps (SampleStep, 1-d array-like of SampleStep, or None, optional) – Specifying how to perform surrogate sampling in each step. If None, will be interpreted as (), i.e. no steps.
  • repeat (1-d array-like of positive int, or None, optional) – If not None, will be interpreted as 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 True by 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 performed. 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) – If not 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 {} by default.
  • sample_repeat (1-d array-like of positive int, or None, optional) – If sample is not a StaticSample, will be used to initialize a StaticSample (as the repeat argument). Set to None by default.

Notes

The default behavior of SampleStrategy initialization may change later.

class bayesfast.Recipe(density, parallel_backend=None, recipe_trace=None, optimize=None, sample=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) – If None, will use the global bayesfast parallel backend. Otherwise, will be passed to construct a ParallelBackend for parallelization.
  • recipe_trace (RecipeTrace, dict or None, optional) – If dict, will be used to initialize a RecipeTrace. If None, will use the arguments below to initialize a RecipeTrace. Set to None by default.
  • optimize (OptimizeStep, dict or None, optional) – The optimize parameter to initialize a RecipeTrace. Only used if recipe_trace is None. Set to None by default.
  • sample (StaticSample, SampleStep, 1-d array-like of SampleStep, or None, optional) – The sample parameter to initialize a RecipeTrace. Only used if recipe_trace is None. Set to None by default.
  • post (PostStep or dict, optional) – The post parameter to initialize a RecipeTrace. Only used if recipe_trace is None. Set to None by default.
  • sample_repeat (1-d array-like of positive int, or None, optional) – The sample_repeat parameter to initialize a RecipeTrace. Only used if recipe_trace is None. Set to None by default.
  • copy_density (bool, optional) – Whether to make a deepcopy of density. Set to True by default.
get()

Getting the results of the Recipe.

Returns:result – The results of the Recipe.
Return type:PostResult
run()

Running the Recipe.

modules

bayesfast.modules.PolyConfig(order[, …]) Configuring the PolyModel.
bayesfast.modules.PolyModel(configs[, …]) Polynomial surrogate model, currently up to cubic order.
bayesfast.modules.Sum(input_vars, output_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 that are activated. If None, will be interpreted as np.arange(input_size), i.e. all the variables are activated. Set to None by default.
  • output_mask (None or 1-d array_like of int, optional) – The indice of output variables that are activated. If None, will be interpreted as np.arange(output_size), i.e. all the variables are 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 configuration of 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.
  • 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 concatenated as one single variable.
  • output_vars (str or 1-d array_like of str) – Name of output variable. Should contain only 1 variable here.
  • delete_vars (str or 1-d array_like of str, optional) – Name(s) of variable(s) to be deleted from 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 (concatenated) 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 to None 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 covariance 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 first be 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. Should contain 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 deleted from 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 the correct 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 compute the 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 to None by default.

utils

bayesfast.utils.Laplace([optimize_method, …]) Evaluating and sampling the Laplace approximation for the target density.
bayesfast.utils.SystematicResampler([nodes, …]) Systematically resamples the input array.
class bayesfast.utils.Laplace(optimize_method='Newton-CG', optimize_tol=1e-05, optimize_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 for the 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 approximated 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 final samples will come from beta * logq. Set to 1. by default.
  • mvn_generator (None or callable, optional) – Random number generator for the multivairate 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 to compute the gradient. Will be ignored if direct expression for the gradient is provided in run. Set to {} by default.
  • hess_options (dict, optional) – Additional keyword arguments for numdifftools to compute the Hessian. Will be ignored if direct expression for the Hessian is provided in run. 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, will use finite difference methods in numdifftools. Set to None by default.
  • 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 of gradient). 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, require_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 all unique. 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