```python
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
plt.style.use('fivethirtyeight')
from numpy.random import default_rng
```
# HW_02
## Calculating and reporting $\pi$
Calculate your own value of $\pi$ using the random x- and y-coordinates. You need to report your findings to other engineers in your field. You should explain _why_ and _how_ you are calculating $\pi$, then describe the data with at least one plot. You can use more if it helps to explain the process or a point you are trying to make, here are some examples from our first $\pi$ calculation.
```python
rng = default_rng(42)
plt.figure()
ax1 = plt.subplot(2, 2, (1, 2))
for N in range(100,10001, 500):
trials = 100
pi_trial = np.zeros(trials)
for i in range(trials):
x = rng.random(N)
y = rng.random(N)
r = x**2 + y**2
pi_trial[i] = np.sum(r < 1**2)/N*4
mean_pi = np.mean(pi_trial)
std_pi = np.std(pi_trial)
ax1.plot(N*np.ones(trials), pi_trial, 'ko', alpha = 0.1)
plt.title(r'Value of $\pi=${:1.3f}$\pm${:1.3f}'.format(mean_pi, std_pi));
ax2 = plt.subplot(2, 2, 3)
ax2.hist(pi_trial, 20, density=True)
x = np.linspace(3, 3.3)
pi_pdf = stats.norm.pdf(x, loc = mean_pi, scale = std_pi)
ax2.plot(x, pi_pdf)
ax3 = plt.subplot(2, 2, 4)
ax3.boxplot(pi_trial, vert=False);
```

```python
ax1 = plt.subplot(1, 1, 1)
for N in range(100,10001, 100):
trials = 100
pi_trial = np.zeros(trials)
for i in range(trials):
x = rng.random(N)
y = rng.random(N)
r = x**2 + y**2
pi_trial[i] = np.sum(r < 1**2)/N*4
mean_pi = np.mean(pi_trial)
std_pi = np.std(pi_trial)
plt.plot(N*np.ones(trials), pi_trial, 'ko', alpha = 0.1)
```

```python
plt.plot(x[r<=1], y[r<=1], 'ms')
plt.plot(x[r>1], y[r>1], 'gs')
plt.axis('equal')
```

(-0.049923631959040385,
1.0499423279665119,
-0.049928402291538575,
1.0495364173601551)
## Some points to consider in your report
- $\pi$ is not a random variable, but the probability that you land in a circle vs a square if your coordinates are x = 0-r and y = 0-r is $\frac{A_{circle}}{A_{square}} = \frac{\pi r^2}{4r^2} = \frac{\pi}{4}$.
- When is the mean "close enough" to $\pi$?
- What do the tails of the calculated $\pi$ values tell us about the prediction?
- What else do you notice in your analysis?
## Intro
The value of $\pi$ is used in over 90% of engineering formulas that include trigonometry or geormetry.
$\pi$ is the ratio of a circle's circumference to its diameter.
In this report, I calculate $\pi$ using a Monte Carlo model.
Monte Carlo methods leverage the Law of Large Numbers.
As sample size increases, the sample mean will get closer to the expected value [1](https://en.wikipedia.org/wiki/Law_of_large_numbers).
The Law of Large Numbers ensures that with enough randomly sampled points, our estimate of $\pi$ will converge to its true value.
## Methods
I calculated $\pi$ using random points within a unit square.
Each x- and y-location was generated between 0 and 1. The distance from the origin was used to calculate radius, _r_, and the ratio of areas is given as
$A_{circle} / A_{square} = \frac{\pi r^2}{4r^2} = \frac{\pi}{4}$
If the radius is greater than 1, the point is outside of the circle.
If the radius is less than 1, the point is inside of the circle.
The ratio of the number of points inside the circle to the total number of points is directly proportional to $\pi$ as such,
$A_{circle} / A_{square} = \frac{number~of~points~in~circle}{total~number~of~points} = \frac{\pi}{4}$.
## Results and Discussion
I generated 10,000 random and calculated $\pi = 3.142\pm 0.0148$.
The simulation was accurate to 0.00041 or 0.013% error.
The scatter plot above shows the distribution of points, with purple points
indicating those within the quarter circle and green points outside it.
The law of large numbers predicts that larger $N$ values will lead to higher precision $\pi$ values. Based on the table below, we see precision increase from 0.16 to 0.06 as N goes from 100 to 1000.
|N| Mean| Standard Deviation|
|---|---|---|
|N=100 | Mean π = 3.1144 | Std π = 0.1635
N=200 | Mean π = 3.1236 | Std π = 0.1037
N=300 | Mean π = 3.1423 | Std π = 0.0856
N=400 | Mean π = 3.1559 | Std π = 0.0834
N=500 | Mean π = 3.1475 | Std π = 0.0635
N=600 | Mean π = 3.1507 | Std π = 0.0634
N=700 | Mean π = 3.1476 | Std π = 0.0655
N=800 | Mean π = 3.1389 | Std π = 0.0552
N=900 | Mean π = 3.1209 | Std π = 0.0618
N=1000 | Mean π = 3.1423 | Std π = 0.056
## Conclusions
This experiment demonstrate the Law of Large numbers in a practical application Monte Carlo simulation to calculate $\pi$.
In this project, I applied Monte Carlo to calculate $\pi$ with high accuracy and high precision.
Larger sample sizes lead to more accuracy and precision results.
Extending Monte Carlo methods to statistical estimates, we need large sample sizes for high precision results.
```python
```