NUTS#
- class cuqi.experimental.mcmc.NUTS(target=None, initial_point=None, max_depth=None, step_size=None, opt_acc_rate=0.6, **kwargs)#
No-U-Turn Sampler (Hoffman and Gelman, 2014).
Samples a distribution given its logpdf and gradient using a Hamiltonian Monte Carlo (HMC) algorithm with automatic parameter tuning.
For more details see: See Hoffman, M. D., & Gelman, A. (2014). The no-U-turn sampler: Adaptively setting path lengths in Hamiltonian Monte Carlo. Journal of Machine Learning Research, 15, 1593-1623.
- 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. If not provided, the initial point is an array of ones.
max_depth (int) – Maximum depth of the tree >=0 and the default is 15.
step_size (None or float) – If step_size is provided (as positive float), it will be used as initial step size. If None, the step size will be estimated by the sampler.
opt_acc_rate (float) – The optimal acceptance rate to reach if using adaptive step size. Suggested values are 0.6 (default) or 0.8 (as in stan). In principle, opt_acc_rate should be in (0, 1), however, choosing a value that is very close to 1 or 0 might lead to poor performance of 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
# Import cuqi import cuqi # Define a target distribution tp = cuqi.testproblem.WangCubic() target = tp.posterior # Set up sampler sampler = cuqi.experimental.mcmc.NUTS(target) # Sample sampler.warmup(5000) sampler.sample(10000) # Get samples samples = sampler.get_samples() # Plot samples samples.plot_pair()
After running the NUTS sampler, run diagnostics can be accessed via the following attributes:
# Number of tree nodes created each NUTS iteration sampler.num_tree_node_list # Step size used in each NUTS iteration sampler.epsilon_list # Suggested step size during adaptation (the value of this step size is # only used after adaptation). sampler.epsilon_bar_list
- __init__(target=None, initial_point=None, max_depth=None, step_size=None, opt_acc_rate=0.6, **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, initial_point, max_depth, ...])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)adapt epsilon during burn-in using dual averaging
Validate the target is compatible with the sampler.
warmup
(Nb[, tune_freq])Warmup the sampler by drawing Nb samples.
Attributes