Sampler#

class cuqi.experimental.mcmc.Sampler(target=None, initial_point=None, callback=None)#

Abstract base class for all samplers.

Provides a common interface for all samplers. The interface includes methods for sampling, warmup and getting the samples in an object oriented way.

Samples are stored in a list to allow for dynamic growth of the sample set. Returning samples is done by creating a new Samples object from the list of samples.

The sampler maintains sets of state and history keys, which are used for features like checkpointing and resuming sampling.

The state of the sampler represents all variables that are updated (replaced) in a Markov Monte Carlo step, e.g. the current point of the sampler.

The history of the sampler represents all variables that are updated (appended) in a Markov Monte Carlo step, e.g. the samples and acceptance rates.

Subclasses should ensure that any new variables that are updated in a Markov Monte Carlo step are added to the state or history keys.

Saving and loading checkpoints saves and loads the state of the sampler (not the history).

Batching samples via the batch_size parameter saves the sampler history to disk in batches of the specified size.

Any other attribute stored as part of the sampler (e.g. target, initial_point) is not supposed to be updated during sampling and should not be part of the state or history.

__init__(target=None, initial_point=None, callback=None)#

Initializer for abstract base class for all samplers.

Any subclassing samplers should simply store input parameters as part of the __init__ method.

The actual initialization of the sampler should be done in the _initialize method.

Parameters:
  • target (cuqi.density.Density) – The target density.

  • initial_point (array-like, optional) – The initial point for the sampler. If not given, the sampler will choose an initial point.

  • callback (callable, optional) – A function that will be called after each sample is drawn. The function should take two arguments: the sample and the index of the sample. The sample is a 1D numpy array and the index is an integer. The callback function is useful for monitoring the sampler during sampling.

Methods

__init__([target, initial_point, callback])

Initializer for abstract base class for all samplers.

get_history()

Return the history of the sampler.

get_samples()

Return the samples.

get_state()

Return the state of the sampler.

initialize()

Initialize the sampler by setting and allocating the state and history before sampling starts.

load_checkpoint(path)

Load the state of the sampler from a file.

reinitialize()

Re-initialize the sampler.

sample(Ns[, batch_size, sample_path])

Sample Ns samples from the target density.

save_checkpoint(path)

Save the state of the sampler to a file.

set_history(history)

Set the history of the sampler.

set_state(state)

Set the state of the sampler.

step()

Perform one step of the sampler by transitioning the current point to a new point according to the sampler's transition kernel.

tune(skip_len, update_count)

Tune the parameters of the sampler.

validate_target()

Validate the target is compatible with the sampler.

warmup(Nb[, tune_freq])

Warmup the sampler by drawing Nb samples.

Attributes

dim

Dimension of the target density.

geometry

Geometry of the target density.

target

Return the target density.