1

I'm trying to perform the integration of f(x) = 42*log(2)*2^(-t) from x = zero to t for different values of t as follows:

import numpy as np
from scipy.integrate import quad
reward = lambda t: 41*np.log(2)*np.power(2,-t)
quad(reward, 0, t)

However, sometimes I get a underflow FloatingPointError for t=13713.773637630298. If I do this in an interactive session, I see the following (which does not produce an underflow error):

# Sanity Checks
>>> quad(reward, 0, 0)
(0.0, 0.0)
>>> quad(reward, 0, np.inf)
(40.99999999999978, 1.2750366198868279e-07)
# Weird Stuff
>>> quad(reward, 0, 13000)
(41.00000000000001, 4.661522155502418e-07)
>>> quad(reward 0, 130000)
(3.511099523711482e-39, 6.981138006465757e-39)

My questions are:

  • Why does the value approach zero for increasing values of t?
  • Why do I get an underflow error when I run the module outside interactive mode, which I am unable to reproduce with an interactive session?

Related: scipy.integrate.quad gives wrong result on large ranges

Community
  • 1
  • 1
CoconutBandit
  • 476
  • 1
  • 3
  • 13
  • `quad` uses different integration rules if you pass it an infinite interval (it computes a Fourier integral). If you pass it a finite range, the integration is done using the Clenshaw-Curtis method. The Fourier integral doesn't evaluate the integrand at arbitrarily high values of the integrand, but the Clenshaw-Curtis method can if one of your limits is a large number. This can cause underflow like you are seeing. – Angus Williams Oct 05 '16 at 18:43
  • Thanks, I still find it interesting that one method produces an floating point exception while interactive mode does not. I also found a related question that might help others that details what you said. – CoconutBandit Oct 05 '16 at 18:48
  • A similar question: http://stackoverflow.com/questions/39515582/scipy-integrate-quad-precision-on-big-numbers – Warren Weckesser Oct 05 '16 at 19:49

0 Answers0