[R] meaning of lenwrk value in adapt function

Frede Aakmann Tøgersen FredeA.Togersen at agrsci.dk
Mon Oct 29 10:28:54 CET 2007

Well the adapt() function in R is based on A. Genz's fortran routine of the same name. In the routine some memory allocation is done statically, but since we're dealing with a adaptive integration routine, meaning that based on how "wiggly" or "wild" your integrand is in the integration area the routine evaluates the integrand in a sufficiently number of points to make the error of integration sufficiently small.

There are a few arguments to adapt() which may be helpfull here: eps and maxpts. See e.g. the 'Details' section in the man for adapt(), which says:

    'Setting 'maxpts' to NULL asks the function to keep doubling maxpts
     (starting at 'max(minpts,500, r(ndim))') until the desired
     precision is achieved or R runs out of memory.   Note that the
     necessary number of evaluations typically grows exponentially with
     the dimension 'ndim', and the underlying code requires 'maxpts >=
     r(ndim)' where r(d) = 2^d + 2 d(d + 3) + 1.'

To dig further into this you'll have to see the fortran source, which for reference is shown below. There you can also see the meaning of various 'ifail' values.

I think that you can get around your problem for a specific integrand by specifying suitable values for maxpts and eps, but I would advice you to explore more closely the behaviour of the integrand in the integration area, before just using 'brutal force'.

Best regards

Frede Aakmann Tøgersen

Faculty of Agricultural Sciences
Dept. of Genetics and Biotechnology
Blichers Allé 20, P.O. BOX 50
DK-8830 Tjele

Phone:   +45 8999 1900
Direct:  +45 8999 1878

E-mail:  FredeA.Togersen at agrsci.dk
Web:	   http://www.agrsci.org				

This email may contain information that is confidential.
Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed.
If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email.

Header of the fortran routine:

c***** input parameters
c  ndim	   number of variables, must exceed 1, but not exceed 20
c  a	   real array of lower limits, with dimension ndim
c  b	   real array of upper limits, with dimension ndim
c  minpts  minimum number of function evaluations to be allowed.
c	   on the first call to adapt minpts should be set to a
c	   non negative value (caution... minpts is altered by adapt).
c	   It is possible to continue a calculation to greater accuracy
c	   by calling adapt again by decreasing eps (described below)
c	   and resetting minpts to any negative value.
c	   minpts must not exceed maxpts.
c  maxpts  maximum number of function evaluations to be allowed,
c	   which must be at least rulcls, where
c	   rulcls =  2**ndim+2*ndim**2+6*ndim+1
c	       for ndim =  2   3   4   5   6   7   8   9   10	12    15      20
c      maxpts >= rulcls = 25  45  73 113 173 269 433 729 1285 4457 33309 1049497
c	  a SUGGESTED value for maxpts is 100 times the above values.
c  functn  externally declared user defined function to be integrated.
c	   it must have parameters (ndim,z), where z is a real array
c	   of dimension ndim.
cTSL this function has been replaced by the fixed function adhlp
c  eps	   required relative accuracy
c  lenwrk  length of array wrkstr of working storage, the routine
c	   needs (2*ndim+3)*(1+maxpts/rulcls)/2 for lenwrk if
c	   maxpts function calls are used.
c	   for guidance, if you set maxpts to 100*rulcls (see table
c	   above) then acceptable values for lenwrk are
c	     for ndim = 2    3	  4    5    6	 7    8	    9
c	     lenwrk =  357  561	 1785 3417 6681 13209 26265 52377
c***** OUTPUT parameters
c  minpts  actual number of function evaluations used by adapt
c  wrkstr  real array of working storage of dimension (lenwrk).
c  relerr  estimated relative accuracy of finest
c  finest  estimated value of integral ["FINal ESTimate"]
c  ifail : return code
c	ifail=0 for normal exit, when estimated relative accuracy
c		relerr is less than eps with maxpts or less function
c		calls made.
c	ifail=1 if maxpts was too small for adapt to obtain the
c		required relative accuracy eps.
c		In this case adapt returns a value of finest
c		with estimated relative accuracy relerr.
c	ifail=2 if lenwrk too small for maxpts function calls.
c		In this case adapt returns a value of finest with
c		estimated accuracy relerr using the working storage
c		available, but relerr will be greater than eps.
c	ifail=3 if ndim < 2, ndim > 20,
c	ifail=4 if minpts > maxpts,
c	ifail=5 if maxpts < rulcls or other memory problems
c		      (which will only be found later)

Med venlig hilsen
Frede Aakmann Tøgersen


> -----Oprindelig meddelelse-----
> Fra: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] På vegne af Eric Archer
> Sendt: 29. oktober 2007 09:18
> Til: r-help at r-project.org
> Emne: [R] meaning of lenwrk value in adapt function
> R-listers,
> In using the adapt function, I am getting the following warning:
> Ifail=2, lenwrk was too small. -- fix adapt() !
>  Check the returned relerr! in: adapt(ndim = 2, lower = 
> lower.limit, upper = upper.limit, functn = pr.set, 
> Would someone explain what the 'lenwrk' value indicates in 
> order to help diagnose this issue.
> Also, what are the possible codes for Ifail, so I can set 
> some traps in my code to handle them correctly.
> I apologize for not including a reproducible example in this 
> query, but the function that is being called in adapt is a 
> rather complex temporal-spatial density function and would 
> require many lines of code along with the necessary data, and 
> would not be useful at this stage in debugging.
> Thanks in advance.
> Cheers,
> Eric Archer, Ph.D.
> 8604 La Jolla Shores Dr.
> La Jolla, CA 92037
> 858-546-7121,7003(FAX)
> eric.archer at noaa.gov
> http://swfsc.nmfs.noaa.gov/prd/PROGRAMS/ETPCetacean/default.htm
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list