Shantanu MULLICK
2013-Oct-28 17:00 UTC
[R] Optimize function in R: unable to find maximum of logistic function
Hello Everyone, I want to perform a 1-D optimization by using the optimize() function. I want to find the maximum value of a "logistic" function. The optimize() function gives the wrong result. My code: f= function (k) { T_s = 20 result = (2- 2/(1+ exp(-2*T_s*k))) return(result) } optimize(f, c(0, 5), tol = 0.0000000000001, maximum= TRUE) The maximum value for the function happens at k=0, and the maximum value is 1. Yet the optimise function, says that the maximum value happens at k4.9995, and the maximum value is 0. Thanks in advance! Warm Regards, Shantanu [[alternative HTML version deleted]]
Rui Barradas
2013-Oct-28 20:49 UTC
[R] Optimize function in R: unable to find maximum of logistic function
Hello, This seems like a bug, removing 'maximum = TRUE' has no effect except that the returned value says it's a minimum, not a maximum. Rui Barradas Em 28-10-2013 17:00, Shantanu MULLICK escreveu:> Hello Everyone, > > I want to perform a 1-D optimization by using the optimize() function. I > want to find the maximum value of a "logistic" function. The optimize() > function gives the wrong result. > > My code: > f= function (k) { > T_s = 20 > result = (2- 2/(1+ exp(-2*T_s*k))) > return(result) > } > optimize(f, c(0, 5), tol = 0.0000000000001, maximum= TRUE) > > The maximum value for the function happens at k=0, and the maximum value is > 1. Yet the optimise function, says that the maximum value happens at k> 4.9995, and the maximum value is 0. > > Thanks in advance! > > Warm Regards, > Shantanu > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Rolf Turner
2013-Oct-28 21:01 UTC
[R] Optimize function in R: unable to find maximum of logistic function
This could be described as a bug, perhaps. Or it could be described as an indication that numerical optimization is inevitably tricky. Notice that if you narrow down your search interval from [0,5] to [0,0.5] you get the right answer: > optimize(f, c(0, 0.5), maximum= TRUE,tol=1e-10) $maximum [1] 4.192436e-11 $objective [1] 1 I guess there's a problem with finding a gradient that is effectively (numerically) zero when "k" is equal to 5. cheers, Rolf Turner On 10/29/13 06:00, Shantanu MULLICK wrote:> Hello Everyone, > > I want to perform a 1-D optimization by using the optimize() function. I > want to find the maximum value of a "logistic" function. The optimize() > function gives the wrong result. > > My code: > f= function (k) { > T_s = 20 > result = (2- 2/(1+ exp(-2*T_s*k))) > return(result) > } > optimize(f, c(0, 5), tol = 0.0000000000001, maximum= TRUE) > > The maximum value for the function happens at k=0, and the maximum value is > 1. Yet the optimise function, says that the maximum value happens at k> 4.9995, and the maximum value is 0. >