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
ModuleBasediffers fromModulein that, you should use it as a base class and define_fun(notfun) 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. thecambandplanck_18modules incosmofast. See the docstring ofModulefor more information about its usage. As mentioned above, you don’t need to specifyfun,jacandfun_and_jacin the initializer.
-
class
bayesfast.Module(fun=None, jac=None, fun_and_jac=None, **kwargs)¶ Bases:
bayesfast.core.module.ModuleBaseBasic wrapper for user-definied functions.
Parameters: - fun (callable or None, optional) – Callable returning the value of function, or None if undefined. Set to
Noneby default. - jac (callable or None, optional) – Callable returning the value of Jacobian, or None if undefined. Set to
Noneby default. - fun_and_jac (callable or None, optional) – Callable returning the function and Jacobian at the same time, or
None if undefined. Set to
Noneby 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
funetc. Otherwise, the input variable(s) will first be concatenated as a 1-d array, and then if the size ofinput_shapesis larger than 1, the input variables will be splitted accordingly. Set toNoneby 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
funetc. Otherwise, the output variable(s) will first be concatenated as a 1-d array, and then if the size ofoutput_shapesis larger than 1, the output variables will be splitted accordingly. Set toNoneby default. - input_scales (None or array_like, optional) – Controlling the scaling of input variables. Set to
Noneby default. - label (str or None, optional) – The label of the module used in
print_summary. Set toNoneby default. - fun_args, jac_args, fun_and_jac_args (array_like, optional) – Additional arguments to be passed to
fun,jacandfun_and_jac. Set to()by default. - fun_kwargs, jac_kwargs, fun_and_jac_kwargs (dict, optional) – Additional keyword arguments to be passed to
fun,jacandfun_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.
- fun (callable or None, optional) – Callable returning the value of function, or None if undefined. Set to
-
class
bayesfast.Surrogate(input_size=None, output_size=None, scope=(0, 1), fit_options=None, **kwargs)¶ Bases:
bayesfast.core.module.ModuleBaseBase 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 toNoneby default. - output_size (int or None, optional) – The size of output variables. If None, will be inferred from
output_shapes. Set toNoneby 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 ofinput_shapeswill be-1.-
fit(*args, **kwargs)¶ Base method for fitting the surrogate module.
- input_size (int or None, optional) – The size of input variables. If None, will be inferred from
-
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._PipelineBaseConstructing 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 toNoneby default. - input_scales (None or array_like of float, optional) – Controlling the scaling of input variables. Set to
Noneby 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, will be applied to all the variables. If array_like, should have shape of(input_size,)or(input_size, 2). Set toFalseby default. - copy_input (bool, optional) – Whether to make a copy of the input before evaluating the pipeline. Set
to
Falseby default. - module_start (int or None, optional) – The index of the module in
module_listat which to start the evaluation. If None, will be interpreted as0, i.e. the first module. Set toNoneby default. - module_stop (int or None, optional) – The index of the module in
module_listafter which to end the evaluation. If None, will be interpreted asn_module - 1, i.e. the last module. Set toNoneby default. - original_space (bool, optional) – Whether the input variables are in the original, untransformed space.
Will be overwritten if the
original_spaceargument offun,jacandfun_and_jacis not None. Set toTrueby default. - use_surrogate (bool, optional) – Whether to use surrogate modules during the evaluation. Will be
overwritten if the
use_surrogateargument offun,jacandfun_and_jacis not None. Set toFalseby 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.
- module_list (Module or 1-d array_like of Module, optional) – Each element should be a subclass object derived from
-
class
bayesfast.Density(density_name='__var__', decay_options=None, return_dict=False, **kwargs)¶ Bases:
bayesfast.core.density.Pipeline,bayesfast.core.density._DensityBaseSpecialized
Pipelinefor 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_optionsduring initialization. Set to{}by default. - return_dict (bool, optional) – Whether to also return the full
VariableDict. Will be overwritten if thereturn_dictargument oflogp,gradandlogp_and_gradis not None. Set toFalseby 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.
- density_name (str, optional) – The name of the variable that stands for the logarithmic probability
density. Set to
-
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._DensityBaseDirectly 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
Noneby default. - grad (callable or None, optional) – Callable returning the gradient of logp, or None if undefined. Set to
Nonebu default. - logp_and_grad (callable or None, optional) – Callable returning the logp and grad at the same time, or None if
undefined. Set to
Noneby default. - input_size (None or positive int, optional) – The size of input variables. Only used to generate starting points for
sampling, when no
x_0is given. Set toNoneby default. - input_scales (None or array_like of float, optional) – Controlling the scaling of input variables. Set to
Noneby 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, will be applied to all the variables. If array_like, should have shape of(input_size,)or(input_size, 2). Set toFalseby default. - copy_input (bool, optional) – Whether to make a copy of the input before evaluating the pipeline. Set
to
Falseby default. - vectorized (bool, optional) – Whether the original definitions of
logp,gradandlogp_and_gradare vectorized. If not, a wrapper will be used to enable vectorization. Set toFalseby default. - original_space (bool, optional) – Whether the input variables are in the original, untransformed space.
Will be overwritten if the
original_spaceargument oflogp,gradandlogp_and_gradis not None. Set toTrueby default. - logp_args, grad_args, logp_and_grad_args (array_like, optional) – Additional arguments to be passed to
logp,gradandlogp_and_grad. Set to()by default. - logp_kwargs, grad_kwargs, logp_and_grad_kwargs (dict, optional) – Additional keyword arguments to be passed to
logp,gradandlogp_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.
- logp (callable or None, optional) – Callable returning the logarithmic probability density, or None if
undefined. Set to
-
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
SampleTraceobject corresponding to the type indicated bysampler(i.e. will get aNTraceifsampler='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_traceis a subclass ofSampleTrace. 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 toNoneby 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
ParallelBackendfor parallelization. - verbose (bool or array_like, optional) – Whether to print the progress messages during the sampling. Set to
Trueby 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._BaseStepConfiguring 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
nparameters, we will evaluate the true model atalpha_n * npoints during each iteration. If negative, will use all the samples available. Set to2by 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.1by default. - eps_pq (positive float, optional) – The convergence threshold for |logp_i - logq_i|. Set to
0.1by default. - max_iter (positive int, optional) – The maximum number of iterations allowed. Set to
5by 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
Noneby default. - fitted (bool, optional) – If True, will assume that the surrogate models have already been fitted.
Set to
Falseby 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
Trueby default. - sample_trace (SampleTrace or dict, optional) – Configuring the sampler parameters. Only used if
run_samplingis True. If dict, will be used to initialize anNTrace. 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
Trueby default.
- surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element should be a subclass object derived from
-
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._BaseStepConfiguring 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
nparameters, we will evaluate the true model atalpha_n * npoints during each iteration. If negative, will use all the samples available. See the notes below for more details about the case wherelogp_cutoffis True. Set to2by 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 asSystematicResampler.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
0by 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
Trueby 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
Trueby 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_cutoffis True. See the notes below for more details. - alpha_supp (float, optional) – Only used when
logp_cutoffis 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
Noneby default. - fitted (bool, optional) – If True, will assume that the surrogate models have already been fitted.
Set to
Falseby default.
Notes
If
logp_cutoffis False, we will just evaluate the true model atalpha_n * npoints and use them to fit the surrogate model. Iflogp_cutoffis True, we will compare the logp and (previous) logq values of thesealpha_n * npoints, and abandon the points whose logp is smaller than the smallest logq. Then, we require that the number of remaining adequate samples is larger thanalpha_n * alpha_min * n, otherwise we will continue to draw more samples until this requirement is satisfied.alpha_suppcontrols how many supplemental samples to draw at each time. It might be useful to use some value larger than1, since some of the new samples may also be rejected.- surrogate_list (Surrogate or 1-d array_like of Surrogate, optional) – Each element should be a subclass object derived from
-
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
0by default. - k_trunc (float, optional) – Truncating the IS weights w at
<w> * n_IS**k_trunc. Set to0.25by default. - evidence_method (str, None, or specified object, optional) – If None, will not compute the evidence. If str, should be one of
GBS,GISandGHM. If dict, will be used to initialize aGBSobject. Otherwise, should have a validrunmethod, with the same signature asGBS.run. Set toNoneby default.
- n_is (int, optional) – The number of samples to do Importance Sampling (IS). If negative, will
use all the samples available. Set to
-
class
bayesfast.StaticSample(sample_steps=None, repeat=None, verbose=True)¶ Bases:
bayesfast.core.recipe._SampleStrategyConfiguring 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 toNoneby default. - verbose (bool, optional) – Whether to print a message before each iteration. Set to
Trueby default.
- sample_steps (SampleStep, 1-d array-like of SampleStep, or None, optional) – Specifying how to perform surrogate sampling in each step. If
-
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
Noneby 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
Noneby 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
sampleis not a StaticSample, will be used to initialize a StaticSample (as therepeatargument). Set toNoneby default.
Notes
The default behavior of SampleStrategy initialization may change later.
- optimize (OptimizeStep, dict or None, optional) – If None, no OptimizeStep will be performed. If dict, will be used to
initialize an OptimizeStep. Set to
-
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
ParallelBackendfor 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
Noneby default. - optimize (OptimizeStep, dict or None, optional) – The
optimizeparameter to initialize a RecipeTrace. Only used ifrecipe_traceis None. Set toNoneby default. - sample (StaticSample, SampleStep, 1-d array-like of SampleStep, or None, optional) – The
sampleparameter to initialize a RecipeTrace. Only used ifrecipe_traceis None. Set toNoneby default. - post (PostStep or dict, optional) – The
postparameter to initialize a RecipeTrace. Only used ifrecipe_traceis None. Set toNoneby default. - sample_repeat (1-d array-like of positive int, or None, optional) – The
sample_repeatparameter to initialize a RecipeTrace. Only used ifrecipe_traceis None. Set toNoneby default. - copy_density (bool, optional) – Whether to make a deepcopy of
density. Set toTrueby 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
Noneby 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
Noneby default.
- order (str) – Specifying the order of the polynomial model. Should be one of
-
class
bayesfast.modules.PolyModel(configs, bound_options=None, *args, **kwargs)¶ Bases:
bayesfast.core.module.SurrogatePolynomial 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.
- configs (str, PolyConfig, or 1-d array_like of them) – Determining the configuration of the model. If str, should be one of
-
class
bayesfast.modules.Sum(input_vars, output_vars, delete_vars=(), b=None, label=None)¶ Bases:
bayesfast.core.module.ModuleBaseComputing 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 ofb * x_inputwill be computed. Set toNoneby default. - label (str or None, optional) – The label of the module used in
print_summary. Set toNoneby 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.ModuleBaseA 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
Noneby 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
Noneby default. - label (str or None, optional) – The label of the module used in
print_summary. Set toNoneby 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
methodparameter forscipy.optimize.minimize. Set to'Newton-CG'by default. - optimize_tol (float, optional) – The
tolparameter forscipy.optimize.minimize. Set to1e-5by default. - optimize_options (dict, optional) – The
optionsparameter forscipy.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_condwill be truncated at this value. Set to1e5by 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 toNoneby default. - beta (positive float, optional) – Scaling the approximate distribution
logq, i.e. the final samples will come frombeta * logq. Set to1.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 usebayesfast.utils.sobol.multivariate_normal. Set toNoneby default. - grad_options (dict, optional) – Additional keyword arguments for
numdifftoolsto compute 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
numdifftoolsto compute 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, will use finite
difference methods in
numdifftools. Set toNoneby 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 toNoneby 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
- optimize_method (str or callable, optional) – The
-
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
Noneby default. - require_unique (bool, optional) – Whether to require that the returned indices are all unique. Set to
Trueby 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
- nodes (1-d array-like of float, optional) – Percentiles dividing the different weights. Set to