Metropolis step by step
Watch a Metropolis chain draw a posterior that has no closed form: it proposes a nearby value, compares two heights and accepts or repeats. With the trace, the warm-up, the acceptance rate and the effective size, to see what happens when the step is too short or too long. It works in your browser, on your phone too.
How to use it
- Start with «One step, slowly»: the chain is at θ = 0.45, proposes a nearby value and decides. Press «Next step» and watch one proposal being accepted, another one rejected and what the chain writes down each time.
- Carry on with «The arrival»: the chain starts far away, at θ = 2, and comes down to the curve. That first stretch of the trace is the warm-up, the part you throw away.
- In «Step too short» and «Step too long» the same chain moves with steps of 0.01 and 3: one accepts nearly everything and goes nowhere, the other rejects nearly everything and stays put. The step in between, 0.25, is the one that works.
- Then touch the controls: choose the step size (or drag the slider) and where the chain starts, and advance with «1 step», «+10» or «+500». «Start over» clears the chain.
- Press «Explain it slowly» at any time to see the latest iteration in parts: the proposal, the ratio r, the number u and the decision.
What it draws
At the top, the coral curve is the exact posterior and the violet bars are the histogram of what the chain has so far: with few iterations they look nothing alike and with many they match. The dot is the current value; the green arc is an accepted proposal and the dashed coral arc a rejected one. Below, the trace of the last 400 iterations: every flat step is a rejection. Next to it go the acceptance rate, the mean of the chain beside the exact one (0.467) and the effective size. The strip at the top shows the R line for each step, such as proposal <- current + rnorm(1, 0, 0.25) or the comparison log(runif(1)) < logpost(proposal) - logpost(current) with its TRUE or FALSE.
The criteria it follows
- The model is the one in the theory of the topic: 12 load times adding up to 27 seconds, an exponential likelihood with rate θ and a lognormal prior (log 0.5, 0.4). That posterior belongs to no known family.
- The proposal is a normal centred on the current value. Since it is symmetric, r is just the ratio of the two posterior heights: it is Metropolis, without the Hastings correction.
- A negative proposal has posterior 0 and is always rejected. Rejecting means repeating the current value, not skipping the iteration.
- The mean and the effective size are computed without the first 10% of the chain. The effective size is N / (1 + 2 · sum of autocorrelations), adding the autocorrelations up to the first one that is not positive (100 lags at most).
For topic 9 of ME2
Metropolis is the heart of the topic on approximate Bayesian inference: the exercises ask you to compute r and the acceptance probability, to say which value is written down for a given u, to read a trace and to judge a chain by its acceptance rate and its effective size. Here you see it in motion before coding it in R. The theory, the practice in R and the exercises of the topic are linked below the simulator, under «Keep learning», as soon as they are published. The challenge below gives you acceptance probabilities, decisions and effective sizes, new every time.
