scipy quad uses only 1 subdivision and gives wrong result

萝らか妹 提交于 2019-12-31 03:39:06

问题


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"]])

Here is the output:

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]

Do I make any mistake?


回答1:


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

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


来源:https://stackoverflow.com/questions/29179778/scipy-quad-uses-only-1-subdivision-and-gives-wrong-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!