← 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 at . Then, at each iteration:
- We generate a new state 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 where 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: ;
- If the new state is higher in energy than the previous one, accept it with a probability (then ), otherwise reject it and store the old state ();
- In order to decide whether to accept or reject the state, we generate a random number from a uniform distribution between [0, 1], and accept the move if .
- We update the step index () 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).”