posterior functionals with nimbleSMC

54 views
Skip to first unread message

Lawson, Andrew B.

unread,
Jul 25, 2025, 9:05:09 AMJul 25
to nimble...@googlegroups.com
Hi
I have started using nimbleSMC and I really appreciate the features.
I am running some space-time state-space models. Besides obtaining the latent states and high level parameter samples, is it at all possible to obtain output from posterior functionals (computed within the code)? I couldn't see how this could be done unless they are added to the latent state in some way.  
Any insights on this would be much appreciated.

best
Andrew


Daniel Turek

unread,
Jul 29, 2025, 3:05:17 PMJul 29
to Lawson, Andrew B., nimble...@googlegroups.com
Andrew, whatever you're after should be possible one way or another.  Can you be a little more specific about which "posterior functionals" you're talking about?  I was a little confused when you said "(computed within the code)".  Do you mean things within the nimbleSMC code?  If so, which things in particular?  Or, things within the model code?

Thanks,
Daniel


--
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/PH7PR11MB7499EEE4E90955752A3BCE7CA359A%40PH7PR11MB7499.namprd11.prod.outlook.com.

Chris Paciorek

unread,
Jul 29, 2025, 3:55:35 PMJul 29
to Daniel Turek, Lawson, Andrew B., nimble...@googlegroups.com
Also, are you asking about functionals of both parameters and latent states or functionals of just latent state values or of just parameter values?

Lawson, Andrew B.

unread,
Jul 30, 2025, 8:24:09 AMJul 30
to Daniel Turek, paci...@stat.berkeley.edu, nimble...@googlegroups.com
Hi Daniel/Chris
I was interested primarily in some goodness of fit measures which could be computed within the model code ...such as a deviance. Other things come to mind such as exceedence measures. These could be functions of the latent states. These could be things you would be able to monitor in conventional MCMC. 
Hope that's clearer....
Let me know what you think

best
Andrew


From: Chris Paciorek <paci...@stat.berkeley.edu>
Sent: Tuesday, July 29, 2025 3:55 PM
To: Daniel Turek <danie...@gmail.com>
Cc: Lawson, Andrew B. <laws...@musc.edu>; nimble...@googlegroups.com <nimble...@googlegroups.com>
Subject: Re: posterior functionals with nimbleSMC
 
Also, are you asking about functionals of both parameters and latent states or functionals of just latent state values or of just parameter values? On Tue, Jul 29, 2025 at 12: 05 PM Daniel Turek <danielturek@ gmail. com> wrote: Andrew, whatever

Daniel Turek

unread,
Jul 30, 2025, 1:25:38 PMJul 30
to Lawson, Andrew B., paci...@stat.berkeley.edu, nimble...@googlegroups.com
If I'm understanding correctly, your questions doesn't specifically have to do with the functions available in nimbleSMC, is that right?  Or am I missing something?

From what you described, one approach would be to code these functionals (as functions of the model latent state and/or parameter values) directly as deterministic nodes into the model itself.  Then, you would simply add monitors for these derived calculations.  From what you said, it seems like that would work fine.

Let me also bring to your attention the recent addition of "derived quantities" to the nimble MCMC system.  You can read about the new functionality here.  You could write your own "derived quantity function" to do whatever calculations you want (using any of the variables in the model), and record the results of these calculations during the course of MCMC sampling.  I'll also point out the "logProb" derived quantity, which is designed to record the log-density of arbitrary model nodes, or the sum-log-density for any sets of model nodes, or the entire (log) model density.

Let me know what you think of these options.

Cheers,
Daniel

Lawson, Andrew B.

unread,
Jul 30, 2025, 3:27:12 PMJul 30
to Daniel Turek, paci...@stat.berkeley.edu, nimble...@googlegroups.com
Hi Daniel
Thanks for the clarification. One issue remains: adding monitors. I can't find any information about how to do that when using nimbleSMC. If you are doing MCMC then configureMCMC following after buildMCMC allows setting monitors. However that changes the sampling in some way. I experimented by simply providing a deterministic node in the code, and then using configureMCMC. I couldn't then access the filtered states. 
If you enlarge on how you monitor deterministic nodes while SMC sampling that would be helpful.

thanks
Andrew





From: Daniel Turek <danie...@gmail.com>
Sent: Wednesday, July 30, 2025 1:24 PM
To: Lawson, Andrew B. <laws...@musc.edu>
Cc: paci...@stat.berkeley.edu <paci...@stat.berkeley.edu>; nimble...@googlegroups.com <nimble...@googlegroups.com>

Subject: Re: posterior functionals with nimbleSMC

Daniel Turek

unread,
Jul 31, 2025, 7:08:13 AMJul 31
to Lawson, Andrew B., paci...@stat.berkeley.edu, nimble...@googlegroups.com
Andrew, thanks for iterating on this.  I must admit, I think I'm not seeing the full picture of the workflow you're after.  In particular, I guess I'm still not clear what function(s) from the nimbleSMC package you're using, and how those nimbleSMC function(s) are part of any larger workflow (if any).

nimbleSMC provides a number of filtering algorithms, for approximating log-density values; for example the bootstrap filter, which is constructed using the "buildBootstrapFilter" function, or the Liu West filter, constructed using the "buildLiuWestFilter" function.  These filters can operate stand-alone on a model; or they can be used internal to an MCMC, embedded inside an MCMC sampler, using the MCMC sampler functions (also provided in nimbleSMC) such as "sampler_RW_PF" or "sampler_RW_PF_block".

I have no doubt that you already understand all of this; I'm only stating it as a starting point for discussion, to address my questions above (copied again here):  I'm still not clear what function(s) from the nimbleSMC package you're using, and how those nimbleSMC function(s) are part of any larger workflow (if any).  Can you help me understand what you're trying to do?

Regarding your question "If you enlarge on how you monitor deterministic nodes while SMC sampling that would be helpful", again my uncertainty stems from exactly what you mean by "while SMC sampling".  The words "monitor" and "sampling" make me think you're using some form of the MCMC, in which case you should be able to monitor any deterministic nodes in the model.  However, if you're not using any form of the nimble MCMC, then I'm not sure what your workflow looks like, or what you mean by "monitor deterministic nodes".  

Thanks, Andrew.  I think we'll figure this out, but I think we're just using different language.

Daniel

Perry de Valpine

unread,
Aug 5, 2025, 3:35:35 AMAug 5
to Daniel Turek, Lawson, Andrew B., paci...@stat.berkeley.edu, nimble...@googlegroups.com
Hi Andrew et al,

I think I see what you're asking about, and sorry to jump in late here. I've been traveling etc.

Since an SMC algorithm doesn't have monitors per se in the same way an MCMC does, the derived quantities features are not an immediate solution (unless you are using the particle MCMC approach).

One approach that I suspect you have already considered would be to do what you want by post-processing. That is, you could write a nimbleFunction -- or simply stay within R -- to iterate over samples returned by the SMC, plug them into the model, and calculate the functionals of interest. However, this would require that you extract all the necessary states, which can be a pain or prohibitive, and hence your question about doing it during the SMC run.

This is a good idea for extending nimbleSMC (which gets used less and receives less development attention than MCMC, by a long shot). You could try modifying the nimbleSMC source code, and really that strikes me as perhaps the most flexible approach, although with some learning curve. For example, if you are using the auxiliary filter, see AuxiliaryFilter.R. In buildAuxiliaryFilter, the run function iterates through the time steps, and each time step is done by an auxFstep object. Between iterations you could obtain and store the functionals of interest. If you need to do them for each particle considered at each time step, you could do that in the run function of auxFstep.

If digging into the nimbleSMC source code is not how you want to spend your time, let me suggest some tricks or workarounds that you could try right away. You can put in some "fake" data for each time step that make only a constant contribution to the likelihood but force some calculations. For example, say x[t] is a latent state in the state-space model. You could make a deterministic node that calculates what you are interested in from x[t]. Then you could make a data node that depends on that deterministic node in a constant manner.  The setup code of any of the filters would decide that the data likelihood contributions needs to be calculated, and that the deterministic node needs to be calculated first (for each particle). That would give you a way to calculate some kinds of functionals (but not for example an easy way to get the deviance), and you would still need to save the values in some way, so it is not a complete solution.

Another trick is to make a nimbleRcall that calls an R function that uses the compiled model from R to get whatever you want. The nimbleRcall could be called from the deterministic node in the previous trick, or you could insert calls to it in the source code, either in the buildAuxiliaryFilter run function or the auxFstep run function (or similar for the other filters). For example, you can have an R function that uses compiled_model$getLogProb(nodes) to sum the current log probabilities of some nodes in the model and save that to a global R object. You can call that R function via a nimbleRcall from the compiled_model itself or from an algorithm using the compiled_model.

Let me know any further questions if either of these tricks seem useful and are not clear. As Daniel said, there should be a way to do it based on nimble's programmability, but it's also pretty clear it should be easier than it is.

HTH
Perry

Lawson, Andrew B.

unread,
Aug 5, 2025, 5:03:00 PMAug 5
to nimble...@googlegroups.com
Hi Perry/Daniel
Thanks for the interest. I appreciate your comments about summarizing outside. I have been producing metrics based on the sampled states in fact. I plan to extend these soon. I also appreciate the tricks you mentioned and will consider some of these in some future developments. I have felt for some time that filtering could be a really useful alternative to full posterior sampling for time-based models. 
Thanks for your help


best wishes
Andrew


From: nimble...@googlegroups.com <nimble...@googlegroups.com> on behalf of Lawson, Andrew B. <laws...@musc.edu>
Sent: Friday, July 25, 2025 9:04 AM
To: nimble...@googlegroups.com <nimble...@googlegroups.com>
Subject: posterior functionals with nimbleSMC
 
Reply all
Reply to author
Forward
0 new messages