MALA#

class cuqi.experimental.mcmc.MALA(target=None, scale=1.0, **kwargs)#

Metropolis-adjusted Langevin algorithm (MALA) (Roberts and Tweedie, 1996)

Samples a distribution given its logd and gradient (up to a constant) based on Langevin diffusion dL_t = dW_t + 1/2*Nabla target.logd(L_t)dt, W_t is the dim-dimensional standard Brownian motion. A sample is firstly proposed by ULA and is then accepted or rejected according to a Metropolis–Hastings step. This accept-reject step allows us to remove the asymptotic bias of ULA.

For more details see: Roberts, G. O., & Tweedie, R. L. (1996). Exponential convergence of Langevin distributions and their discrete approximations. Bernoulli, 341-363.

Parameters:
  • target (cuqi.distribution.Distribution) – The target distribution to sample. Must have logpdf and gradient method. Custom logpdfs and gradients are supported by using a cuqi.distribution.UserDefinedDistribution.

  • initial_point (ndarray) – Initial parameters. Optional

  • scale (float) – The Langevin diffusion discretization time step (In practice, scale must be smaller than 1/L, where L is the Lipschitz of the gradient of the log target density, logd).

  • 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)
gradient_func = lambda x: -2/(std**2)*(x-mu)

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

# Set up sampler
sampler = cuqi.experimental.mcmc.MALA(target, scale=1/5**2)

# Sample
sampler.sample(2000)

A Deblur example can be found in demos/demo28_MALA.py # TODO: update demo once sampler merged

__init__(target=None, scale=1.0, **kwargs)#

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, scale])

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.