Approximation with the Monte Carlo method
18 Apr 2015An interesting way of finding values that fall within a domain is to perform random sample analysis with the Monte Carlo method. This method of finding values relies on random values (lots of them) being measured against some form of deterministic calculation to determine if the value falls within the source function’s scope.
In today’s post, I’m going to illustrate how to use this method in some practical scenarios.
Approximating π
To approximate the value of π, we’re going to treat a square containing a quarter of a unit circle with a lot of random data. We only treat a quarter of a circle (which will contain angles 0 through 90) as we can easily mirror image a single quarter 4 times. Mathematically, you can consider the ratio of the circle’s area with respect to the square that contains it.
For every point that we randomly sample, the following must be true in order for us to consider the point as satisfying the circle:
This tells us, that from the midpoint 0,0
, if the x
and y
values that we’ve randomly selected are within the bounds of the radius; we’ll consider it as “in”.
Once we’ve sampled enough data, we’ll take the ratio of points that are “in” and points that are “out”. We are still only dealing with a quarter of a circle, so we’ll multiply our result out as well and we should get close to π if we’ve sampled enough data. Here’s how it looks in python:
runs
is the number of points that we’re going to sample. radius
is only defined to be clear. If you were to change the radius of the tested area, your output ratio would need to be adjusted also.
Running this code a few times, I get the following results:
Area under a curve
When it comes to finding the area under a curve, nothing really beats numeric integration. In some cases though, your source function doesn’t quite allow for integration. In these cases, you can use a Monte Carlo simulation to work it out. For the purposes of this post though, I’ll work with x^2
.
Let’s integrate it to begin with and work out what the area is between the x-axis points 0 and 3.
So we’re looking for a value close to 9
. It’s also important to note the values of our function’s output at the start of where we want to take the area from to the end as this will setup the bounds of our test:
The area that we’ll be testing from is 0, 0
to 3, 9
. The following code looks very similar to the π case. It has been adjusted to test the area and function:
Here’s some example outputs. Remember, our answer is 9
; we want something close to that:
These are only a couple of the many applications that you can use these for. Good luck and happy approximating.