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
Stages – Please pick a stage: ↓
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: Here we distinguish two cases.

Case Ξ± β‰₯ 1 (proposal at least as plausible as the current position): the proposal is always accepted. The walker thus always moves uphill (or stays at the same height). No randomness is needed here.

Case Ξ± < 1 (proposal less plausible): now chance decides. The computer draws a random number u between 0 and 1 (all values equally likely) and compares it with Ξ±:
 β€’ u < Ξ± β†’ accept (ΞΈ* becomes the new position)
 β€’ u β‰₯ Ξ± β†’ reject (the walker stays put; the old value is counted once more)
Since u is uniformly distributed, u falls below Ξ± with exactly probability Ξ± β€” that is what "accept with probability Ξ±" means.
Example: If the proposal is half as plausible, Ξ± = 0.5. If the computer draws u = 0.31, then 0.31 < 0.5 β†’ accepted. With u = 0.82, 0.82 β‰₯ 0.5 β†’ rejected. In the long run, every second such proposal is accepted.

You can see exactly this comparison in the inspector: ratio Ξ±, random number u and the outcome (βœ“ accepted / βœ— rejected).

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
Further Reading
Lambert (2018) β€” accessible introduction to MCMC and Bayesian thinking