Assignment On Monte Carlo Integration Method


Added on  2019-09-30

9 Pages2055 Words438 Views
Last Name 1Name:Professor:Course:Date:Project 1Q1) Generation of N two-dimensional random co-ordinates in [0, 1]^2:There is a direct instruction available in “numpy” for this task:X = np.random.rand(N, 2)To ensure reproducibility of the results, following command requires to be provided,np.random.seed(1234) where 1234 is any arbitrary number.Q2) 3D-Plot of g values over [0, 1]^2:Purple points are valid points in the region Ω = (H>=0). Yellow points are not in Ω.

Last Name 2Q3) Code for calculating integral of g over region Ω:def my_Monte_Carlo(): X = np.random.rand(n, 2) H = my_h_function(X) G = my_g_function(X) C = H >= 0 return sum(G*C)/nThe value of the integral is 0.03441.Q4) Value of Pi by Monte Carlo Method:def my_g_function(X): m = len(X) return np.array([1 for i1 in range(m)])def my_h_function(X): output = [] for x in X: x1, x2 = x output.append(1 - x1**2 - x2**2) return np.array(output)n = 1000np.random.seed(1234)print("Estimate of Pi with 1000 pairs of random numbers", 4*my_Monte_Carlo())Value of Pi is obtained as 3.128 (actual 3.142)Q5) a) Mean and Standard Deviation for N=1000 and T=500Mean = 0.03151Standard Deviation = 0.001719b) Plot of Mean and Standard Deviations for Nset Samples# Q5(B) Mean and Standard deviation plotsNset = np.round(np.logspace(1, 5, 100))I, t0 = [], time.clock()np.random.seed(1234)for i in range(100):

Last Name 3 print(i, time.clock()-t0) n = int(Nset[i]) J = [] for t in range(500): J.append(my_Monte_Carlo()) I.append(np.array(J))I = np.array(I)plt.figure ()plt.semilogx (Nset, I, 'kx')plt.semilogx (Nset, np.mean(I, 1), 'b', label = 'mean')plt.semilogx (Nset, np.mean(I, 1) + np.std(I, 1), 'r', label = 'mean +/- stdev')plt.semilogx (Nset, np.mean(I, 1) - np.std (I, 1) , 'r')plt.xlabel('number of samples')plt.ylabel('estimated integral')plt.legend()plt.savefig("Q5.png")Plot of Mean and Standard Deviation as Function of Sample Size

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Python Programming - Solutions

Probability & Statistics - Assignment