Background Theory: Monte Carlo Simulations to Sample the Canonical Distribution¶
Go back to the interactive notebook
The Metropolis-Hastings algorithm is initialized by choosing (e.g., randomly) an initial state $x_0$ at $t=0$. Then, at each iteration:
- We generate a new state $x'$ from a random distribution. In our example, we will consider a two-dimensional space, and move by a random amount along each of the two coordinates, where the random amount will be within a range $[-\eta, \eta]$ where $\eta$ is the "max move size" that you can tune in the simulation.
- We compute the energy of the new state.
- If the new state is lower in energy than the previous one, we accept it: $x_{t+1} = x'$;
- If the new state is higher in energy than the previous one, accept it with a probability $\delta = e^{-(E_{new} - E_{old})/(k_B T)}$ (then $x_{t+1} = x'$), otherwise reject it and store the old state ($x_{t+1} = x_t$);
- In order to decide whether to accept or reject the state, we generate a random number $\mu$ from a uniform distribution between [0, 1], and accept the move if $\mu < \delta$.
- We update the step index ($t \rightarrow t+1$) and iterate until a given number of iterations is reached.
Note how we have two points in the algorithm where we generate random numbers: to generate a random move, and to decide whether to accept or reject a move with a given probability.
A more in-depth description of the algorithm is given in "Understanding molecular simulation: from algorithms to applications (Daan Frenkel and Berend Smit, Elsevier, 2001)."