adapt {adapt}R Documentation

Adaptive Integration in 2-20 Dimensions.

Description

adapt() integrates a scalar function over a multidimensional rectangle, ie computes

integral[l .. u] functn(t) d^n(t)

where l =lower, u =upper, n =ndim. Infinite rectangles are not allowed, and ndim must be between 2 and 20.

Usage

adapt(ndim, lower, upper, minpts, maxpts, functn, eps,...)

Arguments

ndim the number of dimensions of the function/integral
lower vector of at least length ndim of the lower bounds on the integral
upper vector of at least length ndim of the upper bounds on the integral
minpts the minimum number of function evaluations
maxpts the maximum number of function evaluations or NULL
functn the name of an R function. functn should take a single vector argument and possibly some parameters and return the function value at that point. functn must return a single numeric value.
eps The desired accuracy for the relative error.
... Other parameters to be passed to functn

Details

This is modified from Mike Meyer's S code. The functions just call A.C. Genz's fortran ADAPT subroutine to do all of the calculations. A work array is allocated within the C/Fortran code.

The Fortran function has been modified to use double precision, for compatibility with R. It only works in two or more dimensions; for one-dimensional integrals use the integrate function in base R.

Setting maxpts to NULL asks the function to keep doubling maxpts (starting at max(minpts,500)) until the desired precision is achieved or R runs out of memory.

Value

A list of class "integration" with components

value the estimated integral
relerr the estimated relative error; < eps argument if
minpts the actual number of function evaluations
ifail an error indicator. If ifail is not equal to 0, the function warns the user of the error condition.

See Also

integrate

Examples

## Example of a three dimensional spherical normal distribution:
ir2pi <- 1/sqrt(2*pi)
fred <- function(z) { ir2pi^length(z) * exp(-0.5 * sum(z * z))}
## not enough evaluations
adapt(3, c(-2,-2,-2),c(2,2,2),100,300,fred,.01)
## enough evaluations
adapt(3, c(-2,-2,-2),c(2,2,2),100,600,fred,.01) 
## no upper limit 
## this took 7465 points, ie 5 attempts (on an Athlon/gcc/g77)
adapt(3, c(-2,-2,-2),c(2,2,2),100,NULL,fred,.00001)