pCN#

class cuqi.sampler.pCN(target, scale=None, x0=None, **kwargs)#

Preconditioned Crank-Nicolson sampler

Parameters:
  • target (cuqi.distribution.Posterior or tuple of likelihood and prior objects) – If target is of type cuqi.distribution.Posterior, it represents the posterior distribution. If target is a tuple of (cuqi.likelihood.Likelihood, cuqi.distribution.Distribution) objects, the first element is considered the likelihood and the second is considered the prior.

  • scale (int)

  • x0 (np.ndarray) – Initial point for the sampler

  • 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

This uses a custom logpdf and sample function.

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

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

# sample function of prior N(0,I)
sample_func = lambda : 0 + 1*np.random.randn(dim,1)

# Define as UserDefinedDistributions
likelihood = cuqi.likelihood.UserDefinedLikelihood(dim=dim, logpdf_func=logpdf_func)
prior = cuqi.distribution.UserDefinedDistribution(dim=dim, sample_func=sample_func)

# Set up sampler
sampler = cuqi.sampler.pCN((likelihood,prior), scale = 0.1)

# Sample
samples = sampler.sample(5000)

Example

This uses CUQIpy distributions.

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

# Define as UserDefinedDistributions
model = cuqi.model.Model(lambda x: x, range_geometry=dim, domain_geometry=dim)
likelihood = cuqi.distribution.Gaussian(mean=model, cov=np.ones(dim)).to_likelihood(mu)
prior = cuqi.distribution.Gaussian(mean=np.zeros(dim), cov=1)

target = cuqi.distribution.Posterior(likelihood, prior)

# Set up sampler
sampler = cuqi.sampler.pCN(target, scale = 0.1)

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

Methods

__init__(target[, scale, x0])

sample(N[, Nb])

sample_adapt(N[, Nb])

single_update(x_t, loglike_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