Hamiltonian Monte Carlo · 1-D Gaussian posterior

Exact orbits vs the leapfrog integrator

The target is q ~ N(μ, σ²). Adding Gaussian momentum turns Hamiltonian dynamics into a harmonic oscillator, so the trajectory has a closed form — a pure rotation in phase space that conserves energy exactly. Leapfrog approximates that same orbit but rides a nearby shadow energy level, and the resulting wobble in H is exactly what the Metropolis step corrects. Trace a single proposal, then let the chain run.

integrator
stable · εω = 0.30
Phase space (q, p) 1.0×
exact orbit leapfrog steps start proposal chain step · p resampled
Energy along the trajectory H vs time (step × ε)
ω = 1/(σ√m)1.000
period 2π/ω6.283
path length L·ε2.40
↳ in periods0.38
H₀ (start)
max |ΔH| leapfrog
accept prob this draw
decision
Samples → target density no samples yet
sampler histogram true N(μ, σ²)
Trace of q last 500 iterations
draws0
acceptance rate
empirical mean
empirical std

What the simulation integrates

potential & Hamiltonian U(q) = (q−μ)² / 2σ²
K(p) = p² / 2m
H = U(q) + K(p)
exact orbit — harmonic oscillator x = q − μ,  ω = 1/(σ√m)
x(t) = x₀cos ωt + (p₀/mω) sin ωt
p(t) = −mω x₀ sin ωt + p₀ cos ωt
leapfrog step (size ε) p ← p − (ε/2)·U′(q)
q ← q + ε·p/m
p ← p − (ε/2)·U′(q)
one HMC iteration Draw p₀ ~ N(0, m), simulate to a proposal (q*, p*), then accept with probability min(1, e^(H₀ − H*)). Under exact dynamics H* = H₀, so every proposal is accepted; leapfrog's energy error makes the Metropolis step do real work — yet both leave the target N(μ, σ²) invariant. Push ε past 2σ√m and leapfrog turns unstable: the orbit spirals outward and acceptance collapses.