integrate() gives horribly wrong answer:
integrate(function (x) dnorm(x, -5, 0.07), -Inf, Inf, subdivisions = 10000L)
# 2.127372e-23 with absolute error <
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, thepcubature
routines perform significantly worse than the vectorizedhcubature
in inappropriate cases. So when in doubt, you are better off usinghcubature
.
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.
Vectorized
hcubature
seems to be a good starting point.For smooth integrands in low dimensions (≤3),
pcubature
might be worth trying out. Experiment before using in a production package.