Prof. John C Nash
2009-Oct-20 12:43 UTC
[R] (no subject) -- rename to optim() crash due to NA returned
While developing updates to optimization tools for R (I was behind 3 of the 5 codes in optim(), but Brian Ripley did the implementation), I've been seeing this kind of error when the objective function cannot be computed so returns NA. Examples: attempts to divide by 0 or sqrt(-ve) or log(0). A crude work-around is to detect bad situations when evaluating the function and return a very large number e.g., something using .Machine$double.xmax (I used this divided by 4 or so to avoid troubles if algorithms try to do some arithmetic, as in gradient based routines). Most routines will "bounce off" such a value, but the scaling of the surface is messed up so it is not always efficient. A better way is if optimization codes can handle a "failed evaluation" flag and simply back off from a step or whatever is needed, but in R non tools do this as yet -- I'm hoping to put this in some of my codes using returned values larger than some threshhold as the "flag". My 1980s BASIC codes had this (and bounds and masks). However, it takes time and .... For information, I'd like to know if this turns out to be the problem, as it would raise the priority for working on such issues. JN> Message: 57 > Date: Mon, 19 Oct 2009 17:48:12 +0200 > From: "Reynaerts, Jo" <Jo.Reynaerts at econ.kuleuven.be> > Subject: [R] (no subject) > To: "r-help at r-project.org" <r-help at r-project.org> > Message-ID: > <603D249CBE0B304490D0F774142B8E0F01CDCB9A4D68 at ECONSRVEX6.econ.kuleuven.ac.be> > > Content-Type: text/plain > > Dear R users > > I have the following problem when calling optim() to minimize a function "f.obj" (= outer loop) that calls another function "f.con" (a contraction mapping, = inner loop). It seems to me that it is a numerical problem that I currently fail to take into account when coding. > > Calling optim(), for the first few iterations of the outer loop, everything seems fine; the contraction mapping is calculated in each run. However, after a number of outer loop iterations, an error occurs and the following message is displayed: > > Error in while (max.dev >= tol.in) { : > missing value where TRUE/FALSE needed > > The previous conditional statement ensures that the iteration in the inner loop should run as long as max.dev <- max(abs(x - x.in)) is greater than the inner loop tolerance level (tol.in <- 1E-9), where x is computed by the contraction mapping using x.in. I used different stopping rules and tolerance levels, but this gives the same result. > > As I said, I think it's a numerical problem. Has anyone had similar experiences using optim() and could you give some coding advice? > > Thanks in advance, > > Jo Reynaerts