【问题标题】:adaptIntegrate upper limit in R not working?R中的adaptIntegrate上限不起作用?
【发布时间】:2018-06-19 15:49:07
【问题描述】:

我有一个二维函数,需要将其中一个变量从 0 积分到无穷大:

library(cubature)

k = 0.5
m = 0.05

integrand = function(variables) {

    x = variables[1]
    r = variables[2]

    p = sqrt(x*(1 - x))*exp((-1/2)*(((k**2)*x*(1 - x)*(r**2)) + ((m**2)/((k**2)*x*(1 - x)))))
    d.p = -((x*(1 - x))**(3/2))*(k**2)*r*exp((-1/2)*(((k**2)*x*(1 - x)*(r**2)) + ((m**2)/((k**2)*x*(1 - x)))))

    return (2*pi*r*(1/(4*(x**2)*((1 - x)**2)))*(((x**2 + (1 - x)**2)*(d.p**2)) + (m*p)**2))

}

adaptIntegrate 但是,不能以Inf 作为上限,所以我尝试与大量集成。下面从r = 0 到r = 45 的图表建议45 是一个很好的上限。

但是,下面的代码

adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 45))$integral

没有给我预期的结果。当我增加上限时,结果会减小,直到最终给出 0。

我尝试在其他函数上使用较大的上限而不是 Inf,无论我将上限设置多高,它似乎都可以工作。这次失败这么多是有原因的吗,有没有办法正确集成这个功能?

【问题讨论】:

  • 你想要的结果是什么?递减现象在数值积分中很常见,因为数值积分只是在某个算法选择的某些点上评估函数,并使用样条或类似的东西来逼近整个函数并计算逼近函数的积分,如果范围是太大,近似函数可以包含负值!从而降低了集成度。
  • “return”开头的行中缺少括号。请在以后测试您的代码。
  • @Consistency 我想要的结果是 6.96。我不相信我的功能在任何地方都会变成负面的;我增加了我的情节的范围,我没有看到任何负值,只有 0 随着 r 的增加和负指数的减少。不过,感谢您对集成方法的深入了解!
  • @42- 哦,你是对的。我已经修好了,抱歉。

标签: r numerical-integration


【解决方案1】:

这是一个 hcubature 的包装器,它可以接受无限的限制:

hcubature.inf <- function() {
  cl <- match.call()
  cl[[1L]] <- quote(cubature::hcubature)
  if(all(is.finite(c(lowerLimit,upperLimit)))) return(eval.parent(cl))

  # convert to new coordinates to incorporate infinities                                                             
  cl[['upperLimit']] <- atan(upperLimit)
  cl[['lowerLimit']] <- atan(lowerLimit)
  # wrap the coordinate transformation                                                                               
  if(!vectorInterface) {
    newfun <- function(x,...) {
      z <- tan(x)
      jac <- rep(prod(1/cos(x))^2, fDim)
      f(z,...) * jac
    }
  } else {
    newfun <- function(x,...) {
      z <- tan(x)
      jac <- rep(apply(x,2,function(x) prod(1/cos(x)))^2, each=fDim)
      f(z,...) * jac
    }
  }
  cl[['f']] <- newfun
  eval.parent(cl)
}
formals(hcubature.inf) <- formals(hcubature)

hcubature.inf(integrand, c(0,0), c(1,Inf))$integral
[1] 6.450675

【讨论】:

    【解决方案2】:

    您似乎有一定的权威认为这个多项式函数应该保持正数。在提供缺少的括号并运行一些测试用例后,我怀疑您在将表达式转录为 R 时出错了。

    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 45))$integral
    [1] 6.3301
    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, Inf))$integral
    [1] NaN
    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 300))$integral
    [1] 6.4436
    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 600))$integral
    [1] 6.4436
    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 6000))$integral
    [1] 2.7391
    # backing up a bit .......
    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 3000))$integral
    [1] 6.4495
    

    您的被积函数在 y 轴上的 3000 到 6000 之间的某个位置似乎与零交叉。 似乎您的函数在积分网格上评估的许多点处返回了 NaN 值。我随着有限值增加,看到结果在第二个限制的广泛值范围内保持为 0。数值积分由于其有限的性质而具有一些固有的局限性:

    > adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 1000000000))$integral
    [1] 0
    

    我相信我已经读过adaptIntegrate 如果结果的梯度太浅,有时会失败。我似乎记得它名称的“适应”部分意味着它应用一些启发式方法来搜索参数空间的适当部分,这取决于被积函数值的变化。

    hcubature method is the default. The help page suggests that usingpcubature` 将应用更严格的测试:

    > hcubature(integrand, lowerLimit = c(0.00001, 0.1), upperLimit = c(.9999999,360))$integral
    [1] 6.4489
    > pcubature(integrand, lowerLimit = c(0.00001, 0.1), upperLimit = c(.9999999,360))$integral
    [1] 6.4505                  # value increased with same arguments
    

    根据这个情节,我认为你提出的上限太胆小了:

    这是我能做到的最好的:

     pcubature(integrand, lowerLimit = c(0.0000000001, 0.000000001), 
                          upperLimit = c(.99999999999999, 360))$integral
    [1] 6.4507
    

    【讨论】:

      【解决方案3】:

      一种可能的解决方案是,由于您无法确定正确的上界,您可以进行一些数学变换:更改变量以将范围从 0 到 Inf 更改为 0 到 1 之类的值。积分理论上应该相同.例如,

      library(cubature)
      
      k = 0.5
      m = 0.05
      
      integrand = function(variables) {
      
          x = variables[1]
          r = variables[2]
      
          p = sqrt(x*(1 - x))*exp((-1/2)*(((k**2)*x*(1 - x)*(r**2)) + ((m**2)/((k**2)*x*(1 - x)))))
          d.p = -((x*(1 - x))**(3/2))*(k**2)*r*exp((-1/2)*(((k**2)*x*(1 - x)*(r**2)) + ((m**2)/((k**2)*x*(1 - x)))))
      
          return(2*pi*r*(1/(4*(x**2)*((1 - x)**2)))*(((x**2 + (1 - x)**2)*(d.p**2)) + (m*p)**2))
      
      }
      
      integrand1 <- function(variables){
          x <- variables[1]
          ## y is the variable from 0 to 1
          y <- variables[2]
          ## r is 0 to Inf by this transformation
          r <- y / (1 - y)
          ## the new function will be the original function times the Jacobian
          ## i.e. the derivative of r = y / (1 - y)
          integrand(c(x, r)) / (1 - y) ^ 2
      }
      
      ## adaptIntegrate(integrand, lowerLimit = c(0, 0), upperLimit = c(1, 45))$integral
      
      adaptIntegrate(integrand1, lowerLimit = c(0, 0), upperLimit = c(1, 1))$integral
      

      而我得到的结果是 6.450692,和 42- 的结果差不多,但和你想要的结果不接近。

      【讨论】:

        猜你喜欢
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 2017-02-25
        • 2018-02-04
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        相关资源
        最近更新 更多