You are on page 1of 3

Example 2: Monte Carlo estimates of pi ( π )

To compute Monte Carlo estimates of pi, you can use the function f ( x )= √ 1−x 2 . The
graph of the function on the interval [0,1] is shown in the plot. The graph of the function
π
forms a quarter circle of unit radius. The exact area under the curve is .
4

There are dozens of ways to use Monte Carlo simulation to estimate pi. One of this is
the "average value method,” which uses random points in an interval to estimate the
average value of a continuous function on the interval.

In calculus you learn that the average value of a continuous function  f  on the interval
[a , b] is given by the following integral:

1
f avg=∫ f ( x ) dx
0

π
In particular, for  f ( x )= √ 1−x 2, the average value is because the integral is the area
4
under the curve. In symbols,
1
π
f avg=∫ √1−x dx=
2

0 4
Recall that if X is a uniformly distributed random variable on [0,1], then Y =f ( X) is a
random variable on [0,1] whose mean is f avg. It is easy to estimate the mean of a
random variable: you draw a random sample and compute the sample mean.

And by using a program like SAS/IML program that generates N=10,000 uniform
variates in [0,1] and uses those value to estemate f avg=E ¿ . Multiplying that estimate by
4 gives an estimate for pi.

proc iml;
call randseed(3141592); /* use digits of pi as a seed! */
N = 10000;
u = randfun(N, "Uniform"); /* U ~ U(0, 1) */
Y = sqrt(1 - u##2);
piEst = 4*mean(Y); /* average value of a function */
print piEst;

SAS/IML Program Code Snippet.

As for this example the pi estimate(piEst) result is 3.1413432 from generating a random
sample of size 10,000. [Note that the pi estimate value differ every pi estimation
due to random variation].

We can visualize this results using histogram of the 1,000 estimates. The following
graph shows the distribution of the average value method. As you can see the
distribution is narrow and has a sharp peak at pi
For the Monte Carlo estimate of pi that we did in this example, this method that
computes the average function value provide great estimate.

You might also like