R-alpha: Optimize

Philippe Lambert phlamber@luc.ac.be
Wed, 12 Nov 1997 10:44:32 +0100 (MET)


The function optimize returns the opposite value of the objective
when maximizing:

> f <- function(x) -x**2+1
> optimize(f,c(-5,5),maximum=TRUE)
$maximum
[1] 0

$objective
[1] -1

> f(0)
[1] 1

What should be corrected:

> optimize
function (f, interval, lower = min(interval), upper = max(interval), 
        maximum = FALSE, tol = .Machine$double.eps^0.25, ...) 
{
        if (maximum) {
                val <- .Internal(fmin(function(arg) -f(arg, ...), 
                        lower, upper, tol))
                list(maximum = val, objective = -f(val,...)
                                               ^^^
SHOULD BE +f(val,...) <-------------------------|

   Philippe

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=