Juliaで学ぶ Hamiltonian Monte Carlo (NUTS 入り)
Upcoming SlideShare
Loading in...5
×
 

Juliaで学ぶ Hamiltonian Monte Carlo (NUTS 入り)

on

  • 341 views

サンプルコード: https://github.com/bicycle1885/JuliaTokyo2HMC

サンプルコード: https://github.com/bicycle1885/JuliaTokyo2HMC

Statistics

Views

Total Views
341
Views on SlideShare
241
Embed Views
100

Actions

Likes
5
Downloads
3
Comments
0

3 Embeds 100

http://juliatokyo.connpass.com 45
https://twitter.com 41
http://bicycle1885.hatenablog.com 14

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Juliaで学ぶ Hamiltonian Monte Carlo (NUTS 入り) Juliaで学ぶ Hamiltonian Monte Carlo (NUTS 入り) Presentation Transcript

  • Julia Tokyo #2 Julias»‚ Hamiltonian Monte Carlo (NUTSâ–) ,‡ W+ (Kenta Sato) @bicycle1885 1 / 55
  • ²òÅòà ߅Ãu Ýê²Ô×7áòŪêì] (MCMC) Metropolis-Hastings Hamiltonian Monte Carlo (HMC) No-U-Turn Sampler (NUTS) JuliasžT—MCMCа–· 2 / 55
  • _ŸvÑÇzkzP¼se MCMCw”—´òÖéò¯žÕè®Û®¸wck[vP Stanvuz´òÖè–zqvž£fkP JuliasžT—´òÖ薞—–kP 3 / 55
  • ¶ ÔzZÖ|MCMCß9{
  • zw÷˜— _se D»0wrÃvø{>?cvPsAaP Æ?w{Qz¶žorPŠeX|š“’T«PXN— WŽc˜ŠgŸ izÔ{izÝs®,crPkl]—tˆW–Še 4 / 55
  • ߅Ãu / а–·Ãu 5 / 55
  • ߅Ãu ,‡ W+ Twitter/GitHub: @bicycle1885 }: rRR»Ç»ó š: Bioinformatics JuliaÉ: ñz|›xW ¾Yv{§: Julia / Haskell ”[žR{§: Python / R 6 / 55
  • HaskellŽ”™c[V,PcŠe ²Haskellw”— ʀ íÖì¯èÞò¯³z™ëÒ䖞cŠc k} 7 / 55
  • а–·Ãu DocOpt.jl - https://github.com/docopt/DocOpt.jl ×êÖຖ·žÐ–¸c|²ÝòÈè£òÂDzЖ¸že— RandomForests.jl - https://github.com/bicycle1885/RandomForests.jl A†»?¡ê³é¹ßRandom ForestzJuliaö GeneOntology.jl - https://github.com/bicycle1885/GeneOntology.jl f»zGene OntologyzÖê¬ÂǞö®cr— 8 / 55
  • Ýê²Ô×7áòŪêì] (MCMC) 9 / 55
  • MCMC]t{ 1 Y {›ft—˜k|± Ú W•z´òÖêžÝê²Ô×7žJ Pr¤—´òÖéò¯]z~tp} ¤•˜k´òÖêß9{ Úz>?– Œvuö“v–ž4‘e— zwž›˜—} Ýê²Ô×7t{|xWzæAz‹sÕzæAz± ÚXEŠ—± f z_tžPR} 1 9U
  •   Y < 9  Y 2 9U  YU  1 9U
  •   Y < 9U  YU Y Y 5 Y Y _ztY|æA W• „·¤e—±ž t„Y|·¤± t‚} 10 / 55
  • Ýê²Ô×7z. ·¤± 5 Y Y  1 9U
  •   Y < 9U  Y 11 / 55
  • ccPMCMC  Y ö0z± Ú W•´òÖéò¯e—w{|·¤±Xºka v]˜|v•vP[õXN—} ÚzÓ/[: §ê³–È[:  Y  D 5 Y Y  Y EY 1 U Y ³  Y BT U ³ 7 GPSBOZ 1  Y ©´òÖè–X_˜•žºketPR_tzÚvu{à{cvP (sYvP)} 12 / 55
  • ûÕpósz´òÖéò¯{ÕcP Q Y Q Y ±Ã_D iz”cLBÃ_D XN—te—} ûÕpósz´òÖéò¯zÕca "ýP"¥°{|óÖz`[´ówK¯crP— Q Y cWci˜Xu_W{´òÖéò¯¼w{ W•vP 2pzƒ 1. izÝW•ýP[„ýP[„t‹ 2. ýPt_™žfp]k•i_W•9Pt_™„{NŠ–íWvP ➠ MCMC{Šawiz”Rvƒžt— 13 / 55
  • Metropolis­Hastings Night View with Tokyo Tower Special Lightup (Shibakouen, Tokyo, Japan) by t-mizo is licensed under CC BY 2.0 14 / 55
  • Metropolis­Hastings MCMC´òÖéò¯z~tps|Ž ÚtPR´òÖéò¯ckP Út{*z ÚW•°BFž–cc|" P–"v•izFž*v c|iRsv]˜|izÝwtuŠ—} R ú<  °BFžfe—Ž Ú {özvPcL Úvu|´ò Öéò¯ceP Úwe—} ú " ú<  N " ú<  N  NJO  Q ú R  N < ú °BF {’Az± s*va˜—: Q  N R ú<  N Q  Q  __s| {´òÖéò¯ckP Ú z”cLBÃ_D 15 / 55
  • Metropolis­Hastings z¡ê³é¹ß ”cLB± ÚD W•´òÖéò¯e— 1. |>æA žE| we— 2. Ž Ú W•kvF žt— 3. ± s ž´òÖêtcr*vc|iRsv]˜| @qe— 4. *va˜kÝ{ tc|@qa˜kÝ{ te— 5. tcr|2~4ž z´òÖêX¤•˜—Šs– 4e Q    N ±  R ú<  N ú " ú<  N ú  N
  •  ± ú  N
  •  ±  N N ± N
  •  . 16 / 55
  • Ž Ú{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
  • 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
  • HQ ­created with Gadfly.jl -4 -2 0 2 4 x Iteration 500 400 300 200 100 1 4 2 0 -2 -4 y Metropolis (ϵ = 1.0) 19 / 55
  • HQ ­created with Gadfly.jl -3 -2 -1 0 1 x Iteration 500 400 300 200 100 1 0.5 0.0 -0.5 -1.0 -1.5 -2.0 y Metropolis (ϵ = 0.1) -3 -2 -1 0 1 2 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y Metropolis (ϵ = 0.5) -4 -2 0 2 4 x Iteration 500 400 300 200 100 1 4 2 0 -2 -4 y Metropolis (ϵ = 1.0) -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y Metropolis (ϵ = 2.0) 20 / 55
  • Metropolis­Hastings zþUF 1. @qzÇë–È©Ô 1 ¸ÅÂÖ´£¹ z–s|@qt[ÿzÇë–È©ÔXN— 2. èò¿ß¥¨–® ´òÖêzÊXèò¿ß¥¨–®že— 21 / 55
  • þU1: @qzÇë–È©Ô ± Úz–XK¯cr—z{`[´ól]} 1 1 ¸ÅÂÖ´£¹ R ➠ RY[”]—X|@qXÖX— ¸ÅÂÖ´£¹ Ÿ ➠ @q{WT•˜—X|NŠ–”]vP MCMCW•v—…[¬€v´òÖꞤ—w{¸ÅÂÖ´£¹žRY [ckPX|@qXÖX—k´òÖéò¯zµXu[v—Çë –È©ÔXN—} 1 Õp(´òÖéò¯e—/D)w”orÐèà–¾ z P–X£v –|ۏXÕcP} 22 / 55
  • þU2: èò¿ß¥¨–® Metropolis-HastingsW•¤•˜k´òÖêÊ{|èò¿ß¥¨–®ž crP— ú  N Ž ÚXÜe—°BF {|xWz– W•‹r[0 X¤”ck–W•ewûorYrcŠR_tXN—  N èò¿ß¥¨–®s{(VVŠWw{or)`ûxDz[$w‡º ck—|cWvP óž…W•…ŠsU—zwWv–`ûxDX«Pwv— 23 / 55
  • Hamiltonian Mote Carlo (HMC) Hamiltonian circuit on a small rhombicosidodecahedron by fdecomite is licensed under CC BY 2.0 24 / 55
  • 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]žP>agkNo-U-Turn Sampler (NUTS){StantPR Ø£¹4zkzÖì¯èÞò¯{§wöa˜rP— 25 / 55
  • 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
  • ÎÞêÇò¦»  S Š¨z唞æT—} žŠ¨z”ŸØ®Çê| žå”£Ø®Çê tckÔzŠ¨z唞E—ÎÞêÇò[ ä: EJ EU ESJ EU  ) SJ  + ) J )  S 6  __s|ÎÞêÇÊ¡ò {ÜÅò¶âê§Ìꭖ t 唧Ìꭖ z÷tcraa˜—} , S )  S  6 
  • , S 27 / 55
  • ´òÖéò¯„z%J /D ”ŸØ®Çê : ´òÖéò¯ckP±/D 唣خÇê : å”zBˆ0v/D §Ìꭖ ÜÅò¶âê§Ìꭖ : Boltzmann ڞ2w 唧Ìꭖ : 2|v唧Ìꭖw ”ŸØ®Çêt唣خÇêz•Ô Ú { ”–’Az”Rw wsY—}  S 6  , S Q  S )  S  6 
  • , S Q  S  FYQ +)  S   FYQ +6   FYQ +, S  ;  ; 28 / 55
  • HMCz*?± Üa˜k°BFwe—*?±{’Az”Rwv—} " <   NJO ú  FYQ )  S + ) ú S ú  ) )  S + ) ú S ú   "   võ0w{| z–{Ó/vzs ’T«f *va˜— ( ) {flX|²òÓ䖾sD–0wÎÞêÇò[ äž|ŒBcrw[t«f¨1XPfe—kxö0w{@q{ »ìsvP} 29 / 55
  • Leapfrog|ŒB ÎÞêÇò[ ä{w}0ww[z{ÕcPzs|D– žíR} i_s{|Leapfrog|ŒBtPR’AzÒäžpWR} SJ U
  • 1 J U
  • 1 SJ U
  • 1  SJ U + 1  6  U J  J U
  • 1SJ U
  • 1  SJ U
  • 1 + 1  6  U
  • 1 J 30 / 55
  • vhLeapfrog|ŒBvzW Q  S )  S •Ô Ú žÓ/we—kw{| 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
  • HMCw”—´òÖéò¯¡ê³é¹ß 1. |>æA žE| we— 2. 唣žcL ÚvuW•´òÖéò¯e— 3. W•¸ÅÂÖ´£¹ sLeapfrog|ŒBw”—Òž x –4c| ž¤— 4. ± s*vc|iRsv] ˜|@qe— 5. *va˜kÝ{ tc|@qa˜kÝ{ te— 6. tcr|2~5ž z´òÖêX¤•˜—Šs– 4e   N ±   N 1 - ú NJO  FYQ )  S + ) ú S ú   N
  •  ± ú  N
  •  ±  N N ± N
  •  . 32 / 55
  • # 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
  • 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
  • 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
  • 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
  • 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
  • HMCXwEck_t §Ìꭖz¶áQžžR_ts|Iÿwvok_t: 1 ¸ÅÂÖ´£¹ žP Ÿa[t˜|Leapfrogz ¨1XŸa [v–|@qž [WT•˜— Š¨X - ¸ÅÂÖ×cr͕Ww¤”e—k|èò¿ß¥¨ –®t‡…r[Šs”]— @qžWTpp¼z”Ÿ”–[Šs”[_tXsY—”Rwv –|¤•˜—´òÖêX”–¬€v´òÖêwßqPk} 38 / 55
  • HMCzÕca HMCzqF{|唞ۏe—2pzÐèà–¾ ¸ÅÂÖ´£¹ ¸ÅÂÖD 1 - z–Xm“Ru P–wa˜rP—tPR_tw•#crP—} Úzæw”orm“Ru P–X/›—k|_˜•z–žuŸ v ÚwŽRŠ[P[”R6e—z{ÓIÿ} 39 / 55
  • 1 - t zÛ^ž«T—tuRv—W 1 ¸ÅÂÖ´£¹ : XŸaeZ— ➠ Š¨XNŠ–”WvP XRYeZ— ➠ leapfrog|ŒBXìeZr@qXÖX— 1 1 1 - žŸa[e—t žRY[cvPtP]vPk|4‘²¸ÇŽW W—} - ¸ÅÂÖD : XŸaeZ— ➠ èò¿ß¥¨–®žcrcŠR XRYeZ— ➠ Š¨XÂY4e (U¾–ò) - - ➠ ߔ0wÐèà–¾žÛckP 40 / 55
  • No­U­Turn Sampler (NUTS) No Turning Back by Pak Gwei is licensed under CC BY-NC-SA 2.0 41 / 55
  • No­U­Turn Sampler HMC{¸ÅÂÖ´£¹ 1 t¸ÅÂÖD - z2pzÐèà–¾wÑ÷l okX| No-U-Turn Sampler (NUTS)s{_˜•zÐèà–¾(§w )žRŠP_tۏcr[˜—} zۏ ➠ ´òÖéò¯¼zdual averagingw”–?2B zۏ ➠ ´òÖéò¯¯zŠ¨z}cP唞ccrµŠ— - 1 - ÑÇXsÐèà–¾zÀä–Êò¯e—_tv[?2vHMC´òÖ è–t•d[•õz P´òÖêX¤•˜—”RwvorP—} 42 / 55
  • ±¿seXNUTSz¡ê³é¹ßse (Hoffman & Gelman, 2014) 43 / 55
  • NUTSzPF aeXwÀóžÃue—z{rcPzsPFžÃue—t| ´òÖêzP…{|0|vp—sàP[XPP vzs6 - žgf|P…žuŸuŸü|crP[ ü|ceZrå”XU¾–òž¥k•|P…žü|ezžµ— izP…wN—Æ–¾FW•|cP´òÖꞤ— P…W•z´òÖéò¯{|ÍQ–PžVavP”Rwe— ÍQ–P(detailed balance)❏ t{| ÚXÓ/wv—kzP ãL 44 / 55
  • No! U­Turn!!  ú P…zàazÔ/B{|¥F W•xWzF ŠszØ®Çêt 唣خÇê Sú zw‡ºe—: ú 5 E ú ú 5Sú  +  +   +  E EU ú+  5 ú+   EU  _z–X ’Awvok•|P…XU¾–òžc¥k_twv—} 45 / 55
  • àPzsDwv—[{´òÖ겖Èznuts.jlžˆ¹crAaP} # L: logarithm of the joint density θ # ∇L: gradient of L # θ₀: initial state # M: number of samples # ϵ: step size function nuts(L::Function, ∇L::Function, θ₀::Vector{Float64}, M::Int, ϵ::Float64) d = length(θ₀) samples = Array(typeof(θ₀), M) θ = θ₀ for m in 1:M r₀ = randn(d) u = rand() * exp(L(θ) - r₀ ⋅ r₀ / 2) θ⁻ = θ⁺ = θ r⁻ = r⁺ = r₀ C = Set([(θ, r₀)]) j = 0 s = 1 while s == 1 v = randbool() ? -1 : 1 if v == -1 θ⁻, r⁻, _, _, C′, s′ = build_tree(L, ∇L, θ⁻, r⁻, u, v, j, ϵ) else _, _, θ⁺, r⁺, C′, s′ = build_tree(L, ∇L, θ⁺, r⁺, u, v, j, ϵ) end if s′ == 1 C = C ∪ C′ end s = s′ * ((θ⁺ - θ⁻) ⋅ r⁻ ≥ 0) * ((θ⁺ - θ⁻) ⋅ r⁺ ≥ 0) j += 1 end θ, _ = rand(C) samples[m] = θ print_sample(θ) end samples end nuts.jl 46 / 55
  • HQ ­created with Gadfly.jl -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y NUTS (ϵ = 0.1) 47 / 55
  • HQ ­created with Gadfly.jl -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 4 2 0 -2 -4 y NUTS (ϵ = 0.01) -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y NUTS (ϵ = 0.05) -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y NUTS (ϵ = 0.1) -3 -2 -1 0 1 2 3 x Iteration 500 400 300 200 100 1 3 2 1 0 -1 -2 -3 y NUTS (ϵ = 0.5) 48 / 55
  • JuliasžT— MCMCа–· 49 / 55
  • JuliasžT—MCMCа–· MCMC.jl - https://github.com/JuliaStats/MCMC.jl ´òÖè–zÆÐ–Ç (12$·!) ȬäàòÇXvP Stan.jl - https://github.com/goedman/Stan.jl StanÏ£òÆ¢ò¯ (via CmdStan) izRmMCMC.jlw–Š˜—o‰P? Mamba.jl - https://github.com/brian-j-smith/Mamba.jl Wv–˜Do‰PJuliakzöJ0vMCMCÔë–ß OözȬäàòÇ 50 / 55
  • Št HMC{Š¨z唞ð…cr´òÖéò¯e—_tw”–|@q žA^•˜— NUTS{HMCzÕcPÐèà–¾Ûž|ߔBcr[˜— Mamba.jlXJuliazöJ0v´òÖè–z¶ö×W 51 / 55
  • ˆæ 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
  • VŠ] 53 / 55
  • Juliaz¼–¸²–ÈtͲ–Èz£àv·Í function build_tree(L::Function, ∇L::Function, θ::Vector{Float64}, r::Vector{Float64}, u::Float64, v::Int if j == 0 θ′, r′ = leapfrog(∇L, θ, r, v * ϵ) C′ = u ≤ exp(L(θ′) - r′ ⋅ r′ / 2) ? Set([(θ′, r′)]) : Set([]) s′ = int(L(θ′) - r′ ⋅ r′ / 2 > log(u) - Δmax) return θ′, r′, θ′, r′, C′, s′ else θ⁻, r⁻, θ⁺, r⁺, C′, s′ = build_tree(L, ∇L, θ, r, u, v, j - 1, ϵ) if v == -1 θ⁻, r⁻, _, _, C″, s″ = build_tree(L, ∇L, θ⁻, r⁻, u, v, j - 1, ϵ) else _, _, θ⁺, r⁺, C″, s″ = build_tree(L, ∇L, θ⁺, r⁺, u, v, j - 1, ϵ) end s′ = s′ * s″ * ((θ⁺ - θ⁻) ⋅ r⁻ ≥ 0) * ((θ⁺ - θ⁻) ⋅ r⁺ ≥ 0) C′ = C′ ∪ C″ return θ⁻, r⁻, θ⁺, r⁺, C′, s′ end end nuts.jl 54 / 55
  • ÎÞêÇÊ¡òzÓ/[zËÚ E) EU ) EYJ ) EQJ 
  • YJ EU QJ EU #J ) ) ) ) #J  +   QJ YJ QJ YJ 55 / 55