owned this note
owned this note
Published
Linked with GitHub
# 𝛑 APPROXIMATIION (Pi-Day)

## Intro
Happy 𝛑-day guys 🥧✨
𝛑 is an important number in math, especially in maths that involves circles or trigonometry. This number has been fascinating people since [history](https://news.web.baylor.edu/news/story/2024/magic-and-mystery-p-pi).
In this project, we are going to approximate the value of pi using randmon simulation, famously known as the Monte Carlo Simulation. What basically it is, it's a method that uses random sampling to estimate the value of π.
## Learning objectives
* Practice loops
* Practice visual plotting with the `matplotlib` module
* practice the python' `random` module using a uniform distribution
* `MONTE CARLO SIMULATION` using randomness
* Margin of error of our simulation
## Task 0: Setup
In your IDE, create a python file called, pi_approximation
So here is the way this simulation work...
Imagine you have an outer square of side `x`, and an inner circle of radius `r` such that the `2r = x`

Using simple mathematical formulars we can come with the relation existing between the areas of the two shapes

> NOTE: VERY IMPORTANT...
> According the a simple understanding of a mathmatical space, **a space** is A collection of points (a set), plus rules that define how those points relate to each other.
With that in mind, we can randomly populate the inner square to a point where we can cover the whole area (space). And with this simulation, we will calculate the number of dots that went inside the circle as representing the area of the circle itself (it's just an approximation).
Finally, we are going to compare that ratio in comparison the real ration as shown in the image above, to find an approximation of 𝛑.
## Task 1:
Import the mathplotlib module and the random module
and create the variables that you are going to need to compute this program. We are starting by doing in total trials (total number of dots shot) of 500, and we are going to increase it and see how it affects the final results
## Task 2:
Populate the square with dots, using a uniform randomness `only inside the square`, plot them on a plot, using scatter mode, makig dots who entered inside the circle with a distinct unique marker/colot from those who didn't, and always keeping track of the number of dots who entered inside the circle.
```
#this is how we generate random coordinates for any dot that we will throw.
# And keep doing this a number of times (total_trials)
x = random.uniform(-radius, radius)
y = random.uniform(-radius, radius)
```

## Task 3:
Now we can approximate the value of 𝛑 using the relation we had before.
> the ratio of the inner circle to the square is equal to `𝛑/4`
> assuming that ratio holds for true, approximate the value of pie, by using the ratio of number of dots inside the circle to the total number of dots thrown.
> `in_circle/total_trials = 𝛑/4`
## Task 4:
Try to play around with the inital number of trials, and see what changes.
## Task 5: (Final)
Finaly we want to see how close we get close to approximating 𝛑 by keeping track of the value of pie over time, as we keep throwing the dots.
Create a different file and call it, version 2, and copy paste all we have done so far, and delete the code part that deals which ploting the dots at each through (`any plt.scatter`)
Create a varible called, `inst_approx` which is an empty list, and after each trial (each dot throw) calculate the pi approximation at that time and `append` it to the list. Then, after the loop, plot the inst_approx on a plot, and how close we keep getting approximating the right value of pi.