The interactive film is in the book's Companion

Play it, pause it, turn it and drag it with Mathematics for AI and Machine Learning.

Get the book

Metropolis-Hastings: A Random Walk That Samples

Chapter 18 · Mathematics for AI and Machine Learning · Beginner · 1:24

An interactive live film: the book's two-peaked example sampled by Metropolis-Hastings; proposals are accepted with probability min(1, p(x′)/p(xₜ)), the chain runs into the picture, and its histogram grows into the density. The proposal width decides whether it crawls, mixes or stalls.

Narration

We want samples from this density with two peaks, the blue curve, at minus two and at two. We can evaluate it, but we cannot sample from it directly.

Metropolis Hastings takes a walk. From the current point it proposes a random step, here with a spread sigma of one half.

The rest of the narration is in the book's Companion.

More from Chapter 18

Interactive1:22Langevin Sampling: Drift Uphill, Kick at RandomChapter 18Preview1:42MALA: Removing Discretization Bias with Accept-RejectChapter 18Preview1:40Underdamped Langevin and HMC: Momentum-Driven ExplorationChapter 18Preview1:29Langevin Dynamics and SGLD: Sampling vs OptimizationChapter 18Preview1:46Speculative Decoding: Exact Sampling With a Draft ModelChapter 18