I am doing some online research and tutorial, then got stuck when i tried to find an approximation of pi using monte Carlo.
import math
import random
import time
import numpy as np
import matplotlib.pyplot as plt
random.seed(1234)
old_est = 0
n = 1
MC_N= 1000000
results = np.repeat(0, MC_N)
for i in range(MC_N):
x = random.random()
y = random.random()
A_n = math.sqrt(x*2 + y*2)
new_est = ((n-1)/n) * old_est + (1/n)*A_n
results[n] = new_est
if A_n <= 1:
n +=1
if n > MC_N:
break
old_est = new_est
A_n = 4 * n / MC_N
print(results)
plt.plot(results)
plt.ylabel('numbers')
plt.show()
So I am using monte Carlo method and want to have an early stopping method to get the best estimation. I suppose to get around 3.14 but I got 0 Can somebody help me why I am wrong?
PS: It supposed to be calculating the approximation number of pi, To estimate the value of PI, we need the area of the square and the area of the circle. To find these areas, we will randomly place dots on the surface and count the dots that fall inside the circle and dots that fall inside the square. Such will give us an estimated amount of their areas. Therefore instead of using the actual areas, we will use the count of dots to use as areas. Also, I want to show that the error in estimation also decreased exponentially as the number of iterations increased.