MCMC is either stuck or in never-ending loop

105 views
Skip to first unread message

Lukmann Haqeem

unread,
Jul 9, 2021, 6:22:47 AM7/9/21
to nimble-users
Hi all,

When I ran the model code below for ten iterations, it appears the MCMC was stuck or in a never-ending loop. My machine's memory was also exhausted. I'm not sure what the real issue here, but I suspect it has to do with the line highlighted in red. Could you identify the problem? Also, if you ever come across the same problem, do share with me how you solved it—many thanks. Those objects in CAPS are the input data.

code <- nimbleCode({
# 1. SPECIFY PRIORS
nhS ~ dbeta(9, 1)
  
for (s in 1:sex) {
  pie[s] ~ dbeta(1, 1) 
}

for (y in 1:year) {
  for (a in 1:age) {
    for (s in 1:sex) {
      hS[y,a,s] ~ dbeta(1, 1) 
    }
  }
}
  
#size at y=1
n[1,1:3,1] <- c(50000, 30000, 10000) 
n[1,1:3,2] <- c(50000, 30000, 10000) 

#2. STATE PROCESS
for (y in 1:(year-1)) {
    for (s in 1:sex) {
    ns[y,1:3,s] <- n[y,1:3,s] * hS[y,1:3,s] * nhS
      
    na[y,1,s] <- sum(ns[y,1,s] * c(1-pie[1], 0,        0))
    na[y,2,s] <- sum(ns[y,2,s] * c(pie[1],   1-pie[2], 0))
    na[y,3,s] <- sum(ns[y,3,s] * c(0,        pie[2],   1))
      
    nb[y,1,s] <- sum(na[y,1,s] * c(RHO[y,1], RHO[y,2], RHO[y,3]))
    nb[y,2,s] <- sum(na[y,2,s] * c(0,        1,        0))
    nb[y,3,s] <- sum(na[y,3,s] * c(0,        0,        1))
      
    n[y+1,1:3,s] <- nb[y,1:3,s] * SEX[y,s]
    }
}

#3. OBSERVATION PROCESS
for (y in 1:year) {
  for (a in 1:age) { 
    for (s in 1:sex) {
    CULL[y,a,s] <- n[y,a,s] * (1-hS[y,a,s])
    }
  }
}

#4. DERIVED PARAMETERS
for (y in 1:year) {
  yearling[y] <- sum(n[y,1,1:2])
  juvenile[y] <- sum(n[y,2,1:2])
  adult[y] <- sum(n[y,3,1:2])
  Total[y] <- sum(n[y,1:3,1:2])
}
})

Cheers,
Lukmann

Lukmann Haqeem

unread,
Jul 9, 2021, 6:32:35 AM7/9/21
to nimble-users
Hi again,

Forgot to highlight the line I suspect is 'the problem'. It is this one:    

CULL[y,a,s] <- n[y,a,s] * (1-hS[y,a,s])  

Perry de Valpine

unread,
Jul 9, 2021, 12:05:59 PM7/9/21
to Lukmann Haqeem, nimble-users
Hi Lukmann,

I suspect the problem occurs when the MCMC is being set up, not when it is being run, but I can't tell from the information provided.

But first, your model strikes me as somewhat unusual because I can't see nodes that look like they would be for data.  After the priors for nhS, hS, and pie, everything is deterministic.  For data to inform parameters, the model would need some stochastic nodes for the data.

Setting that aside, could you please try one or both of these lines before running your code and let me know if either or both makes the problem go away:
nimbleOptions(MCMCuseConjugacy = FALSE)
nimbleOptions(MCMCjointlySamplePredictiveBranches = FALSE)

Another part of your model code that did not make sense to me were the lines like this:
na[y,1,s] <- sum(ns[y,1,s] * c(1-pie[1], 0,        0))

That looks equivalent to
na[y,1,s] <- ns[y,1,s] * (1-pie[1])

Are you wanting to make one line of a matrix multiplication, i.e. to sum a set of components, which could be done either like this
na[y,1,s] <- sum(ns[y,1:3,s] * c(1-pie[1], 0,        0))
or, equivalently, like this
na[y,1,s] <- inprod(ns[y,1:3,s], c(1-pie[1], 0,        0)) ?

Those would still be equivalent to
na[y,1,s] <- ns[y,1,s] * (1-pie[1])
because of the zeros, but at least they would be a framework you might extend as you develop your model.  I'm guessing here because I couldn't make sense of how it was written.

-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/6cda5028-4289-471b-a344-3e326e5a8394n%40googlegroups.com.

Lukmann Haqeem

unread,
Jul 9, 2021, 11:49:16 PM7/9/21
to nimble-users
Hi Perry, 

I will try to insert more stochasticity into the model after I solved the MCMC run issue. Yes, the matrix multiplication process can be simplified further, but I put it there just to serve as a visual representation of the processes (for my colleagues). 

Thanks for the feedback.   

Reply all
Reply to author
Forward
0 new messages