gwin.sampler.base module

This modules provides classes and functions for using different sampler packages for parameter estimation.

class gwin.sampler.base.BaseMCMCSampler(sampler, model)[source]

Bases: gwin.sampler.base._BaseSampler

This class is used to construct the MCMC sampler from the kombine-like packages.

Parameters:
  • sampler (sampler instance) – An instance of an MCMC sampler similar to kombine or emcee.
  • model (model class) – A model from gwin.models.
sampler

The MCMC sampler instance used.

p0

nwalkers x ndim array – The initial position of the walkers. Set by using set_p0. If not set yet, a ValueError is raised when the attribute is accessed.

pos

{None, array} – An array of the current walker positions.

acceptance_fraction

Get the fraction of steps accepted by each walker as an array.

classmethod compute_acfs(fp, 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:
  • fp (InferenceFile) – An open file handler to read the samples from.
  • 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:

A FieldArray of the ACF vs iteration for each parameter. If per-walker is True, the FieldArray will have shape nwalkers x niterations.

Return type:

FieldArray

classmethod compute_acls(fp, start_index=None, end_index=None)[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 the returned ACL is inf, will default to the number of current iterations.

Parameters:
  • fp (InferenceFile) – An open file handler to read the samples from.
  • 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.
Returns:

A dictionary giving the ACL for each parameter.

Return type:

dict

model_stats

Returns the model stats as a FieldArray, with field names corresponding to the type of data returned by the model. The returned array has shape nwalkers x niterations. If no additional stats were returned to the sampler by the model, returns None.

classmethod n_independent_samples(fp)[source]

Returns the number of independent samples stored in a file.

The number of independent samples are counted starting from after burn-in. If the sampler hasn’t burned in yet, then 0 is returned.

Parameters:fp (InferenceFile) – An open file handler to read.
Returns:The number of independent samples.
Return type:int
name = None
nwalkers

Get the number of walkers.

p0
pos
static read_acceptance_fraction(fp, walkers=None)[source]

Reads the acceptance fraction from the given file.

Parameters:
  • fp (InferenceFile) – An open file handler to read the samples from.
  • walkers ({None, (list of) int}) – The walker index (or a list of indices) to retrieve. If None, samples from all walkers will be obtained.
Returns:

Array of acceptance fractions with shape (requested walkers,).

Return type:

array

static read_acls(fp)[source]

Reads the acls of all the parameters in the given file.

Parameters:fp (InferenceFile) – An open file handler to read the acls from.
Returns:A dictionary of the ACLs, keyed by the parameter name.
Return type:dict
classmethod read_samples(fp, parameters, thin_start=None, thin_interval=None, thin_end=None, iteration=None, walkers=None, flatten=True, samples_group=None, array_class=None)[source]

Reads samples for the given parameter(s).

Parameters:
  • fp (InferenceFile) – An open file handler to read the samples from.
  • parameters ((list of) strings) – The parameter(s) to retrieve. A parameter can be the name of any field in fp[fp.samples_group], a virtual field or method of FieldArray (as long as the file contains the necessary fields to derive the virtual field or method), and/or a function of these.
  • thin_start (int) – Index of the sample to begin returning samples. Default is to read samples 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 samples (set thin_interval to 1).
  • thin_end (int) – Index of the last sample to read. If not given then fp.niterations is used.
  • iteration (int) – Get a single iteration. If provided, will override the thin_{start/interval/end} arguments.
  • walkers ({None, (list of) int}) – The walker index (or a list of indices) to retrieve. If None, samples from all walkers will be obtained.
  • flatten ({True, bool}) – The returned array will be one dimensional, with all desired samples from all desired walkers concatenated together. If False, the returned array will have dimension requested walkers x requested iterations.
  • samples_group ({None, str}) – The group in fp from which to retrieve the parameter fields. If None, searches in fp.samples_group.
  • array_class ({None, array class}) – The type of array to return. The class must have a from_kwargs class method and a parse_parameters method. If None, will return a FieldArray.
Returns:

Samples for the given parameters, as an instance of a the given array_class (FieldArray if array_class is None).

Return type:

array_class

sampler
samples

Returns the samples in the chain as a FieldArray.

If the sampling args are not the same as the model params, the returned samples will have both the sampling and the model params.

The returned FieldArray has dimension [additional dimensions x] nwalkers x niterations.

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 – An nwalkers x ndim array of the initial positions that were set.

Return type:

array

write_acceptance_fraction(fp)[source]

Write acceptance_fraction data to file. Results are written to fp[acceptance_fraction].

Parameters:fp (InferenceFile) – A file handler to an open inference file.
static write_acls(fp, acls)[source]

Writes the given autocorrelation lengths to the given file.

The ACL of each parameter is saved to fp['acls/{param}']. The maximum over all the parameters is saved to the file’s ‘acl’ attribute.

Parameters:
  • fp (InferenceFile) – An open file handler to write the samples to.
  • acls (dict) – A dictionary of ACLs keyed by the parameter.
Returns:

The maximum of the acls that was written to the file.

Return type:

ACL

write_chain(fp, start_iteration=None, max_iterations=None)[source]

Writes the samples from the current chain to the given file.

Results are written to:

fp[fp.samples_group/{field}/(temp{k}/)walker{i}],

where {i} is the index of a walker, {field} is the name of each field returned by model_stats, and, if the sampler is multitempered, {k} is the temperature.

Parameters:
  • fp (InferenceFile) – A file handler to an open inference file.
  • start_iteration (int, optional) – Write results to the file’s datasets starting at the given iteration. Default is to append after the last iteration in the file.
  • max_iterations (int, optional) – Set the maximum size that the arrays in the hdf file may be resized to. Only applies if the samples have not previously been written to file. The default (None) is to use the maximum size allowed by h5py.
  • samples_group (str) – Name of samples group to write.
write_metadata(fp, **kwargs)[source]

Writes metadata about this sampler to the given file. Metadata is written to the file’s attrs.

Parameters:
  • fp (InferenceFile) – A file handler to an open inference file.
  • **kwargs – All keyword args are written to the file’s attrs.
write_model_stats(fp, start_iteration=None, max_iterations=None)[source]

Writes the model_stats to the given file.

Results are written to:

fp[fp.stats_group/{field}/(temp{k}/)walker{i}],

where {i} is the index of a walker, {field} is the name of each field returned by model_stats, and, if the sampler is multitempered, {k} is the temperature. If nothing is returned by model_stats, this does nothing.

Parameters:
  • fp (InferenceFile) – A file handler to an open inference file.
  • start_iteration (int, optional) – Write results to the file’s datasets starting at the given iteration. Default is to append after the last iteration in the file.
  • max_iterations (int, optional) – Set the maximum size that the arrays in the hdf file may be resized to. Only applies if the samples have not previously been written to file. The default (None) is to use the maximum size allowed by h5py.
Returns:

stats – The stats that were written, as a FieldArray. If there were no stats, returns None.

Return type:

{FieldArray, None}

write_results(fp, start_iteration=None, max_iterations=None, **metadata)[source]

Writes metadata, samples, model stats, and acceptance fraction to the given file. Also computes and writes the autocorrleation lengths of the chains. See the various write function for details.

Parameters:
  • fp (InferenceFile) – A file handler to an open inference file.
  • start_iteration (int, optional) – Write results to the file’s datasets starting at the given iteration. Default is to append after the last iteration in the file.
  • max_iterations (int, optional) – Set the maximum size that the arrays in the hdf file may be resized to. Only applies if the acceptance fraction has not previously been written to the file. The default (None) is to use the maximum size allowed by h5py.
  • **metadata – All other keyword arguments are passed to write_metadata.
static write_samples_group(fp, samples_group, parameters, samples, start_iteration=None, max_iterations=None)[source]

Writes samples to the given file.

Results are written to:

fp[samples_group/{vararg}],

where {vararg} is the name of a model params. The samples are written as an nwalkers x niterations array.

Parameters:
  • fp (InferenceFile) – A file handler to an open inference file.
  • samples_group (str) – Name of samples group to write.
  • parameters (list) – The parameters to write to the file.
  • samples (FieldArray) – The samples to write. Should be a FieldArray with fields containing the samples to write and shape nwalkers x niterations.
  • start_iteration (int, optional) – Write results to the file’s datasets starting at the given iteration. Default is to append after the last iteration in the file.
  • max_iterations (int, optional) – Set the maximum size that the arrays in the hdf file may be resized to. Only applies if the samples have not previously been written to file. The default (None) is to use the maximum size allowed by h5py.