How do I implement the total/pure HMC method in Stan?

hmc_fit = rstan::sampling(mystanfile,data = data,
iter = 20,
algorithm = “HMC”,
chain = 1,
control =list (stepsize = 0.001))

So I want to use a total/pure hmc method in Stan. The stan guide (14.2 HMC Algorithm Parameters | Stan Reference Manual) says there are three parameters discretization time ϵ, mass matrix Σ, and
number of steps taken L.

I try to set control =list (stepsize = 0.001), which I think is ϵ. But still, the move seems too large. I want to set a small ϵ and L to see how my model goes every small step, which means that I don’t want my parameters to be tuned based on the acceptance.

I have read many online materials that I can find, but still cannot figure out how to implement a pure hmc in stan…

If I understand the question correctly, you need engine=static. See here:

Thank you so much for your help!
Yes, I reviewed that before. It might help if I set engine=static. But this is cmdstan guide, I cannot find how to set it in rstan::sampling()…

1 Like

Sorry–you’re right. It’s been a while since I used rstan! Setting algorithm = 'HMC' as you’ve done should correspond to engine=static. Not sure what else you might need to do. Can you give a more specific example of what is going wrong and how you know that it’s gone wrong?

Never mind… I found that I also need to set int_time to make it small…

hmc_fit = rstan::sampling(mystanfile,data = data,
iter = 20,
algorithm = “HMC”,
chain = 1,
control =list (stepsize = 0.001, int_time = 0.001))

It works for me! Still thank you for your help!

Keep in mind that the static Hamiltonian Monte Carlo sampler in Stan is not the “Hybrid Monte Carlo” sampler that is often presented as “Hamiltonian Monte Carlo”. In particular it doesn’t propose just the final state and then accept/reject based on the Hamiltonian value at that final state but rather generates an entire numerical trajectory of L = t_{\text{integration}} / \epsilon around the initial state and then samples a final state from that trajectory based on the relative Hamiltonian values. See for example Section A.2 and A.3 of https://arxiv.org/pdf/1701.02434.pdf.

Also the step size, and hence the induced number of leapfrog steps, will be adapted unless adaptation is explicitly turned off with control=list(adapt_engaged=F). Otherwise stepsize is just the step size that initializes the adaption.

1 Like