Bayesian Monte Carlo and MCMCLesson 71 of 76 · 10 min

Build a Metropolis chain

Implement symmetric-proposal Metropolis while retaining rejected states.

A Markov chain generates its next state from its current state. Metropolis proposes a move, then accepts or keeps the current value. A symmetric proposal has equal forward and reverse move densities: normal steps centred on the current value have this property. Its acceptance chance is the candidate target density divided by the current target density, capped at one.

Stationarity means a whole target distribution is unchanged by one transition when the current state already has that distribution. A valid acceptance rule preserves it; this does not prove approach to it from an arbitrary starting value, called convergence. Use log densities to avoid tiny ratios. Reject out-of-domain proposals; clipping them would change the proposal rule.

Keep rejected states as repeated values: they are part of the chain. Warm-up is an initial segment set aside to reduce dependence on the chosen start; some algorithms also tune during it. Here its fixed length is a teaching convention, not proof of convergence. Compare the retained mean with the known beta reference and inspect movement, not acceptance fraction alone.

Current x and proposed y are candidate values of the unknown probability p. The target is beta(20,14); the normal proposal supplies a step from x, not a new independent posterior draw.

Rejection still produces a recorded state
019.839.559.27900.250.50.751Parameter pIteration, warm-up included
019.839.559.27900.250.50.751Parameter pIteration, warm-up included
Reference illustration. First eighty iterations of the actual seeded Metropolis reference, including warm-up. Flat segments retain rejected states; removing them would change the chain. A trace alone cannot certify convergence.

Should a rejected proposal disappear from the recorded chain?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Acceptance fraction: 0.609
Kept chain mean: 0.5887 exact: 0.5882352941176471
Repeated-state fraction: 0.393
Plot generated from the published Python during the build. Refer to its labelled axes and the text output.
Open full-size figure ↗

Read the first result

The trace is a diagnostic view, not a convergence certificate. Repeated states are an essential part of the sampler.

Try an edit

In the proposal line, replace rng.normal(0,.12) with rng.normal(0,.005) and run. Then use rng.normal(0,.8). Select Run Python again. Compare Acceptance fraction, Repeated-state fraction and the trace movement across the original and both edits. Are high acceptance and good movement the same thing?

Back to the Python editor

Scope. One chain and one trace cannot rule out nonconvergence. This unimodal benchmark is intentionally easier than many posterior targets.

Try a fresh case

A tiny proposal has a high acceptance fraction but hardly moves. What does this suggest?

Choose an answer
Need a hint?
Hint 1

Look at distance moved per iteration.

Hint 2

A long sequence of nearby states may provide little new information.

Your notes