Numerical problems with complex RTMB model

69 views
Skip to first unread message

Jan-Ole Koslik

unread,
Dec 12, 2025, 8:51:02 AM (10 days ago) Dec 12
to TMB Users
Hi,

I am trying to fit a rather complicated non-linear and non-Gaussian state space model.

Specifically, the model is a state-switching continuous-time correlated random walk with measurement error. For each state, I have an integrated OU process and I am jointly modelling latent locations and velocities, which need to be integrated out by RTMB.

Due to the complexity, I wasn't expecting anything to run smoothly, but nonetheless I wanted to ask for tips. 

The issue I have is that the nested optimisation looks good for the most part, i.e. first 20-30 iterations. Inner gradients become zero in each outer iteration and the outer mgc also becomes fairly small. But then at some point, I'm always getting a lot of line searches, followed by the gradient "exploding", i.e. just becoming larger and larger from that point on.

The crazy thing is, if I just let it run, the estimated model in the end is not bad. Positions are also filtered pretty nicely.

So I just wanted to ask if people have tips for numerically stabilising this optimisation, or if it's just a terrible idea to fit this with TMB in general.

Cheers,
Ole

Hans Skaug

unread,
Dec 14, 2025, 12:10:56 PM (8 days ago) Dec 14
to TMB Users

I have had similar situations, where I believe the problem has been that the Laplace approximation is inaccurate in some part of the (outer) parameter space, and the optimizer gets attracted to this area due to error in the likelihood approximation. This can be really hard (and time consuming) to diagnose. My attempt has been to fix one parameter at the time, and see if the problems go away. This would indicate which part of the model is causing the problem, but it is not solving the problem.

When you say “non-Gaussian state space model” I believe that the prior distribution for each latent variable (random effect) is Gaussian, still. Otherwise that is a candidate for the culprit.

Cole Monnahan - NOAA Federal

unread,
Dec 15, 2025, 11:21:46 AM (7 days ago) Dec 15
to TMB Users
This is what the experimental feature "ridge.correct" in RTMB tries to fix. Try setting it to TRUE. I couldn't find any documentation but I know there is at least a presentation where Kasper introduces the concept and shows how it works. But I can't seem to find it online.

Matt Cheng

unread,
Dec 15, 2025, 11:25:50 AM (7 days ago) Dec 15
to Cole Monnahan - NOAA Federal, TMB Users

I believe this was partly discussed during the 2024 ICES MGWG?
kasper_slides.pdf

Cole Monnahan - NOAA Federal

unread,
Dec 15, 2025, 11:32:11 AM (7 days ago) Dec 15
to TMB Users

marks...@gmail.com

unread,
Dec 15, 2025, 4:14:40 PM (7 days ago) Dec 15
to TMB Users
Just chiming in to say this looks incredibly useful. I have run into the issue described in the Kasper's slides several times and it can be really frustrating and difficult to troubleshoot. 

Imai-Eaton, Jeffrey W

unread,
Dec 15, 2025, 6:47:32 PM (7 days ago) Dec 15
to Matt Cheng, Cole Monnahan - NOAA Federal, TMB Users
Thanks for sharing these slides — this has been a longstanding intermittent issue for us, with limited traction on addressing.

To confirm, can the ridge correction be tested in ‘normal’ TMB (not RTMB) simply by specifying the argument below?

obj <- MakeADFun(..., ridge.correct=TRUE)

Thanks,
Jeff

--
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 the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/tmb-users/CA%2B6UmeWOooLnV7FV_J3TMcmMVJKx577fPeeXkfXik90B5LyqfQ%40mail.gmail.com.

Kasper Kristensen

unread,
Dec 16, 2025, 9:19:48 AM (6 days ago) Dec 16
to TMB Users
Jeff, so far the method is only available in RTMB.
Reply all
Reply to author
Forward
0 new messages