Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

python - scipy quad uses only 1 subdivision and gives wrong result

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...