hessian has wrong sign if `fnscale=-1`
Thanks to Dan for the bug report.
When the user does two things simultaneously
- changes from a minimization routine to a maximization routine via the argument control=list(fnscale=-1) and
- requests a return of the hessian via the argument hessian=TRUE,
then optimParallel()
fails to flip the sign of the Hessian.
set.seed(13)
x <- rnorm(1000, 5, 2)
negll <- function(par, x) { -sum(dnorm(x=x, mean=par[1], sd=par[2], log=TRUE)) }
posll <- function(par, x) { sum(dnorm(x=x, mean=par[1], sd=par[2], log=TRUE)) }
out_1 <- optim(par=c(1,1), fn=negll, x=x, method = "L-BFGS-B", lower=c(-Inf, .0001), hessian=T)
out_2 <- optim(par=c(1,1), fn=posll, x=x, method = "L-BFGS-B", lower=c(-Inf, .0001), hessian=T, control=list(fnscale=-1))
out_1$hessian
out_2$hessian
library("optimParallel")
cl <- makeCluster(5)
setDefaultCluster(cl=cl)
out_p1 <- optimParallel(par=c(1,1), fn=negll, x=x, method = "L-BFGS-B", lower=c(-Inf, .0001), hessian=T)
out_p2 <- optimParallel(par=c(1,1), fn=posll, x=x, method = "L-BFGS-B", lower=c(-Inf, .0001), hessian=T, control=list(fnscale=-1))
out_p1$hessian
out_p2$hessian