The limits of quad integration in scipy with np.inf

Here is an example with the results:

I integrates over the Gaussian distribution (mu = 800, sigma = 1) with ~ + -2sigma ppf and the same integral from -infto +inf. For some reason, the second integral leads to zero, but in practice it should be more accurate.

Can someone explain why such an anomaly occurs or where I made a mistake?

code:

from scipy.integrate import quad
import numpy as np
from scipy.stats import norm

def integrand(x):
    return x*norm.pdf(x, 800, 1)
print quad(integrand, norm.ppf(0.05, 800,1), norm.ppf(0.95, 800,1))
print quad(integrand, -np.inf, np.inf)

(719.9999999999894, 5.913323331834147e-11)
(0.0, 0.0) 

EDIT: By the way, when the average is small (like 2), it works great - the results of both integrals are very close.

+4
source share
2 answers

quad , . , . .

quad, " ", :

>>> quad(integrand,0,1000)
(3.8929062783235445e-32, 7.210678067622767e-32)
>>> quad(integrand,0,1000,points=[750])
(799.9999999999993, 2.0260999142842332e-07)

quad full_output:

>>>quad(integrand,0,1000,full_output=True)[2]['rlist'].max()
3.8929062783235445e-32

quad , 1-31, .

+1

, .

- -inf inf, quad 800, .

, , , , quad , , , , . (, - , ). , : ( ). , (, , ).

0
source

Source: https://habr.com/ru/post/1624966/


All Articles