This is a simple program made to demonstrate the underlying concept of the Monte Carlo method for approximating an area of a shape. In that instance we used an already known formula of a circle to approximate the value of PI.
In this very simple introduction we will introduce the very basic idea of the method. Imagine being given a complex 2 dimensional shape and being asked to calculate the area inside using a computer. For the simplicity of this example we assume the inside the boundaries of the points (0,0) (1,0) (0,1) (1,1) any shape surface calculated in this box can be transformed to be bigger and vice versa . How can you calculate this area analytically? Even if possible this may be very complex let alone this is a computer and the surface maybe irrational so the only option is an approximation.
For our example (and the example in the code) our shape will be a circle centered (0.5,0.5) with a radius of 0.5, a simple shape and the surface could easily be calculated analytically but it fits the simplicity of this example.
Next step would be to random a large amount of points inside the box (0,0) (1,0) (0,1) (1,1). We call each random point inside the circle a HIT and a point outside the circle a MISS. The probability of a HIT point is the area of the circle divided by the total area of the square in which we random our point in.
Before jumping into equation let us first establish common notations:
We can now write the equation:
Simply put, the probability of a HIT point is equal to the area of the circle. The probability if a HIT can be approximated with the our random points, the more points used the better.
Our simple program calculates this simple equation by evaluating 1 million random points.
If we can approximate the area of the circle we can easily re-arrange the equation using what we already know about the surface of a circle to approximate PI since the two are bound together mathematically. note that r is the radius and has the value of 0.5.
The output of the this simple program is a table showing the approximation value after each 10,000 random points added to the calculation. You can look into the main package and have a look at the code which is very simple and friendly. Possible output:
|--------------------------------|
| Iteration | Evaluation |
|--------------------------------|
| 10000 | 3.1444 |
|--------------------------------|
| 20000 | 3.1468 |
|--------------------------------|
| 30000 | 3.1493333333333333 |
|--------------------------------|
| 40000 | 3.1409 |
|--------------------------------|
| 50000 | 3.1364 |
|--------------------------------|
| 60000 | 3.1354 |
|--------------------------------|
| 70000 | 3.135885714285714 |
|--------------------------------|
| 80000 | 3.1341 |
|--------------------------------|
| 90000 | 3.1384 |
|--------------------------------|
| 100000 | 3.139 |
|--------------------------------|
| 110000 | 3.1398545454545457 |
|--------------------------------|
| 120000 | 3.1372666666666666 |
|--------------------------------|
| 130000 | 3.1374153846153847 |
|--------------------------------|
| 140000 | 3.1353142857142857 |
|--------------------------------|
| 150000 | 3.1346933333333333 |
|--------------------------------|
| 160000 | 3.1347 |
|--------------------------------|
| 170000 | 3.134564705882353 |
|--------------------------------|
| 180000 | 3.135466666666667 |
|--------------------------------|
| 190000 | 3.1352 |
|--------------------------------|
| 200000 | 3.1367 |
|--------------------------------|
| 210000 | 3.1365714285714286 |
|--------------------------------|
| 220000 | 3.1371818181818183 |
|--------------------------------|
| 230000 | 3.1379130434782607 |
|--------------------------------|
| 240000 | 3.1389666666666667 |
|--------------------------------|
| 250000 | 3.138512 |
|--------------------------------|
| 260000 | 3.1394 |
|--------------------------------|
| 270000 | 3.139940740740741 |
|--------------------------------|
| 280000 | 3.1402571428571426 |
|--------------------------------|
| 290000 | 3.1403172413793103 |
|--------------------------------|
| 300000 | 3.1406533333333333 |
|--------------------------------|
| 310000 | 3.140941935483871 |
|--------------------------------|
| 320000 | 3.142525 |
|--------------------------------|
| 330000 | 3.142581818181818 |
|--------------------------------|
| 340000 | 3.142894117647059 |
|--------------------------------|
| 350000 | 3.1432 |
|--------------------------------|
| 360000 | 3.1431666666666667 |
|--------------------------------|
| 370000 | 3.143535135135135 |
|--------------------------------|
| 380000 | 3.143642105263158 |
|--------------------------------|
| 390000 | 3.1435897435897435 |
|--------------------------------|
| 400000 | 3.14388 |
|--------------------------------|
| 410000 | 3.1440682926829266 |
|--------------------------------|
| 420000 | 3.1441714285714286 |
|--------------------------------|
| 430000 | 3.144325581395349 |
|--------------------------------|
| 440000 | 3.1439909090909093 |
|--------------------------------|
| 450000 | 3.1430933333333333 |
|--------------------------------|
| 460000 | 3.1426608695652174 |
|--------------------------------|
| 470000 | 3.1424425531914895 |
|--------------------------------|
| 480000 | 3.142516666666667 |
|--------------------------------|
| 490000 | 3.142604081632653 |
|--------------------------------|
| 500000 | 3.142216 |
|--------------------------------|
| 510000 | 3.1420627450980394 |
|--------------------------------|
| 520000 | 3.1418846153846154 |
|--------------------------------|
| 530000 | 3.1420830188679245 |
|--------------------------------|
| 540000 | 3.1419407407407407 |
|--------------------------------|
| 550000 | 3.141730909090909 |
|--------------------------------|
| 560000 | 3.14185 |
|--------------------------------|
| 570000 | 3.141178947368421 |
|--------------------------------|
| 580000 | 3.1408758620689654 |
|--------------------------------|
| 590000 | 3.141064406779661 |
|--------------------------------|
| 600000 | 3.1411066666666665 |
|--------------------------------|
| 610000 | 3.14087868852459 |
|--------------------------------|
| 620000 | 3.140864516129032 |
|--------------------------------|
| 630000 | 3.1410539682539684 |
|--------------------------------|
| 640000 | 3.14099375 |
|--------------------------------|
| 650000 | 3.1410646153846153 |
|--------------------------------|
| 660000 | 3.141109090909091 |
|--------------------------------|
| 670000 | 3.141319402985075 |
|--------------------------------|
| 680000 | 3.1410764705882355 |
|--------------------------------|
| 690000 | 3.140550724637681 |
|--------------------------------|
| 700000 | 3.1405714285714286 |
|--------------------------------|
| 710000 | 3.1408788732394366 |
|--------------------------------|
| 720000 | 3.1408444444444443 |
|--------------------------------|
| 730000 | 3.1408876712328766 |
|--------------------------------|
| 740000 | 3.1410864864864867 |
|--------------------------------|
| 750000 | 3.1413973333333334 |
|--------------------------------|
| 760000 | 3.1417684210526318 |
|--------------------------------|
| 770000 | 3.141672727272727 |
|--------------------------------|
| 780000 | 3.141620512820513 |
|--------------------------------|
| 790000 | 3.141640506329114 |
|--------------------------------|
| 800000 | 3.141675 |
|--------------------------------|
| 810000 | 3.141767901234568 |
|--------------------------------|
| 820000 | 3.141731707317073 |
|--------------------------------|
| 830000 | 3.1415710843373494 |
|--------------------------------|
| 840000 | 3.1416809523809524 |
|--------------------------------|
| 850000 | 3.141755294117647 |
|--------------------------------|
| 860000 | 3.141706976744186 |
|--------------------------------|
| 870000 | 3.141471264367816 |
|--------------------------------|
| 880000 | 3.141490909090909 |
|--------------------------------|
| 890000 | 3.1415191011235954 |
|--------------------------------|
| 900000 | 3.141591111111111 |
|--------------------------------|
| 910000 | 3.1416131868131867 |
|--------------------------------|
| 920000 | 3.141591304347826 |
|--------------------------------|
| 930000 | 3.1418408602150536 |
|--------------------------------|
| 940000 | 3.1418382978723405 |
|--------------------------------|
| 950000 | 3.1417178947368423 |
|--------------------------------|
| 960000 | 3.1417291666666665 |
|--------------------------------|
| 970000 | 3.141620618556701 |
|--------------------------------|
| 980000 | 3.141669387755102 |
|--------------------------------|
| 990000 | 3.141640404040404 |
|--------------------------------|
| 1000000 | 3.141624 |
|--------------------------------|