Monte Carlo Method (part I)

Today, I’m going to explain as simple (and as thoroughly) as possible a method used in a lot of areas for different computations and for obtaining number results based on random samples. I’m not just going to explain it, but also show how to apply this method in Java (if someone wants the Python version, just let me know in the comments section).

So, what’s Monte Carlo Method? Well, according to wikipedia, Monte Carlo method is used mostly in physical and mathematical problems and is very useful when it is difficult or impossible to apply other mathematical methods. Not very clear, is it? In a (very) simplified version, Monte Carlo can be explained like this: a method that uses randomly generated numbers to calculate something. This method is used in mathematics, physics, engineering, finance, AI development etc, but I am more interested in it’s financial applications, financial derivatives and investments evaluation to be more precise. There is no consensus on how Monte Carlo should be defined, so it will be easier just to show you. It is important to know that usually Monte Carlo methods works with pseudo-random numbers as well.

Pseudo-random number – a number that looks like random, but is not(Computer generated random numbers are pseudo-random numbers usually).

Before we jump to the financial application of Monte Carlo method let us take a classic example of how this method actually works, calculating Pi:

  • Imagine you have a circle with R radius: Area of the circle is Pi*R**2.
  • Now, imagine this circle is inscribed inside a square with side length 2*R: Area of the square is (2*R)**2 = 4*R**2.
  • The area of the circle to the area of the square equals to: Pi/4.
  • Now, if we were to drop a bag of rice (N rice grains) on the entire square it means that  N*Pi/4 grains will fall inside the circle.
  • If we were to count the actual number of grains inside the circle (n) we could get what Pi equals to: n = N*Pi/4 => Pi = 4*n/N.

There is no simpler way to explain this, but the “hard” part while implementing this method in Java is to count the grains inside the circle. In Java, instead of grains, we’re going to use points with random (x,y) coordinates. Still, how are we going to get the number of the points inside the circle?

Let’s say R equals 1, so OX  = OY = 1. To simplify, we’re going to generate random coordinates x and y from 0 (zero) to 1 [no negative values], meaning we will get points only in the top-right quadrant (this does not change anything as the area of square is divided by 4, as is the area of the circle). Knowing the value of x and y, allows us to calculate the length of the vector from the origin (0,0) to (x,y) using this formula: l = sqrt(x*x + y*y). If the value is larger than 1, it means that the vector is longer than R ( which we said equals 1) and it is outside the circle. So basically, it all comes down to generate N number of points (x,y) and to check if each point is inside the circle or not, and find out how many of the N cases are inside the circle (n cases). Then, using the formula we agreed upon earlier (Pi = 4*n/N) we can easily calculate Pi.

A very simple Java implementation looks like this:

import java.text.DecimalFormat;

public class Pi {

	public static void main(String[] args) {
		int inCircle = 0;
		int numberOfIterations = 2;
		int currentIterationNumber = 0;
		double x;
		double y;
		double pi;

		while (currentIterationNumber < numberOfIterations) {
			x = Math.random();
			y = Math.random();

			if (Math.hypot(x, y) < 1) {
				inCircle++;
			}
			currentIterationNumber++;
		}

		pi = 4*(double)inCircle/numberOfIterations;
		DecimalFormat df = new DecimalFormat("#.########");
		System.out.println("Pi equals: " + df.format(pi));
	}

}

I repeat, this is a very long and simple (I hope) way to do this in Java, and I did this because my goal is to demonstrate Monte Carlo and not to practice java.

Here are the results of this code:

Test number Number of iterations Pi value
1   1000 3.16
2   1000 3.092
3   1000 3.16
4   10000 3.1808
5   10000 3.1336
6   10000 3.134
7   100000 3.14984
8   100000 3.14188
9   100000 3.13196

I hope now it became clearer what is Monte Carlo method and how it is used, even though it is used for much more complex calculations this should be enough to understand the logic behind it. In the next part we will move on to understanding financial derivatives and how Monte Carlo can be used to evaluate them.

Leave a comment