HomeAI & Machine LearningHamiltonian Monte Carlo: Phase-Space Leapfrog (2D)

Hamiltonian Monte Carlo: Phase-Space Leapfrog (2D)

A 2D Hamiltonian Monte Carlo sampler: watch the leapfrog integrator trace real (theta, momentum) phase-space orbits and a live energy trace alongside the posterior contour map, with accept/reject decisions and acceptance-rate tracking.

AI & Machine Learning2DAdvanced60 FPS📱 Mobile-adapted⇄ 3D version
2d-bayesian-inference-mcmc-sampling ↗ Open standalone

Fitting a Bayesian model means characterising a posterior distribution that almost never has a closed form — so instead you sample from it. This 2D simulator runs the same Hamiltonian Monte Carlo algorithm as Stan or PyMC — sample a random momentum, integrate a physical trajectory with the leapfrog method, accept or reject by how well total energy was conserved — but renders it as two linked 2D views instead of a rotating 3D terrain: a contour map of the posterior density with the accepted-sample trail, and a genuine phase-space portrait of (θ₁, momentum r₁) with a live energy trace beneath it, so you can watch the leapfrog integrator's near-conservation of H directly instead of inferring it from a rolling ball. Tune the step size and trajectory length to see acceptance rate trade off against exploration, and switch to a bimodal target to see where even a gradient-aware sampler can get stuck in one mode.

⚙ Under the hood

A 2D Hamiltonian Monte Carlo sampler: watch the leapfrog integrator trace real (theta, momentum) phase-space orbits and a live energy trace alongside the posterior contour map, with accept/reject decisions and acceptance-rate tracking.

bayesian inferencemcmchamiltonian monte carloleapfrog integrationphase spaceposterior samplingmachine learning

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)