pycbc.inference.sampler package¶
Submodules¶
pycbc.inference.sampler.base module¶
Defines the base sampler class to be inherited by all samplers.
-
class
pycbc.inference.sampler.base.
BaseSampler
(model)[source]¶ Bases:
object
Abstract base class for all inference samplers.
All sampler classes must inherit from this class and implement its abstract methods.
Parameters: model (Model) – An instance of a model from pycbc.inference.models
.-
checkpoint
()[source]¶ The sampler must have a checkpoint method for dumping raw samples and stats to the file type defined by
io
.
-
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False)[source]¶ This should initialize the sampler given a config file.
-
io
¶ A class that inherits from
BaseInferenceFile
to handle IO with an hdf file.This should be a class, not an instance of class, so that the sampler can initialize it when needed.
-
model_stats
¶ A dict mapping model’s metadata fields to arrays of values for each sample in
raw_samples
.The arrays may have any shape, and may or may not be thinned.
-
name
= None¶
-
run
()[source]¶ This function should run the sampler.
Any checkpointing should be done internally in this function.
-
samples
¶ A dict mapping variable_params to arrays of samples currently in memory. The dictionary may also contain sampling_params.
The sample arrays may have any shape, and may or may not be thinned.
-
sampling_params
¶ Returns the sampling params used by the model.
-
static_params
¶ Returns the model’s fixed parameters.
-
variable_params
¶ Returns the parameters varied in the model.
-
-
pycbc.inference.sampler.base.
create_new_output_file
(sampler, filename, **kwargs)[source]¶ Creates a new output file.
Parameters: - sampler (sampler instance) – Sampler
- filename (str) – Name of the file to create.
- **kwargs – All other keyword arguments are passed through to the file’s
write_metadata
function.
-
pycbc.inference.sampler.base.
initial_dist_from_config
(cp, variable_params)[source]¶ Loads a distribution for the sampler start from the given config file.
A distribution will only be loaded if the config file has a [initial-*] section(s).
Parameters: - cp (Config parser) – The config parser to try to load from.
- variable_params (list of str) – The variable parameters for the distribution.
Returns: The initial distribution. If no [initial-*] section found in the config file, will just return None.
Return type:
-
pycbc.inference.sampler.base.
setup_output
(sampler, output_file)[source]¶ Sets up the sampler’s checkpoint and output files.
The checkpoint file has the same name as the output file, but with
.checkpoint
appended to the name. A backup file will also be created.Parameters: - sampler (sampler instance) – Sampler
- output_file (str) – Name of the output file.
pycbc.inference.sampler.base_cube module¶
Common utilities for samplers that rely on transforming between a unit cube and the prior space. This is typical of many nested sampling algorithms.
pycbc.inference.sampler.base_mcmc module¶
Provides constructor classes and convenience functions for MCMC samplers.
-
class
pycbc.inference.sampler.base_mcmc.
BaseMCMC
[source]¶ Bases:
object
Abstract base class that provides methods common to MCMCs.
This is not a sampler class itself. Sampler classes can inherit from this along with
BaseSampler
.This class provides
set_initial_conditions
,run
, andcheckpoint
methods, which are some of the abstract methods required byBaseSampler
.This class introduces the following abstract properties and methods:
- base_shape
- [property] Should give the shape of the samples arrays used by the sampler, excluding the iteraitons dimension. Needed for writing results.
- run_mcmc(niterations)
- Should run the sampler for the given number of iterations. Called by
run
.
- clear_samples()
- Should clear samples from memory. Called by
run
.
- set_state_from_file(filename)
- Should set the random state of the sampler using the given filename.
Called by
set_initial_conditions
.
- write_results(filename)
- Writes results to the given filename. Called by
checkpoint
.
- compute_acf(filename, **kwargs)
- [classmethod] Should compute the autocorrelation function using the given filename. Also allows for other keyword arguments.
- compute_acl(filename, **kwargs)
- [classmethod] Should compute the autocorrelation length using the given filename. Also allows for other keyword arguments.
-
p0
¶
-
pos
¶
-
nwalkers
¶
-
niterations
¶
-
checkpoint_interval
¶
-
checkpoint_signal
¶
-
target_niterations
¶
-
target_eff_nsamples
¶
-
thin_interval
¶
-
max_samples_per_chain
¶
-
thin_safety_factor
¶
-
burn_in
¶
-
effective_nsamples
¶
-
acls
¶
-
acts
¶
-
acls
The autocorrelation lengths of each parameter’s thinned chain.
-
acts
The autocorrelation times of each parameter.
The autocorrelation time is defined as the ACL times the
thin_interval
. It gives the number of iterations between independent samples.
-
base_shape
¶ What shape the sampler’s samples arrays are in, excluding the iterations dimension.
For example, if a sampler uses 20 walkers and 3 temperatures, this would be
(3, 20)
. If a sampler only uses a single walker and no temperatures this would be()
.
-
burn_in
The class for doing burn-in tests (if specified).
-
static
checkpoint_from_config
(cp, section)[source]¶ Gets the checkpoint interval from the given config file.
This looks for ‘checkpoint-interval’ in the section.
Parameters: - cp (ConfigParser) – Open config parser to retrieve the argument from.
- section (str) – Name of the section to retrieve from.
Returns: The checkpoint interval, if it is in the section. Otherw
Return type:
-
checkpoint_interval
The number of iterations to do between checkpoints.
-
checkpoint_signal
The signal to use when checkpointing.
-
static
ckpt_signal_from_config
(cp, section)[source]¶ Gets the checkpoint signal from the given config file.
This looks for ‘checkpoint-signal’ in the section.
Parameters: - cp (ConfigParser) – Open config parser to retrieve the argument from.
- section (str) – Name of the section to retrieve from.
Returns: The checkpoint interval, if it is in the section. Otherw
Return type:
-
compute_acf
(filename, **kwargs)[source]¶ A method to compute the autocorrelation function of samples in the given file.
-
compute_acl
(filename, **kwargs)[source]¶ A method to compute the autocorrelation length of samples in the given file.
-
effective_nsamples
The effective number of samples post burn-in that the sampler has acquired so far.
-
get_thin_interval
()[source]¶ Gets the thin interval to use.
If
max_samples_per_chain
is set, this will figure out what thin interval is needed to satisfy that criteria. In that case, the thin interval used must be a multiple of the currently used thin interval.
-
max_samples_per_chain
The maximum number of samplers per chain that is written to disk.
-
niterations
The current number of iterations.
-
nwalkers
The number of walkers used.
-
p0
A dictionary of the initial position of the walkers.
This is set by using
set_p0
. If not set yet, aValueError
is raised when the attribute is accessed.
-
pos
A dictionary of the current walker positions.
If the sampler hasn’t been run yet, returns p0.
-
set_burn_in_from_config
(cp)[source]¶ Sets the burn in class from the given config file.
If no burn-in section exists in the file, then this just set the burn-in class to None.
-
set_p0
(samples_file=None, prior=None)[source]¶ Sets the initial position of the walkers.
Parameters: - samples_file (InferenceFile, optional) – If provided, use the last iteration in the given file for the starting positions.
- prior (JointDistribution, optional) – Use the given prior to set the initial positions rather than
model
’s prior.
Returns: p0 – A dictionary maping sampling params to the starting positions.
Return type:
-
set_state_from_file
(filename)[source]¶ Sets the state of the sampler to the instance saved in a file.
-
set_target
(niterations=None, eff_nsamples=None)[source]¶ Sets the target niterations/nsamples for the sampler.
One or the other must be provided, not both.
-
set_target_from_config
(cp, section)[source]¶ Sets the target using the given config file.
This looks for
niterations
to set thetarget_niterations
, andeffective-nsamples
to set thetarget_eff_nsamples
.Parameters: - cp (ConfigParser) – Open config parser to retrieve the argument from.
- section (str) – Name of the section to retrieve from.
-
set_thin_interval_from_config
(cp, section)[source]¶ Sets thinning options from the given config file.
-
target_eff_nsamples
The target number of effective samples the sampler should get.
-
target_niterations
The number of iterations the sampler should run for.
-
thin_interval
Returns the thin interval being used.
-
thin_safety_factor
The minimum value that
max_samples_per_chain
may be set to.
-
class
pycbc.inference.sampler.base_mcmc.
MCMCAutocorrSupport
[source]¶ Bases:
object
Provides class methods for calculating ensemble ACFs/ACLs.
-
classmethod
compute_acf
(filename, start_index=None, end_index=None, per_walker=False, walkers=None, parameters=None)[source]¶ Computes the autocorrleation function of the model params in the given file.
By default, parameter values are averaged over all walkers at each iteration. The ACF is then calculated over the averaged chain. An ACF per-walker will be returned instead if
per_walker=True
.Parameters: - filename (str) – Name of a samples file to compute ACFs for.
- start_index ({None, int}) – The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample.
- end_index ({None, int}) – The end index to compute the acl to. If None, will go to the end of the current iteration.
- per_walker (optional, bool) – Return the ACF for each walker separately. Default is False.
- walkers (optional, int or array) – Calculate the ACF using only the given walkers. If None (the default) all walkers will be used.
- parameters (optional, str or array) – Calculate the ACF for only the given parameters. If None (the default) will calculate the ACF for all of the model params.
Returns: Dictionary of arrays giving the ACFs for each parameter. If
per-walker
is True, the arrays will have shapenwalkers x niterations
.Return type:
-
classmethod
compute_acl
(filename, start_index=None, end_index=None, min_nsamples=10)[source]¶ Computes the autocorrleation length for all model params in the given file.
Parameter values are averaged over all walkers at each iteration. The ACL is then calculated over the averaged chain. If an ACL cannot be calculated because there are not enough samples, it will be set to
inf
.Parameters: - filename (str) – Name of a samples file to compute ACLs for.
- start_index (int, optional) – The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample.
- end_index (int, optional) – The end index to compute the acl to. If None, will go to the end of the current iteration.
- min_nsamples (int, optional) – Require a minimum number of samples to compute an ACL. If the
number of samples per walker is less than this, will just set to
inf
. Default is 10.
Returns: A dictionary giving the ACL for each parameter.
Return type:
-
classmethod
-
pycbc.inference.sampler.base_mcmc.
blob_data_to_dict
(stat_names, blobs)[source]¶ Converts list of “blobs” to a dictionary of model stats.
Samplers like
emcee
store the extra tuple returned byCallModel
to a list called blobs. This is a list of lists of tuples with shape niterations x nwalkers x nstats, where nstats is the number of stats returned by the model’sdefault_stats
. This converts that list to a dictionary of arrays keyed by the stat names.Parameters: - stat_names (list of str) – The list of the stat names.
- blobs (list of list of tuples) – The data to convert.
Returns: A dictionary mapping the model’s
default_stats
to arrays of values. Each array will have shapenwalkers x niterations
.Return type:
-
pycbc.inference.sampler.base_mcmc.
get_optional_arg_from_config
(cp, section, arg, dtype=<type 'str'>)[source]¶ Convenience function to retrieve an optional argument from a config file.
Parameters: Returns: val – If the argument is present, the value. Otherwise, None.
Return type:
-
pycbc.inference.sampler.base_mcmc.
raw_samples_to_dict
(sampler, raw_samples)[source]¶ Convenience function for converting ND array to a dict of samples.
The samples are assumed to have dimension
[sampler.base_shape x] niterations x len(sampler.sampling_params)
.Parameters: - sampler (sampler instance) – An instance of an MCMC sampler.
- raw_samples (array) – The array of samples to convert.
Returns: A dictionary mapping the raw samples to the variable params. If the sampling params are not the same as the variable params, they will also be included. Each array will have shape
[sampler.base_shape x] niterations
.Return type:
pycbc.inference.sampler.base_multitemper module¶
Provides constructor classes provide support for parallel tempered MCMC samplers.
-
class
pycbc.inference.sampler.base_multitemper.
MultiTemperedAutocorrSupport
[source]¶ Bases:
object
Provides class methods for calculating multi-tempered ACFs/ACLs.
-
classmethod
compute_acf
(filename, start_index=None, end_index=None, per_walker=False, walkers=None, parameters=None, temps=None)[source]¶ Computes the autocorrleation function of the model params in the given file.
By default, parameter values are averaged over all walkers at each iteration. The ACF is then calculated over the averaged chain for each temperature. An ACF per-walker will be returned instead if
per_walker=True
.Parameters: - filename (str) – Name of a samples file to compute ACFs for.
- start_index ({None, int}) – The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample.
- end_index ({None, int}) – The end index to compute the acl to. If None, will go to the end of the current iteration.
- per_walker (optional, bool) – Return the ACF for each walker separately. Default is False.
- walkers (optional, int or array) – Calculate the ACF using only the given walkers. If None (the default) all walkers will be used.
- parameters (optional, str or array) – Calculate the ACF for only the given parameters. If None (the default) will calculate the ACF for all of the model params.
- temps (optional, (list of) int or 'all') – The temperature index (or list of indices) to retrieve. If None (the default), the ACF will only be computed for the coldest (= 0) temperature chain. To compute an ACF for all temperates pass ‘all’, or a list of all of the temperatures.
Returns: Dictionary of arrays giving the ACFs for each parameter. If
per-walker
is True, the arrays will have shapentemps x nwalkers x niterations
. Otherwise, the returned array will have shapentemps x niterations
.Return type:
-
classmethod
compute_acl
(filename, start_index=None, end_index=None, min_nsamples=10)[source]¶ Computes the autocorrleation length for all model params and temperatures in the given file.
Parameter values are averaged over all walkers at each iteration and temperature. The ACL is then calculated over the averaged chain.
Parameters: - filename (str) – Name of a samples file to compute ACLs for.
- start_index ({None, int}) – The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample.
- end_index ({None, int}) – The end index to compute the acl to. If None, will go to the end of the current iteration.
- min_nsamples (int, optional) – Require a minimum number of samples to compute an ACL. If the
number of samples per walker is less than this, will just set to
inf
. Default is 10.
Returns: A dictionary of ntemps-long arrays of the ACLs of each parameter.
Return type:
-
classmethod
-
class
pycbc.inference.sampler.base_multitemper.
MultiTemperedSupport
[source]¶ Bases:
object
Provides methods for supporting multi-tempered samplers.
-
static
betas_from_config
(cp, section)[source]¶ Loads number of temperatures or betas from a config file.
This looks in the given section for:
ntemps
:- The number of temperatures to use. Either this, or
inverse-temperatures-file
must be provided (but not both).
inverse-temperatures-file
:- Path to an hdf file containing the inverse temperatures (“betas”)
to use. The betas will be retrieved from the file’s
.attrs['betas']
. Either this orntemps
must be provided (but not both).
Parameters: - cp (WorkflowConfigParser instance) – Config file object to parse.
- section (str) – The name of the section to look in.
Returns: - ntemps (int or None) – The number of temperatures to use, if it was provided.
- betas (array) – The array of betas to use, if a inverse-temperatures-file was provided.
-
ntemps
¶ The number of temeratures that are set.
-
static
pycbc.inference.sampler.dynesty module¶
This modules provides classes and functions for using the dynesty sampler packages for parameter estimation.
-
class
pycbc.inference.sampler.dynesty.
DynestySampler
(model, nlive, dlogz, nprocesses=1, loglikelihood_function=None, use_mpi=False, **kwargs)[source]¶ Bases:
pycbc.inference.sampler.base.BaseSampler
This class is used to construct an Dynesty sampler from the dynesty package.
Parameters: - model (model) – A model from
pycbc.inference.models
. - nlive (int) – Number of live points to use in sampler.
- dlogz (float) – Tolerance limit to the value of logz (also a convergence criteria)
- pool (function with map, Optional) – A provider of a map function that allows a function call to be run over multiple sets of arguments and possibly maps them to cores/nodes/etc.
-
checkpoint
()[source]¶ The sampler must have a checkpoint method for dumping raw samples and stats to the file type defined by
io
.
-
classmethod
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False, loglikelihood_function=None)[source]¶ Loads the sampler from the given config file.
-
io
¶
-
logz
¶ return bayesian evidence estimated by dynesty sampler
-
logz_err
¶ return error in bayesian evidence estimated by dynesty sampler
-
model_stats
¶
-
name
= 'dynesty'¶
-
niterations
¶
-
posterior_samples
¶ Returns posterior samples from nested samples and weights given by dynsety sampler
-
run
()[source]¶ This function should run the sampler.
Any checkpointing should be done internally in this function.
-
samples
¶
- model (model) – A model from
pycbc.inference.sampler.emcee module¶
This modules provides classes and functions for using the emcee sampler packages for parameter estimation.
-
class
pycbc.inference.sampler.emcee.
EmceeEnsembleSampler
(model, nwalkers, checkpoint_interval=None, checkpoint_signal=None, logpost_function=None, nprocesses=1, use_mpi=False)[source]¶ Bases:
pycbc.inference.sampler.base_mcmc.MCMCAutocorrSupport
,pycbc.inference.sampler.base_mcmc.BaseMCMC
,pycbc.inference.sampler.base.BaseSampler
This class is used to construct an MCMC sampler from the emcee package’s EnsembleSampler.
Parameters: - model (model) – A model from
pycbc.inference.models
. - nwalkers (int) – Number of walkers to use in sampler.
- pool (function with map, Optional) – A provider of a map function that allows a function call to be run over multiple sets of arguments and possibly maps them to cores/nodes/etc.
-
base_shape
¶
-
burn_in_class
¶
-
finalize
()[source]¶ All data is written by the last checkpoint in the run method, so this just passes.
-
classmethod
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False)[source]¶ Loads the sampler from the given config file.
-
io
¶
-
model_stats
¶ A dict mapping the model’s
default_stats
to arrays of values.The returned array has shape
nwalkers x niterations
.
-
name
= 'emcee'¶
-
run_mcmc
(niterations)[source]¶ Advance the ensemble for a number of samples.
Parameters: niterations (int) – Number of iterations to run the sampler for.
-
samples
¶ A dict mapping
variable_params
to arrays of samples currently in memory.The arrays have shape
nwalkers x niterations
.
- model (model) – A model from
pycbc.inference.sampler.emcee_pt module¶
This modules provides classes and functions for using the emcee_pt sampler packages for parameter estimation.
-
class
pycbc.inference.sampler.emcee_pt.
EmceePTSampler
(model, ntemps, nwalkers, betas=None, checkpoint_interval=None, checkpoint_signal=None, loglikelihood_function=None, nprocesses=1, use_mpi=False)[source]¶ Bases:
pycbc.inference.sampler.base_multitemper.MultiTemperedAutocorrSupport
,pycbc.inference.sampler.base_multitemper.MultiTemperedSupport
,pycbc.inference.sampler.base_mcmc.BaseMCMC
,pycbc.inference.sampler.base.BaseSampler
This class is used to construct a parallel-tempered MCMC sampler from the emcee package’s PTSampler.
Parameters: - model (model) – A model from
pycbc.inference.models
. - ntemps (int) – Number of temeratures to use in the sampler.
- nwalkers (int) – Number of walkers to use in sampler.
- betas (array) – An array of inverse temperature values to be used in emcee_pt’s
temperature ladder. If not provided,
emcee_pt
will use the number of temperatures and the number of dimensions of the parameter space to construct the ladder with geometrically spaced temperatures. - loglikelihood_function (str, optional) – Set the function to call from the model for the
loglikelihood
. Default isloglikelihood
. - nprocesses (int, optional) – The number of parallel processes to use. Default is 1 (no paralleliztion).
- use_mpi (bool, optional) – Use MPI for parallelization. Default (False) will use python’s multiprocessing.
-
base_shape
¶
-
betas
¶
-
burn_in_class
¶ alias of
pycbc.inference.burn_in.MultiTemperedMCMCBurnInTests
-
classmethod
calculate_logevidence
(filename, thin_start=None, thin_end=None, thin_interval=None)[source]¶ Calculates the log evidence from the given file using
emcee_pt
’s thermodynamic integration.Parameters: - filename (str) – Name of the file to read the samples from. Should be an
EmceePTFile
. - thin_start (int) – Index of the sample to begin returning stats. Default is to read stats after burn in. To start from the beginning set thin_start to 0.
- thin_interval (int) – Interval to accept every i-th sample. Default is to use the fp.acl. If fp.acl is not set, then use all stats (set thin_interval to 1).
- thin_end (int) – Index of the last sample to read. If not given then fp.niterations is used.
Returns: - lnZ (float) – The estimate of log of the evidence.
- dlnZ (float) – The error on the estimate.
- filename (str) – Name of the file to read the samples from. Should be an
-
finalize
()[source]¶ Calculates the log evidence and writes to the checkpoint file.
If sampling transforms were used, this also corrects the jacobian stored on disk.
The thin start/interval/end for calculating the log evidence are retrieved from the checkpoint file’s thinning attributes.
-
classmethod
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False)[source]¶ Loads the sampler from the given config file.
The following options are retrieved in the
[sampler]
section:name
:- Required. This must match the samlper’s name.
nwalkers
:- Required. The number of walkers to use.
ntemps
:- The number of temperatures to use. Either this, or
inverse-temperatures-file
must be provided (but not both).
inverse-temperatures-file
:- Path to an hdf file containing the inverse temperatures (“betas”)
to use. The betas will be retrieved from the file’s
.attrs['betas']
. Either this orntemps
must be provided (but not both).
niterations
:- The number of iterations to run the sampler for. Either this or
effective-nsamples
must be provided (but not both).
effective-nsamples
:- Run the sampler until the given number of effective samples are
obtained. A
checkpoint-interval
must also be provided in this case. Either this orniterations
must be provided (but not both).
thin-interval
:- Thin the samples by the given value before saving to disk. May
provide this, or
max-samples-per-chain
, but not both. If neither options are provided, will save all samples.
max-samples-per-chain
:- Thin the samples such that the number of samples per chain per
temperature that are saved to disk never exceeds the given value.
May provide this, or
thin-interval
, but not both. If neither options are provided, will save all samples.
checkpoint-interval
:- Sets the checkpoint interval to use. Must be provided if using
effective-nsamples
.
checkpoint-signal
:- Set the checkpoint signal, e.g., “USR2”. Optional.
logl-function
:- The attribute of the model to use for the loglikelihood. If
not provided, will default to
loglikelihood
.
Settings for burn-in tests are read from
[sampler-burn_in]
. In particular, theburn-in-test
option is used to set the burn in tests to perform. SeeMultiTemperedMCMCBurnInTests.from_config()
for details. If noburn-in-test
is provided, no burn in tests will be carried out.Parameters: - cp (WorkflowConfigParser instance) – Config file object to parse.
- model (pycbc.inference.model.BaseModel instance) – The model to use.
- output_file (str, optional) – The name of the output file to checkpoint and write results to.
- nprocesses (int, optional) – The number of parallel processes to use. Default is 1.
- use_mpi (bool, optional) – Use MPI for parallelization. Default is False.
Returns: The sampler instance.
Return type:
-
io
¶
-
model_stats
¶ Returns the log likelihood ratio and log prior as a dict of arrays.
The returned array has shape ntemps x nwalkers x niterations.
Unfortunately, because
emcee_pt
does not have blob support, this will only return the loglikelihood and logprior (with the logjacobian set to zero) regardless of what stats the model can return.Warning
Since the logjacobian is not saved by emcee_pt, the logprior returned here is the log of the prior pdf in the sampling coordinate frame rather than the variable params frame. This differs from the variable params frame by the log of the Jacobian of the transform from one frame to the other. If no sampling transforms were used, then the logprior is the same.
-
name
= 'emcee_pt'¶
-
run_mcmc
(niterations)[source]¶ Advance the ensemble for a number of samples.
Parameters: niterations (int) – Number of samples to get from sampler.
-
samples
¶ A dict mapping
variable_params
to arrays of samples currently in memory.The arrays have shape
ntemps x nwalkers x niterations
.
- model (model) – A model from
pycbc.inference.sampler.epsie module¶
This module provides classes for interacting with epsie samplers.
-
class
pycbc.inference.sampler.epsie.
EpsieSampler
(model, nchains, ntemps=None, betas=None, proposals=None, default_proposal=None, default_proposal_args=None, seed=None, swap_interval=1, checkpoint_interval=None, checkpoint_signal=None, loglikelihood_function=None, nprocesses=1, use_mpi=False)[source]¶ Bases:
pycbc.inference.sampler.base_multitemper.MultiTemperedAutocorrSupport
,pycbc.inference.sampler.base_multitemper.MultiTemperedSupport
,pycbc.inference.sampler.base_mcmc.BaseMCMC
,pycbc.inference.sampler.base.BaseSampler
Constructs an MCMC sampler using epsie’s parallel-tempered sampler.
Parameters: - model (model) – A model from
pycbc.inference.models
. - nwalkers (int) – Number of walkers to use in the sampler.
- ntemps (int, optional) – Number of temperatures to use in the sampler. A geometrically-spaced
temperature ladder with the gievn number of levels will be constructed
based on the number of parameters. If not provided, must provide
betas
. - betas (array, optional) – An array of inverse temperature values to be used in for the
temperature ladder. If not provided, must provide
ntemps
. - proposals (dict, optional) – Dictionary mapping sampling parameter names to proposal classes. Any
parameters not listed will use the
default_proposal
. Note: proposals should be specified for the sampling parameters, not the variable parameters. - default_proposal (an epsie.Proposal class, optional) – The default proposal to use for parameters not in
proposals
. Default isepsie.proposals.Normal
. - default_proposal_args (dict, optional) – Dictionary of arguments to pass to the default proposal.
- swap_interval (int, optional) – The number of iterations between temperature swaps. Default is 1.
- seed (int, optional) – Seed for epsie’s random number generator. If None provided, will create one.
- checkpoint_interval (int, optional) – Specify the number of iterations to do between checkpoints. If not provided, no checkpointin will be done.
- checkpoint_signal (str, optional) – Set the signal to use when checkpointing. For example, ‘USR2’.
- loglikelihood_function (str, optional) – Set the function to call from the model for the
loglikelihood
. Default isloglikelihood
. - nprocesses (int, optional) – The number of parallel processes to use. Default is 1 (no paralleliztion).
- use_mpi (bool, optional) – Use MPI for parallelization. Default (False) will use python’s multiprocessing.
-
base_shape
¶
-
betas
¶ The inverse temperatures being used.
-
burn_in_class
¶ alias of
pycbc.inference.burn_in.MultiTemperedMCMCBurnInTests
-
classmethod
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False)[source]¶ Loads the sampler from the given config file.
The following options are retrieved in the
[sampler]
section:name
:- (required) must match the samlper’s name
nchains
:- (required) the number of chains to use
ntemps
:- The number of temperatures to use. Either this, or
inverse-temperatures-file
must be provided (but not both).
inverse-temperatures-file
:- Path to an hdf file containing the inverse temperatures (“betas”)
to use. The betas will be retrieved from the file’s
.attrs['betas']
. Either this orntemps
must be provided (but not both).
niterations
:- The number of iterations to run the sampler for. Either this or
effective-nsamples
must be provided (but not both).
effective-nsamples
:- Run the sampler until the given number of effective samples are
obtained. A
checkpoint-interval
must also be provided in this case. Either this orniterations
must be provided (but not both).
thin-interval
:- Thin the samples by the given value before saving to disk. May
provide this, or
max-samples-per-chain
, but not both. If neither options are provided, will save all samples.
max-samples-per-chain
:- Thin the samples such that the number of samples per chain per
temperature that are saved to disk never exceeds the given value.
May provide this, or
thin-interval
, but not both. If neither options are provided, will save all samples.
checkpoint-interval
:- Sets the checkpoint interval to use. Must be provided if using
effective-nsamples
.
checkpoint-signal
:- Set the checkpoint signal, e.g., “USR2”. Optional.
seed
:- The seed to use for epsie’s random number generator. If not provided, epsie will create one.
logl-function
:- The attribute of the model to use for the loglikelihood. If
not provided, will default to
loglikelihood
.
swap-interval
:- The number of iterations between temperature swaps. Default is 1.
Jump proposals must be provided for every sampling parameter. These are retrieved from subsections
[jump_proposal-{params}]
, where params is apycbc.VARARGS_DELIM
separated list of parameters the proposal should be used for. Seeinference.jump.epsie_proposals_from_config()
for details.Note
Jump proposals should be specified for sampling parameters, not variable parameters.
Settings for burn-in tests are read from
[sampler-burn_in]
. In particular, theburn-in-test
option is used to set the burn in tests to perform. SeeMultiTemperedMCMCBurnInTests.from_config()
for details. If noburn-in-test
is provided, no burn in tests will be carried out.Parameters: - cp (WorkflowConfigParser instance) – Config file object to parse.
- model (pycbc.inference.model.BaseModel instance) – The model to use.
- output_file (str, optional) – The name of the output file to checkpoint and write results to.
- nprocesses (int, optional) – The number of parallel processes to use. Default is 1.
- use_mpi (bool, optional) – Use MPI for parallelization. Default is False.
Returns: The sampler instance.
Return type: EpsiePTSampler
-
io
¶
-
model_stats
¶ A dict mapping the model’s
default_stats
to arrays of values.The arrays have shape
ntemps x nchains x niterations
.
-
name
= 'epsie'¶
-
nchains
¶ Alias for
nwalkers
.
-
pos
¶ A dictionary of the current chain positions.
-
run_mcmc
(niterations)[source]¶ Advance the chains for a number of iterations.
Parameters: niterations (int) – Number of samples to get from sampler.
-
samples
¶ A dict mapping
variable_params
to arrays of samples currently in memory.The arrays have shape
ntemps x nchains x niterations
.The dictionary also contains sampling parameters.
-
seed
¶ The seed used for epsie’s random bit generator.
This is not the same as the seed used for the prior distributions.
-
set_p0
(samples_file=None, prior=None)[source]¶ Sets the initial position of the walkers.
Parameters: - samples_file (InferenceFile, optional) – If provided, use the last iteration in the given file for the starting positions.
- prior (JointDistribution, optional) – Use the given prior to set the initial positions rather than
model
’s prior.
Returns: p0 – A dictionary maping sampling params to the starting positions.
Return type:
-
set_state_from_file
(filename)[source]¶ Sets the state of the sampler back to the instance saved in a file.
-
swap_interval
¶ Number of iterations between temperature swaps.
- model (model) – A model from
pycbc.inference.sampler.multinest module¶
This modules provides classes and functions for using the Multinest sampler packages for parameter estimation.
-
class
pycbc.inference.sampler.multinest.
MultinestSampler
(model, nlivepoints, checkpoint_interval=1000, importance_nested_sampling=False, evidence_tolerance=0.1, sampling_efficiency=0.01, constraints=None)[source]¶ Bases:
pycbc.inference.sampler.base.BaseSampler
This class is used to construct a nested sampler from the Multinest package.
Parameters: - model (model) – A model from
pycbc.inference.models
. - nlivepoints (int) – Number of live points to use in sampler.
-
check_if_finished
()[source]¶ Estimate remaining evidence to see if desired evidence-tolerance stopping criterion has been reached.
-
checkpoint_interval
¶ Get the number of iterations between checkpoints.
-
dlogz
¶ Get the current error estimate of the log evidence.
-
finalize
()[source]¶ All data is written by the last checkpoint in the run method, so this just passes.
-
classmethod
from_config
(cp, model, output_file=None, nprocesses=1, use_mpi=False)[source]¶ Loads the sampler from the given config file.
-
get_posterior_samples
()[source]¶ Read posterior samples from ASCII output file created by multinest.
-
importance_dlogz
¶ Get the current error estimate of the importance weighted log evidence.
-
importance_logz
¶ Get the current importance weighted estimate of the log evidence.
-
io
¶
-
logz
¶ Get the current estimate of the log evidence.
-
model_stats
¶ A dict mapping the model’s
default_stats
to arrays of values.
-
name
= 'multinest'¶
-
niterations
¶ Get the current number of iterations.
-
nlivepoints
¶ Get the number of live points used in sampling.
-
samples
¶ A dict mapping
variable_params
to arrays of samples currently in memory.
-
set_initial_conditions
(initial_distribution=None, samples_file=None)[source]¶ Sets the initial starting point for the sampler.
If a starting samples file is provided, will also load the random state from it.
-
set_state_from_file
(filename)[source]¶ Sets the state of the sampler back to the instance saved in a file.
-
setup_output
(output_file)[source]¶ Sets up the sampler’s checkpoint and output files.
The checkpoint file has the same name as the output file, but with
.checkpoint
appended to the name. A backup file will also be created.Parameters: - sampler (sampler instance) – Sampler
- output_file (str) – Name of the output file.
- model (model) – A model from
pycbc.inference.sampler.ultranest module¶
This modules provides classes and functions for using the dynesty sampler packages for parameter estimation.
-
class
pycbc.inference.sampler.ultranest.
UltranestSampler
(model, **kwargs)[source]¶ Bases:
pycbc.inference.sampler.base.BaseSampler
This class is used to construct an Ultranest sampler from the ultranest package.
Parameters: model (model) – A model from pycbc.inference.models
.-
checkpoint
()[source]¶ The sampler must have a checkpoint method for dumping raw samples and stats to the file type defined by
io
.
-
classmethod
from_config
(cp, model, output_file=None, **kwds)[source]¶ Loads the sampler from the given config file.
-
io
¶
-
logz
¶ return bayesian evidence estimated by dynesty sampler
-
logz_err
¶ return error in bayesian evidence estimated by dynesty sampler
-
model_stats
¶
-
name
= 'ultranest'¶
-
niterations
¶
-
run
()[source]¶ This function should run the sampler.
Any checkpointing should be done internally in this function.
-
samples
¶
-
Module contents¶
This modules provides a list of implemented samplers for parameter estimation.
-
pycbc.inference.sampler.
load_from_config
(cp, model, **kwargs)[source]¶ Loads a sampler from the given config file.
This looks for a name in the section
[sampler]
to determine which sampler class to load. That sampler’sfrom_config
is then called.Parameters: - cp (WorkflowConfigParser) – Config parser to read from.
- model (pycbc.inference.model) – Which model to pass to the sampler.
- **kwargs – All other keyword arguments are passed directly to the sampler’s
from_config
file.
Returns: The initialized sampler.
Return type: sampler