ULA#
- class cuqi.experimental.mcmc.ULA(target=None, scale=1.0, **kwargs)#
Unadjusted Langevin algorithm (ULA) (Roberts and Tweedie, 1996)
It approximately samples a distribution given its logpdf gradient based on the Langevin diffusion dL_t = dW_t + 1/2*Nabla target.logd(L_t)dt, where W_t is the dim-dimensional standard Brownian motion. ULA results from the Euler-Maruyama discretization of this Langevin stochastic differential equation (SDE).
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 logd 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 and gradient as UserDefinedDistribution target = cuqi.distribution.UserDefinedDistribution(dim=dim, logpdf_func=logpdf_func, gradient_func=gradient_func) # Set up sampler sampler = cuqi.experimental.mcmc.ULA(target, scale=1/dim**2) # Sample sampler.sample(2000)
A Deblur example can be found in demos/demo27_ULA.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.
Return the history of the sampler.
Return the samples.
Return the state of the sampler.
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.
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 the target is compatible with the sampler.
warmup
(Nb[, tune_freq])Warmup the sampler by drawing Nb samples.
Attributes