quad

scipy.integrate.quad gives wrong result on large ranges

允我心安 提交于 2019-11-27 07:56:34
问题 I am trying to integrate over the sum of two 'half' normal distributions. scipy.integrate.quad works fine when I try to integrate over a small range but returns 0 when I do it for large ranges. Here's the code: mu1 = 0 mu2 = 0 std1 = 1 std2 = 1 def integral_fun(x): nor1 = 0.5 * ((1 / (np.sqrt(2 * np.pi) * std1)) * (np.e ** ((-(x-mu1) ** 2) / (2 * std1 **2)))) nor2 = 0.5 * ((1 / (np.sqrt(2 * np.pi) * std2)) * (np.e ** ((-(x-mu2) ** 2) / (2 * std2 **2)))) return nor1 + nor2 integrate.quad

NumPy vectorization with integration

三世轮回 提交于 2019-11-26 21:53:35
问题 I have a vector and wish to make another vector of the same length whose k-th component is The question is: how can we vectorize this for speed? NumPy vectorize() is actually a for loop, so it doesn't count. Veedrac pointed out that "There is no way to apply a pure Python function to every element of a NumPy array without calling it that many times". Since I'm using NumPy functions rather than "pure Python" ones, I suppose it's possible to vectorize, but I don't know how. import numpy as np