epsie.samplers package

Submodules

epsie.samplers.base module

Base class and functions for samplers.

class epsie.samplers.base.BaseSampler[source]

Bases: abc.ABC

Base class for samplers.

property acceptance

The history of all acceptance stats from all of the chains.

property blobs

The history of all of the blobs from all of the chains.

property chains

List of the chains used.

checkpoint(fp, path=None, dsetname='sampler_state')[source]

Save the sampler’s state to an HDF file.

Parameters
  • fp (:py:class:h5py.File) – Open file handler to an hdf5 file. The file handler must have write permission.

  • path (str, optional) – What group to write the state to in the hdf file. Default is the top-level.

  • dsetname (str, optional) – The name of dataset to store the state to. Default is “sampler_state”.

clear()[source]

Clears all of the chains.

property current_blobs

The current blob data.

This will default to the blob data of the start positions if the chains haven’t been run yet.

property current_positions

The current position of the chains.

This will default to the start position if the chains haven’t been run yet.

property current_stats

The current stats of the chains.

This will default to the stats of the start positions if the chains haven’t been run yet.

property lastclear

The iteration that the last clear occurred at.

property model

The model used.

property nchains

The number of chains being used.

property niterations

The number of iterations the chains have been run for.

property parameters

The sampled parameters as a tuple.

property pool

The pool being used for parallelization.

property positions

The history of positions from all of the chains.

property proposals

List of the proposals used for the sampled parameters.

run(niterations)[source]

Evolves all of the chains by niterations.

Parameters

niterations (int) – The number of iterations to evolve the chains for.

property seed

The seed used for the random bit generator.

set_proposals(proposals=None, default_proposal=None, default_proposal_args=None)[source]

Sets the proposals for all parameters.

If no proposals are given for one or more parameters, the default proposal will be used for them.

Parameters
  • proposals (list, optional) – A list of proposals.BaseProposal instances. If none provided, will use the default_proposal for all parameters.

  • default_proposal (proposals.BaseProposal type class, optional) – The default proposal class to use for parameters that are not specified in proposals. Default (None) is to use proposals.Normal.

  • default_proposal_args (dict, optional) – Dictionary of keyword arguments to pass to the default proposal when initializing.

set_state(state)[source]

Sets the state of the all of the chains.

Parameters

state (dict) – Dictionary mapping chain id to the state to set the chain to. See chain.Chain.set_state() for details.

set_state_from_checkpoint(fp, path=None)[source]

Loads a state from an HDF file.

Parameters
  • fp (:py:class:h5py.File) – Open file handler to an hdf5 file.

  • path (str, optional) – What group to store the file to in the hdf file. Default is the top-level (‘/’).

property start_position

The start position of the chains.

Will raise a ValueError is set_start hasn’t been run.

Returns

Dictionary mapping parameters to arrays with shape nchains giving the starting position of each chain.

Return type

dict

property state

The state of all of the chains.

Returns a dictionary mapping chain ids to their states.

property stats

The history of stats from all of the chains.

epsie.samplers.mhsampler module

class epsie.samplers.mhsampler.MetropolisHastingsSampler(parameters, model, nchains, proposals=None, default_proposal=None, default_proposal_args=None, seed=None, pool=None)[source]

Bases: epsie.samplers.base.BaseSampler

A standard Metropolis-Hastings sampler.

Parameters
  • parameters (tuple or list) – Names of the parameters to sample.

  • model (object) – Model object.

  • nchains (int) – The number of chains to create. Must be greater than zero.

  • proposals (list, optional) – List of proposals to use. Any parameters that do not have a proposal provided will use the default_propsal.

  • default_proposal (an epsie.Proposal class, optional) – The default proposal to use for parameters not in proposals. Default is epsie.proposals.Normal.

  • default_proposal_args (dict, optional) – Dictionary of arguments to pass to the default proposal.

  • seed (int, optional) – Seed for the random number generator. If None provided, will create one.

  • pool (Pool object, optional) – Specify a process pool to use for parallelization. Default is to use a single core.

create_chains(nchains)[source]

Creates a list of chain.Chain.

Parameters

nchains (int) – The number of Markov chains to create.

epsie.samplers.ptsampler module

Classes for parallel tempered Markov chains.

class epsie.samplers.ptsampler.ParallelTemperedSampler(parameters, model, nchains, betas, swap_interval=1, proposals=None, adaptive_annealer=None, reset_after_swap=False, default_proposal=None, default_proposal_args=None, seed=None, pool=None)[source]

Bases: epsie.samplers.base.BaseSampler

Evolves a collection of parallel tempered Markov chains.

Parameters
  • parameters (tuple or list) – Names of the parameters to sample.

  • model (object) – Model object.

  • nchains (int) – The number of chains to create per temperature. Must be greater than zero.

  • betas (numpy.ndarray of floats) – The betas (= 1 / temperatures) to use. All betas must be between [0,1]. Must provide at least one. If one is not included in the betas, a warning will be printed.

  • swap_interval (int, optional) – How often to calculate temperature swaps. Default is 1 (= swap on every iteration).

  • proposals (list, optional) – List of proposals to use. Any parameters that do not have a proposal provided will use the default_propsal.

  • adaptive_annealer (object, optional) – Adaptive annealing that adjusts the temperature levels during runtime. By default None, meaning no annealing.

  • reset_after_swap (bool, optional) – Whether to reset proposals’ adaptation after each swap. By default no reset.

  • default_proposal (an epsie.Proposal class, optional) – The default proposal to use for parameters not in proposals. Default is epsie.proposals.Normal.

  • default_proposal_args (dict, optional) – Dictionary of arguments to pass to the default proposal.

  • seed (int, optional) – Seed for the random number generator. If None provided, will create one.

  • pool (Pool object, optional) – Specify a process pool to use for parallelization. Default is to use a single core.

property betas

The betas used for each chain. Shape is (nchains, ntemps). If temperatures are being dynamically adjusted each chain will have a different set of temperatures.

create_chains(nchains, betas, swap_interval=1, adaptive_annealer=None, reset_after_swap=False)[source]

Creates a list of chain.ParallelTemperedChain.

Parameters
  • nchains (int) – The number of parallel tempered chains to create.

  • betas (array) – Array of inverse temperatures to use for each parallel tempered chain.

  • swap_interval (int, optional) – How often to calculate temperature swaps. Default is 1 (= swap on every iteration).

  • adaptive_annealer (object, optional) – Adaptive anneler adjusting temperatures on the go. Default is None.

  • reset_after_swap (bool, optional) – Whether to reset proposals’ adaptation after each swap. By default no reset.

property ntemps

Returns the number of temperatures used.

property swap_interval

Returns the swap interval used for parallel tempering.

property temperature_acceptance

The history of the temperature acceptance from all of the chains.

If ntemps is 1, just returns None.

Note

This does not return a structured array, since there is only one field.

property temperature_swaps

The history of all the temperature swaps from all of the chains.

If ntemps is 1, just returns None.

Note

This does not return a structured array, since there is only one field.

property temperatures

The temperatures used. Shape is (nchains, ntemps)

Module contents