# A Taste of Monte Carlo Methods: Compute Pi

• Introduction

• Monte Carlo experiments are experiments with a random outcome, with a certain probability of success (e.g., die roll 6).

If the probability of success is difficult to compute, we can find an approximation by performing many of the experiments and observe the outcome.

The probability of success can be approximated by the quotion of:

```	 # times that the outcome is succesfull
-----------------------------------------
# times that the experiment are performed
```

• The Monte Carlo method is to a computer simulation to perform the experiments.

This is done by generating random events that corresponds to the experiments and evaluate their outcome

We will looks at a simple experiment to find Pi

• Finding an estimate for Pi using a random event experiment

• Problem:

 Assume we have no knowledge of the value of Pi I.e., you cannot use Math.Pi or your calculator or any other information source. How can we find an estimate for Pi ???

• Consider the following funny looking dart board:

• The dimension of the dart board is 1 x 1.

• A quarter circle with radius r = 1 is inscribed in the board.

• Suppose you throw dart at this board blindfolded - and we check only the darts that land on the board.

• When the darts are throw blindfolded, then the probability of landing on each position inside the 1 x 1 quared board is identical

• Then the fraction of the darts that landed inside the quarter circle is equal to:
```	Area of the quarter circle
--------------------------
Area of the 1 x 1 square
```
• The area of the 1 x 1 square is equal to 1.

• The area of the quarter circle can be seen as follows:

• The area of the full circle = Pi × 1 2 = Pi
• The area of the quarter circle = Pi / 4

• Therefore, the fraction of the darts that landed inside the quarter circle is equal to:
```        Area of the quarter circle
-------------------------- = Pi/4
Area of the 1 x 1 square
```

• Finding an estimate for Pi using Psuedo random numbers

• The above description give rise to a Monte Carlo experiment that we can use to estimate Pi.

• Recall that a Monte Carlo experiment is an experiment with a random outcome (throwing darts blindfolded will hit the dart board randomly - each point with the same probability), with a certain probability of success (darts land inside the quarter cicle with probability = Pi/4)

• Initial version of algorithm to estimate Pi:

 ``` int i; int nThrows = 0; int nSuccess = 0; for (i = 0; i < aLargeNumber; i++) { "Throw a dart"; nThrows++; if ( "dart lands inside quarter circle" ) nSuccess++; } System.out.println("Pi/4 = " + (double)nSuccess/nThrows ); ```

• Recall the method nextDouble() returns a random decimal number between 0 and 1.

• We can represent the throwing a dart by:

 Generate 2 random numbers: x between 0 and 1 y between 0 and 1 that represent the coordinates of the position that a dart lands when thrown blindfolded (i.e., at random)

• We can check the whether a dart landed within the quarter cicle by the test:

 x * x + y * y <= 1.0

• We must throw a large number of darts to get a good estimate, and the for loop is ideal for expressing what we want to do in Java.

• Refined version:

 ``` int i; int nThrows = 0; int nSuccess = 0; Random genX = new Random(); Random genY = new Random(); double x, y; for (i = 0; i < aLargeNumber; i++) { x = genX.nextDouble(); // y = genY.nextDouble(); // "Throw a dart"; nThrows++; if ( x*x + y*y <= 1 ) nSuccess++; } System.out.println("Pi/4 = " + (double)nSuccess/nThrows ); ```

• Example Program: (Demo above code)

• More inrestesting simulations

• In the 1960's, Edward Thorp wrote and published the book "Beat the Dealer" on Black Jack: click here

• The book presents a system of card counting and how to play Black Jack

• Many people have written computer programs that simulate a Black Jack games (among them was me :-))

• The simulations show that Thorpe system does work - you can win at Black jack with his system by counting cards.

• As a result, casinos in Las Vegas impose measures to throw out people that count cards...