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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…