Possible bug with `as.list(...$mvSamples2)`?

10 views
Skip to first unread message

PierGianLuca

unread,
Jul 19, 2024, 8:07:10 AM (3 days ago) Jul 19
to nimble-users
Dear Nimble devs,

For a compiled Nimble MCMC run, I have `monitors2` set to one particular vector variable 'K[1:30]', which holds the cluster number occupation of 30 datapoints. In the MCMC run I have the arguments

...
niter = 1024,
thin2 = 1024,
...

so that 'K' is only saved during the last MCMC step. A slice sampler is used for 'K', and in the Nimble configuration output I correctly see

```
...
===== Samplers =====
slice sampler (31)
...
- K[] (30 elements)
...
```


All works fine, but I notice a quirk when I eventually try to access `mvSamples2`. If I access it with `as.matrix()`, then 'K' is correctly given as a 30-element vector. But if I access it with `as.list()`, then 'K' is incorrectly given as a single scalar. Here is a snippet of debugging lines inserted into the code:

```
cat('\nNow as matrix:\n')
str(as.matrix(Cmcsampler$mvSamples2))
cat('\nNow as list:\n')
str(as.list(Cmcsampler$mvSamples2))
cat('\n***\n')
```

and here is the corresponding output:

```
Now as matrix:
num [1, 1:30] 46 46 46 46 45 46 46 45 45 46 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:30] "K[1]" "K[2]" "K[3]" "K[4]" ...

Now as list:
List of 1
$ K: num [1, 1] 46

***
```

Sorry for not sending a minimal example yet. I was wondering whether it's maybe straightforward for you to check for a possible bug in the Nimble code about this. My guess is that something happen because there's only one MCMC sample of 'K'. If it's best for you to have a minimal working example, I'm happy to prepare one!

Cheers,
Luca

Daniel Turek

unread,
Jul 19, 2024, 12:31:28 PM (3 days ago) Jul 19
to PierGianLuca, nimble-users
Luca, thanks for reporting this.

I was able to recreate the problem you described.  It is indeed a malfunction in the "as.list" function, used for changing a modelValues object into a list representation of the MCMC samples.  And, as you identified, it only occurs in this case of retaining one single (post-thinning) MCMC sample, of a vector-valued model parameter, or also a higher-dimensional-valued model parameter (for example, a 2-dimensional array of parameters).

We'll look into fixing this.  One workaround for you, is the "as.matrix" function, for changing a modelValues object into a matrix representation of the MCMC samples, works correctly for this situation.  So, in a pinch, you can use:

as.matrix(Cmcsampler$mvSamples2)

to extract the samples, although I see you've already noted that in your email.

Cheers, and thanks again,
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 on the web visit https://groups.google.com/d/msgid/nimble-users/567d0f0c-a934-ed0d-0be4-ec5c8483633d%40magnaspesmeretrix.org.

PierGianLuca

unread,
10:22 AM (7 hours ago) 10:22 AM
to Daniel Turek, nimble-users
Hi Daniel,

Thank you for confirming this, and for the fixing tips! "as.list" is a life-saver (great idea by you devs!) when the sampled variables include a combination of scalars, vectors, matrices, and arrays. I'll simply change thin2 into "floor(niter/2)" to bypass the problem; it's actually useful to have a look at the middle of the sampling steps in any case, so it's a win-win for me :)

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