Monte Carlo methods are computational methods used to make simulations of complex systems in physics, finance, biology and other fields.  The methods involve using  random numbers and statistics to determine an unknown quantity.  In most cases, the system being simulated is far too complicated to solve analytically.  Some applications include modeling the behavior of gas molecules in a container, simulating and forecasting weather systems,  modeling the interior of the sun, and modeling the universe.

On this page, the basic principle behind Monte Carlo methods will be demonstrated with a simple example that most people should be able to understand easily:  the approximation of the number  π (pi).

As you know, π is the ratio of the circumference of a circle to the diameter of the circle. You also know that the number π shows up in the equation for the area of a circle.  

This gives us a clue of how to go about calculating π.  If you can measure the area and radius of a circle, you should be able to calculate π based on your measurements.  If the circle is inscribed inside of a square, as shown in the figure below, the length of the side of the square is the diameter of the circle or two times the radius. 

                      

If a number of dots were randomly scattered over this page then you would expect the number of dots falling inside of the circle to be proportional to the area of the circle, and the number of dots inside of the square should be proportional to the area of the square.  

      and          

This is where the Monte Carlo method comes in.  Random points are used to approximate the area of the two shapes of interest.  The area of the square is simply the length of a side squared.

Examining the figure above in further detail, you will notice that if you focus on any particular quadrant of the figure, the ratio of the area of the circle to the area of the square is the same as the ratio for the entire figure.  For example, let's look at the upper-right quadrant:

The area of this quarter circle is

and the area of the quarter square is

The ratio of the number of points in the circle to the number of points in the square is approximately equal to the ratio of the respective areas.  This is in turn equal the the ratio of the areas in the quadrant:

Now solving for π, we have our final result:

So, π is approximately four times the ratio of the number of points falling inside of the circle, to the number of points inside of the square.

In order to write this into a program, all one needs to do is use a function that outputs random x and y coordinates and determine whether the random points generated fall inside of a circle of a given radius.  I have written a simple program which does just that.  First the program asks the user for a number called the grid resolution.  This number specifies the number of grid points that are placed on each side of the square grid.  For example, a grid resolution of 6 looks like this: 

The total number of points on the grid that can now be specified is (6+1)2 = 72.  Points are then randomly scattered onto the grid as shown: 

With this grid resolution, there are 35 points inside of the circle and 49 points in the square.  this grid would lead to an approximation for π of 4*35/49 = 2.857.  If the grid resolution is increased then the areas are approximated more accurately: 

This is a grid resolution of 12.  The total number of points is (12+1)2 = 169.  The number of points inside of the circle is 123.  This leads to an approximation for π of 4*123/169 = 2.911.  As the grid resolution increases, the total number of points increases very rapidly until the number of operations required becomes unmanageable.  to solve this problem. the program I wrote only makes use of 55,000,000 randomly scattered points.  Once there are significantly more than 55,000,000 grid points, the error in the approximation becomes huge.

The optimal grid resolution for 55,000,000 random points is approximately 100,000.  At this resolution, the program approximates π accurately to 4 decimal places.

 

Download the program here and play with changing the grid resolution:

pi.exe

    More information about Monte Carlo methods can be found here