Good beginner exercise for improving programming: Monte Carlo simulation of the approximation of number pi (π)

Vitomir Jovanović
Towards Dev
Published in
4 min readDec 24, 2021

--

Using Monte Carlo for calculation of number pi

Introduction: Monte Carlo simulation

Monte Carlo simulation is in the simplest way random sampling. Random sampling is computational algorithm that heavily relies on randomness to solve a problem that are deterministic in principle. The art of Monte Carlo sampling is that we can use it to solve some mathematical problem that is hard to solve on the other way.

Random sampling in its essence is the repeating of the process of sampling but enabling the randomness of the process. Imagine we have the population of 100 men and we want to know their height. We could pull 10 people (random sample) by chance and try to conclude the mean height of our population. We could repeat this process (we will get the normal distribution of the samples mean, according to the central limit theorem, but it is another story). Let’s see how to apply the Monte Carlo simulation in solving the real mathematical problem.

Approximation of number π

Everyone knows what is a π. The number π is a mathematical constant, approximately equal to 3.14159. It is defined in Euclidean geometry as the ratio of a circle’s circumference to its diameter, and also has various equivalent definitions. The number appears in many formulas in all areas of mathematics and physics. The earliest known use of the Greek letter π to represent the ratio of a circle’s circumference to its diameter was by Welsh mathematician William Jones in 1706. It is also referred to as Archimedes’s constant.

We use in calculation of the area of the circle (r²*π).

Now imagine we want to approximate this value with simulation.

Monte Carlo simulation of π

The simulation works on the principle of generating random points in the square of the r = 1. At the same time this r is radius of the imagined circle which quarter is inside the square. Based on the distance of the point of the center of the coordinate system, it is possible to determine whether the point is inside or outside imaginary quarters of a circle. Here is the spot where Monte Carlo simulation goes in. Monte Carlo simulation will ‘shoot’ the spots in the square, but in the same time inside and outside of the imagined quarter for circle. Through proportion of the spots which are inside and outside of the quarter of circle we can determine π.

More formally: having in mind that a quarter of the surface of a circle (r²*π)/4 and area of square is number π can be approximated as

4 * k/m

where k is the number of points within a quarter of a circle and m is the total number of simulated random points by Monte Carlo shooting. As the number of randomly generated points increases, it is possible to calculate the number π with greater precision at the expense of time efficiency.

Simulation function

Simple function just compare whether the coordinates of the spot shooted by Monte Carlo is in or out of a circle and based on that compares the proportion of the spots inside of the circle and total number of spots, multiplied by 4, as explained above:

import randomdef pi_aproximation(num_of_spots):

inside_of_circle = []
outside_of_circle = []

num_of_spots_inside_of_circle = 0
for i in range(num_of_spots):
x, y = random.random(), random.random()
if ((x**2+y**2)**0.5) <= 1:
inside_of_circle.append((x,y))
num_of_spots_inside_of_circle += 1
else:
outside_of_circle.append((x,y))

approx_pi = (num_of_spots_inside_of_circle/num_of_spots) * 4

return approx_pi

Now we will calculate the approximated pi number for [10, 100, 10000, 1000000, 1000000000] spots and calculate time needed for task operation.

import timefor i in [10, 100, 10000, 1000000, 1000000000]:
start = time.monotonic()
print('pi_approximated:', pi_aproximation(i))
end = time.monotonic()
print(f'time needed for calculation of pi number based on {i} spots:', round(end-start,4), 'seconds')
print(40*'==')

Output:

Approximations of pi number with different number of spots by Monte Carlo simulation

Visualisation

At the end, The beauty of the Data Science is that when you understand the concept, you could easily plot it. With the less spots, the circle is less clear, so the pi number is harder to approximate. As we increase the number of spots, the circle is much clearer, so the pi approximation is better.

Conclusion

Now we got much better intuition why we can use a simulation and how it can help us. Even in solving different types of problems, like Bayes conditional probability in the famous Monty Hall problem, which you can see here. My feeling that simulation is more in flavor of frequentionistic approach which i like more. Hope you liked this subject. You can find all code, including codes for plotting here. Bye!

--

--

PhD in Psychometrics, Data Scientist, works in tech, from Belgrade, Serbia