【问题标题】:performance of adaptIntegrate vs. integrate适应集成与集成的性能
【发布时间】:2011-12-17 00:16:20
【问题描述】:

我想在一维中执行数值积分,其中被积函数是向量值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

谢谢。

【问题讨论】:

  • 我不明白,对不起;我对标量值被积函数进行的一对一比较有什么问题?
  • 我用fDim=2 进行了测试(最后一个例子,345 次评估),比较只是调用integrate 两次的情况,str(lapply(c(integrand1, integrand2), integrate, -1,1, rel.tol=tolerance, subdivisions=Nmax)) 给出 10+1 = 11 次评估。我的观点是,是的,adaptIntegrate 的目标是多维积分,并且可以选择向量值被积函数,但是一维积分的情况比反复调用integrate 效率低得多,但幅度很大(~30 倍在这里)。
  • 你见过这个包吗:cran.r-project.org/web/packages/R2Cuba
  • 我没有,谢谢指点。
  • @Hemmo 是否愿意将其转化为答案并在它被浪费之前获得赏金?

标签: r numerical-integration


【解决方案1】:

CRAN 中还有R2Cuba 包,它实现了几种多维积分算法:

我尝试使用您的示例函数对此进行测试,但在这种简单的情况下,我无法让所有算法都正常工作(尽管我并没有真正努力),而且我真正开始工作的方法很少比默认设置下的adaptIntegrate 慢,但也许在你的真实应用程序中这个包值得一试。

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 2015-08-03
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多