monitoring posterior probability in nimble

51 views
Skip to first unread message

Brook Milligan

unread,
May 27, 2026, 6:40:58 PMMay 27
to nimble-users
I am aware of the manual approach of monitoring the posterior probability of a model as described here from several years ago:


https://danielturek.github.io/public/sumPostLogDens/sumPostLogDens.html

In the intervening time, has a more automated means (e.g., nimble function) of accomplishing this been developed?

Cheers,
Brook

Daniel Turek

unread,
May 28, 2026, 7:27:53 AMMay 28
to Brook Milligan, nimble-users
Brook, within the past year or so we added a system for monitoring and recording "derived quantities" during an MCMC run.  There are pre-written mechanisms for monitoring:

- the cumulative mean (and/or variance) of any specified model nodes
- the sum of the posterior (log) probability of the entire model, or of arbitrary sets of nodes
- one can also write their own "derived quantity" nimbleFunction, for recording any quantity you might want

So, using the pre-written option for posterior (log) probabilities is what you're looking for.  Instead of describing all the syntax here, I'll point you to some documentation, which should explain everything:


Also documented in the Nimble User Manual:

After you set up the derived quantities you want (when configuring the MCMC), the recorded values will be available in the object returned from runMCMC().

Let me know if anything doesn't make sense, or you have additional questions!

Cheers,
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/C9932335-E0C4-4937-8439-8FC1277CC5F7%40biology.nmsu.edu.

Brook Milligan

unread,
Sep 17, 2026, 5:51:45 PMSep 17
to Daniel Turek, nimble-users

> On May 28, 2026, at 05:27, Daniel Turek <danie...@gmail.com> wrote:
>
> Brook, within the past year or so we added a system for monitoring and recording "derived quantities" during an MCMC run. There are pre-written mechanisms for monitoring:

Yes, that is exactly what I was looking for.

Section 7.2.3 of the NIMBLE manual states the following:

log-probabilities (log-density values) for single or (summed) groups of nodes

and section 7.2.3.1 states the following:

Alternatively, the mean, variance, and logProb derived quantities are directly supported
as arguments to configureMCMC, buildMCMC, and nimbleMCMC.

I have added the following argument to my nimbleMCMC() function call:

logProb=c(“a_0”, “a_1”) # these are two of the parameters in the model

The result is that the value generated by nimbleMCMC() includes $derived$logProb, which is a matrix of MCMC samples x parameters, but every value in the matrix is identical. Nothing changes across samples.

From the documentation, I expected the sum of ln(P) (not individual values per parameter) and I expected it to change with each sample.

Please explain how I should be doing this, as what seems to be the straightforward interpretation of the documentation is not consistent with what happens. There must be an (obvious?) catch.

Thanks a lot for your help.

Cheers,
Brook

Daniel Turek

unread,
Sep 17, 2026, 9:23:40 PMSep 17
to Brook Milligan, nimble-users
Brook, I apologize for such a brief response.

Can you confirm that the posterior samples (the values) of a_0 and a_1 are themselves changing, between these instances you mentioned?  If the values of these two parameters never transition (change), then it's entirely plausible that "every value in the matrix [of log-probabilities] is identical".  But, if the sample values of a_0 and a_1 do in fact change, then this feels odd.

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.

Brook Milligan

unread,
Sep 17, 2026, 9:40:49 PMSep 17
to Daniel Turek, nimble-users
Hi Daniel,

Thanks for responding.

Yes, the values of the parameters certainly do change. Only the reported ln(P) does not.

Cheers,
Brook
> To view this discussion visit https://groups.google.com/d/msgid/nimble-users/CAKbe0hqyP-r_o_h1GE8M4%3DfTb5Po0ukve9_TGwecV-MNSuUQQw%40mail.gmail.com.

Daniel Turek

unread,
Sep 18, 2026, 7:13:37 AMSep 18
to Brook Milligan, nimble-users
Yes, the documentation attempts to make the following distinction (character vector vs. list), but perhaps it's unclear.

When the logProb argument is provided as a *character vector*, then each element of the character vector has the logProb reported separately:

As in your case:
     logProb=c(“a_0”, “a_1”)     ## character vector; LP for a_0 and a_1 will be reported separately.

When the logProb argument is provided as a *list*, then each element of the list has a logProb reported, which represents the *sum* of the nodes comprising that individual element of the list.

For examples:
    logProb = list("a_0", "a_1")     ## a list, with two elements.  LP for each element, a_0 and a_1 will be reported separately.
    logProb = list(    c("a_0", "a_1")   )     ## a list, with one element (itself a character vector).  the sumed LP for the elements of this list will be reported
    logProb = list(   "x",    "y[1:10]",     c("a", "b", "c")   )     ## a list, with three elements...  3 different LPs will be reported, the second one being the sum for y[1:10], and the third being the summed LPs for nodes a, b, and c.

As for the static values of the LPs you're seeing, I can't address that without seeing a reproducible example.  Hopefully it's not a bug in the derived quantity system itself, although that's never out of the question.

Cheers,
Daniel



Brook Milligan

unread,
Sep 18, 2026, 10:33:39 AMSep 18
to Daniel Turek, nimble-users
Hi Daniel,

Thanks for the response. The main manual is not as clear about the nature of the logProb argument, but I see that the function-specific documentation is much better. Improving the former would be helpful.

logProb=TRUE seems to perform as advertised, as does “.all”. However, neither logProb=c() nor logProb=list() do, at least with respect to the specific nodes specified; perhaps I still do not understand how to set up the vectors/lists. I am concerned that neither change with the MCMC sample.

Please see the attached code.

Thanks a lot for your help.

Cheers,
Brook

log_P.R

Daniel Turek

unread,
Sep 18, 2026, 2:38:59 PMSep 18
to Brook Milligan, nimble-users
Brook, I'm pleased to report that I believe everything is operating as intended.  Though I see how the results you're seeing appear confusing.

The model you specified, in particular the priors for sigma, a_0, a_1, is:

MCMC_code <- nimbleCode({
    sigma ~ dunif(0,20)
    a_0 ~ dunif(-10,10)
    a_1 ~ dunif(-10,10)
    ...
    ...
})

The priors used for each of sigma, a_0, and a_1 is a uniform distribution, each spanning an interval of width = 20.  No matter what different values each of these nodes attain (so long as they remain within the specified intervals, [0,20] for sigma, and [-10, 10] for the a_0, a_1 terms), the logProb for any of these nodes will be constant, and equal to:

log(1/20) = -2.995732

which explains the value you're seeing.  Using the list syntax, when you *sum* the LP for these three nodes, the value you're seeing is:

log(1/20) + log(1/20) + log(1/20) = -8.987197

When you specify ".all" to sum the LP over *all* stochastic model nodes, this now adds together the logProbs for sigma, a_0, a_1, and each of the 10 y[i] terms.  The LP for each y[i] is a (log) normal density, the value of which depends on the current parameter values, which is why these LP values are changing.  You could verify all the arithmetic, by also recording the LP's for the 10 y[i] terms, verifying those yourself by evaluating the (log) normal density using the different sets of sampled parameter values (in combination with the different x[i] and y[i] values), then finally adding together the 13 different logProb contributions to arrive at the final "_all_nodes_" LP value.

Does this resolve the mystery?

Cheers,
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.

Brook Milligan

unread,
Sep 18, 2026, 3:07:18 PMSep 18
to Daniel Turek, nimble-users
Hi Daniel,

Got it. That makes perfect sense and I should have realized it from the outset. Sorry to bother. Thanks for taking a look.

Cheers,
Brook
Reply all
Reply to author
Forward
0 new messages