Diffusion forward.py
From Werner KRAUTH
[edit]
Context
This page is part of my 2024 Beg Rohu Lectures on "The second Markov chain revolution" at the Summer School "Concepts and Methods of Statistical Physics" (3 - 15 June 2024).
We consider a particle diffusing on a path graph with five sites, but we start, at time t=0, at all possible positions. As in the naive program Diffusion.py, arrows go "down", "straight" and "up" with equal probability. In application of the Metropolis algorithm, an arrow that goes outside the range [0, N - 1] is rejected, that is, replaced by a straight arrow. The stationary distribution of this reversible Markov chain is uniform on the N sites, because the probability to move from site i to site j equals the probability to move from site j to site i.
[edit]
Python program
import random import matplotlib.pyplot as plt N = 5 pos = [] for stat in range(10000): posit = set(range(N)) t = 0 while True: t += 1 posit = set([min(max(b + random.choice([-1, 0, 1]), 0), N - 1) for b in posit]) if len(posit) == 1: break pos.append(posit.pop()) plt.title('Forward coupling: 1-d with walls: position of the coupled config.') plt.hist(pos,bins=N,range=(-0.5, N - 0.5), density=True) plt.savefig('ForwardCoupling.png') plt.show()