integrate() gives totally wrong number

后端 未结 4 1186
傲寒
傲寒 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:50

    Try package cubature.

    library(cubature)
    
    hcubature(function (x) dnorm(x, -5, 0.07), -Inf, Inf)
    #$integral
    #[1] 1
    #
    #$error
    #[1] 9.963875e-06
    #
    #$functionEvaluations
    #[1] 405
    #
    #$returnCode
    #[1] 0
    

    Note that function pcubature in the same package also returns 0.

    From vignette("cubature"), section Introduction. My emphasis.

    This R cubature package exposes both the hcubature and pcubature routines of the underlying C cubature library, including the vectorized interfaces.

    Per the documentation, use of pcubature is advisable only for smooth integrands in dimensions up to three at most. In fact, the pcubature routines perform significantly worse than the vectorized hcubature in inappropriate cases. So when in doubt, you are better off using hcubature.

    Since in this case the integrand is the normal density, a smooth and 1-dimensional function, there would be reasons to prefer pcubature. But it doesn't give the right result. The vignette concludes the following.

    1. Vectorized hcubature seems to be a good starting point.

    2. For smooth integrands in low dimensions (≤3), pcubature might be worth trying out. Experiment before using in a production package.

提交回复
热议问题