Get only hessian diagonal instead of the whole hessian matrix?

24 views
Skip to first unread message

Tomas T.

unread,
Sep 9, 2025, 2:40:40 PMSep 9
to TMB Users
Hello,

Is there a possibility to calculate just the second derivatives of each vector element, i.e. just the diagonal of the hessian matrix instead of the whole matrix?

I am calling diag(F$jacfun()$jacobian()) on a Tape object F to get the second derivatives, but since the hessian dimensions are 9000x9000, it takes way too long, and all the elements except of the diagonal are zero anyways.

I tried to call $jacfun(sparse = TRUE), but it doesn't help, result is the same.

Below is a simplified reproducible code. 

Thanks in advance!

Tomas



library(RTMB)

N <- 9000
d <- data.frame(present = rbinom(N, 1, 0.3))

inv.logit <- plogis

# log likelihood but I need the vector for each data point rather than just a sum
ll_pa <- function (data, par)
{
y <- data
p <- inv.logit(par)

y0 <- y$present == 0
y1 <- y$present == 1
ll_ <- numeric(nrow(y))
ll_[y1] <- log(p[y1])
ll_[y0] <- log(1 - p[y0])
sum(ll_)
}

cmb <- function(f, data) function(par) f(data, par)
# closure trick somewhere from the RTMB docs, to tie this to particular data and prevent unnecessary headaches!


F <- MakeTape(cmb(ll_pa, data = d), numeric(N))


p1 <- rnorm(N)

F$jacobian(p1) # first derivatives are alright

# second derivatives
der2 <- F$jacfun()$jacobian(p1)
# takes too long, calculating the whole hessian matrix, when I need just the diagonal

der2b <- F$jacfun(sparse = TRUE)$jacobian(p1) # surprisingly, this yields exactly the same result

identical(der2, der2b)

Kasper Kristensen

unread,
Sep 10, 2025, 2:54:24 AM (14 days ago) Sep 10
to TMB Users
You can construct a function representing the sparse Hessian like this:
H <- F$jacfun()$jacfun(sparse=TRUE)
And then evaluate it by diag(H(p1)).

Tomas T.

unread,
Sep 10, 2025, 10:37:46 AM (13 days ago) Sep 10
to Kasper Kristensen, TMB Users
Thanks Kasper! This works perfectly, and it is fast!

I was also able to get the 3rd derivative by F$jacfun()$jacfun(sparse = TRUE)$jacfun(sparse = TRUE).

Thanks!

Tomas


--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to a topic in the Google Groups "TMB Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tmb-users/fAaEhwW1niU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tmb-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/tmb-users/589419cf-ecd1-4a84-83b8-1725bf6d23e2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages