AdaptIntegrate 与集成的性能
我想在一维中执行数值积分,其中被积函数是向量值。 integrate()
只允许标量被积函数,因此我需要多次调用它。 cubature 包似乎很适合,但对于一维积分来说它的表现似乎很差。考虑以下示例(标量值被积函数和一维积分),
library(cubature)
integrand <- function(x, a=0.01) exp(-x^2/a^2)*cos(x)
Nmax <- 1e3
tolerance <- 1e-4
# using cubature's 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
不知何故,adaptIntegrate
似乎使用更多的函数评估来获得类似的精度。两种方法显然都使用 Gauss-Kronrod 求积(一维情况:15 点高斯求积规则),尽管 ?integrate
添加了“Wynn 的 Epsilon 算法”。这能解释巨大的时间差异吗?
我愿意接受处理向量值被积函数的替代方法的建议,例如
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。
I'd like to perform a numerical integration in one dimension, where the integrand is vector-valued. integrate()
only allows scalar integrands, thus I would need to call it several times. The cubature
package seems well suited, but it seems to perform quite poorly for 1D integrals. Consider the following example (scalar-valued 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's 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
Somehow, adaptIntegrate
seems to be using many more function evaluations for a similar precision. Both methods apparently use Gauss-Kronrod quadrature (1D case: 15-point Gaussian quadrature rule), though ?integrate
adds a "Wynn's Epsilon algorithm". Would that explain the large timing difference?
I'm open to suggestions of alternative ways of dealing with vector-valued 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CRAN 中还有 R2Cuba 包,它实现了几种多维集成算法:
我尝试使用您的示例函数对此进行测试,在这样一个简单的情况下,我无法让所有算法都工作(尽管我没有真正努力尝试),并且我确实工作的方法很少比 adaptIntegrate 慢得多与默认设置,但也许在您真正的应用程序中这个包值得尝试。
There is also R2Cuba package in CRAN which implements several multidimensional integration algorithms:
I tried to test this with your example function, and in such a simple case I couldn't get all the algorithms to work (although I didn't try really hard), and few methods I did get to work were considerably slower than
adaptIntegrate
with default setting, but perhaps in your true application this package could be worth of trying.