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

From: <polzehl_at_pubhealth.ku.dk>
Date: Fri 12 Jan 2007 - 17:49:43 GMT


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 ...



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat Jan 13 08:06:02 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 16 Jan 2007 - 19:32:52 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.