[R] L-BFGS-B needs finite values of 'fn'

Zaihra T zaihra at uwindsor.ca
Wed Apr 2 20:08:36 CEST 2008


   Hey paul,

    Chk  out some of these points as mentioned somewhere earlier in R help  too
    1. Trying to exponentiate potentially negative numbers is also asking for
       trouble
    2. Use lgamma() or choose() or lchoose() rather than gamma() or
       factorial()
    3. Worry about fnscale. It seems unlikely that all args are indeed on
       the same scale.
    4. Try to add and subtract logarithms in preference to
       multiplying and dividing as cancellations usually are more
       accurate and you are less likely to run into machine accuracy
       issues.
    5. If x's in the function  are <=0 by any chance then u might wanna rewrite
       ur log(x) as log(ifelse(x<=0,1,x)) or  log(ifelse(x<=0,x+c,x))  whre c
       is a very small number , depending upon whether u  dont want to consider
       the contribution of zero or less than 0 x's in ur function or vice versa
    6. As dividing by very small numbers is also a potential killer, make sure
       ur fuction doesnt ! have any divisor which might be very small

   hope this helps u in  fixing ur problem.

   cheers!

   Zaihra

   On Wed, 2 Apr 2008 18:48:47 +0100 "Paul Smith" wrote:
   > Thanks, Ravi. The analytical solution, (x_1,x_2,...,x_10), should
   > satisfy this equality:
   >
   > x_t / x_(t-1) = 0.3.
   >
   > Unfortunately, the procedure that you suggest does not lead to a
   > solution that satisfies such an equality.
   >
   > Paul
   >
   >
   >
   > On Wed, Apr 2, 2008 at 5:12 PM, Ravi Varadhan wrote:
   > > Paul,
   > >
   > > Have you tried using "BFGS" without bounds?
   > >
   >    >    sols    <-   optim(rep(20,nvar),   f,   gr,   method="BFGS",
   control=list(fnscale=-1))
   > >
   > > This converges to a solution, although I don't know if t! he converged
   > > solution is what you want.
   > > ;
   > > Ravi.
   > >
   > >
   ----------------------------------------------------------------------------
   > > -------
   > >
   > > Ravi Varadhan, Ph.D.
   > >
   > > Assistant Professor, The Center on Aging and Health
   > >
   > > Division of Geriatric Medicine and Gerontology
   > >
   > > Johns Hopkins University
   > >
   > > Ph: (410) 502-2619
   > >
   > > Fax: (410) 614-9625
   > >
   > > Email: rvaradhan at jhmi.edu
   > >
   > > Webpage:
   http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
   > >
   > >
   > >
   > >
   ----------------------------------------------------------------------------
   > > --------
   > >
   > >
   > >
   > > -----Original Message-----
   > > From: r-help-bounce! s at r-project.org
   [mailto:r-help-bounces at r-project.org] On
   > > Behalf Of Paul Smith
   > >
   > > Sent: Monday, March 31, 2008 2:25 PM
   > > To: R-help
   > >
   > >
   > > Subject: Re: [R] L-BFGS-B needs finite values of 'fn'
   > >
   > > On Mon, Mar 31, 2008 at 2:57 PM, Zaihra T wrote:
   > > > try something like this before wrapping up your function else i guess
   > > u'll
   > > > have to stick to Prof Brian Ripley suggestion his suggestions are
   usually
   > > > best bet .
   > > >
   > > > f <- function(x) {
   > > >
   > > > n <- length(x)
   > > >
   > > > r <- sum((b^(0:(n-1)))*log(x)) - 2000000*(sum(x)-k)^2
   > > > if(!is.finite(r))
   > > >
   > > > r<-1e+20 return(r)
   > > >
   &! gt; > > }
   > > >
   > > > have a nice day.
   > > >
   > > >
   > > >
   > > >
   > > > On Mon, 31 Mar 2008 12:24:09 +0100 "Paul Smith" wrote:
   > > > > Dear All,
   > > > >
   > > > > I am trying to solve the optimization problem below, but I am always
   > > > > getting the following error:
   > > > >
   > > > > Error in optim(rep(20, nvar), f, gr, method = "L-BFGS-B", lower =
   rep(0,
   > > :
   > > > > L-BFGS-B needs finite values of 'fn'
   > > > >
   > > > > Any ideas?
   > > > >
   > > > > Thanks in advance,
   > > > >
   > > > > Paul
   > > > >
   > > > > -----------------------------------------! ------
   > > > >
   > > > > k <- 10000
   > > > > b! <- 0.3
   > > > >
   > > > > f <- function(x) {
   > > > >
   > > > > n <- length(x)
   > > > >
   > > > > r <- sum((b^(0:(n-1)))*log(x)) - 2000000*(sum(x)-k)^2
   > > > >
   > > > > return(r)
   > > > >
   > > > > }
   > > > >
   > > > > gr <- function(x) {
   > > > >
   > > > > n <- length(x)
   > > > >
   > > > > r <- (b^(0:(n-1)))*(1/x) - 4000000*(sum(x)-k)
   > > > >
   > > > > return(r)
   > > > >
   > > > > }
   > > > >
   > > > > nvar <- 10
   > > > > (sols <-
   > > > >
   > > >
   >>optim(rep(20,nvar),f,gr,method="L-BFGS-B",lower=rep(0,nvar),upper=rep!
   (k,nvar
   >           >           ),control=list(fnscale=-1,parscale=rep(2000,nv
   ar),factr=1e-300,pgtol=1e-300)
   > > ))
   > >
   > > Not much progress, Zaihra. Unfortunately! I am wondering whether one
   > > can transform the original problem into an equivalent one and solvable
   > > with optim.
   > >
   > > I know the analytical solution; I am just trying to check how far can
   > > R go regarding optimization problems.
   > >
   > > Paul
   > >
   > > ______________________________________________
   > >
   > > 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.
   > >
   >
   > ______________________________________________
   > 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