updating data

103 views
Skip to first unread message

Jan-Ole Koslik

unread,
Aug 13, 2024, 5:48:22 AM8/13/24
to TMB Users
Hi all,

is there a simple way to update the data when using RTMB without remaking the AD function/ Tape every time?

So if I have

par = list(...)
data = list(...)

f = function(par){
   getAll(par, data)
   ...
}

can I use MakeTape() in combination with DataEval() to only replace the data in the objective?

Cheers,
Ole

Kasper Kristensen

unread,
Aug 14, 2024, 5:17:17 AM8/14/24
to TMB Users
RTMB has the capabilities to do this (example below) but there are some side effects to be aware of:
* The tape optimizer can do a better job when knowing the data can't change. It's therefore often worth waiting for the tape to be rebuilt with the new data rather than implementing data updates.
* The tape forward loop is only triggered when parameters change compared to previous evaluation. That is, if you change the data, and re-evaluate the likelihood for the same parameters as previous, nothing will happen (see example)!
* DataEval evaluates an R function and is therefore not thread safe. It therefore won't play well with a parallel version of RTMB.

library(RTMB)
mydat <- rivers
f <- function(p) {
    getDat <- function(x) {
        print("Get data")
        .GlobalEnv$mydat
    }
    ## dummy parameter (DataEval expects an argument)
    empty <- rep(p$mu, 0)
    ## Add R function to the AD tape
    y <- DataEval(getDat, empty)
    -sum(dnorm(y, p$mu, p$sd, log=TRUE))
}
obj <- MakeADFun(f, list(mu=0, sd=1))
obj$fn(obj$par)
obj$fn(obj$par) ## Same parameter as previous - nothing happens!
## Fit with mydat
opt <- nlminb(obj$par, obj$fn, obj$gr)
opt$par
## Refit with new data
mydat <- mydat * .1
opt <- nlminb(obj$par, obj$fn, obj$gr)
opt$par

Jan-Ole Koslik

unread,
Aug 14, 2024, 5:37:30 AM8/14/24
to TMB Users
Thanks Kasper,

can DataEval() only get numeric vectors or also lists?
When trying on my real example I get 

Error: Not compatible with requested type: [type=list; target=double].

Kasper Kristensen

unread,
Aug 14, 2024, 5:43:15 AM8/14/24
to TMB Users
Yes, it's limited to numeric vectors of fixed length. The first evaluation of DataEval() stores the length and checks that subsequent data lengths are the same.
Reply all
Reply to author
Forward
0 new messages