4

From scipy reference manual, dblquad is mathematically equivalent to repeated quad twice. Initially, I thought dblquad must have performance advantage over twice quad (besides the convenience of the method). To my surprise, it seems dblquad performance is even worse. I took examples from "SciPy Reference Guide, Release 0.14.0" pages 12-13 with some modifications:

import scipy
import math
import timeit

def integrand(t, n, x):
    return math.exp(-x*t) / t**n

def expint(n, x):
    return scipy.integrate.quad(integrand, 1, scipy.Inf, args=(n, x))[0]

def I11():
    res = []
    for n in range(1,5):
        res.append(scipy.integrate.quad(lambda x: expint(n, x), 0, scipy.Inf)[0])
    return res

def I2():
    res = []
    for n in range(1,5):
        res.append(scipy.integrate.dblquad(lambda t, x: integrand(t, n, x), 0, scipy.Inf, lambda x: 1, lambda x: scipy.Inf)[0])
    return res

print('twice of quad:')
print(I11())
print(timeit.timeit('I11()', setup='from __main__ import I11', number=100))
print('dblquad:')
print(I2())
print(timeit.timeit('I2()', setup='from __main__ import I2', number=100))

My outputs look like this:

twice of quad:
[1.0000000000048965, 0.4999999999985751, 0.33333333325010883, 0.2500000000043577]
5.42371296883
dblquad:
[1.0000000000048965, 0.4999999999985751, 0.33333333325010883, 0.2500000000043577]
6.31611323357

We see the two methods produce the same results (exact results should be 1, 1/2, 1/3, 1/4). But the dblquad performs worse.

Does someone have some insight what is going on with dblquad? I also have the same question for tplquad and nquad.

sunheng
  • 43
  • 4
  • Just to make sure Inf (improper integrations) does not cause the problem, I changed Inf to a number like 5, 10, and 50. The outputs show dblquad spends about 15% more time than double quad. – sunheng Oct 19 '14 at 06:09

1 Answers1

3

Have a look at the source code. It's clear that dblquad is just a repeated integration, just like what you're doing here.

Re efficiency: scipy versions >0.14 might be better for multivariate functions, see https://github.com/scipy/scipy/pull/3262

ev-br
  • 24,968
  • 9
  • 65
  • 78
  • Thanks to Zhenya for the answer. (But I cannot vote.) I will try a future's version when it is available. I am using 0.13 packed in Ubuntu (Linux Mint). It is still a puzzle why dblquad is slower. I would expect it to have the same performance as repeated quad, if not better. But I would be that picky. Just wait to test the improvement Zhenya quoted in future. – sunheng Oct 21 '14 at 03:43
  • @sunheng despite you cannot vote yet, considering accepting the answer if it fits your needs... – Saullo G. P. Castro Oct 25 '14 at 10:33
  • @sunheng for a True multidimensional numerical integration method, consider the [Cubature](http://ab-initio.mit.edu/wiki/index.php/Cubature) package, available in programming many languages... – Saullo G. P. Castro Oct 25 '14 at 10:34
  • Saullo Castro, Thank you for your python wrapper of Cubature, and the comment on accepting the answer. Now I saw a white check for that -- did not know this before. – sunheng Oct 28 '14 at 01:02