Introduction to Monte Carlo Simulations

Monte Carlo method

Estimation of Pi by Monte Carlo Mathematical methods that use random numbers for solving quantitative problems are commonly called Monte Carlo methods. For example, consider a problem of estimating the of the value of Pi from the ratio of areas of a circle and a square that inscribes the circle. From elementary geometry we know that:
Area of Circle over Area of Square = Pi/4 Four
Monte Carlo method solves this problem by randomly selecting a large number of points within the square, and determining how many of these points fall within the circle. The ratio of number of points within the circle to the number points within the square approximates the ratio of areas of the circle to the square, and provides an estimate for Pi/4. A simple Python implementation of a Monte Carlo algorithm for estimating the value of Pi is given below:

#!/usr/bin/env python2.4

import sys
from random import random               # Mersienne Twister as the core generator

points  = int(sys.argv[ 1 ])            # Number of MC attempts

in_square = 0

for point in range(points):
   x = 2 * ( random() - 0.5 )
   y = 2 * ( random() - 0.5 )
   d = x*x + y*y
   if d <= 1:
      in_square = in_square + 1

Pi  = 4.0 * in_square / points

print 'Approximation to Pi after', points, 'points:', Pi

Convergence of Pi by Monte Carlo The accuracy of the Monte Carlo estimate for Pi depends on the number of randomly chosen points, or Monte Carlo trials. For example, a glance at the figure on the right shows that a single Monte Carlo calculation with 500 trials might suggest that Pi is 3.04, or that it is 3.20, depending on your luck. A similar calculation with 50,000 trials, however, is likely to yield estimates that are between 3.13 and 3.15. In other words, with more points we can estimate Pi more accurately. However, the convergence is rather slow and in practice a very large number of Monte Carlo trials are often used. For example, a calculation with 100 million Monte Carlo trials gave an estimate 3.14159848 for Pi, which is accurate to the six digit (Pi = 3.14159265...).

It turns out that the Monte Carlo method is much less efficient than many methods that estimate Pi from infinite sum formulae. For example, the above 100 million point MC estimate, accurate to six digits, took several minutes on a modern personal computer while an estimate based on the Ramanujan formula gives hundreds of digits accurately in a fraction of a second.

Educational material by Dr. Kalju Kahn, Department of Chemistry and Biochemistry , UC Santa Barbara. 2005