I am wanting to generate eight random numbers within a range (0 to pi/8), add them together, take the sine of this sum, and after doing this N times, take the mean result. After scaling this up I get the correct answer, but it is too slow for N > 10^6, especially when I am averaging over N trials n_t = 25 more times! I am currently getting this code to run in around 12 seconds for N = 10^5, meaning that it will take 20 minutes for N = 10^7, which doesn't seem optimal (it may be, I don't know!).
My code is as follows:
import random
import datetime
from numpy import pi
from numpy import sin
import numpy
t1 = datetime.datetime.now()
def trial (N):
total = []
uniform = numpy.random.uniform
append = total.append
for j in range (N):
sum = 0
for i in range (8):
sum+= uniform(0, pi/8)
append(sin(sum))
return total
N = 1000000
n_t = 25
total_squared = 0
ans = []
for k in range (n_t):
total = trial(N)
f_mean = (numpy.sum(total))/N
ans.append(f_mean*((pi/8)**8)*1000000)
sum_square = 0
for e in ans:
sum_square += e**2
sum = numpy.sum(ans)
mean = sum/n_t
variance = sum_square/n_t - mean**2
s_d = variance**0.5
print (mean, " ± ", s_d)
t2 = datetime.datetime.now()
print ("Execution time: %s" % (t2-t1))
If anyone can help me optimise this it would be much appreciated!
Thank you :)
Aucun commentaire:
Enregistrer un commentaire