【问题标题】:Integration inconsistency with adaptIntegrate与 adaptIntegrate 的集成不一致
【发布时间】:2014-09-03 10:58:10
【问题描述】:

我正在估计一个物种在网格景观中分散的概率,给定一个具有最大分散距离的分散核(距离的函数)。我正在尝试计算等式中所述的区域到区域的扩散概率。 8 个this (open access) paper。这涉及四重积分,分别评估源和目标单元中源点和目标点的每个可能组合的分散函数的值。

我已经使用cubature 包中的adaptIntegrate 实现了这一点,如下所示,对于源单元 A、目标单元 B 和一个简化的分散内核,其中当点间距离 > 1.25 和 0 时分散为 1否则。如下图所示,其中单元格 B 的红色区域无法到达,因为单元格 A 中的任何点都在 1.25 的距离内。

library(cubature)
f <- function(xmin, xmax, ymin, ymax) {
  adaptIntegrate(function(x) {
    r <- sqrt((x[3] - x[1])^2 + (x[4] - x[2])^2)
    ifelse(r > 1.25, 0, 1)
  }, 
  lowerLimit=c(-0.5, -0.5, xmin, ymin), 
  upperLimit=c(0.5, 0.5, xmax, ymax), 
  maxEval=1e5)
}

f(xmin=1.5, xmax=2.5, ymin=-0.5, ymax=0.5)

# $integral
# [1] 0.01949567
# 
# $error
# [1] 0.001225998
# 
# $functionEvaluations
# [1] 100035
# 
# $returnCode
# [1] 0

在考虑目标单元格 C 时,我得到了一个不同的积分,该单元格 C 放置在距离相同的位置,但在单元格 A 的上方而不是右侧。

f(xmin=-0.5, xmax=0.5, ymin=1.5, ymax=2.5)

# $integral
# [1] 0.01016105
# 
# $error
# [1] 0.0241325
# 
# $functionEvaluations
# [1] 100035
# 
# $returnCode
# [1] 0

为什么这些积分如此不同(0.01949567 vs 0.01016105)?我编码不正确吗?更改容差和最大评估次数似乎没有太大区别。或者,是否有更好的方法来编写此类问题的解决方案?

我意识到有关一般方法的问题可能更适合 stats.stackexchange.com,但我已在此处发布,因为我怀疑编码本身可能会忽略某些内容。


编辑: 对于A -&gt; B 情况,嵌套的integrate 返回类似于第一个adaptIntegrate 解决方案的解决方案。对于A -&gt; C 情况,它返回Error in integrate(function(ky) { : the integral is probably divergent。
g <- function(Bx, By, Ax, Ay) {
  r <- sqrt((Ax - Bx)^2 + (Ay - By)^2)
  ifelse(r > 1.25, 0, 1)
}

integrate(function(Ay) {
  sapply(Ay, function(Ay) {
    integrate(function(Ax) {
      sapply(Ax, function(Ax) {
        integrate(function(By) {
          sapply(By, function(By) {
            integrate(function(Bx) g(Bx, By, Ax, Ay), 1.5, 2.5)$value # Bx
          })
        }, -0.5, 0.5)$value # By
      })
    }, -0.5, 0.5)$value # Ax
  })
}, -0.5, 0.5)$value # Ay

# [1] 0.019593

【问题讨论】:

    标签: r numerical-integration


    【解决方案1】:

    原因似乎是adaptIntegrate 的工作方式,因为很明显,您唯一更改的是集成顺序。不一致的结果可能是因为仅是近似积分(参见第一个回复here),但这似乎更像是一个错误。

    这里是计算f(xmin=1.5, xmax=2.5, ymin=-0.5, ymax=0.5)时r的值

    和f(xmin=-0.5, xmax=0.5, ymin=1.5, ymax=2.5)

    所以函数内部肯定发生了一些事情,因为值的范围有很大的不同。

    对此的一种替代方法是蒙特卡洛积分,这在这种情况下很好,因为您的点分布均匀。

    MCI <- function(Ax, Ay, Bx, By, N, r) {
      d <- sapply(list(Ax, Ay, Bx, By), function(l) runif(N, l[1], l[2]))
      sum(sqrt((d[, 1] - d[, 3])^2 + (d[, 2] - d[, 4])^2) <= r) / N
    }
    
    set.seed(123)
    MCI(c(-0.5, 0.5), c(-0.5, 0.5), c(1.5, 2.5), c(-0.5, 0.5), 100000, 1.25)
    # [1] 0.0194
    MCI(c(-0.5, 0.5), c(-0.5, 0.5), c(-0.5, 0.5), c(1.5, 2.5), 100000, 1.25)
    # [1] 0.01929
    

    【讨论】:

    • 谢谢@Julius - 看起来蒙特卡洛集成可能是要走的路。介意告诉我你是如何保持r 值的吗? (我可以cat 将它们写入文件,但有更快的方法吗?)如果其他人对adaptIntegrate 差异的原因有更深入的了解,我会将问题保持更长时间。
    • @jbaums,当然,我把rs &lt;- numeric(100035); cnt &lt;- 1放在函数前面,rs[cnt] &lt;&lt;- r; cnt &lt;&lt;- cnt + 1放在函数里面。
    【解决方案2】:

    通常距离度量是(x1-x2)^2+(y1-y2)^2。你能解释一下为什么在构造 r 时要从 y 中减去 x 吗?考虑替代版本:

    f <- function(xmin, xmax, ymin, ymax) {
       adaptIntegrate(function(x) {
         r <- sqrt((x[4] - x[3])^2 + (x[2] - x[1])^2)
         ifelse(r > 1.25, 0, 1)
       }, 
       lowerLimit=c(-0.5, -0.5, xmin, ymin), 
       upperLimit=c(0.5, 0.5, xmax, ymax), 
       maxEval=1e5)
     }
    
     f(xmin=1.5, xmax=2.5, ymin=-0.5, ymax=0.5)
    #-------------
    $integral
    [1] 0.01016105
    
    $error
    [1] 0.0241325
    
    $functionEvaluations
    [1] 100035
    
    $returnCode
    [1] 0
    #---------
     f(xmin=-0.5, xmax=0.5, ymin=1.5, ymax=2.5)
    #---------
    $integral
    [1] 0.01016105
    
    $error
    [1] 0.0241325
    
    $functionEvaluations
    [1] 100035
    
    $returnCode
    [1] 0
    

    【讨论】:

    • 我相信我计算的距离是正确的,因为 x 的元素顺序是由两个 *Limit 向量的元素顺序给出的。它们与沿四个维度中的每一个的位置有关,即当我们在第一和第二维度中从 -0.5 移动到 0.5(分别表示单元格 A 的 x 和 y 坐标的边界),来自xmin在第三个(单元格 B 的 x 边界)中到 xmax,在第四个(单元格 B 的 y 边界)中从 ymin 到 ymax。我的包装函数f 只是将xmin、xmax、ymin 和ymax 提供给adaptIntegrate。
    【解决方案3】:

    R cubature 包 (Naras) 的维护者告诉我,Cucture C 库给出的结果与我在上述问题中报告的结果相同,而且这不太可能是错误;相反,在某些情况下,h 自适应 cubature 例程(R 包是其接口)不如 Cubature 的 p 自适应例程准确,后者在适当的区域doubles the number of sampling points。

    Naras 还提供了以下 julia 代码,该代码为我的问题中提出的两种情况演示了一致的 pcubature 解决方案(返回值的元素是估计的积分,然后是估计的绝对误差)。

    using Cubature
    
    # integrand
    f = x -> ifelse(sqrt((x[3] - x[1])^2 + (x[4] - x[2])^2) > 1.25, 0, 1)
    
    # A to B case
    pcubature(f, [-0.5, -0.5, 1.5, -0.5], [0.5, 0.5, 2.5, 0.5], abstol=1e-5)    
    # (0.019593408732917292,3.5592555263398717e-6)
    
    # A to C case
    pcubature(f, [-0.5, -0.5, -0.5, 1.5], [0.5, 0.5, 0.5, 2.5], abstol=1e-5)   
    # (0.019593408732918302,3.559255527241928e-6)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2020-12-30
      相关资源
      最近更新 更多