0
votes

I would like to find the maxima of the function:

Fd <- 224 * d1 + 84 * d2 + d1 * d2 - 2 * d1^2 - d2^2

I can do that using an 'exhaustive' search using the following code:

my.data <- expand.grid(x1 = seq(0, 200, 1), x2 = seq(0, 200, 1))

d1 <- my.data[,1]
d2 <- my.data[,2]
Fd <- 224 * d1 + 84 * d2 + d1 * d2 - 2 * d1^2 - d2^2

new.data <- data.frame(Fd = Fd, d1 = d1, d2 = d2)

# identify values of d1 and d2 that maximize Fd
new.data[new.data$Fd == max(new.data$Fd),]
#          Fd d1 d2
# 16157 11872 76 80

The function has a maximum value of 11872 when d1 = 76 and d2 = 80.

I can also locate the maxima using optim with the code below:

Fd <- function(betas) {
     b1 = betas[1]
     b2 = betas[2]
     -1 * (224 * b1 + 84 * b2 + b1 * b2 - 2 * b1^2 - b2^2)
}

optim(c(1,1), Fd, hessian = TRUE)

I multiply the function above by -1 to obtain the maxima and noticed the maximum value returned by this code is the negative of the true maximum value. Also, the Hessian returned by this code is the true Hessian * -1:

true.hessian <- matrix(c(-4, 1, 1, -2), nrow = 2, byrow = TRUE)
true.hessian

estimated.hessian <- -1 * true.hessian
estimated.hessian

I never realized this until now and did not see it mentioned on the optim page. Should I be concerned? If so, in what circumstances?

Is there an option in the optim statement to return the true Hessian of the original function rather than -1 * the original function when finding the maxima? Or should I just be aware that the Hessian is multiplied by -1 when I search for the maxima (rather than search for the minima) and correct the Hessian myself?

1

1 Answers

1
votes

This will return the correct Hessian when maximizing the function:

Fd <- function(betas) {

     b1 = betas[1]
     b2 = betas[2]

     (224 * b1 + 84 * b2 + b1 * b2 - 2 * b1^2 - b2^2)

}

optim(c(1,1), Fd, control=list(fnscale=-1), hessian = TRUE)

# $par
# [1] 76.00046 79.99999
#
# $value
# [1] 11872
#
# $counts
# function gradient 
#      129       NA 
#
# $convergence
# [1] 0
#
# $message
# NULL
#
# $hessian
#           [,1]      [,2]
# [1,] -3.999998  0.999999
# [2,]  0.999999 -2.000000