python - scipy quad uses only 1 subdivision and gives wrong result -
i want use quad mean of gaussian distribution. first try , 2nd try gets different result. , 2nd try of quad uses 1 subdivision.
mu =1 sigma =2 import scipy sp import scipy.integrate si import scipy.stats ss f = lambda x: x * ss.norm(loc=mu, scale=sigma).pdf(x) = 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 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 make mistake?
because limits of integration far out in tails of gaussian, you've fooled quad
thinking function 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 several ways, 1 of add argument points=[mu]
call quad
:
in [110]: b = si.quad(f, -1001., 1001., full_output=true, points=[mu]) b in [111]: b[0] out[111]: 1.0000000000000002
Comments
Post a Comment