Metropolis Algorithm

The Metropolis algorithm generates a Markov chain targeting a density . At each iteration, propose a new value using a symmetric proposal, then accept it or repeat the current value.

For a normal random walk with stepsize , the proposal is symmetric: . The proposal densities cancel in the Metropolis–Hastings ratio, leaving the acceptance probability

Starting from , each iteration is

Adjust the current value and the proposed value using the number inputs or sliders. The proposal is , centered at the current value. On the x-axis, the orange dot marks the current value and the green dot marks the proposed value. Dashed guides connect this point to each density and show their heights on the y-axis.

The proposal is accepted with probability %.

Trace plot and histogram

Move iterations to reveal the chain. Change stepsize to generate a new chain with a different proposal standard deviation. The left panel shows the trace in orange and rejected proposals as small light-gray dots. At a rejection, the chain stays at its previous value; the gray dot shows the value it tried instead. Hover over a gray dot for details. Proposals outside the shared vertical range are clipped. The right panel compares the chain's histogram (orange, after 20 iterations) with the target density (blue) and the proposal density centered at the current value (green). Both panels share the same vertical scale.