Adaptive Integration Performance with Integration

I would like to perform numerical integration in one dimension , where the integrand is a vector value . integrate() only allows scalar integrands, so I will need to call it several times. The cubature package seems well suited, but it seems to work pretty poorly for 1D integrals. Consider the following example (scalar integrand and 1D integration),

 library(cubature) integrand <- function(x, a=0.01) exp(-x^2/a^2)*cos(x) Nmax <- 1e3 tolerance <- 1e-4 # using cubature adaptIntegrate time1 <- system.time(replicate(1e3, { a <<- adaptIntegrate(integrand, -1, 1, tol=tolerance, fDim=1, maxEval=Nmax) }) ) # using integrate time2 <- system.time(replicate(1e3, { b <<- integrate(integrand, -1, 1, rel.tol=tolerance, subdivisions=Nmax) }) ) time1 user system elapsed 2.398 0.004 2.403 time2 user system elapsed 0.204 0.004 0.208 a$integral > [1] 0.0177241 b$value > [1] 0.0177241 a$functionEvaluations > [1] 345 b$subdivisions > [1] 10 

Be that as it may, adaptIntegrate seems to use many more functions for similar accuracy. Both methods seem to use the Gauss-Kronrod quadratic form (1D case: 15-point Gaussian quadrature rule), although ?integrate adds the “Wynn Epsilon algorithm”. Will this explain the big time difference?

I am open to suggestions on alternative ways to handle vector integrands such as

 integrand <- function(x, a = 0.01) c(exp(-x^2/a^2), cos(x)) adaptIntegrate(integrand, -1, 1, tol=tolerance, fDim=2, maxEval=Nmax) $integral [1] 0.01772454 1.68294197 $error [1] 2.034608e-08 1.868441e-14 $functionEvaluations [1] 345 

Thanks.

+6
source share
1 answer

There is also an R2Cuba package in CRAN that implements several multidimensional integration algorithms:

I tried to check this with your sample function, and in such a simple case I couldn’t get all the algorithms to work (although I didn’t try very hard), and several methods that I really worked were much slower than adaptIntegrate with the default setting, but maybe in your true application this package might be worth a try.

+2
source

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


All Articles