scipy.integrate.quad precision on big numbers
<p>I try to compute such an integral (actually cdf of exponential distribution with its pdf) via <code>scipy.integrate.quad()</code>:</p>

<pre><code>import numpy as np
from scipy.integrate import quad

def g(x):
    return .5 * np.exp(-.5 * x)

print quad(g, a=0., b=np.inf)
print quad(g, a=0., b=10**6)
print quad(g, a=0., b=10**5)
print quad(g, a=0., b=10**4)
</code></pre>

<p>And the result is as follows:</p>

<pre><code>(1.0, 3.5807346295637055e-11)
(0.0, 0.0)
(3.881683817604194e-22, 7.717972744764185e-22)
(1.0, 1.6059202674761255e-14)
</code></pre>

<p>All the attempts to use a big upper integration limit yield an incorrect answer though the usage of <code>np.inf</code> solves the problem.</p>

<p>Similiar case is discussed in <a href="https://github.com/scipy/scipy/issues/5428" rel="nofollow">scipy issue #5428 at GitHub</a>.</p>

<p>What should I do to avoid such an error in integrating other density functions?</p>

<p>Use the <code>points</code> argument to tell the algorithm where the support of your function roughly is:</p>

<pre><code>import numpy as np
from scipy.integrate import quad

def g(x):
    return .5 * np.exp(-.5 * x)

print quad(g, a=0., b=10**3, points=[1, 100])
print quad(g, a=0., b=10**6, points=[1, 100])
print quad(g, a=0., b=10**9, points=[1, 100])
print quad(g, a=0., b=10**12, points=[1, 100])
</code></pre>

python numpy scipy calculus