NUTS#
- class cuqi.sampler.NUTS(target, x0=None, max_depth=15, adapt_step_size=True, 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
.x0 (ndarray) – Initial parameters. Optional
max_depth (int) – Maximum depth of the tree.
adapt_step_size (Bool or float) – Whether to adapt the step size. If True, the step size is adapted automatically. If False, the step size is fixed to the initially estimated value. If set to a scalar, the step size will be given by user and not adapted.
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).
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.sampler.NUTS(target) # Sample samples = sampler.sample(10000, 5000) # 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). The suggested step size is None if # adaptation is not requested. sampler.epsilon_bar_list # Additionally, iterations' number can be accessed via sampler.iteration_list
- __init__(target, x0=None, max_depth=15, adapt_step_size=True, opt_acc_rate=0.6, **kwargs)#
Methods
__init__
(target[, x0, max_depth, ...])sample
(N[, Nb])sample_adapt
(N[, Nb])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