Hamiltonian Monte Carlo Dr. Jarad Niemi Iowa State University September 12, 2017 Adapted from Radford Neal’s MCMC Using Hamltonian Dynamics in Handbook of Markov Chain Monte Carlo (2011). Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 1 / 29
Hamiltonian dynamics Hamiltonian system Considering a body in a frictionless 1-dimensional environment, let m be its mass, θ be its position, and ω be its momentum. The mass has potential energy U ( θ ) (which is proportional to its height) and kinetic energy K ( ω ) = ω 2 / (2 m ). Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 2 / 29
Hamiltonian dynamics Hamilton’s equations Hamilton’s equations Extending this to d dimensions, we have position vector θ and momentum vector ω . The Hamiltonian H ( θ, ω ) describes the time evolution of the system through d θ i ∂ H = dt ∂ω i d ω i = − ∂ H ∂θ i dt for i = 1 , . . . , d . Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 3 / 29
Hamiltonian dynamics Hamilton’s equations Potential and kinetic energy For Hamiltonian Monte Carlo, we usually use Hamiltonian functions that can be written as follows: H ( θ, ω ) = U ( θ ) + K ( ω ) where U ( θ ) is called the potential energy and will be defined to be minus the log probability density of the distribution for θ (plus any constant that is convenient) and K ( ω ) is called the kinetic energy and is usually defined as K ( ω ) = ω ⊤ M − 1 ω/ 2 where M is a symmetric, positive-definite “mass matrix”, which is typically diagonal, and is often a scalar multiple of the identity matrix. This form for K ( ω ) corresponds to minus the log probability density (plus a constant) of the zero-mean Gaussian distribution with covariance matrix M . The resulting Hamilton’s equations are d θ i d ω i dt = − ∂ U dt = [ M − 1 ω ] i , . ∂θ i Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 4 / 29
Hamiltonian dynamics One-dimensional example One-dimensional example Suppose U ( θ ) = θ 2 / 2 , K ( ω ) = ω 2 / 2 H ( θ, ω ) = U ( θ ) + K ( ω ) , The dynamics resulting from this Hamiltonian are d θ d ω dt = ω, dt = − θ. Solutions of the form θ ( t ) = r cos ( a + t ) , ω ( t ) = − r sin ( a + t ) for some constants r and a . Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 5 / 29
Hamiltonian dynamics One-dimensional example One-dimensional example simulation 0.5 0.5 0.4 0.4 Potential Energy Kinetic Energy 0.3 0.3 0.2 0.2 0.1 0.1 0.0 0.0 −1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 Position Momentum Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 6 / 29
Hamiltonian dynamics Reversibility Hamiltonian dynamics is reversible, i.e. the mapping T s from the state at time t , ( θ ( t ) , omega ( t )), to the state at time t + s , ( θ ( t + s ) , p ( t + s )), is one-to-one, and hence as an inverse, T − s . Under our usual assumptions for HMC, the inverse mapping can be obtained by negative ω , applying T s , and then negating ω again. The reversibility of Hamiltonian dynamics is important for showing convergence of HMC. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 7 / 29
Hamiltonian dynamics Reversibility Conservation of the Hamiltonian The dynamics conserve the Hamiltonian since � � = � d d θ i ∂ H ∂θ i + d ω i ∂ H dH i =1 ∂ω i dt dt dt � � = � d ∂ H ∂ H ∂θ i − ∂ H ∂ H i =1 ∂ω i ∂θ i ∂ω i If h is conserved, then the acceptance probability based on Hamiltonian dynamics is 1. In practice, we can oly make H approximately invariant. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 8 / 29
Hamiltonian dynamics Reversibility Conservation of the Hamiltonian 1.0 0.5 Momentum 0.0 −0.5 −1.0 −1.0 −0.5 0.0 0.5 1.0 Location Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 9 / 29
Hamiltonian dynamics Reversibility Volume preservation If we apply the mapping T s to point in some region R of ( θ, ω ) space with volume V , the image of R under T s will also have volume V . This feature simplifies calculation of the acceptance probability for Metropolis updates. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 10 / 29
Hamiltonian dynamics Euler’s method Euler’s method For simplicity, assume d ω 2 � i H ( θ, ω ) = U ( θ ) + K ( ω ) , K ( ω ) = . 2 m i i =1 One way to simulate Hamiltonian dynamics is to discretize time into increments of e , i.e. = ω i ( t ) + e d ω i = ω i ( t ) − e ∂ U ω i ( t + e ) dt ( t ) ∂θ i ( θ ( t )) = θ i ( t ) + e d θ i = θ i ( t ) + e ω i ( t ) θ i ( t + e ) dt ( t ) m i Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 11 / 29
Hamiltonian dynamics Euler’s method Leapfrog method An improved approach is the leapfrog method which has the following updates: = ω i ( t ) − ( e / 2) ∂ U ω i ( t + e / 2) ∂θ i ( θ ( t )) = θ i ( t ) + e ω i ( t + e / 2) θ i ( t + e ) m i = ω i ( t + e / 2) − ( e / 2) ∂ U ω i ( t + e ) ∂θ i ( θ ( t + e )) The leapfrog method is reversible and preserves volume exactly. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 12 / 29
Hamiltonian dynamics Euler’s method Leap-frog simulator leap_frog = function(U, grad_U, e, L, theta, omega) { omega = omega - e/2 * grad_U(theta) for (l in 1:L) { theta = theta + e * omega if (l<L) omega = omega - e * grad_U(theta) } omega = omega - e/2 * grad_U(theta) return(list(theta=theta,omega=omega)) } Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 13 / 29
Hamiltonian dynamics Euler’s method Leap-frog simulator Position Momentum 4 4 Potential Energy 3 Kinetic Energy 3 2 2 1 1 0 0 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 x x Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 14 / 29
Hamiltonian dynamics Euler’s method Conservation of the Hamiltonian 2 1 Momentum 0 −1 −2 −2 −1 0 1 2 Location Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 15 / 29
Hamiltonian Monte Carlo Probability distributions The Hamiltonian is an energy function for the joint state of “position”, θ , and “momentum”, ω , and so defines a joint distribution for them, via P ( θ, ω ) = 1 Z exp ( − H ( θ, ω )) where Z is the normalizing constant. If H ( θ, ω ) = U ( θ ) + K ( ω ), the joint density is P ( θ, ω ) = 1 Z exp ( − U ( θ )) exp ( − K ( ω )) . If we are interested in a posterior distribution, we set U ( θ ) = − log [ p ( y | θ ) p ( θ )] . Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 16 / 29
Hamiltonian Monte Carlo Hamiltonian Monte Carlo algorithm Set tuning parameters L : the number of steps e : stepsize D = { d i } : covariance matrix for ω Let θ ( i ) be the current value of the parameter θ . The leap-frog Hamiltonian Monte Carlo algorithm is 1. Sample ω ∼ N d (0 , D ). 2. Simulate Hamiltonian dynamics on location θ ( i ) and momentum ω via the leapfrog method (or any reversible method that preserves volume) for L steps with stepsize e . Call these updated values θ ∗ and − ω ∗ . 3. Set θ ( i +1) = θ ∗ with probability min { 1 , ρ ( θ ( i ) , θ ∗ ) } where ρ ( θ ( i ) , θ ∗ ) = p ( θ ∗ | y ) p ( ω ∗ ) p ( y | θ ∗ ) p ( θ ∗ ) N d ( ω ∗ ; 0 , D ) p ( ω ( i ) ) = p ( θ ( i ) | y ) p ( y | θ ( i ) ) p ( θ ( i ) ) N d ( ω ( i ) ; 0 , D ) otherwise set θ ( i +1) = θ ( i ) . Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 17 / 29
Hamiltonian Monte Carlo Reversibility Reversibility for the leapfrog means that if you simulate from ( θ, ω ) to ( θ ∗ , ω ∗ ) for some step size e and number of steps L then if you simulate from ( θ ∗ , ω ∗ ) for the same e and L , you will end up at ( θ, ω ). If we use θ to denote our simulation “density”, then reversibility means θ ( θ ∗ , ω ∗ | θ, ω ) = θ ( θ, ω | θ ∗ , ω ∗ ) and thus in the Metropolis-Hastings calculation, the proposal is symmetric. In order to ensure reversibility of our proposal, we need to negate momentum after we complete the leap-frog simulation. So long as p ( ω ) = p ( − ω ), which is true for a multivariate normal centered at 0, this will not affect our acceptance probability. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 18 / 29
Hamiltonian Monte Carlo Conservation of Hamiltonian results in perfect acceptance The Hamiltonian is conserved if H ( θ, ω ) = H ( θ ∗ , ω ∗ ) which implies p ( θ ∗ | y ) p ( ω ∗ ) = exp ( − H ( θ ∗ , ω ∗ )) = exp ( − H ( θ, ω )) = p ( θ | y ) p ( ω ) and thus the Metropolis-Hastings acceptance probability is p ( θ ∗ | y ) p ( ω ∗ ) ρ ( θ ( i ) , θ ∗ ) = p ( θ ( i ) | y ) p ( ω ( i ) ) = 1 . This will only be the case if the simulation is perfect! But we have discretization error. The acceptance probability accounts for this error. Jarad Niemi (Iowa State) Hamiltonian Monte Carlo September 12, 2017 19 / 29
Recommend
More recommend