【问题标题】:Numerical integration over mean of normal in scipyscipy中正常平均值的数值积分
【发布时间】:2014-11-27 22:07:53
【问题描述】:

在集成普通 pdf 时,我从 scipy 集成.quad 函数中得到了一些奇怪的输出。这是我尝试使用的代码:

inpdf = lambda c: norm.pdf(50, loc=c, scale = 1)
result = integrate.quad(inpdf, -np.inf, np.inf)
print result

返回 (3.281718223506531e-99, 0.0) 这显然是错误的。 当我将 x 值更改为 0 时,在下面的 sn-p 中,我得到了适当的输出:

inpdf = lambda c: norm.pdf(0, loc=c, scale = 1)
result = integrate.quad(inpdf, -np.inf, np.inf)
print result

返回 (0.9999999999999998, 1.0178191320905743e-08) 非常接近 1,这是应该的。使用 R 的积分函数会发生完全相同的事情,所以也许这只是正交算法的一个已知属性?有谁知道为什么会这样?

【问题讨论】:

  • 两个代码 sn-ps 是相同的 - 请编辑。
  • sum(stats.norm.pdf(50, loc=arange(0,100,0.01), scale = 1)) * 0.01 -> 1

标签: python r numpy scipy numerical-integration


【解决方案1】:

至少在 R 中,并且可能在 scipy 中,这个问题相当简单。

您要求程序做的是在整个实线上集成一个功能。求积算法试图找到函数在哪里是非零的,在普通 pdf 的情况下无处不在,但重要的部分很难找到。

在 R 的情况下,集成函数在多个点上进行评估,默认情况下为 100,并且很可能它们不会出现在您的函数相当大的小区间内。相反,如果您尝试了类似

> integrate(dnorm,lower=45,upper=55,mean=50)
0.9999994 with absolute error < 8.7e-10

你得到正确的结果或足够接近。如果你走得更远,求积算法又开始失败

> integrate(dnorm,lower=-1000,upper=1000,mean=50)
0 with absolute error < 0

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 2012-06-19
    • 2016-07-11
    • 2016-08-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多