kaskr / RTMB

R bindings to TMB
Other
48 stars 6 forks source link

dgmrf bug when used with dseparable #15

Closed kaskr closed 1 year ago

kaskr commented 1 year ago

Example:

library(RTMB)
library(Matrix)
I <- .symDiagonal(2)
parameters <- list(u=matrix(0,2,2), delta=.123)
func <- function(parms) {
  getAll(parms)
  f <- function(x)dgmrf(x,Q=I*delta,log=TRUE)
  -dseparable(f,f)(u)
}
obj <- MakeADFun(func, parameters, random="u")
obj$fn() ## Should be zero
obj$gr() ## Should be zero

Gives

> obj$gr()
[1] 32.52033

Reason: dgmrf throws away 'advector' class attribute when called with numeric x and AD precision: https://github.com/kaskr/RTMB/blob/cd8bcb91a7ed0668ce7e24c2faf84f7163c3b610/RTMB/R/mvgauss.R#L177 https://github.com/kaskr/RTMB/blob/cd8bcb91a7ed0668ce7e24c2faf84f7163c3b610/RTMB/R/mvgauss.R#L61