Monday, April 22, 2013

A First Encounter With Monte Carlo Simulation

In my second-year undergraduate course on Statistical Inference for economists, I use Monte Carlo simulation with EViews to illustrate the notion of the "sampling distribution" of a statistic, such as an estimator. This is hardly unusual. However, before we get started I have to persuade the students that this whole Monte Carlo thing might actually work!

So, we go through an exercise where we use simulation to approximate the value of π.

Specifically, we use one of the rather nice "computable document format" (cdf) apps from Wolfram Associates. For more background information on these, see my earlier post, here. The particular file we use is to simulate the value of pi can be found here. If you want to run it yourself, you'll first need to download the free cdf player.

Here's the highly complex math behind this particular simulation experiment:
  • The area of a circle, of radius r, is Ac = πr2
  • The area of a square, with side-length 2r, is As = 4r2
  • So, π = 4(Ac / As)
So, we can think of π as being 4 times a population ratio.

Notice that we can "square the circle":


If we now generate lots of random values uniformly within the square, then we can simulate the value of π by computing 4 times the (sample) fraction of these values that also lie within the circle. Generating the random values in the square is easy. We just take a random drawing from a Uniform [0 , 2r] distribution, and this locates the x-coordinate for a point. Then, independently, we take another random drawing from the same distribution, and this locates the y-coordinate for the point.

Here are some screenshots of the cdf app. in action, first with 500 Monte Carlo replications, and then with 10,000 replications:




It's very easy to write a few lines of code that will achieve the same objective as the cdf app., but without the interactive niceties. You'll find an 11-line EViews program that does this on the code page of this blog. 

This little exercise convinces most students that Monte Carlo simulation works. Try it!


© 2013, David E. Giles

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.