# 2025 Spring, Montecarlo methods and its applications ##Homework Buffon’s needle ### Seonggyu Lee #### 2025.03.17
$$ p = (2l)/(\pi d) $$
Let $d=2$ and $l=1$. Then $p= 1/\pi$, equivalently $\pi = 1/p$.
Let $C$ and $\Theta$ be random variables following uniform distributions, $U[0,2]$ and $U[0,\pi]$, respectively.
Let $\hat{p}_n$ be an estimate of $p$, when $n$ needles are thrown.
Whenver the i-th needle is thrown, $C_i~U[0,2]$ and $\Theta_i~U[0,1]$ are sampled independently.
$$\hat{p}_n = \sum_{i=1}^{n} \frac{\mathbf{1}(C_i+\sin(\Theta_i) > d)} {n}$$
Then an estimate $\hat{\pi}_n= \frac{1}{\hat{p}_n}$ is obtained.
I set $n\in \{10, 100, 1000,10000, 100000,1000000,10000000,100000000 \}$ and evaluated $\pi_n$.
import pandas as pd
import math
# Given values
l = 1
d = 2
pi_ = math.pi
n_lists = [10, 100, 1000,10000, 100000,1000000,10000000, 100000000]
# Lists to store results
results = []
# Monte Carlo simulation
for n in n_lists:
total_frequencies = 0
for _ in range(n):
C_i = random.random() * d
Theta_i = random.random() * pi_
if C_i + math.sin(Theta_i) > d:
total_frequencies += 1
p_hat_n = total_frequencies / n
pi_hat_n = 1 / p_hat_n if p_hat_n > 0 else float('inf') # Avoid division by zero
results.append([n, pi_hat_n])
# Create DataFrame with proper column names for LaTeX rendering
df = pd.DataFrame(results, columns=["n", "Estimate of π"])
df_buffon = df
df_buffon
n Estimate of π
0 10 2.500000
1 100 3.571429
2 1000 3.389831
3 10000 3.054368
4 100000 3.121099
5 1000000 3.147564
6 10000000 3.145419
7 100000000 3.142076