R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("adapt-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('adapt') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "adapt" > > ### * adapt > > flush(stderr()); flush(stdout()) > > ### Name: adapt > ### Title: Adaptive Numerical Integration in 2-20 Dimensions > ### Aliases: adapt print.integration > ### Keywords: math utilities > > ### ** Examples > > ## Example of p - dimensional spherical normal distribution: > ir2pi <- 1/sqrt(2*pi) > fred <- function(z) { ir2pi^length(z) * exp(-0.5 * sum(z * z))} > > adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred) value relerr minpts lenwrk ifail 1.039222 0.0007911264 231 73 0 > adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred, eps = 1e-4) value relerr minpts lenwrk ifail 1.000237 1.653498e-05 655 143 0 > adapt(2, lo = c(-5,-5), up = c(5,5), functn = fred, eps = 1e-6) value relerr minpts lenwrk ifail 1.000039 3.22439e-07 1719 283 0 > ## adapt "sees" function ~= constantly 0 --> wrong result > adapt(2, lo = c(-9,-9), up = c(9,9), functn = fred) value relerr minpts lenwrk ifail 1.14355 0.004778851 427 73 0 > ## fix by using much finer initial grid: > adapt(2, lo = c(-9,-9), up = c(9,9), functn = fred, min = 1000) value relerr minpts lenwrk ifail 1.003012 0.0004258552 1021 283 0 > adapt(2, lo = c(-9,-9), up = c(9,9), functn = fred, min = 1000, eps = 1e-6) value relerr minpts lenwrk ifail 1.000017 8.541229e-07 2713 563 0 > > i1 <- print(integrate(dnorm, -2, 2))$value 0.9544997 with absolute error < 1.8e-11 > > ## True values for the following example: > i1 ^ c(3,5) [1] 0.8696158 0.7922807 > > for(p in c(3,5)) { + cat("\np = ", p, "\n------\n") + f.lo <- rep(-2., p) + f.up <- rep(+2., p) + ## not enough evaluations: + print(adapt(p, lo=f.lo, up=f.up, max=100*p, functn = fred)) + ## enough evaluations: + print(adapt(p, lo=f.lo, up=f.up, max=10^p, functn = fred)) + ## no upper limit; p=3: 7465 points, ie 5 attempts (on an Athlon/gcc/g77): + print(adapt(p, lo=f.lo, up=f.up, functn = fred, eps = 1e-5)) + } p = 3 ------ Warning in adapt(p, lo = f.lo, up = f.up, max = 100 * p, functn = fred) : Ifail=1, maxpts was too small. Check the returned relerr! value 0.8694218 relerr 0.01110889 minpts 357 lenwrk 34 ifail 1 warn Ifail=1, maxpts was too small. Check the returned relerr! value relerr minpts lenwrk ifail 0.8691975 2.579554e-05 561 104 0 value relerr minpts lenwrk ifail 0.8696156 1.902827e-06 7465 804 0 p = 5 ------ Warning in adapt(p, lo = f.lo, up = f.up, max = 100 * p, functn = fred) : Ifail=1, maxpts was too small. Check the returned relerr! value -0.9022304 relerr 1 minpts 251 lenwrk 35 ifail 1 warn Ifail=1, maxpts was too small. Check the returned relerr! value relerr minpts lenwrk ifail 0.7967753 0.006314478 7597 5758 0 value relerr minpts lenwrk ifail 0.792253 9.562748e-06 45017 3687 0 > > > > ### *