In [18]:
%%capture 
%run pi_approximation_computation.ipynb

Approximation of Pi

Theory

We approximated the value of $\pi$ (the ratio of a circle's perimeter to its diameter) using the Monte Carlo Method.

For that, we sample uniformly the available points of the square $[-1, +1]^2$. We then compute the ratio of pionts that follow the implicit circle equation $x^2 + y^2 <= 1$. This will give us an approximation of $\pi$.

Why, might you ask?

Each point is either in the circle, or it isn't. This means that the chance of having a randomly-chosen point inside the circle is the ratio of the circle's area to the square's area.

The area of the $[-1, +1]^2$ square is $2^2 = 4$. The area of the unitary circle is $\pi * 1^2 = \pi$.

This means that we have a chance of $\frac{\pi}{4}$ of choosing a point that is inside the circle.

The Monte Carlo part is about taking many samples to approximate that chance.

Thus, $$\frac{\pi}{4} ≈ \frac{N_{inside}}{N_{outside}}$$

Which is the same as $$\pi ≈ 4*\frac{N_{inside}}{N_{outside}}$$


In [19]:
plot_approximation()


Results


In [6]:
print("Pi was approximated at %.5f, when the real value is %.5f..." % (best_pi_approximation, real_pi_value))


Pi was approximated at 3.14166, when the real value is 3.14159...

However, this method isn't very fast and can only approximate Pi, never truly compute the exact value.


In [4]:
plot_approximation_evolution_graph()



In [ ]: