Hamiltonian Monte Carlo (HMC) is an MCMC method that uses Hamiltonian dynamics to efficiently explore the target distribution. It simulates the trajectory of a particle under Hamiltonian mechanics to propose new states that are usually accepted. No-U-Turn Sampler (NUTS) improves on HMC by automatically tuning the integration time.
24. Ž
Ú{cL
Ú( randn)
# p: (unnormalized) probability density function
# θ₀: initial state
# M: number of samples
# ϵ: step size
function metropolis(p::Function, θ₀::Vector{Float64}, M::Int, ϵ::Float64)
d = length(θ₀)
# allocate samples' holder
samples = Array(typeof(θ₀), M)
# set the current state to the initial state
θ = θ₀
for m in 1:M
# generate a candidate sample from
# the proposal distribution (normal distribution)
θ̃ = randn(d) * ϵ + θ
if rand() min(1.0, p(θ̃) / p(θ))
# accept the proposal
θ = θ̃
end
samples[m] = θ
print_sample(θ)
end
samples
end
metropolis.jl
17 / 55
25. cc
2/Dz/DwözN—cL
Ú
# mean
μ = [0.0, 0.0]
# covariance matrix
Σ = [1.0 0.8;
0.8 1.0]
# precision matrix
Λ = inv(Σ)
# unnormalized multivariate normal distribution
normal = x - exp(-0.5 * ((x - μ)' * Λ * (x - μ))[1])
|–x₀|´òÖêDM|¸ÅÂÖüϵž®cr´òÖéò¯
samples = metropolis(normal, x₀, M, ϵ)
18 / 55
31. Hamiltonian Mote Carlo (HMC)
Hamiltonian circuit on a small rhombicosidodecahedron by fdecomite is licensed under CC BY 2.0
24 / 55
32. Hamiltonian Monte Carlo
Hamiltonian Monte Carlo](HMC){|ÎÞêÇò¦»(Hamiltonian
dynamics)žpwæŽa˜kMCMC]z~tp}
±Ã_Dz¶žqJe— (|Œ0v±
Ú{sYvP)
ószŠ¨z唞ðor´òÖꞤ—
)zMCMCz¡ê³é¹ßt‡¶cr|öz vP P´òÖ
êX¤•˜—
_z]žPagkNo-U-Turn Sampler (NUTS){StantPR
Ø£¹4zkzÖì¯èÞò¯{§wöa˜rP—
25 / 55
33. Boltzmann
Ú
Y Y
1 Y
æA z§Ìê– t±
Ú {Õz”Rw×Ô]•
˜—}
;
1 Y
FYQ + Y
;
__s| {±
ÚzcLBDsN—}
_˜žuwžT||±
Úz§Ìê–X4‘sY—}
Y
+ MPH 1 Y
+ MPH ;
26 / 55
34. ÎÞêÇò¦»
S
Š¨z唞æT—} žŠ¨z”ŸØ®Çê| žå”£Ø®Çê
tckÔzŠ¨z唞E—ÎÞêÇò[ ä:
EJ
EU
ESJ
EU
)
SJ
+
)
J
)
S
6
__s|ÎÞêÇÊ¡ò {ÜÅò¶âê§Ìê– t
唧Ìê– z÷tcraa˜—}
, S
)
S
6
47. vhLeapfrog|ŒBvzW
Q
S
)
S
•Ô
Ú žÓ/we—kw{| z9žÓ/w
cv]˜|v•vP
cWc|Euler]vus{(h_zuažÏÁcrŽ)9X/Bc
rcŠRzs| Q
S
XÓ/wv•vP
Leapfrog|ŒBs{|3pzÒä{i˜j˜¹ (shear
mapping)vzs|i˜j˜2JcrŽ9X/BcvP
VerticalShear m=1.25 by RobHar - Own work using Inkscape. Licensed under Public domain via Wikimedia Commons -
http://commons.wikimedia.org/wiki/File:VerticalShear_m%3D1.25.svg#mediaviewer/File:VerticalShear_m%3D1.25.svg
31 / 55
48. HMCw”—´òÖéò¯¡ê³é¹ß
1. |æA žE| we—
2. 唣žcL
ÚvuW•´òÖéò¯e—
3. W•¸ÅÂÖ´£¹ sLeapfrog|ŒBw”—Òž x
–4c| ž¤—
4. ± s*vc|iRsv]
˜|@qe—
5. *va˜kÝ{ tc|@qa˜kÝ{
te—
6. tcr|2~5ž z´òÖêX¤•˜—Šs–
4e
N ±
N
1 -
ú
NJO
FYQ )
S
+ ) ú
S
ú
N
52. # U : potential energy function
# ∇U : gradient of the potential energy function
# θ₀ : initial state
# M : number of samples
# ϵ : step size
# L : number of steps
function hmc(U::Function, ∇U::Function, θ₀::Vector{Float64}, M::Int, ϵ::Float64, L::Int)
d = length(θ₀)
# allocate sampels' holder
samples = Array(typeof(θ₀), M)
# set the current sate to the initail state
θ = θ₀
for m in 1:M
# sample momentum variable
p = randn(d)
H = U(θ) + p ⋅ p / 2
θ̃ = θ
for l in 1:L
p -= ϵ / 2 * ∇U(θ̃) # half step in momentum variable
θ̃ += ϵ * p # full step in location variable
p -= ϵ / 2 * ∇U(θ̃) # half step in momentum variable again
end
H̃ = U(θ̃) + p ⋅ p / 2
if randn() min(1.0, exp(H - H̃))
# accept the proposal
θ = θ̃
end
samples[m] = θ
print_sample(θ)
end
samples
end
hmc.jl
33 / 55
53. HQ created
with Gadfly.jl
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
4
2
0
-2
-4
y
HMC (ϵ = 0.1, L = 10)
- 34 / 55
54. HQ created
with Gadfly.jl
-2 -1 0 1
x
Iteration
500
400
300
200
100
1
1
0
-1
-2
y
HMC (ϵ = 0.01, L = 10)
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
3
2
1
0
-1
-2
-3
y
HMC (ϵ = 0.05, L = 10)
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
4
2
0
-2
-4
y
HMC (ϵ = 0.1, L = 10)
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
4
2
0
-2
-4
y
HMC (ϵ = 0.5, L = 10)
-
35 / 55
55. HQ created
with Gadfly.jl
-0.3 -0.2 -0.1 0.0 0.1
x
Iteration
500
400
300
200
100
1
0.1
0.0
-0.1
-0.2
-0.3
y
HMC (ϵ = 0.01, L = 1)
-1.0 -0.5 0.0 0.5
x
Iteration
500
400
300
200
100
1
0.5
0.0
-0.5
-1.0
-1.5
y
HMC (ϵ = 0.05, L = 1)
-2 -1 0 1
x
Iteration
500
400
300
200
100
1
1
0
-1
-2
y
HMC (ϵ = 0.1, L = 1)
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
3
2
1
0
-1
-2
-3
y
HMC (ϵ = 0.5, L = 1)
-
36 / 55
56. HQ created
with Gadfly.jl
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
3
2
1
0
-1
-2
-3
y
HMC (ϵ = 0.01, L = 50)
-3 -2 -1 0 1 2 3
x
Iteration
500
400
300
200
100
1
4
2
0
-2
-4
y
HMC (ϵ = 0.05, L = 50)
-4 -2 0 2 4
x
Iteration
500
400
300
200
100
1
4
2
0
-2
-4
y
HMC (ϵ = 0.1, L = 50)
-2 -1 0 1 2 3
x
Iteration
500
400
300
200
100
1
3
2
1
0
-1
-2
y
HMC (ϵ = 0.5, L = 50)
-
37 / 55
71. ˆæ
Bishop, C. M. (2006). Pattern recognition and machine learning.
New York: springer. (pJÙ (2012) ´òÖéò¯] о–òéå
tA†»? A, pp.237-273. ½ch)
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, 1351–1381.
MacKay, D. J. C. (2003). Information Theory, Inference, and
Learning Algorithms. Cambridge University Press.
Neal, R. M. (2011). MCMC Using Hamiltonian Dynamics. In
Handbook of Markov Chain Monte Carlo, pp.113-162. Chapman
Hall/CRC.
iJ;. (2008). Ýê²Ô×7áòŪêì] ÒÞ„?
52 / 55