j.c.rougier@durham.ac.uk
2003-Feb-24 18:06 UTC
[Rd] Problem with hessian in deriv3 (PR#2577)
Hi Everyone, I noticed the following in deriv3:> deriv3(expression(x * y - b * z), c("x", "y", "z"))expression({ .value <- x * y - b * z .grad <- array(0, c(length(.value), 3), list(NULL, c("x", "y", "z"))) .hessian <- array(0, c(length(.value), 3, 3), list(NULL, c("x", "y", "z"), c("x", "y", "z"))) .grad[, "x"] <- y .grad[, "y"] <- x .grad[, "z"] <- -b .hessian[, "z", "z"] <- 0 attr(.value, "gradient") <- .grad attr(.value, "hessian") <- .hessian .value }) where the hessian is incorrectly computed as being 0 everywhere -- it should be 1 at (x, y) and (y, x). Interestingly, if I precede x * y by unary minus the hessian is correct:> deriv3(expression(- x * y - b * z), c("x", "y", "z"))expression({ .expr1 <- -x .value <- .expr1 * y - b * z .grad <- array(0, c(length(.value), 3), list(NULL, c("x", "y", "z"))) .hessian <- array(0, c(length(.value), 3, 3), list(NULL, c("x", "y", "z"), c("x", "y", "z"))) .grad[, "x"] <- -y .hessian[, "x", "x"] <- 0 .hessian[, "x", "y"] <- .hessian[, "y", "x"] <- -1 .hessian[, "x", "z"] <- .hessian[, "z", "x"] <- 0 .grad[, "y"] <- .expr1 .hessian[, "y", "y"] <- 0 .hessian[, "y", "z"] <- .hessian[, "z", "y"] <- 0 .grad[, "z"] <- -b .hessian[, "z", "z"] <- 0 attr(.value, "gradient") <- .grad attr(.value, "hessian") <- .hessian .value }) Cheers, Jonathan. --please do not edit the information below-- Version: platform = i686-pc-linux-gnu arch = i686 os = linux-gnu system = i686, linux-gnu status = major = 1 minor = 6.1 year = 2002 month = 11 day = 01 language = R Search Path: .GlobalEnv, Autoloads, package:base