Re: [R] Optimization function producing negative parameter values
This is likely because Hessian is being approximated. Numerical approximation to Hessian will overstep the bounds because the routines that are called don't respect the bounds (they likely don't have the bounds available). Writing numerical approximations that respect bounds and other constraints is an interesting and very challenging problem. It's likely a lot easier to do the work and get the derivatives analytically. JN On 2021-03-21 1:50 p.m., Bill Dunlap wrote: > Does optim go out of bounds when you specify hessian=FALSE? > hessian=TRUE causes some out-of-bounds evaluations of f. > >> optim(c(X=1,Y=1), >> function(XY){print(unname(XY));(XY[["X"]]+1)^4+(XY[["Y"]]-2)^4}, method= >> "L-BFGS-B", lower=c(0.001,0.001), upper=c(1.5,1.5), hessian=TRUE) > [1] 1 1 > [1] 1.001 1.000 > [1] 0.999 1.000 > [1] 1.000 1.001 > [1] 1.000 0.999 > [1] 0.001 1.500 > [1] 0.002 1.500 > [1] 0.001 1.500 > [1] 0.001 1.500 > [1] 0.001 1.499 > [1] 0.003 1.500 > [1] 0.001 1.500 > [1] 0.002 1.501 > [1] 0.002 1.499 > [1] 0.001 1.500 > [1] -0.001 1.500 > [1] 0.000 1.501 > [1] 0.000 1.499 > [1] 0.002 1.501 > [1] 0.000 1.501 > [1] 0.001 1.502 > [1] 0.001 1.500 > [1] 0.002 1.499 > [1] 0.000 1.499 > [1] 0.001 1.500 > [1] 0.001 1.498 > $par > X Y > 0.001 1.500 > > $value > [1] 1.066506 > > On Sun, Mar 21, 2021 at 10:22 AM Shah Alam wrote: >> >> Dear all, >> >> I am using optim() to estimate unknown parameters by minimizing the >> residual sums of squares. I created a function with the model. The model is >> working fine. The optim function is producing negative parameter values, even >> I have introduced upper and lower bounds (given in code). Therefore, >> the model produces *NAs*. >> >> Following is my code. >> >> param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial >>> parameter values >>> opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B", >>> lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001), >> >> upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01), >>> control=list(maxit=10), hessian = T) >> >> >> Error: >> >> *"NAs producedError in if (rnd_1 < liferisk) { : missing value where >> TRUE/FALSE needed "* >> >> The model function which produces NA due to negative parameter values >> >> liferisk <- rnorm(n = 1, mean = >> (calib_para[which(names(calib_para)=="r_mu")]),sd = >> (calib_para[which(names(calib_para)=="r_sd")])) >> >> rnd_1 <- runif(1, 0, 1) >> >> if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0} >> >> >> How to design/ modify optim() function, and upper-lower bounds to stop >> producing negative values during parameter search? >> Thanks >> >> Best regards, >> Shah >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Optimization function producing negative parameter values
Does optim go out of bounds when you specify hessian=FALSE? hessian=TRUE causes some out-of-bounds evaluations of f. > optim(c(X=1,Y=1), > function(XY){print(unname(XY));(XY[["X"]]+1)^4+(XY[["Y"]]-2)^4}, method= > "L-BFGS-B", lower=c(0.001,0.001), upper=c(1.5,1.5), hessian=TRUE) [1] 1 1 [1] 1.001 1.000 [1] 0.999 1.000 [1] 1.000 1.001 [1] 1.000 0.999 [1] 0.001 1.500 [1] 0.002 1.500 [1] 0.001 1.500 [1] 0.001 1.500 [1] 0.001 1.499 [1] 0.003 1.500 [1] 0.001 1.500 [1] 0.002 1.501 [1] 0.002 1.499 [1] 0.001 1.500 [1] -0.001 1.500 [1] 0.000 1.501 [1] 0.000 1.499 [1] 0.002 1.501 [1] 0.000 1.501 [1] 0.001 1.502 [1] 0.001 1.500 [1] 0.002 1.499 [1] 0.000 1.499 [1] 0.001 1.500 [1] 0.001 1.498 $par X Y 0.001 1.500 $value [1] 1.066506 On Sun, Mar 21, 2021 at 10:22 AM Shah Alam wrote: > > Dear all, > > I am using optim() to estimate unknown parameters by minimizing the > residual sums of squares. I created a function with the model. The model is > working fine. The optim function is producing negative parameter values, even > I have introduced upper and lower bounds (given in code). Therefore, > the model produces *NAs*. > > Following is my code. > > param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial > > parameter values > > opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B", > > lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001), > > upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01), > > control=list(maxit=10), hessian = T) > > > Error: > > *"NAs producedError in if (rnd_1 < liferisk) { : missing value where > TRUE/FALSE needed "* > > The model function which produces NA due to negative parameter values > > liferisk <- rnorm(n = 1, mean = > (calib_para[which(names(calib_para)=="r_mu")]),sd = > (calib_para[which(names(calib_para)=="r_sd")])) > > rnd_1 <- runif(1, 0, 1) > > if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0} > > > How to design/ modify optim() function, and upper-lower bounds to stop > producing negative values during parameter search? > Thanks > > Best regards, > Shah > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Optimization function producing negative parameter values
Can you put together your example as a single runnable scipt? If so, I'll try some other tools to see what is going on. There have been rumours of some glitches in the L-BFGS-B R implementation, but so far I've not been able to acquire any that I can reproduce. John Nash (maintainer of optimx package and some other optimization tools) On 2021-03-21 1:20 p.m., Shah Alam wrote: > Dear all, > > I am using optim() to estimate unknown parameters by minimizing the > residual sums of squares. I created a function with the model. The model is > working fine. The optim function is producing negative parameter values, even > I have introduced upper and lower bounds (given in code). Therefore, > the model produces *NAs*. > > Following is my code. > > param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial >> parameter values >> opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B", >> lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001), > > upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01), >> control=list(maxit=10), hessian = T) > > > Error: > > *"NAs producedError in if (rnd_1 < liferisk) { : missing value where > TRUE/FALSE needed "* > > The model function which produces NA due to negative parameter values > > liferisk <- rnorm(n = 1, mean = > (calib_para[which(names(calib_para)=="r_mu")]),sd = > (calib_para[which(names(calib_para)=="r_sd")])) > > rnd_1 <- runif(1, 0, 1) > > if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0} > > > How to design/ modify optim() function, and upper-lower bounds to stop > producing negative values during parameter search? > Thanks > > Best regards, > Shah > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Optimization function producing negative parameter values
Dear all, I am using optim() to estimate unknown parameters by minimizing the residual sums of squares. I created a function with the model. The model is working fine. The optim function is producing negative parameter values, even I have introduced upper and lower bounds (given in code). Therefore, the model produces *NAs*. Following is my code. param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial > parameter values > opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B", > lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001), upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01), > control=list(maxit=10), hessian = T) Error: *"NAs producedError in if (rnd_1 < liferisk) { : missing value where TRUE/FALSE needed "* The model function which produces NA due to negative parameter values liferisk <- rnorm(n = 1, mean = (calib_para[which(names(calib_para)=="r_mu")]),sd = (calib_para[which(names(calib_para)=="r_sd")])) rnd_1 <- runif(1, 0, 1) if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0} How to design/ modify optim() function, and upper-lower bounds to stop producing negative values during parameter search? Thanks Best regards, Shah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.