Simple demonstration of the Metropolis method

(c) David J.C. MacKay | Lecture course | Book | Other demonstrations |
This demo uses perl (version 4) and gnuplot. randomwalkscreenshot

Please download Demonstration program (perl) (save as demo.p) and Xdefaults file (save as Xdefaults.gnu). The latter is not essential, but improves the appearance.

1: Modify your X windows defaults so that the gnuplot colours and fonts come out right.

       xrdb -load Xdefaults.gnu

2: Run demo.p

This program writes output to your terminal (24 lines recommended) and also creates a load of files for gnuplot to plot.

It shows a metropolis method simulation of a system with 21 states. The target distribution is uniform. The proposal density is 0.5,0.5 over the two adjacent states left and right. Rejections only occur when proposals take the state off the end.
Notice how long it takes to reach an end-state.
And then how long it takes to reach the other end-state.

Simple monte carlo methods, even in one dimension, take a huge time to converge and a huge time to generate independent samples.

3: When the program finishes (1200 iterations), please type

 load 'gnucom' 
This will show how the histogram evolves during the simulation you have seen.
David MacKay <>
Last modified: Tue Apr 15 20:48:31 2003