A Monte Carlo Example

Thought Experiment

Suppose you are a pre-modern geometrician (geometer? Turns out either is acceptable. I thought I had made at least one of them up.) Your goal is to figure out the mystical number which relates the area of a circle to its diameter. So you lay out a square, the length of whose sides equals two units. You already know that the area of such a square equals two units times two units, or four square units. Now you inscribe a circle whose circumference exactly touches the square on all four sides. You suspect that the area of the circle is some multiple of the diameter. But you don't know exactly what the area is - it is not nearly as obvious, nor easy to measure, as the answer for the square.

The sides of the square, and the diameter of the circle, are each 2 units. The area of the square is 2x2. If you are new at this sort of thing, you might first think that the area of the circle might be some other number times 2. But what is that other number?

You can use the Monte Carlo method to experimentally determine a ratio between the area of the square and the area of the circle by selecting a large number of random points inside the square. Then you count the number of points that are (1) only inside the circle, and the number that are (2) inside the overall square. The ratio of those two numbers should be proportional to the relative areas of the circle and the square. And that will tell you the value of the missing number (let's call it "P") in your proposed formula.

By the way, that sort of experimentation seems sort of uncommon in the field of maths. I think of math as a "pure" science - as far as I know, there are no "experimental" mathematicians. Perhaps that's what makes the Monte Carlo method interesting to me.

Since your pre-modern world has not even discovered printing yet, much less computers, how might you come up with some random points to count? Looking around you for a plentiful, manageable substance, maybe you decide to scatter rice grains across your square. You carefully count the number of grains in the entire square, and the number in the circle. You count 1240 grains in the square, of which 925 are in the circle. This gives you a ratio between the two areas of 925/1240=.746 (approximately).

So if the area of your square is 4 square units, the area of your circle must be 4 x .746 = 2.984 square units, and your unknown number P must be 1.492.

Using this new number, you quickly try it on a square of 4 units, which has an area of 16 square units. So you calculate the area of your circle as 4 x 1.492 = 5.968. But that's not even close to the prediction of your rice ratio, which would be more like 16 x .746 = 11.936.

You are forced to admit your original hypothesis is wrong The area of a circle must not be P times the diameter.

If you were not so new to this to line of thinking, you would have probably realized early on that areas are related to a "squared" number. The area of your square is 2 "square" units. You get square units by multiplying length units together, like the sides of a square or rectangle. With some more experimentation, you eventually conclude that it is half the diameter (the "radius") of the circle that must be squared. This, of course, changes the value of "P" to something like 3.1416. Congratulations - you've just discovered P, or, in your language, perhaps it is called "pi". Now go invent the television. (Or not. See Eyeballs.)

Computing Experiment

I decided to try doing my own Monte Carlo calculations of pi. The Wikipedia example says the ratio of the area of an inscribed circle to the area of it enclosing square is pi/4.

Of course I wanted to verify that for myself, since I'm doing this whole project for fun anyway.

For a circle of diameter a, its area = pi*((a/2)*(a/2)=(pi*(a^2))/4

and for a square whose side length is a, the area = a^2. (a^2 is shorthand for "squared" here. See note below re math symbols.)

If you place the first formula over the second as a ratio, then simplify, the a's cancel out (as they should), and you confirm that the ratio of the area of an inscribed circle to the area of its square = pi/4. The clickable image to the right shows a (literal) back-of-the envelope calculation.

Now we need to the formula for plotting a circle centered on the x and y axes. Ol' Pythagoras helps us out here. (x^2)+(y^2) = 1 defines a circle in the rectangular coordinate system: if the random number pair produces a number less than or equal to 1, it is inside the circle, and vice versa.

As compensation for writing those formulas in computer shorthand instead of real math symbols, allow me to direct you to this really nice article on methods for presenting mathematical formulae in web pages at this site. If you read even a little bit of the article, you will quickly realize why I took the easy way out.

So, given these formulas, I was ready to try my own version of the Monte Carlo method. I wrote and ran a PERL script, printed below. (If you are not familiar with PERL, and its creator Larry Wall, you should be. See The Internet for quotes, interviews, comments, etc.)

Anyway, here's the PERL code I used to perform this computing experiment. (Warning - I am not a real PERL programmer. Fortunately, the PERL motto is TMTOWTDI, pronounced Tim-Toady, and standing for 'There's More Then One Way To Do It.')

----------------------------------------------
#!/usr/bin/perl
# monte carlo simulation for a circle in a square
use strict;
print "Enter diameter and number of iterations\n";
my $dia = ;
my $continue = 1;
my $iterations = ;
my $counter = 0;
my $squarecount = 0;
my $circlecount = 0;

print "Diameter = $dia \n";
print "counter = $counter, iterations = $iterations \n";
while ($counter < $iterations) {
my $randx = $dia/2 - rand $dia;
my $randy = $dia/2 - rand $dia;
my $dfc = sqrt(($randx**2)+($randy**2));
#print "Random x and y = $randx, $randy \n";
#print "Distance from center = $dfc \n";
$squarecount++;
if ($dfc <= $dia/2) {$circlecount++};
#print "Continue? Enter 1 or 0:";
#print "$counter \n";
$counter++;
}
print "circlecount = $circlecount, squarecount = $squarecount \n";
my $ratio = $circlecount/$squarecount;
my $mypi = $ratio*4;
print "ratio = $ratio \n";
print "my pi = $mypi\n";
my $pi = atan2(1,1) * 4;
print "pi = $pi\n";
----------------------------------------------

I tried several attempts with different values. Here are some of my attempts, and the outcomes:

js-mac-mini:development jcb$ ./monte2.pl
Enter diameter and number of iterations
2
1000
Diameter = 2


counter = 0, iterations = 1000

circlecount = 796, squarecount = 1000
ratio = 0.796
my pi = 3.184
pi = 3.14159265358979
js-mac-mini:development jcb$ ./monte2.pl
Enter diameter and number of iterations
2
10000
Diameter = 2

counter = 0, iterations = 10000

circlecount = 7853, squarecount = 10000
ratio = 0.7853
my pi = 3.1412
pi = 3.14159265358979
js-mac-mini:development jcb$ ./monte2.pl
Enter diameter and number of iterations
2
10000
Diameter = 2

counter = 0, iterations = 10000

circlecount = 7934, squarecount = 10000
ratio = 0.7934
my pi = 3.1736
pi = 3.14159265358979
js-mac-mini:development jcb$

As you can see from the first two examples, the more iterations I performed, the closer the answer was to "real" pi, or at least what PERL thinks is pi. I included the third example to show that 10000 iterations isn't really enough to get a consistent value for pi.

OK, if you aren't tired by now, I definitely am. So, bye!

Updated Nov 24 to fix some typos and to add the back-of-the-envelope image.