← Go back to the interactive notebook

Background Theory: Monte Carlo Simulations to Sample the Canonical Distribution

Original notebook: theory_monte_carlo_parabolic.ipynb


Monte Carlo simulations and the Metropolis-Hastings algorithm

Monte Carlo simulations are often used to sample the canonical distribution (at a given temperature), given a potential energy surface. The Metropolis-Hastings algorithm is one of the most popular sampling algorithms to generate a sequence of states. The sequence can be used to approximate the distribution and to calculate averages (e.g. the expected value of some observable at a given temperature).

The Metropolis-Hastings algorithm is initialized by choosing (e.g., randomly) an initial state x0x_0 at t=0t=0. Then, at each iteration:

  1. We generate a new state x′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.
  2. We compute the energy of the new state.
    1. If the new state is lower in energy than the previous one, we accept it: xt+1=x′x_{t+1} = x';
    2. If the new state is higher in energy than the previous one, accept it with a probability δ=e−(Enew−Eold)/(kBT)\delta = e^{-(E_{new} - E_{old})/(k_B T)} (then xt+1=x′x_{t+1} = x'), otherwise reject it and store the old state (xt+1=xtx_{t+1} = x_t);
    3. 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.
  3. We update the step index (t→t+1t \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).”