Algorithm 2.
Hamiltonian Monte Carlo
| 1: | |
| procedure HMC(θ(0), log f(θ), M, N, ϵ, L) | |
| 2: | |
| Calculate log f(θ(0)) | |
| 3: | |
| for t = 1,..., N do | |
| 4: | |
| p ← N(0, M) | |
| 5: | |
| θ(t) ← θ(t−1), θ ← θ(t−1), p ← p | |
| 6: | |
| for i = 1,..., L do | |
| 7: | |
| θ, p ← Leapfrog(θ, p, ϵ, M) | |
| 8: | |
| end for | |
| 9: | |
| 10: | |
| With probability α, θ(t) ← θ and p(t) ← −p | |
| 11: | |
| end for | |
| 12: | |
| return θ(1),..., θ(N) | |
| 13: | |
| function Leapfrog(θ*, p*, ϵ, M) | |
| 14: | |
| p ← p* + (ϵ / 2)∇θ log f(θ*) | |
| 15: | |
| θ ← θ* + ϵM−1p | |
| 16: | |
| p ← p + (ϵ / 2)∇θ log f(θ) | |
| 17: | |
| return θ, p | |
| 18: | |
| end function | |
| 19: | |
| end procedure |