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, check_nsamples=True)[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
¶

nchains
¶

niterations
¶

checkpoint_interval
¶

checkpoint_signal
¶

target_niterations
¶

target_eff_nsamples
¶

thin_interval
¶

max_samples_per_chain
¶

thin_safety_factor
¶

burn_in
¶

raw_acls
¶

act
¶

raw_acts
¶

acl
()[source] The autocorrelation length.
This method should convert the raw ACLs into an integer or array that can be used to extract independent samples from a chain.

act
The autocorrelation time(s).
The autocorrelation time is defined as the autocorrelation length times the
thin_interval
. It gives the number of iterations between independent samples. Depending on the sampler, this may either be a single integer or an array of values.Returns
None
if no ACLs have been calculated.

base_shape
¶ What shape the sampler’s samples arrays are in, excluding the iterations dimension.
For example, if a sampler uses 20 chains 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 burnin tests (if specified).

static
checkpoint_from_config
(cp, section)[source]¶ Gets the checkpoint interval from the given config file.
This looks for ‘checkpointinterval’ 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 ‘checkpointsignal’ 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
()[source] The effective number of samples post burnin 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.

nchains
The number of chains used.

niterations
The current number of iterations.

p0
A dictionary of the initial position of the chains.
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.

raw_acls
Dictionary of parameter names > autocorrelation lengths.
Depending on the sampler, the ACLs may be an integer, or an arrray of values per chain and/or per temperature.
Returns
None
if no ACLs have been calculated.

raw_acts
Dictionary of parameter names > autocorrelation time(s).
Returns
None
if no ACLs have been calculated.

set_burn_in_from_config
(cp)[source]¶ Sets the burn in class from the given config file.
If no burnin section exists in the file, then this just set the burnin class to None.

set_p0
(samples_file=None, prior=None)[source]¶ Sets the initial position of the chains.
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
, andeffectivensamples
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.
EnsembleSupport
[source]¶ Bases:
object
Adds support for ensemble MCMC samplers.

acl
¶ The autocorrelation length of the ensemble.
This is calculated by taking the maximum over all of the
raw_acls
. This works for both single and paralleltempered ensemble samplers.Returns
None
if no ACLs have been set.

effective_nsamples
¶ The effective number of samples post burnin that the sampler has acquired so far.

nwalkers
¶ The number of walkers used.
Alias of
nchains
.


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.
ensemble_compute_acf
(filename, start_index=None, end_index=None, per_walker=False, walkers=None, parameters=None)[source]¶ Computes the autocorrleation function for an ensemble MCMC.
By default, parameter values are averaged over all walkers at each iteration. The ACF is then calculated over the averaged chain. An ACF perwalker will be returned instead if
per_walker=True
.Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 start_index (int, optional) – The start index to compute the acl from. If None (the default), will try to use the number of burnin 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 (the default), will go to the end of the current iteration.
 per_walker (bool, optional) – Return the ACF for each walker separately. Default is False.
 walkers (int or array, optional) – Calculate the ACF using only the given walkers. If None (the default) all walkers will be used.
 parameters (str or array, optional) – 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
perwalker
is True, the arrays will have shapenwalkers x niterations
.Return type:

pycbc.inference.sampler.base_mcmc.
ensemble_compute_acl
(filename, start_index=None, end_index=None, min_nsamples=10)[source]¶ Computes the autocorrleation length for an ensemble MCMC.
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 burnin 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:

pycbc.inference.sampler.base_mcmc.
get_optional_arg_from_config
(cp, section, arg, dtype=<class '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.
MultiTemperedSupport
[source]¶ Bases:
object
Provides methods for supporting multitempered 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
inversetemperaturesfile
must be provided (but not both).
inversetemperaturesfile
: 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 inversetemperaturesfile was provided.

ntemps
¶ The number of temeratures that are set.

static

pycbc.inference.sampler.base_multitemper.
acl_from_raw_acls
(acls)[source]¶ Calculates the ACL for one or more chains from a dictionary of ACLs.
This is for parallel tempered MCMCs in which the chains are independent of each other.
The ACL for each chain is maximized over the temperatures and parameters.
Parameters: acls (dict) – Dictionary of parameter names > ntemps x nchains arrays of ACLs (the thing returned by compute_acl()
).Returns: The ACL of each chain. Return type: array

pycbc.inference.sampler.base_multitemper.
compute_acf
(filename, start_index=None, end_index=None, chains=None, parameters=None, temps=None)[source]¶ Computes the autocorrleation function for independent MCMC chains with parallel tempering.
Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 start_index (int, optional) – The start index to compute the acl from. If None (the default), will try to use the burn in iteration for each chain; 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.
 chains (optional, int or array) – Calculate the ACF for only the given chains. If None (the default) ACFs for all chains will be estimated.
 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 parameter name > ACF arrays. The arrays have shape
ntemps x nchains x niterations
.Return type:

pycbc.inference.sampler.base_multitemper.
compute_acl
(filename, start_index=None, end_index=None, min_nsamples=10)[source]¶ Computes the autocorrleation length for independent MCMC chains with parallel tempering.
ACLs are calculated separately for each 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 burnin 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 x nchains arrays of the ACLs of each parameter.
Return type:

pycbc.inference.sampler.base_multitemper.
ensemble_compute_acf
(filename, start_index=None, end_index=None, per_walker=False, walkers=None, parameters=None, temps=None)[source]¶ Computes the autocorrleation function for a parallel tempered, ensemble MCMC.
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 perwalker will be returned instead if
per_walker=True
.Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 start_index (int, optional) – The start index to compute the acl from. If None (the default), will try to use the number of burnin 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 (the default), will go to the end of the current iteration.
 per_walker (bool, optional) – Return the ACF for each walker separately. Default is False.
 walkers (int or array, optional) – Calculate the ACF using only the given walkers. If None (the default) all walkers will be used.
 parameters (str or array, optional) – Calculate the ACF for only the given parameters. If None (the default) will calculate the ACF for all of the model params.
 temps ((list of) int or 'all', optional) – 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
perwalker
is True, the arrays will have shapentemps x nwalkers x niterations
. Otherwise, the returned array will have shapentemps x niterations
.Return type:

pycbc.inference.sampler.base_multitemper.
ensemble_compute_acl
(filename, start_index=None, end_index=None, min_nsamples=10)[source]¶ Computes the autocorrleation length for a parallel tempered, ensemble MCMC.
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 (int, optional) – The start index to compute the acl from. If None (the default), will try to use the number of burnin 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 of ntempslong arrays of the ACLs of each parameter.
Return type:
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, nprocesses=1, checkpoint_time_interval=None, maxcall=None, loglikelihood_function=None, use_mpi=False, run_kwds=None, **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.
 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.

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
¶ 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.

logz
¶ return bayesian evidence estimated by dynesty sampler

logz_err
¶ return error in bayesian evidence estimated by dynesty sampler

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
= 'dynesty'¶

niterations
¶

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.

set_initial_conditions
(initial_distribution=None, samples_file=None)[source]¶ Sets up the starting point for the sampler.
Should also set the sampler’s random state.
 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.EnsembleSupport
,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
¶ What shape the sampler’s samples arrays are in, excluding the iterations dimension.
For example, if a sampler uses 20 chains and 3 temperatures, this would be
(3, 20)
. If a sampler only uses a single walker and no temperatures this would be()
.

burn_in_class
¶

static
compute_acf
(filename, **kwargs)[source]¶ Computes the autocorrelation function.
Calls
base_mcmc.ensemble_compute_acf()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 **kwargs – All other keyword arguments are passed to
base_mcmc.ensemble_compute_acf()
.
Returns: Dictionary of arrays giving the ACFs for each parameter. If
perwalker
is True, the arrays will have shapenwalkers x niterations
.Return type:

static
compute_acl
(filename, **kwargs)[source]¶ Computes the autocorrelation length.
Calls
base_mcmc.ensemble_compute_acl()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACLs for.
 **kwargs – All other keyword arguments are passed to
base_mcmc.ensemble_compute_acf()
.
Returns: A dictionary giving the ACL for each parameter.
Return type:

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
¶ 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 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.MultiTemperedSupport
,pycbc.inference.sampler.base_mcmc.EnsembleSupport
,pycbc.inference.sampler.base_mcmc.BaseMCMC
,pycbc.inference.sampler.base.BaseSampler
This class is used to construct a paralleltempered 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
¶ What shape the sampler’s samples arrays are in, excluding the iterations dimension.
For example, if a sampler uses 20 chains and 3 temperatures, this would be
(3, 20)
. If a sampler only uses a single walker and no temperatures this would be()
.

betas
¶

burn_in_class
¶ alias of
pycbc.inference.burn_in.EnsembleMultiTemperedMCMCBurnInTests

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 ith 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

static
compute_acf
(filename, **kwargs)[source]¶ Computes the autocorrelation function.
Calls
base_multitemper.ensemble_compute_acf()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 **kwargs – All other keyword arguments are passed to
base_multitemper.ensemble_compute_acf()
.
Returns: Dictionary of arrays giving the ACFs for each parameter. If
perwalker=True
is passed as a keyword argument, the arrays will have shapentemps x nwalkers x niterations
. Otherwise, the returned array will have shapentemps x niterations
.Return type:

static
compute_acl
(filename, **kwargs)[source]¶ Computes the autocorrelation length.
Calls
base_multitemper.ensemble_compute_acl()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACLs for.
 **kwargs – All other keyword arguments are passed to
base_multitemper.ensemble_compute_acl()
.
Returns: A dictionary of ntempslong arrays of the ACLs of each parameter.
Return type:

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
inversetemperaturesfile
must be provided (but not both).
inversetemperaturesfile
: 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
effectivensamples
must be provided (but not both).
effectivensamples
: Run the sampler until the given number of effective samples are
obtained. A
checkpointinterval
must also be provided in this case. Either this orniterations
must be provided (but not both).
thininterval
: Thin the samples by the given value before saving to disk. May
provide this, or
maxsamplesperchain
, but not both. If neither options are provided, will save all samples.
maxsamplesperchain
: 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
thininterval
, but not both. If neither options are provided, will save all samples.
checkpointinterval
: Sets the checkpoint interval to use. Must be provided if using
effectivensamples
.
checkpointsignal
: Set the checkpoint signal, e.g., “USR2”. Optional.
loglfunction
: The attribute of the model to use for the loglikelihood. If
not provided, will default to
loglikelihood
.
Settings for burnin tests are read from
[samplerburn_in]
. In particular, theburnintest
option is used to set the burn in tests to perform. SeeMultiTemperedMCMCBurnInTests.from_config()
for details. If noburnintest
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
¶ 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
¶ 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.MultiTemperedSupport
,pycbc.inference.sampler.base_mcmc.BaseMCMC
,pycbc.inference.sampler.base.BaseSampler
Constructs an MCMC sampler using epsie’s paralleltempered sampler.
Parameters:  model (model) – A model from
pycbc.inference.models
.  nchains (int) – Number of chains to use in the sampler.
 ntemps (int, optional) – Number of temperatures to use in the sampler. A geometricallyspaced
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 (list, optional) – List of proposals to use. Any parameters that do not have a proposal
provided will use the
default_propsal
. 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.

acl
¶ The autocorrelation lengths of the chains.

base_shape
¶ What shape the sampler’s samples arrays are in, excluding the iterations dimension.
For example, if a sampler uses 20 chains and 3 temperatures, this would be
(3, 20)
. If a sampler only uses a single walker and no temperatures this would be()
.

betas
¶ The inverse temperatures being used.

burn_in_class
¶ alias of
pycbc.inference.burn_in.MultiTemperedMCMCBurnInTests

static
compute_acf
(filename, **kwargs)[source]¶ Computes the autocorrelation function.
Calls
base_multitemper.compute_acf()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACFs for.
 **kwargs – All other keyword arguments are passed to
base_multitemper.compute_acf()
.
Returns: Dictionary of arrays giving the ACFs for each parameter. The arrays will have shape
ntemps x nchains x niterations
.Return type:

static
compute_acl
(filename, **kwargs)[source]¶ Computes the autocorrelation length.
Calls
base_multitemper.compute_acl()
; see that function for details.Parameters:  filename (str) – Name of a samples file to compute ACLs for.
 **kwargs – All other keyword arguments are passed to
base_multitemper.compute_acl()
.
Returns: A dictionary of ntempslong arrays of the ACLs of each parameter.
Return type:

effective_nsamples
¶ The effective number of samples post burnin that the sampler has acquired so far.

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
inversetemperaturesfile
must be provided (but not both).
inversetemperaturesfile
: 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
effectivensamples
must be provided (but not both).
effectivensamples
: Run the sampler until the given number of effective samples are
obtained. A
checkpointinterval
must also be provided in this case. Either this orniterations
must be provided (but not both).
thininterval
: Thin the samples by the given value before saving to disk. May
provide this, or
maxsamplesperchain
, but not both. If neither options are provided, will save all samples.
maxsamplesperchain
: 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
thininterval
, but not both. If neither options are provided, will save all samples.
checkpointinterval
: Sets the checkpoint interval to use. Must be provided if using
effectivensamples
.
checkpointsignal
: 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.
loglfunction
: The attribute of the model to use for the loglikelihood. If
not provided, will default to
loglikelihood
.
swapinterval
: 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 burnin tests are read from
[samplerburn_in]
. In particular, theburnintest
option is used to set the burn in tests to perform. SeeMultiTemperedMCMCBurnInTests.from_config()
for details. If noburnintest
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
¶ 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 the model’s
default_stats
to arrays of values.The arrays have shape
ntemps x nchains x niterations
.

name
= 'epsie'¶

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 chains.
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 evidencetolerance 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
¶ 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.

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 ultranest sampler packages for parameter estimation.

class
pycbc.inference.sampler.ultranest.
UltranestSampler
(model, log_dir=None, stepsampling=False, enable_plots=False, **kwargs)[source]¶ Bases:
pycbc.inference.sampler.base.BaseSampler
This class is used to construct an Ultranest sampler from the ultranest package.
Parameters: 
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
¶ 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.

logz
¶ return bayesian evidence estimated by ultranest sampler

logz_err
¶ return error in bayesian evidence estimated by ultranest sampler

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
= 'ultranest'¶

niterations
¶

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.

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