Estimating the value of pi using Monte Carlo

Imagine a quadrant of a circle is placed inside a square, as shown next, and we generate some random points inside the square. You can see that some of the points fall inside the circle while others are outside the circle:

We can write:

We know that the area of a circle is πr2 and the area of a square is a2:

Let's consider that the radius of a circle is one half and the square's side is 1, so we can substitute:

Now we get the following:

The steps to estimate π are very simple:

  1. First, we generate some random points inside the square.
  2. Then we can calculate the number of points that fall inside the circle by using the equation .
  3. Then we calculate the value of π by multiplying four to the division of the number of points inside the circle to the number of points inside the square.
  4. If we increase the number of samples (number of random points), the better we can approximate

Let's see how to do this in Python step by step. First, we import necessary libraries:

import numpy as np
import math
import random
import matplotlib.pyplot as plt
%matplotlib inline

Now we initialize the square size and number of points inside the circle and square. We also initialize the sample size, which denotes the number of random points to be generated. We define arc, which is basically the circle quadrant: 

square_size = 1
points_inside_circle = 0
points_inside_square = 0
sample_size = 1000
arc = np.linspace(0, np.pi/2, 100)

Then we define a function called generate_points(), which generates random points inside the square:

def generate_points(size):
x = random.random()*size
y = random.random()*size
return (x, y)

We define a function called is_in_circle(), which will check if the point we generated falls within the circle:

def is_in_circle(point, size):
return math.sqrt(point[0]**2 + point[1]**2) <= size

Then we define a function for calculating the π value:

def compute_pi(points_inside_circle, points_inside_square):
return 4 * (points_inside_circle / points_inside_square)

Then for the number of samples, we generate some random points inside the square and increment our points_inside_square variable, and then we will check if the points we generated lie inside the circle. If yes, then we increment the points_inside_circle variable:

plt.axes().set_aspect('equal')
plt.plot(1*np.cos(arc), 1*np.sin(arc))

for i in range(sample_size):
point = generate_points(square_size)
plt.plot(point[0], point[1], 'c.')
points_inside_square += 1
if is_in_circle(point, square_size):
points_inside_circle += 1

Now we calculate the value of π using the compute_pi(), function which will print an approximate value of π:

print("Approximate value of pi is {}" .format(calculate_pi(points_inside_circle, points_inside_square)))

If you run the program, you will get the output shown as follows:

Approximate value of pi is 3.144

The complete program looks as follows:

import numpy as np
import math
import random
import matplotlib.pyplot as plt
%matplotlib inline

square_size = 1
points_inside_circle = 0
points_inside_square = 0
sample_size = 1000
arc = np.linspace(0, np.pi/2, 100)

def generate_points(size):
x = random.random()*size
y = random.random()*size
return (x, y)

def is_in_circle(point, size):
return math.sqrt(point[0]**2 + point[1]**2) <= size

def compute_pi(points_inside_circle, points_inside_square):
return 4 * (points_inside_circle / points_inside_square)

plt.axes().set_aspect('equal')
plt.plot(1*np.cos(arc), 1*np.sin(arc))

for i in range(sample_size):
point = generate_points(square_size)
plt.plot(point[0], point[1], 'c.')
points_inside_square += 1
if is_in_circle(point, square_size):
points_inside_circle += 1

print("Approximate value of pi is {}" .format(calculate_pi(points_inside_circle, points_inside_square)))

Thus, the Monte Carlo method approximated the value of pi by using random sampling. We estimated the value of pi using the random points (samples) generated inside the square. The greater the sampling size, the better our approximation will be. Now we will see how to use Monte Carlo methods in RL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.235.219