Utilities¶
Provides a collection of functions useful for using/mixing PRISM with other applications.
-
prism.utils.
get_hybrid_lnpost_fn
(lnpost_fn, pipeline_obj, *, emul_i=None, unit_space=False, impl_prior=True, par_dict=False)[source]¶ Returns a function definition
hybrid_lnpost(par_set, *args, **kwargs)
.This hybrid_lnpost() function can be used to calculate the natural logarithm of the posterior probability, which analyzes a given par_set first in the provided pipeline_obj at iteration emul_i and passes it to lnpost_fn if it is plausible.
This function needs to be called by all MPI ranks.
Parameters: - lnpost_fn (function) – Function definition that needs to be called if the provided par_set
is plausible in iteration emul_i of pipeline_obj. The used call
signature is
lnpost_fn(par_set, *args, **kwargs)
. All MPI ranks will call this function unless called within theworker_mode
context manager. - pipeline_obj (
Pipeline
object) – The instance of thePipeline
class that needs to be used for determining the validity of the proposed sampling step.
Other Parameters: - emul_i (int or None. Default: None) – If int, number indicating the requested emulator iteration. If None, the last iteration of the emulator will be used.
- unit_space (bool. Default: False) – Bool determining whether or not par_set will be given in unit space.
- impl_prior (bool. Default: True) – Bool determining whether or not the hybrid_lnpost() function should use the implausibility values of a given par_set as an additional prior.
- par_dict (bool. Default: False) – Bool determining whether or not par_set will be an array_like (False) or a dict (True).
Returns: hybrid_lnpost (function) – Definition of the function
hybrid_lnpost(par_set, *args, **kwargs)
.See also
get_walkers()
- Analyzes proposed init_walkers and returns valid p0_walkers.
worker_mode
- Special context manager within which all code is executed in worker mode.
Note
The input arguments unit_space and par_dict state in what form par_set will be provided to the hybrid_lnpost() function, such that it can be properly converted to the format used in
Pipeline
. The par_set that is passed to lnpost_fn is unchanged.Warning
Calling this function factory will disable all regular logging in pipeline_obj (
do_logging
set to False), in order to avoid having the same message being logged every time hybrid_lnpost() is called.- lnpost_fn (function) – Function definition that needs to be called if the provided par_set
is plausible in iteration emul_i of pipeline_obj. The used call
signature is
-
prism.utils.
get_walkers
(pipeline_obj, *, emul_i=None, init_walkers=None, req_n_walkers=None, unit_space=False, lnpost_fn=None, **kwargs)[source]¶ Analyzes proposed init_walkers and returns plausible p0_walkers.
Analyzes sample set init_walkers in the provided pipeline_obj at iteration emul_i and returns all samples that are plausible to be used as starting positions for MCMC walkers. The provided samples and returned walkers should be/are given in unit space if unit_space is True.
If init_walkers is None, returns
impl_sam
instead if it is available.This function needs to be called by all MPI ranks.
Parameters: pipeline_obj (
Pipeline
object) – The instance of thePipeline
class that needs to be used for determining the plausibility of the proposed starting positions.Other Parameters: emul_i (int or None. Default: None) – If int, number indicating the requested emulator iteration. If None, the last iteration of the emulator will be used.
init_walkers (2D array_like, dict, int or None. Default: None) – Sample set of proposed initial MCMC walker positions. All plausible samples in init_walkers will be returned. If int, generate an LHD of provided size and return all plausible samples. If None, return
impl_sam
corresponding to iteration emul_i instead.req_n_walkers (int or None. Default: None) – The minimum required number of plausible starting positions that should be returned. If None, all plausible starting positions in init_walkers are returned instead.
New in version 1.2.0.
unit_space (bool. Default: False) – Bool determining whether or not the provided samples and returned walkers are given in unit space.
lnpost_fn (function or None. Default: None) – If function, call
get_hybrid_lnpost_fn()
using lnpost_fn and the same values for pipeline_obj, emul_i and unit_space, and return the resulting function definition hybrid_lnpost(). Any additionally provided kwargs are also passed to it.
Returns: - n_walkers (int) – Number of returned MCMC walkers. Note that this number can be higher than req_n_walkers if not None.
- p0_walkers (2D
ndarray
object or dict) – Array containing plausible starting positions of valid MCMC walkers. If init_walkers was provided as a dict, p0_walkers will be a dict. - hybrid_lnpost (function (if lnpost_fn is a function)) – The function returned by
get_hybrid_lnpost_fn()
using lnpost_fn, pipeline_obj, emul_i, unit_space and kwargs as the input values.
See also
get_hybrid_lnpost_fn()
- Returns a function definition
hybrid_lnpost(par_set, *args, **kwargs)
. worker_mode
- Special context manager within which all code is executed in worker mode.
Notes
If init_walkers is None and emulator iteration emul_i has not been analyzed yet, a
RequestError
will be raised.If req_n_walkers is not None, a custom Metropolis-Hastings sampling algorithm is used to generate the required number of starting positions. All plausible samples in init_walkers are used as the start of every MCMC chain. Note that if the number of plausible samples in init_walkers is small, it is possible that the returned p0_walkers are not spread out properly over parameter space.