Monte Carlo Simulation
We actually provide all the code that implements the Monte Carlo simulation, but in case you're interested. Here is what we're doing.
Monte Carlo simulation is named after the city in Monaco, where the primary attractions are casinos that have games of chance. Gambling games, like roulette, dice, and slot machines, exhibit random behavior. It is an estimation technique that uses randomness to produce estimates of potentially hard to measure values.
For example, consider the problem of computing the ratio of the circumference of a circle to its diameter (that would be pi). If you have taken trigonometry or calculus, then you may know how to compute an approximate value for by any one of several means, but for the time being let's suppose that you do not know much math, but have a geometry book you can consult. Glancing quickly through the geometry book, you find the following useful bits of information:
- The area of a circle with radius r is (pi)r^2
- The area of a square with a side of length r is r^2.
- If point (x, y) is inside a circle of radius r, centered at the origin, then x^2 + y^2 <= r^2
- If you draw a square with a side of length 2r, and then draw a circle with radius r that has its center in the middle of the square, then the square will have an area of 4r^2 and the circle will have an area of (pi)r^2.
Therefore, the ratio of the area of the circle to the area of the square is going to be (pi)r^2/4r^2 which reduces to pi/4. So, if we could determine this ratio, we could compute pi.
What if we did the following: generate a bunch of points inside the square -- that would require generating random coordinates for x and y such that both values range from [-1, 1]. For each value, we can determine if the point lies in the circle using the equation above. If we generate N points and C of them are on the circle, then we know that C/N is equal to pi/4 and pi is equal to 4(C/N).
So, we are using randomness (the generation of points in the square) to produce an estimate of the value of pi. You may be quite surprised how accurate a value we can get with relatively few points. You'll see how well our Uber passengers do!