Monte Carlo Simulations
Two types of processes exist, deterministic and in-deterministic. The outcomes of deterministic processes can be calculated using previously known theories. But, in-deterministic processes are inherently random and their outcomes are not fixed. However, even though the outcome of one process maybe random but the collective data about outcomes of a large number of occurrences of the same process reflects relevant results about it. Monte Carlo simulations involve collecting results from many runs of the same process by using randomness to obtain outcome of each run and obtaining something meaningful.
The Simplest Example
An easy example to explain Monte Carlo simulations is trying to determine the value of using the fact that the ratio of areas of a square of size 2 units and a circle inscribed in it is
We proceed as follows. Assume the centre of the figure to lie at , and then produce random points whose both coordinates lie between 0 and 2. Keep a tab on the total number of points placed, and maintain a separate count for the number of points that fell inside the circle. The ratio of these numbers multiplied by 4 should give us the value of pi. Let us try it out. The python code is given below
1 import random 2 3 #number of points 4 N = 100 5 #inside circle 6 c = 0 7 8 for i in xrange(N): 9 x = random.random()*2 10 y = random.random()*2 11 12 if (x-1.0)*(x-1.0) + (y-1.0)*(y-1.0) < 1: 13 c = c+1 14 15 print (float(c)/N)*4.0
N = 100, the output is
3.16 but with
N=1000000, we have enough sampling and the result is
3.143312 which is accurate upto another significant digit. This is a crude method but in many scientific scenarios, this is the best we've got.