On this page
Suppose a function p(x) tells you how likely each state is relative to the others, but not the total that turns those weights into probabilities. That happens in physics, where a Boltzmann weight lacks its partition function, and in Bayesian statistics, where a posterior's normalising integral is too hard (see our Bayesian vs frequentist statistics coin example). The Metropolis algorithm samples from p without ever needing that total.
What is the Metropolis algorithm?
It began as "a modified Monte Carlo integration over configuration space" for working out equations of state of substances made of interacting molecules, in a 1953 paper by Nicholas Metropolis, Arianna Rosenbluth, Marshall Rosenbluth, Augusta Teller and Edward Teller in The Journal of Chemical Physics. Its results, for a two-dimensional system of rigid spheres, were obtained on the Los Alamos MANIAC computer.
The recipe, for a target p(x) known up to a constant:
Start at any state x.
Propose a new state y with a symmetric rule: the chance of proposing y from x equals the chance of proposing x from y.
Accept with probability min(1, p(y)/p(x)). If accepted, move to y. If not, stay at x and record x again.
Repeat, recording the state after every step.
Uphill moves are always accepted; downhill moves only sometimes, which lets the chain explore instead of sitting on a peak. Only the ratio p(y)/p(x) appears, so any constant multiplying p cancels. For proposals that are not symmetric, the rule gains a correction factor; W. K. Hastings' 1970 paper in Biometrika covers that general version.
How does Metropolis work on a 5-state example?
Take states 1 to 5 with weights 1, 2, 4, 2, 1. Pretend you do not know they sum to 10, which would give the target 0.1, 0.2, 0.4, 0.2, 0.1. The proposal is a fair coin: step left or right. A step off either end has weight 0 and is always rejected.
Work out every move:
From 1 to 2: ratio 2/1, always accepted, so 1/2 (the coin) times 1 = 1/2. Stay at 1 with probability 1/2.
From 2 to 3: ratio 4/2, accepted, so 1/2. From 2 to 1: ratio 1/2, so 1/2 times 1/2 = 1/4. Stay at 2 with probability 1/4.
From 3 to 2 or to 4: ratio 2/4, so 1/4 each. Stay at 3 with probability 1/2.
States 4 and 5 mirror states 2 and 1.
Now check it. Starting from the target, the probability flowing into state 1 in one step is 0.1 times 1/2 plus 0.2 times 1/4 = 0.1, exactly what state 1 already had. Do the same for state 3: 0.2 times 1/2 + 0.4 times 1/2 + 0.2 times 1/2 = 0.4. The target is unchanged by a step, which is what our guide to solving πP = π by hand calls a stationary distribution.
Why does detailed balance make it work?
Detailed balance is a local condition: for every pair of states, π(x)P(x→y) = π(y)P(y→x). The flow from x to y equals the flow back, so summing over x returns π, and π is stationary.
In the example, π(1)P(1→2) = 0.1 times 1/2 = 0.05 and π(2)P(2→1) = 0.2 times 1/4 = 0.05. Next pair: π(2)P(2→3) = 0.2 times 1/2 = 0.1 and π(3)P(3→2) = 0.4 times 1/4 = 0.1.
The general proof is one line. With proposal chance q both ways and p(y) smaller than p(x), p(x) times q times p(y)/p(x) = p(y) times q times 1 = q times min(p(x), p(y)). Dividing by the unknown total changes nothing.
What are burn-in and step size in MCMC?
Start the example chain at state 1. After one step the distribution is (1/2, 1/2, 0, 0, 0). After two it is (3/8, 3/8, 1/4, 0, 0). After three it is (9/32, 11/32, 10/32, 2/32, 0): state 1 still holds 9/32 of the probability against its target of 1/10, and state 5 has not been reached. Early samples remember the start. Discarding them is called burn-in; running chains from different starts until they agree shows roughly how many to discard.
Step size is the other dial. In continuous problems the proposal is usually x plus a random step. Tiny steps are almost always accepted but crawl; huge steps mostly land where p is small and are rejected. For random-walk Metropolis in many dimensions, Roberts, Gelman and Gilks showed in The Annals of Applied Probability that the asymptotically optimal acceptance rate is 0.234 under quite general conditions, as the number of dimensions grows. They proved it for targets built from many independent, identical components. Treat that as a guide for high-dimensional problems, not a law for five states.
A checklist before you trust a run
Your proposal is symmetric, or you used the corrected rule.
A rejected step records the current state again; skipping it is a common bug.
You compare log p values, so tiny ratios do not underflow.
You discarded burn-in and checked that chains from different starts agree.
Your acceptance rate is neither near 0 nor near 1.
If you want a weighted average of rare events rather than samples from a whole distribution, importance sampling is the Monte Carlo tool to compare. If Markov chains or stationary distributions still feel shaky, the Fermi Concept Library gives every concept in mathematics and computing its own lesson and its place in the map.
Try it yourself: run the five-state chain with a coin and a die for 50 steps, tally the visits, and watch state 3 pull ahead.



