Fit a generic model using Hamiltonian Monte Carlo (HMC)
This function runs the HMC algorithm on a generic model provided the logPOSTERIOR
and gradient glogPOSTERIOR
functions. All parameters specified within the list param
are passed to these two functions. The tuning parameters epsilon
and L
are passed to the Leapfrog algorithm.
hmc( N = 10000, theta.init, epsilon = 0.01, L = 10, logPOSTERIOR, glogPOSTERIOR, randlength = FALSE, Mdiag = NULL, constrain = NULL, verbose = FALSE, varnames = NULL, param = list(), chains = 1, parallel = FALSE, ... )
N
: Number of MCMC samplestheta.init
: Vector of initial values for the parametersepsilon
: Step-size parameter for leapfrog
L
: Number of leapfrog
steps parameterlogPOSTERIOR
: Function to calculate and return the log posterior given a vector of values of theta
glogPOSTERIOR
: Function to calculate and return the gradient of the log posterior given a vector of values of theta
randlength
: Logical to determine whether to apply some randomness to the number of leapfrog steps tuning parameter L
Mdiag
: Optional vector of the diagonal of the mass matrix M
. Defaults to unit diagonal.constrain
: Optional vector of which parameters in theta
accept positive values only. Default is that all parameters accept all real numbersverbose
: Logical to determine whether to display the progress of the HMC algorithmvarnames
: Optional vector of theta parameter namesparam
: List of additional parameters for logPOSTERIOR
and glogPOSTERIOR
chains
: Number of MCMC chains to runparallel
: Logical to set whether multiple MCMC chains should be run in parallel...
: Additional parameters for logPOSTERIOR
Object of class hmclearn
hmclearn
objectsN
: Number of MCMC samplestheta
: Nested list of length N
of the sampled values of theta
for each chainthetaCombined
: List of dataframes containing sampled values, one for each chainr
: List of length N
of the sampled momentatheta.all
: Nested list of all parameter values of theta
sampled prior to accept/reject step for eachr.all
: List of all values of the momenta r
sampled prior to accept/rejectaccept
: Number of accepted proposals. The ratio accept
/ N
is the acceptance rateaccept_v
: Vector of length N
indicating which samples were acceptedM
: Mass matrix used in the HMC algorithmalgorithm
: HMC
for Hamiltonian Monte Carlovarnames
: Optional vector of parameter nameschains
: Number of MCMC chainslogPOSTERIOR
and glogPOSTERIOR
functionslinear_posterior
: Linear regression: log posteriorg_linear_posterior
: Linear regression: gradient of the log posteriorlogistic_posterior
: Logistic regression: log posteriorg_logistic_posterior
: Logistic regression: gradient of the log posteriorpoisson_posterior
: Poisson (count) regression: log posteriorg_poisson_posterior
: Poisson (count) regression: gradient of the log posteriorlmm_posterior
: Linear mixed effects model: log posteriorg_lmm_posterior
: Linear mixed effects model: gradient of the log posteriorglmm_bin_posterior
: Logistic mixed effects model: log posteriorg_glmm_bin_posterior
: Logistic mixed effects model: gradient of the log posteriorglmm_poisson_posterior
: Poisson mixed effects model: log posteriorg_glmm_poisson_posterior
: Poisson mixed effects model: gradient of the log posterior# Linear regression example set.seed(521) X <- cbind(1, matrix(rnorm(300), ncol=3)) betavals <- c(0.5, -1, 2, -3) y <- X%*%betavals + rnorm(100, sd=.2) fm1_hmc <- hmc(N = 500, theta.init = c(rep(0, 4), 1), epsilon = 0.01, L = 10, logPOSTERIOR = linear_posterior, glogPOSTERIOR = g_linear_posterior, varnames = c(paste0("beta", 0:3), "log_sigma_sq"), param=list(y=y, X=X), parallel=FALSE, chains=1) summary(fm1_hmc, burnin=100) # poisson regression example set.seed(7363) X <- cbind(1, matrix(rnorm(40), ncol=2)) betavals <- c(0.8, -0.5, 1.1) lmu <- X %*% betavals y <- sapply(exp(lmu), FUN = rpois, n=1) fm2_hmc <- hmc(N = 500, theta.init = rep(0, 3), epsilon = 0.01, L = 10, logPOSTERIOR = poisson_posterior, glogPOSTERIOR = g_poisson_posterior, varnames = paste0("beta", 0:2), param = list(y=y, X=X), parallel=FALSE, chains=1) summary(fm2_hmc, burnin=100)
Neal, Radford. 2011. MCMC Using Hamiltonian Dynamics. In Handbook of Markov Chain Monte Carlo, edited by Steve Brooks, Andrew Gelman, Galin L. Jones, and Xiao-Li Meng, 116–62. Chapman; Hall/CRC.
Betancourt, Michael. 2017. A Conceptual Introduction to Hamiltonian Monte Carlo.
Thomas, S., Tu, W. 2020. Learning Hamiltonian Monte Carlo in R.
Samuel Thomas samthoma@iu.edu , Wanzhu Tu wtu@iu.edu
Useful links