scipy quad 仅使用 1 个细分并给出错误的结果 [英] scipy quad uses only 1 subdivision and gives wrong result

查看:40
本文介绍了scipy quad 仅使用 1 个细分并给出错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 quad 来获得高斯分布的均值.我的第一次尝试和第二次尝试得到不同的结果.而quad的第二次尝试只使用了1个细分.

I want to use quad to get the mean of a Gaussian distribution. My first try and 2nd try gets different result. And the 2nd try of quad uses only 1 subdivision.

mu =1 
sigma =2 
import scipy as sp
import scipy.integrate as si
import scipy.stats as ss

f = lambda x: x * ss.norm(loc=mu, scale=sigma).pdf(x)
a = si.quad(f, -999., 1001., full_output=True)
print a[0]
#print sum(a[2]["rlist"][:a[2]["last"]])
print a[2]["last"]

b = si.quad(f, -1001., 1001., full_output=True)
print b[0]
#print sum(b[2]["rlist"][:b[2]["last"]])
print b[2]["last"]

print sorted(a[2]["alist"][:a[2]["last"]])
print sorted(b[2]["alist"][:b[2]["last"]])

这是输出:

1.0
16
0.0
1
[-999.0, -499.0, -249.0, -124.0, -61.5, -30.25, -14.625, -6.8125, 1.0, 8.8125, 16.625, 32.25, 63.5, 126.0, 251.0, 501.0]
[-1001.0]

我有没有搞错?

推荐答案

因为积分的极限在 Gaussian 的尾部太远了,你已经欺骗 quad 认为函数相同为 0:

Because the limits of integration are so far out in the tails of the Gaussian, you've fooled quad into thinking that the function is identically 0:

In [104]: f(-1000)
Out[104]: -0.0

In [105]: f(-500)
Out[105]: -0.0

In [106]: f(-80)
Out[106]: -0.0

In [107]: f(-50)
Out[107]: -6.2929842629835128e-141

您可以通过多种方式解决此问题,其中一种方法是将参数 points=[mu] 添加到对 quad 的调用中:

You can fix this several ways, one of which is to add the argument points=[mu] to the call to quad:

In [110]: b = si.quad(f, -1001., 1001., full_output=True, points=[mu])
b
In [111]: b[0]
Out[111]: 1.0000000000000002

这篇关于scipy quad 仅使用 1 个细分并给出错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆