In this example we calculate π
by placing random points in the square below, and counting
the number that fall within the circle. The ratio of the
number that fall within the circle to the total number of
points (number of MC
iterations, n_{iter}) is
proportional to π.

The MC iterations are split into batches
of n_trial iterations. After each
batch, an estimate of π is
calculated, π_{sub}. The
average of these gives our estimate
of π, while the standard
deviation can be used to find the statistical error (given
here as the 95% confidence interval), shown in the top
left plot. The error will decrease
as (n_{iter})^{-0.5},
shown in the bottom left plot.

We are essentially sampling from a binomial distribution
which has probability P = π/4
of a success (i.e. the point is within the circle)
for n = n_trial independent
experiments. According to the central-limit theorem, if we
draw a large number of random samples from any type of
distribution, the distribution
of π_{sub} values will
always be a normal distribution (so long as the sample
size is large enough, typically > 30), shown in the bottom
right plot. Try varying n_trial
to see how this changes.

π against number of MC iterations

Zoom: click and drag, Restore: double click

Percentage error (given here as the 95% confidence
interval) in π against the
number of MC
iterations n_{iter}. Red
circles shows the result, while the solid black line shows
the expected value, given as the standard deviation of the
underlying distribution function divided by the
square-root of the number of
iterations, (n_{iter})^{-0.5}.

The error in our result is given by:

where ⟨...⟩ denotes
the mean after n_{iter}
iterations

Probability density function
of π_{sub} values. Red
circles are calculated result, solid black line is the
expected result based on the central-limit theorem i.e. a
normal distribution.