In this Monte Carlo experiment, we generate random X and Y cordinates from -1 to 1. We then count the proportion of generated points that fall within a circle of radius 1 centred at (0, 0). The area of a circle of radius 1 is π, so the proportion of points that fall within satisfy X2 + Y2 ≤ 1 times the area of the square (which is 4) gives us an estimate of the area of the circle and hence the value of π.
The more sample points we take, the smaller our deviation from the true value of π. Here's the math:
Let sample i be represented by Xi that takes the value of 1 if point i falls within the circle, and 0 if it doesn't. Let Mn be the proportion of points that fall within the circle.
Expectation of Xi, E[Xi] = π⁄4
Squared expectation of Xi, E[Xi2] = π⁄4
Variance of Xi, Var[Xi] = E[Xi2] - E[Xi] = (π⁄4) - (π⁄4)2 = (π⁄4)(1 - π⁄4)
Expectation of Mn, E[Mn] = π⁄4
Variance of Mn, Var[Mn] = Var[Xi]⁄n = (π⁄4)(1 - π⁄4)⁄n
Thus as we can see, the expected value of our estimation is π, and the variance of our estimation from the true value decreases as the number of trials increases. None of this is really surprising mathematically, this is the equivalent of a binomial random variable being divided by its number of trials.
Blablabla, who cares about all the math, am I right? Here's a pretty circle, numbers flashing across the screen, and a pretty neat chart plotting our estimated value (powered by SmoothieCharts).
Number of samples: 0
Number of sample points that fall within the circle: 0
Our estimation of π, 4 x proportion of samples within circle: