ADintegrate: AD adaptive numerical integration.

ADintegrateR Documentation

AD adaptive numerical integration.

Description

Univariate adaptive integration extending R's native integrate function to work in both standard and AD evaluation modes.

Usage

## 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
)

Arguments

f

Vectorized integrand.

lower

Lower integration limit. May be infinite.

upper

Upper integration limit. May be infinite.

...

Passed to f.

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.

Details

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.

Value

List with components "value", "abs.error" and "subdivisions".

Note

It is often advantageous to use integrate with Vectorize.

Examples

## 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))

RTMB documentation built on March 20, 2026, 5:08 p.m.