Explore how the accuracy of your
monte_carlo_pi(N) integration varies with N. To do this, you will call
monte_carlo_pi(N) function 100 times for each of the following values of N [10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]. Then for each N value, calculate the average and standard deviations of your estimates.
Write a function
accuracy() that does this work, and returns an array having one row for each N value, and three columns: the first containing the N value, the 2nd should contain the average of the 100 calculations and the 3rd is the standard deviation (ddof=1). This will function may take several seconds to execute. You might want to have it print out something periodically so that you know its still running (perhaps one line per N value). While you are developing and testing this, you may want to omit the largest couple of values of N.
My code for monte_carlo_pi(N) is as below:
import numpy as np def monte_carlo_pi(N): x = np.random.random(N) y = np.random.random(N) count = 0 for i in range(N): if x[i]**2 + y[i]**2 < 1: count += 1 ratio = (count/N)*4 return ratio
And this is what I’ve done so far:
def accuracy(): #putting the N values into an array for easy manipulation: N = np.array([10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]) #calling the function for each N value: for n in N: row = np.array([n]) #calling the function 100 times: arr = np.array() for n in range(1,101): monte = monte_carlo_pi(n) arr = np.append(arr, monte) #storing the values into an array mean = np.sum(arr) / 100 std = np.std(mean, ddof=1) #appending the value into each row: row = np.append(row, mean) row = np.append(row, std) #making a new array N_arr = np.array() N_arr = np.append(N_arr, row) return N_arr
Please help!!! I’ll love you forever!