我知道Python不是速度最快的语言,但我试图估计参数并模拟计算统计数据,但这需要很多时间。有没有什么方法可以优化这段代码,让它在Python上运行得更快?重复100次花了我25分钟,我想做10000次。
from numpy import log, array, random, append
from scipy.stats import expon, kurtosis, skew
from pygosolnp import solve
from tabulate import tabulate
from time import time
inicial_time = time()
def simulation(n, re, alpha):
def exp2(o):
return -sum(log(expon.pdf(v, scale=o)))
mean = array(["Mean"])
variance = array(["Variance"])
bias = array(["Bias"])
eqm = array(["EQM"])
skewness = array(["Skewness"])
kurtose = array(["Kurtose"])
for i in n:
param = array([])
for j in range(re):
v = random.exponential(alpha, size=i)
param = append(param, array(solve(exp2, [0], [10]).best_solution.parameters))
med = param.mean()
varia = param.var()
b = Alpha - med
eqma = b ** 2 + varia
skewn = skew(param)
kur = kurtosis(param)
mean = append(mean, med)
variance = append(variance, varia)
bias = append(bias, b)
eqm = append(eqm, eqma)
skewness = append(skewness, skewn)
kurtose = append(kurtose, kur)
data = [mean, variance, bias, eqm, skewness, kurtose]
print(tabulate(data, headers=["Statistics", "n = 30", "n = 50", "n = 100", "n = 200", "n = 300"]))
N = [30, 50, 100, 200, 300]
RE = 100
Alpha = 1/5
simulation(N, RE, Alpha)
print(f'{time()-inicial_time} seconds')
1条答案
按热度按时间kmpatx3s1#
除了你得到的有帮助的评论之外,我认为指数函数也是一个需要计算机计算时间的函数。您可以使用JIT编译与Numba来使计算更快。它是Python的即时(JIT)编译器,将Python和NumPy代码的子集转换为快速机器码。你可以这样做来使用它:
这将在调用exp2()时保存一些时间。