Error with dynamic indexing

92 views
Skip to first unread message

Charles Kemp

unread,
Jul 9, 2021, 1:51:27 AM7/9/21
to nimble-users
Hi,

I'm trying to set up a very simple model in which variable y is generated from a categorical distribution that depends on variable x. The code (pasted below) gives me the error

Error in componentExtptrs[[1]] : subscript out of bounds

regardless of whether I introduce a local variable (code_v2) or not (code_v1).

I think that I'm using dynamic indexing incorrectly but can't figure out where the problem lies -- any suggestions would be appreciated!

My ultimate goal is to set up a more complicated example involving conditional probability tables -- e.g. something along the lines of Pearl's sprinkler example on slide 5 of 


But I think if I can figure out the two variable version below it will be straightforward to scale up to more complex models.

Thanks,

Charles.


code_v1 <- nimbleCode({
       x ~ dcat(px[1:2])
       y ~ dcat(py[x,1:2])
})

code_v2 <- nimbleCode({
       x ~ dcat(px[1:2])
       yd[1:2] <- py[x,1:2]
       y ~ dcat(yd[1:2])
})

px <- c(0.9,0.1)
py <- array(c(0.9,0.45, 0.10, 0.55), dim = c(2,2))

data <- list(
  x = 1,
  px = px,
  py = py 
)

inits = list(y=1)

samples <- nimbleMCMC(
    code = code_v1,
    data = data,
    inits = inits,
    niter = 100,
    nburnin = 20,
    thin = 1)

Perry de Valpine

unread,
Jul 9, 2021, 10:36:13 AM7/9/21
to Charles Kemp, nimble-users
Hi Charles,

Thanks for posting this clear and minimal reproducible example.

What I see going on is perhaps a minor misunderstanding which happened to reveal a minor bug.

The issue is that no variables are being monitored (stored by the MCMC to return in the samples).  You've set x to be data, which means it won't be sampled.  By default, monitored variables are top-level parameters, i.e. those with no stochastic parameters.  The only node sampled in either code_v1 or code_v2 will be y, and that is not monitored by default, so in these examples there are no monitored variables. I suspect you want x to be sampled by MCMC and hence should move it out of data and give it a value in inits.  It would then be monitored by default.  If you want it held fixed as data and only want 'y' monitored, please try adding "monitors = 'y' " so that something is monitored.

Meanwhile, I'll file a bug report in our GitHub repository that extracting samples from the compiled MCMC object fails if there are no columns to extract.  At this point it is surprising that this problem hasn't been discovered (or reported) previously.

HTH,
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/0a192761-7147-4449-afaa-91817e2e9dden%40googlegroups.com.

Charles Kemp

unread,
Jul 9, 2021, 7:47:21 PM7/9/21
to nimble-users
Thanks Perry -- this does indeed solve my problem!  I hadn't realized that I should specify 'monitors = y'

Charles

Reply all
Reply to author
Forward
0 new messages