Access previous values of nodes

25 views
Skip to first unread message

Patrick Schmidt

unread,
Dec 15, 2021, 5:15:36 AM12/15/21
to nimble-users

Hi everyone,

what is the best way to handle access to previous values of nodes? I mean not only the current value of the chain, but all values. Something like

values(models, nodes, iterations)

Is this possible with a user-written sampler by accessing the modelValues object appropriately? Or is it even possible with a user-written distribution (that depends on past values) that could then be used with all standard samplers?

-----------------------------

Probably irrelevant background:

I am working on a Quasi-Bayesian/Laplace-type approach to Generalized Method of Moments estimation. The basic idea is to substitute the likelihood in an MCMC procedure with a quasi-likelihood that is motivated by the CLT on the moments of a given model.

Consider a moment function m(θ) with parameter θ . One would specify a “quasi-likelihood” with average moments m and weighting matrix W as

p(D|θ)∼exp(m^T W m).

and estimate the “quasi-posterior” with a MCMC procedure.

The problem I have is that the weighting matrix W should be the inverse of a consistent covariance estimate of the moments at the true parameter. As this parameter is unknown, one usually relies on iterative procedures. One solution is to take a covariance estimator using the posterior mean of the preceding chain.

Perry de Valpine

unread,
Dec 16, 2021, 8:58:02 PM12/16/21
to Patrick Schmidt, nimble-users
Hi Patrick,

This sounds very interesting.  I'm going to give some fairly high-level answers and am happy to dig into details later if it helps.

The short answer is no, but there are several ways you could use tricks to access what you want.  I'll also point you to some source code.

The main nimbleFunction driving the MCMC iterations is returned from buildMCMC.  It's in MCMC_build.R and is, I hope, somewhat readable.  There are two (or three) modelValues objects created in its setup code.  mvSaved has only one set of all model variables.  That is used to hold the "current" state of the model for purposes of steps such as Metropolis-Hastings accept/reject.  mvSamples is where the MCMC output is stored.  (mvSamples2 is the potential third one involved, which also stores MCMC output if a monitors2 and optionally thin2 are defined, but this doesn't seem important to discuss.).  Importantly, only buildMCMC (but not the samplers) uses mvSamples.  Normally, each sampler only uses mvSaved but does not have access to mvSamples.  So that is why in normal operation, samplers can't look at past states of the chain.

Here are several ways you could use some flexibility to do it.  First, you could have a sampler create its own modelValues or more simply its own matrix and store a history of states on its own.  This would be independent of whatever is being stored in mvSamples.  This (using a matrix) is the approach used for adaptation (self-tuning) in a block (multivariate) MH sampler, so you could look at sampler_RW_block in MCMC_samplers.R to see how it's done there.  See the empirSamp matrix.

Second, you could have a sampler (that you write) call back to R using a nimbleRcall.  From R, you are free to access the compiled MCMC object, including its mvStates.  This would not be the most efficient approach, since going into R evaluation is always slower and you'd be extracting the mvStates matrix anew every time.  But it would work and if you are more worried about methodology than computational efficiency right now, it might be your easiest option. 

Third, you could write a sampler and have its setup code look up into its calling environment (parent.frame()) and find mvSamples there.  Then if you use mvSamples in run code, the nimble compiler should arrange for your sampler to be accessing the compiled modelValues object from the MCMC driving function (the buildMCMC object).  This would be tricky, non-standard, and I could imagine circumstances where it could be confusing to an end user.  However again if your goal is a research system, it might work.

I hope one or more of these ideas helps!

-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 on the web visit https://groups.google.com/d/msgid/nimble-users/81fdc3ee-614b-419f-9ba4-fb2a77cd36fan%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages