MH#

class cuqi.sampler.MH(target, proposal=None, scale=None, x0=None, dim=None, **kwargs)#

Metropolis Hastings sampler.

Allows sampling of a target distribution by random-walk sampling of a proposal distribution along with an accept/reject step.

Parameters:
  • target (cuqi.distribution.Distribution or lambda function) – The target distribution to sample. Custom logpdfs are supported by using a cuqi.distribution.UserDefinedDistribution.

  • proposal (cuqi.distribution.Distribution or callable method) – The proposal to sample from. If a callable method it should provide a single independent sample from proposal distribution. Defaults to a Gaussian proposal. Optional.

  • scale (float) – Scale parameter used to define correlation between previous and proposed sample in random-walk. Optional.

  • x0 (ndarray) – Initial parameters. Optional

  • dim (int) – Dimension of parameter space. Required if target and proposal are callable functions. Optional.

  • callback (callable, Optional) – If set this function will be called after every sample. The signature of the callback function is callback(sample, sample_index), where sample is the current sample and sample_index is the index of the sample. An example is shown in demos/demo31_callback.py.

Example

# Parameters
dim = 5 # Dimension of distribution
mu = np.arange(dim) # Mean of Gaussian
std = 1 # standard deviation of Gaussian

# Logpdf function
logpdf_func = lambda x: -1/(std**2)*np.sum((x-mu)**2)

# Define distribution from logpdf as UserDefinedDistribution (sample and gradients also supported)
target = cuqi.distribution.UserDefinedDistribution(dim=dim, logpdf_func=logpdf_func)

# Set up sampler
sampler = cuqi.sampler.MH(target, scale=1)

# Sample
samples = sampler.sample(2000)
__init__(target, proposal=None, scale=None, x0=None, dim=None, **kwargs)#

Metropolis-Hastings (MH) sampler. Default (if proposal is None) is random walk MH with proposal that is Gaussian with identity covariance

Methods

__init__(target[, proposal, scale, x0, dim])

Metropolis-Hastings (MH) sampler.

sample(N[, Nb])

sample_adapt(N[, Nb])

single_update(x_t, target_eval_t)

step(x)

Perform a single MCMC step

step_tune(x, *args, **kwargs)

Perform a single MCMC step and tune the sampler.

tune()

Tune the sampler parameters.

Attributes