How to specify "inits"?

336 views
Skip to first unread message

Luca

unread,
Sep 14, 2021, 5:43:21 AM9/14/21
to nimble-users
Dear Nimble devs and users,

Suppose I have a compiled MCMC sampler for a compiled Nimble model, defined eg with the standard sequence

model <- nimbleModel(...)
Cmodel <- compileNimble(model)
confmodel <- configureMCMC(Cmodel)
mcmcsampler <- buildMCMC(confmodel)
Cmcmcsampler <- compileNimble(mcmcsampler)


The command runMCMC() allows me to run the Monte Carlo sampler, and it conveniently allows me to choose initial values for the sampled parameters and also (constant) values for the hyperparameters, via the "inits=" argument – without needing to recompile anything.

I wonder how I can set initial values for parameters and values for hyperparameters if I'd like to simulate via the

...$run()

method instead. I noticed that it doesn't accept an "inits=" argument (this time I checked properly 😬).

It seems that buildMCMC() and configureMCMC() do not accept "inits" either, so the only place to set new inits would be in the compiled model itself. Would this mean that the MCMC sampler would need to be re-configureMCMC()'d and re-buildMCMC()'ed?

Cheers!
Luca

Daniel Turek

unread,
Sep 14, 2021, 7:43:22 AM9/14/21
to Luca, nimble-users
Yes, no problem Luca.  Prior to running Cmcmcsampler$run, you will need to store the initial values into the compiled model object (Cmodel).

This can be done as easily as:

Cmodel$a <- 5

or for a vector:

Cmodel$vector_variable <- c(4, 6, 9)

I will also encourage you to look at chapter 6 (in particular section 6.2) of the NIMBLE user manual, which describes using model objects:

The compiled MCMC (Cmcmcsampler) will perform calculations using the compiled model object, thus starting out the sampling at whatever "initial" values are current in the compiled model (Cmodel).





--
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/8cf6c57c-3226-8edf-423e-f545345e766d%40gmail.com.

Luca

unread,
Sep 14, 2021, 9:11:44 AM9/14/21
to db...@williams.edu, nimble-users
Hi Daniel, thank you once more!!

> I will also encourage you to look at chapter 6 (in particular section 6.2) of the NIMBLE user manual, which describes using model objects

I did, and read about the method "...$setInits()", but I wasn't sure whether using that method would require recompilation of the Monte Carlo sampler – I think the Manual is silent on that. This is not a criticism of the Manual though, which is amazing! :)

By the way, there's a passage that's a little confusing, in §6.1.1.2:

"To change data values without any modification of which nodes are flagged as containing data, simply use R’s usual assignment syntax [...]
It is possible to change the data values in a compiled model using setData, but we don’t recommend doing this because setData won’t modify which nodes are flagged as
containing data in the already-constructed MCMC"

It says that either way won't change the flagging of the nodes, so it isn't clear why "setData" is not recommended.

Cheers,
Luca

Daniel Turek

unread,
Sep 14, 2021, 9:45:05 AM9/14/21
to Luca, nimble-users
model$setInits(initsList) does not require recompilation of the model.  You can set new values (initial values) into either the uncompiled Rmodel object, or the compiled Cmodel object.  But understand, the uncompiled MCMC uses the uncompiled model, and the compiled MCMC uses the compiled model object.  So if you're running the compiled MCMC, then you should set new initial values into the compiled model.

model$setInits(initsList) is the same as calling:
model$variable <- value
for all the elements in initsList.

I can see why that passage from the manual is confusing.  What it means is to say is:

"To change values in the model (which could be flagged as data, or not) without changing which values in the model are flagged as data, use the usual assignment syntax [....] to store these new values into the model.  Otherwise, if you store new values into the model using model$setData(...), then you'll store new values into the model, and also modify the data flags in the model.  However, if you change the data flags in a compiled model using Cmodel$setData(...), this probably won't accomplish what you want because if you already created an MCMC (and/or compiled it), the the model data flags have already been inspected (at the time of calling configureMCMC) to determine which nodes need to be sampled by the MCMC, so if you change the data flags in the model, it won't change the sampling strategy of the [already-exisiting] MCMC.  If you want to change which nodes are data, and have the sampling of the MCMC reflect that, then you'll need to update the model data flags using model$setData, then go back to configureMCMC, buildMCMC, and then compile the newly build MCMC algorithm".

But that was too much to put in the manual.  The administration just wouldn't have it  ;)

Daniel



Luca

unread,
Sep 15, 2021, 6:25:38 AM9/15/21
to db...@williams.edu, nimble-users
Hi Daniel, now it's clear, thank you very much again for a detailed and patient explanation!

Cheers,
Luca

On 2021-09-14 15:44, Daniel Turek wrote:
> model$setInits(initsList) does not require recompilation of the model.  You can set new values (initial values) into either the uncompiled Rmodel object, or the compiled Cmodel object.  But understand, the uncompiled MCMC uses the uncompiled model, and the compiled MCMC uses the compiled model object.  So if you're running the compiled MCMC, then you should set new initial values into the compiled model.
>
> model$setInits(initsList) is the same as calling:
> model$variable <- value
> for all the elements in initsList.
>
> I can see why that passage from the manual is confusing.  What it means is to say is:
>
> "To change values in the model (which could be flagged as data, or not) without changing which values in the model are flagged as data, use the usual assignment syntax [....] to store these new values into the model.  Otherwise, if you store new values into the model using model$setData(...), then you'll store new values into the model, and also modify the data flags in the model.  However, if you change the data flags in a compiled model using Cmodel$setData(...), this probably won't accomplish what you want because if you already created an MCMC (and/or compiled it), the the model data flags have already been inspected (at the time of calling configureMCMC) to determine which nodes need to be sampled by the MCMC, so if you change the data flags in the model, it won't change the sampling strategy of the [already-exisiting] MCMC.  If you want to change which nodes are data, and have the sampling of the MCMC reflect that, then you'll need to update the model data flags using
> model$setData, then go back to configureMCMC, buildMCMC, and then compile the newly build MCMC algorithm".
>
> But that was too much to put in the manual.  The administration just wouldn't have it  ;)
>
> Daniel
>
>
>
>
> On Tue, Sep 14, 2021 at 9:11 AM Luca <pgl...@gmail.com <mailto:pgl...@gmail.com>> wrote:
>
> Hi Daniel, thank you once more!!
>
> > I will also encourage you to look at chapter 6 (in particular section 6.2) of the NIMBLE user manual, which describes using model objects
>
> I did, and read about the method "...$setInits()", but I wasn't sure whether using that method would require recompilation of the Monte Carlo sampler – I think the Manual is silent on that. This is not a criticism of the Manual though, which is amazing! :)
>
> By the way, there's a passage that's a little confusing, in §6.1.1.2 <http://6.1.1.2>:
Reply all
Reply to author
Forward
0 new messages