integrate() gives totally wrong number

后端 未结 4 1211
傲寒
傲寒 2021-01-12 00:02

integrate() gives horribly wrong answer:

integrate(function (x) dnorm(x, -5, 0.07), -Inf, Inf, subdivisions = 10000L)
# 2.127372e-23 with absolute error <         


        
4条回答
  •  既然无缘
    2021-01-12 00:53

    Expanding a little further on @r2evan's and @Limey's comments:

    @Limey: for very general problems like this, there is simply no way to guarantee a generic solution.

    One way to solve such problem is to use more knowledge of the properties of the integrand (@r2evans's answer); the answer referenced by @Limey goes into detail for a different problem.

    One "gotcha" that you may not have thought of is that trying out a bunch of generic methods, tuning settings, etc. may mislead you into concluding that some settings/methods are generically better than the first one you tried that failed to get the right answer. (Methods that work may work better because they're generically better, but trying them on one example doesn't prove it!)

    As an example, the description of pcubature() (in ?cubature::pcubature says

    This algorithm is often superior to h-adaptive integration for smooth integrands in a few (<=3) dimensions, but is a poor choice in higher dimensions or for non-smooth integrands.

    However, recall that pcubature() happens to fail for your example, which is a smooth low-dimensional case - exactly where pcubature() is supposed to perform better - which suggests that it may be just luck that hcubature() works and pcubature() doesn't in this case.

    An illustration of how sensitive the results can be to parameters (lower/upper limits in this case):

    library(emdbook)
    cc <- curve3d(integrate( dnorm, mean=-5, sd=0.07,
            lower=x, upper=y, subdivisions=1000L)$value,
           xlim=c(-30,-10), ylim=c(0,30), n = c(61, 61),
           sys3d="image", col=c("black", "white"),
        xlab="lower", ylab="upper")
    

    White squares are successful (integral=1), black squares are bad (integral=0).

提交回复
热议问题