[Rd] incorrect result of deriv (PR#9449)

polzehl at pubhealth.ku.dk polzehl at pubhealth.ku.dk
Fri Jan 12 18:49:43 CET 2007


Full_Name: Joerg Polzehl
Version: 2.3.1
OS: x86_64, linux-gnu
Submission from: (NULL) (62.141.176.22)


I observed an incorrect behavior of function deriv when evaluating arguments of
dnorm

deriv(~dnorm(z,0,s),"z")
expression({
    .value <- dnorm(z, 0, s)
    .grad <- array(0, c(length(.value), 1), list(NULL, c("z")))
    .grad[, "z"] <- -(z * dnorm(z))
    attr(.value, "gradient") <- .grad
    .value
})

deriv(~exp(-z^2/(2*s^2))/s/sqrt(2*pi),"z")
expression({
    .expr4 <- 2 * s^2
    .expr6 <- exp(-z^2/.expr4)
    .expr9 <- sqrt(2 * pi)
    .value <- .expr6/s/.expr9
    .grad <- array(0, c(length(.value), 1), list(NULL, c("z")))
    .grad[, "z"] <- -(.expr6 * (2 * z/.expr4)/s/.expr9)
    attr(.value, "gradient") <- .grad
    .value
})

should provide the same expression but

> eval(deriv(~exp(-z^2/(2*s^2))/s/sqrt(2*pi),"z"),list(z=1,s=2))
[1] 0.1760327
attr(,"gradient")
               z
[1,] -0.04400817

> eval(deriv(~dnorm(z,0,s),"z"),list(z=1,s=2))
[1] 0.1760327
attr(,"gradient")
              z
[1,] -0.2419707

provide different gradients ...



More information about the R-devel mailing list