首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >集成函数在之前的工作后返回舍入误差

集成函数在之前的工作后返回舍入误差
EN

Stack Overflow用户
提问于 2019-05-31 03:14:07
回答 1查看 2.5K关注 0票数 1

当使用integrate从2000 -> Inf中对对数正态密度函数进行积分时,返回一个错误。我以前使用过一个非常类似的方程,没有任何问题。

我已尝试禁用出错时停止,并将rel.tol设置得更低。我是一个新手,对r也不熟悉,所以如果这两个人都没有做过任何事情,我道歉。

代码语言:javascript
运行
复制
> integrand = function(x) {(x-2000)*(1/x)*(1/(.99066*((2*pi)^.5)))*exp(-((log(x)-7.641)^2)/((2*(.99066)^2)))}
> integrate(integrand,lower=2000,upper=Inf)
1854.002 with absolute error < 0.018
#returns value fine

> integrand = function(x) {(x-2000)*(1/x)*(1/(1.6247*((2*pi)^.5)))*exp(-((log(x)-9.0167)^2)/((2*(1.6247)^2)))}
> integrate(integrand,lower=2000,upper=Inf)
Error in integrate(integrand, lower = 2000, upper = Inf) : 
  roundoff error is detected in the extrapolation table
#small change in the mu and sigma in the lognormal density function results in roundoff error

> integrate(integrand,lower=1293,upper=Inf)
29005.08 with absolute error < 2
#integrating on lower bound works fine, but having lower=1294 returns a roundoff error again
> integrate(integrand,lower=1294,upper=Inf)
Error in integrate(integrand, lower = 1294, upper = Inf) : 
  roundoff error is detected in the extrapolation table

我应该得到一个值,不是吗?我很难理解对值进行非常轻微的更改会如何导致函数不再集成。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-31 04:12:59

首先,我认为你在定义被积函数时会很复杂,因为你写下了整个表达式,使用内置的dlnorm函数似乎更好。

代码语言:javascript
运行
复制
g <- function(x, deduce, meanlog, sdlog){
  (x - deduce) * dlnorm(x, meanlog = meanlog, sdlog = sdlog)
}

curve(g(x, deduce = 2000, meanlog = 9.0167, sdlog = 1.6247), 
      from = 1294, to = 1e4)

至于集成问题,当integrate失败时,package cubature通常会做得更好。以下所有操作都会生成结果,并且没有错误。

代码语言:javascript
运行
复制
library(cubature)

cubintegrate(g, lower = 1293, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)

cubintegrate(g, lower = 1294, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)

cubintegrate(g, lower = 2000, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56384330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档