Learn R Programming

TMB (version 1.7.4)

run_mcmc.hmc: [BETA VERSION] Draw MCMC samples from a model posterior using a Hamiltonian sampler.

Description

[BETA VERSION] Draw MCMC samples from a model posterior using a Hamiltonian sampler.

Usage

run_mcmc.hmc(nsim, fn, gr, params.init, L, eps = NULL, covar = NULL, delta = 0.5, Madapt = NULL, diagnostic = FALSE)

Arguments

nsim
The number of samples to return.
fn
A function that returns the log of the posterior density.
gr
A function that returns a vector of gradients of the log of the posterior density (same as fn).
params.init
A vector of initial parameter values.
L
The number of leapfrog steps to take. The NUTS algorithm does not require this as an input. If L=1 this function will perform Langevin sampling. In some contexts L can roughly be thought of as a thinning rate.
eps
The length of the leapfrog steps. If a numeric value is passed, it will be used throughout the entire chain. A NULL value will initiate adaptation of eps using the dual averaging algorithm during the first Madapt steps.
covar
An optional covariance matrix which can be used to improve the efficiency of sampling. The lower Cholesky decomposition of this matrix is used to transform the parameter space. If the posterior is approximately multivariate normal and covar approximates the covariance, then the transformed parameter space will be close to multivariate standard normal. In this case the algorithm will be more efficient, but there will be overhead in the matrix calculations which need to be done at each step. The default of NULL specifies to not do this transformation.
delta
The target acceptance rate if using apative eps. Defaults to 50%.
Madapt
An optional argument for how many iterations to adapt eps in the dual averaging algorithm. A value of NULL results in a default of Madapt=nsim/2.
diagnostic
Whether to return a list of diagnostic metrics about the chain. Useful for assessing efficiency and tuning chain.

Value

If diagnostic is FALSE (default), returns a matrix of nsim samples from the posterior. Otherwise returns a list containing samples ('par'), proposed samples ('par.proposed'), vector of which were accepted ('accepted'), and the total function and gradient calls ('n.calls'), which for this algorithm is nsim*(L+2).

Details

This function implements algorithm 5 of Hoffman and Gelman (2014), which includes adaptive step sizes (eps) via an algorithm called dual averaging. In theory neither the step length nor step size needs to be input by the user to obtain efficient sampling from the posterior.

References

  • Neal, R. M. (2011). MCMC using Hamiltonian dynamics. Handbook of Markov Chain Monte Carlo.
  • Hoffman and Gelman (2014). The No-U-Turn sampler: Adaptively setting path lengths in Hamiltonian Monte Carlo. J. Mach. Learn. Res. 15:1593-1623.

See Also

run_mcmc, run_mcmc.nuts, run_mcmc.rwm