Bayesian inference · Markov chain Monte Carlo

How MCMC Works

You have data, and a question with no single right answer, only more and less plausible ones. Somewhere in a series the level changed; how steep a trend really is; whether a coin is loaded. MCMC sends a walker through the space of guesses, and where it spends its time is the answer. Press Next and the demo explains itself one beat at a time, or skip the story and drive.

What you are looking at

Three rules

Propose, take it uphill, gamble downhill

From where it stands the walker proposes a nearby guess. If the new guess scores higher it always moves; if it scores lower it moves with probability equal to the ratio of the two scores. That downhill gamble is what makes it explore the whole map rather than climb the nearest hill and stop there.

Footprints are the answer

The histogram is the posterior

Nobody ever computes the whole map. The walker only feels the ground under its feet, yet the record of where it has been settles into the distribution of plausible answers. Press Reveal to overlay the answer computed the slow way, by scoring every possibility, and watch the two agree.

In your browser

Nothing is running off screen

Every proposal is scored against the real data, in JavaScript, on this page. The change-point series is generated from a seed, so Reset replays the same walk on the same data, and New data hands the walker a series nobody has looked at, with a change point nobody has told it about.

The maths, briefly

Three lines carry the whole method. The demo shows all three happening: the score meter over the data is the first, the decision card beside the map is the second, and the readouts under it are the third.

The score of a guess

p(θ | data)  ∝  p(data | θ) · p(θ)

Bayes' rule: how plausible a guess θ is, given the data, is how well it explains the data times how plausible it was before we looked. The constant that would make this a proper probability is never needed, which is what makes the whole method possible: the walker only ever compares two guesses, and the constant cancels.

The walker's rule

θ' = θ + ε,  ε ~ Normal(0, s²)
move with probability
    min(1, p(θ' | data) / p(θ | data))

Propose a nudge to every dial, then compare. Uphill the ratio is at least one, so the walker always moves. Downhill it moves sometimes, and less often the lower the ground. The step size s is the slider in the demo: too small and it crawls, too big and nearly every proposal is rejected. This is the Metropolis algorithm, from 1953.

The answer

E[ f(θ) ]  ≈  (1 / N) · Σ f(θᵢ)

Any question about θ is answered by averaging over the footprints. The most likely change point is where the histogram peaks. A 95% interval is where 95% of the footprints landed. The chance that version B beats version A is the fraction of footprints on B's side of the line. No calculus, just counting.

Where this gets used

The four scenarios in the demo are the same walker pointed at four different questions. The first is the one that led to it being built.

Change points, ten thousand at a time

This demo began as a job. Ten thousand line charts, each of which may have shifted its level and its spread at some point in its history, and nobody to look at them one by one. One walker with the same three rules ran over every chart, and each came back with its own most likely change point, its own levels before and after, and its own honest interval. Tight intervals were real regime changes; wide ones were the ambiguous charts, which is exactly the list a human should be handed.

Trends with their uncertainty

A trend line has two dials, and they are tied together: a steeper slope only fits if the starting level drops. The walker's footprints in the slope-and-level plane form a tilted ellipse, which is the honest answer to how steep the trend is. Not one number, but a shape, and a fan of plausible lines drawn over the data.

Experiments that give a probability

An A/B test rarely ends with a clean winner. The walker turns two conversion counts into the chance that B is really better and the plausible size of its lift, which is the sentence a decision actually needs. Run the same walk on a bigger test and the cloud shrinks.

Anything with hidden dials

Reserving for claims that have not arrived yet, estimating a failure rate from a handful of failures, calibrating a simulation to the few measurements that exist, pricing a risk from sparse history. The dials change; the walker does not. It scales to hundreds of dials, where a grid would need more cells than there are atoms.

A demonstration of the method, not a product. The same approach, a walker that turns data and a model into the full distribution of plausible answers with its uncertainty attached, is what we apply to change detection, reserving and calibration work for clients, on their data.

Talk to us about your own problem →