让F(x; s) 是正态(即高斯)分布的 CDF
标准差s。你在计算
F(x1;s) - F(x0;s),其中x0 = 1e-3 和x1 = 0.3。
这可以重写为S(x0;s) - S(x1;s),其中S(x;s) = 1 - F(x;s) 是
“生存功能”。
您可以使用scipy.stats 的norm 对象的sf 方法计算此值。
In [99]: x0 = 1e-3
In [100]: x1 = 0.3
In [101]: s = 9.5e-5
In [102]: from scipy.stats import norm
In [103]: norm.sf(x0, scale=s)
Out[103]: 3.2671026385171459e-26
In [104]: norm.sf(x1, scale=s)
Out[104]: 0.0
注意norm.sf(x1, scale=s) 给出 0。这个表达式的确切值是
小于可以表示为 64 位浮点值的数字(正如@Zhenya 在评论中指出的那样)。
所以这个计算给出的答案是 3.267e-26。
您也可以使用scipy.special.ndtr 计算它。 ndtr 计算标准正态分布的 CDF,并通过对称性计算 S(x; s) = ndtr(-x/s)。
In [105]: from scipy.special import ndtr
In [106]: ndtr(-x0/s)
Out[106]: 3.2671026385171459e-26
如果您想使用数值积分获得相同的结果,则必须尝试积分算法的误差控制参数。例如,为了使用scipy.integrate.romberg 得到这个答案,我调整了divmax 和tol,如下:
In [60]: from scipy.integrate import romberg
In [61]: def integrand(x, s):
....: return np.exp(-0.5*(x/s)**2)/(np.sqrt(2*np.pi)*s)
....:
In [62]: romberg(integrand, 0.001, 0.3, args=(9.5e-5,), divmax=20, tol=1e-30)
Out[62]: 3.2671026554875259e-26
对于scipy.integrate.quad,它需要告诉它 0.002 是一个需要更多工作的“特殊”点:
In [81]: from scipy.integrate import quad
In [82]: p, err = quad(integrand, 0.001, 0.3, args=(9.5e-5,), epsabs=1e-32, points=[0.002])
In [83]: p
Out[83]: 3.267102638517144e-26
In [84]: err
Out[84]: 4.769436484142494e-37