MCMC Visualizer
Metropolis-Hastings · 3 Stages · Trace Plots · 2D Posterior · Convergence
© Dr. Rainer Düsing · Interactive Tools by Claude
How does the computer find the Posterior?  ·  MCMC samples rather than fully computing — the walker in the fog
Start
Choose a stage above and click ▶ Start or → One Step.
Last Step
Current position
Proposal
P(Proposal)
P(Current)
Ratio α
Random number u
No step yet.
Acceptance rate
0 % Good: 20–50 %
Adjust step size to optimise exploration.
Legend
Warmup (discarded)
Sampling (recorded)
Proposal accepted
Proposal rejected
- - Proposal distribution N(θ, s)
HistogramApproximation of Posterior
Trace — θ over stepsGood: «caterpillar»
Step size s 0.80
Warmup 150
Speed 30/s
Steps: 0
Phase:
Position:
Samples: 0
Acceptance rate:
Step size s — what really happens
The step size s determines how far the walker can jump at each step.

s very small: Almost every proposal lands close to the current position — almost always accepted. But: the walker only shuffles, barely explores. Even at 90% acceptance the exploration can be poor.

s very large: Proposals land far away, often in very flat regions — frequently rejected. The walker often stands still.

The optimal step size is the one that yields good exploration with a reasonable acceptance rate. The rule of thumb is 20–50 % — but what matters is the trace plot, not the rate alone. brms adjusts s automatically during warmup (adaptive sampling).
Warmup, Trace Plot & Convergence
Warmup (burn-in): The chain starts somewhere and needs time to find the high-probability region. These first steps are discarded — they do not yet represent the posterior.

Reading the trace plot: A converged chain looks like a «fuzzy caterpillar» — randomly jumping around a stable level. A visible upward trend or long plateaus → warmup not long enough or s not optimal.

R̂ (Gelman-Rubin): In brms multiple chains run in parallel. When all explore the same distribution they converge. R̂ < 1.01 = good. Divergences → often a sign of poorly chosen priors or difficult posterior geometry — increasing adapt_delta usually helps.
▸ Metropolis-Hastings — the mathematics
At each step three rules apply:

1. Proposal: New value θ* = θ + ε, ε ~ Normal(0, s).

2. Ratio: α = P(θ*|data) / P(θ|data). How much more plausible is the proposal?

3. Decision: α ≥ 1 → always accept. α < 1 → accept with probability α.

The trick: Only ratios are needed — the intractable normalising constant cancels out.

Why does this converge? The transition rule satisfies detailed balance: after infinitely many steps the visit frequency exactly matches the posterior distribution. Mathematically provable.
ℹ MCMC Visualizer — Help
What will I learn here?
Bayesian inference requires the posterior P(θ|data) — a distribution over all plausible parameter values. Because the denominator of Bayes' theorem involves an intractable integral, the posterior is sampled via MCMC rather than computed directly. This tool shows step by step how the Metropolis-Hastings algorithm works.
Recommended exploration
Key displays
Main plot: Posterior landscape + walker's path

Inspector: Last step in detail — ratio α = P(proposal)/P(current); if α ≥ u → accept

Acceptance rate: Green zone (20–50 %) = optimal step size

Histogram + Trace: Growing posterior approximation and trajectory over all steps — a «caterpillar» trace signals good convergence

Narrative banner: Explains each step in plain language — ideal for step-by-step learning