Your model lacks sparsity because it's not initially formulated as a state space model. It may seem like a natural strategy to try to make the current implementation run faster. However, that's not gonna do any good, because the model has structural issues. Going forward I'd recommend thinking carefully about what the state vector should be (natural space or frequency domain?) in order to minimize the number of FFT transformations. Make sure to connect states via
X(t) ~ Distribution(f(X(t-1)))
and make sure that all further state calculations only access one 'X(t)' at a time. It may be useful to start with a small toy example and build up, rather than starting with the full mizer model...
How to diagnose model issues:## Normally not needed, but here we set it to not run out of memory
TMB::config(tmbad.sparse_hessian_compress=TRUE, DLL="RTMB")
## Build model object as before
obj <- MakeADFun(function(p)do.call(simple_mizer,p), log_pars , map = map , random = c('rdd' , 'effort'))
## Build the sparse hessian tape - takes a LONG time
## 1208.890 seconds for only 379 random effects!
system.time(obj$env$spHess)
## Check the sparsity pattern
h <- obj$env$spHess(random=TRUE)
Matrix::image(h) ## Dense! - not what you want
## What's on the gradient tape? ~21000 Fourier transforms (yikes!):
TMB:::op_table(obj$env$ADGrad)
## FFT 10518
## iFFT 10518
If each of the 379 random effects link to all these FFTs it means there are 7.5 million FFTs on the sparse hessian tape! That's exactly what you need to avoid.