Anyway to calculate without updating?

12 views
Skip to first unread message

dirkdouw...@gmail.com

unread,
Feb 9, 2026, 2:56:04 PM (11 days ago) Feb 9
to nimble-users
Hello Everyone,

I asked this a few years ago, and it did not seem possible. However, maybe the situation has changed.

I have my own custom sampler in which I have to calculate the sum of some log densities under different values of a parameter. I use the calculate function for this, but unfortunately, it updates the model, so I have to run it an extra time to go back. Anyway, I will paste the code below.

         end_lf <- model$getLogProb(f_dependencies[f_start_depend[ct]:f_end_depend[ct]])
        lf[ct,1] <<- end_lf
        lf[ct,3] <<- end_lf
       
        #now need to calcl epi_lf
        model$S[loc,ct] <<- 2
        epi_lf <- model$calculate(f_dependencies[f_start_depend[ct]:f_end_depend[ct]])
        lf[ct,2] <<- epi_lf
       
        #now have to go back, no way around this
        model$S[loc,ct] <<- original_state
        model$calculate(nodes=f_dependencies[f_start_depend[ct]:f_end_depend[ct]])

Ideally, it would look something like this.


         end_lf <- model$getLogProb(f_dependencies[f_start_depend[ct]:f_end_depend[ct]])
        lf[ct,1] <<- end_lf
        lf[ct,3] <<- end_lf
       
        #now need to calcl epi_lf
        model$S[loc,ct] <<- 2
        epi_lf <-  model$calculate(f_dependencies[f_start_depend[ct]:f_end_depend[ct]],update=FALSE)
        lf[ct,2] <<- epi_lf
       
        #now have to go back, no way around this
        model$S[loc,ct] <<- original_state

I was wondering if there was anyway to avoid the last calculate. Hopefully, it makes sense.

Best,
Dirk 




Perry de Valpine

unread,
Feb 9, 2026, 3:31:59 PM (11 days ago) Feb 9
to dirkdouw...@gmail.com, nimble-users
Hi Dirk,
The way this is typically done in nimble's built-in samplers is with the "mvSaved" modelValues data structure (which is available to all samplers that are used within nimble's main MCMC system) and the nimCopy() function.  For example, the sampler_RW (adaptive random-walk Metropolis-Hastings) includes copying from the model to mvSaved if a new value is accepted (it is at that point already in the model and calculations have been done, and by copying it into mvSaved that is kept up to date for the next sampler) or copying from the mvSaved to the model if the new value is rejected (restoring the model state, including for nodes that were calculated). In built-in samplers you will see the calculations broken into two or three different vectors of node names so that for efficiency purposes some can be skipped when not needed, but it is fine to have them all in one vector of node names. See MCMC_samplers.R in the source code for more examples and let us know further questions.
HTH,
Perry



--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/350d571e-ebd3-4067-8810-a442ee22dbaen%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages