Integrate: the integral is probably divergent

后端 未结 2 970
遥遥无期
遥遥无期 2021-01-19 23:50

I was doing some integration into a loop using integrate and I come up with an error I can\'t understand neither get rid of. Here is a MWE I could extract:

相关标签:
2条回答
  • 2021-01-19 23:58

    I'd use this work-around:

    integrate(f = function(v){pnorm(v, mean = m, sd = s, lower.tail =  FALSE)}, 
          max(u_min,m-10*s),min(u_max,m+10*s))$value  + (u_min-m+10*s)*(u_min<m+10*s)
    

    What I've done:

    • pnorm with lower.tail=FALSE is basically zero when very far on the right from the mean. So there is no point in "stretching" the right limit of the integral. So, when u_max > m+10*s, you just integrate to m + 10*s. You can of course change the 10 factor to add precision;
    • on the other hand, on the left pnorm is basically always 1; so you can enhance the left limit and the missing part is just u_min - m+10*s. Same logic as above.
    0 讨论(0)
  • 2021-01-20 00:03

    The default tolerance of .Machine$double.eps^0.25 (= 0.0001220703) needs to be lowered. Try, for example, this:

    f <- function(v) pnorm(v, mean = m, sd = s, lower.tail =  FALSE)
    integrate(f, u_min, u_max, rel.tol = 1e-15)
    
    ## 0.0009421867 with absolute error < 1.1e-17
    
    0 讨论(0)
提交回复
热议问题