| ADintegrate | R Documentation |
Univariate adaptive integration extending R's native integrate function to work in both standard and AD evaluation modes.
## S4 method for signature 'ANY'
integrate(
f,
lower,
upper,
...,
subdivisions = 100L,
rel.tol = .Machine$double.eps^0.25,
abs.tol = rel.tol,
stop.on.error = TRUE,
keep.xy = FALSE,
aux = NULL
)
f |
Vectorized integrand. |
lower |
Lower integration limit. May be infinite. |
upper |
Upper integration limit. May be infinite. |
... |
Passed to |
subdivisions |
Max number of subdivisions. |
rel.tol |
Relative tolerance. |
abs.tol |
Absolute tolerance. |
stop.on.error |
Stop on error? |
keep.xy |
Not used. |
aux |
Not used. |
Standard evaluation mode simply calls stats::integrate while
AD evaluation mode re-directs to a specialized RTMB implementation.
The latter imitates the R (QUADPACK) implementation by using:
Adaptive Gauss-Kronrod (K21/G10) quadrature with fast retaping.
Wynn convergence acceleration to handle boundary singularites.
Accuracy requirements are specified via relative (rel.tol) and absolute (abs.tol) tolerances.
The AD implementation tries to follow the same stopping criterion as that used by QUADPACK, which is to stop if either (not both!) of these tolerances are satisfied:
|error| < max ( |result| * rel.tol , abs.tol ).
It follows that a tolerance can be disabled by setting it to zero. This is especially useful when integrating probability densities, where the relative tolerance is the relevant measure, i.e. pass abs.tol=0 in this case.
List with components "value", "abs.error" and "subdivisions".
It is often advantageous to use integrate with Vectorize.
## Example with many sub-divisions
f <- function(x) sin(exp(x))
F <- MakeTape(function(x) integrate(f, 0, x)$value, 0)
F(7)
integrate(f, 0, 7)
## Example with singularity
f <- function(x) dbeta(x, shape1=.1, shape2=.1)
F <- MakeTape(function(x) integrate(f, 0, x)$value, 0)
F(.1)
integrate(f, 0, .1)
## Example using Vectorize (note the speed of G versus g)
f <- Vectorize(function(x) integrate(dnorm, -Inf, x)$value)
g <- Vectorize(function(x) integrate(f, -Inf, x)$value)
G <- MakeTape(g, numeric(1e3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.