Get startedGet started for free

The Monte Carlo process

1. The Monte Carlo process

Let's walk through the entire process of a Monte Carlo simulation. To illustrate each step, we'll use the example of calculating pi.

2. Simulation steps

There are four steps to any Monte Carlo simulation. First, we define the input variables and pick the right probability distributions for these variables. Second, we generate inputs by sampling from these distributions. Third, we perform a deterministic calculation of the simulated inputs. Finally, we summarize the results to answer questions of interest to us.

3. Calculating the value of pi

To calculate pi using a simulation, we generate random points (x,y) where x and y are in the interval from negative one to one. With a radius of one, the area of this circle equals pi multiplied by one squared, which is pi. Since it has a width of two, the area of the square around the circle is two multiplied by two, which is four. Therefore, the area of the circle divided by the area of the square equals pi over four, which can be approximated by dividing the number of red points by the number of all points in the square. So, our estimate of pi is four multiplied by the number of red points divided by the number of all points.

4. Step 1

Let's translate this process into Python using the steps we just learned. First, we define our inputs to be the individual points represented by x and y coordinates. We then pick a probability distribution: in this case, the distributions of x and y values are uniform and cover the interval from negative one to one. We will learn many more probability distributions later in this chapter. Next, we initialize variables in Python that will hold summary statistics. circle_points represents the number of points inside the circle and square_points represents the number of points in the square, whether or not those points are also in the circle.

5. Step 2

In step two, we generate n points by randomly sampling x and y coordinates from uniform distributions between negative one and one using random-dot-uniform.

6. Step 3

Next, we determine whether each simulated point lies within the circle by calculating the distance from the origin, where x and y both equal zero. This calculation is deterministic given x and y values because there is no randomness to calculating a point's distance from the origin as there is with sampling each point in the first place. If a point's distance from the origin is less than or equal to one, then it is within the circle, and we increment circle_points. We always increment square_points.

7. Step 4

After many rounds of simulations, we arrive at step four: summarizing results. Here, we calculate the value of pi by multiplying the area of the square, four, by the number of points in the circle divided by the total number of points in the square.

8. All together now

Here's what the simulation code looks like all together. We initiate the number of simulations as n and set it to four million. We also define the circle_point and square_point variables. Next, we generate random points by sampling from uniform distributions for the x and y coordinates. We calculate the distance from the origin to determine whether each simulated point should be counted as a circle_point. Lastly, after many rounds of simulations, we calculate the value of pi. After sampling four million times, we get an estimated pi value of around 3-point-142518. That's accurate to two decimal places!

9. Let's practice!

Time to practice the Monte Carlo process!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.