error building derivatives for HMC

70 views
Skip to first unread message

Abby Keller

unread,
Nov 20, 2023, 8:57:22 PM11/20/23
to nimble-users
Hi!

I'm trying to use HMC to help with sampling a spatial gaussian process, and I am getting some errors when compiling with compileNimble(myModel) -- I wasn't getting these errors before using HMC, and I'm imagining that they are from building derivatives. Based on printErrors() and looking through the temporary directory created by nimble, I think at least one error is coming from this function:

#write function for calculating capture probabilities
  calc_prob <- nimbleFunction(
    #input and output types
    run = function(nobs = double(0), # scalar
                   nsize = double(0), #scalar
                   hazard = double(2)) # 2-d array
    {
      returnType(double(1))  # 1-d array return
     
      #create empty array
      p <- rep(NA,nsize)
      #loop through sizes
      for(k in 1:nsize){
        #1-exp(-P(captured at all))
        p[k] <- 1-exp(-sum(hazard[1:nobs,k]))
      }
      return(p)
    },
    buildDerivs = TRUE
  )

Many error messages refer to "Interm_1082", and in a file in the temporary nimble directory, I'm finding that this refers to:
Screenshot 2023-11-20 174419.png

Is there something incompatible with building derivatives and taking the exponent of a sum of a vector?

I'm also having trouble parsing through all the error messages, so I've attached my code in case the errors are referring to something else. The nimble functions and model code are at the top, and building the model begins on line 552.

I think I've replaced all of the discrete distributions with continuous distributions, so I'm wondering if there's anything else I should be looking out for when trying to use HMC?

Thanks so much as always for your help!
Best,
Abby
Keller_IPM_20231120.R

Abby Keller

unread,
Dec 4, 2023, 5:03:52 PM12/4/23
to nimble-users
Documenting my solution to this problem in case anyone else encounters the same issue!

In my user-defined nimbleFunctions, I had to make sure to specify that derivatives should not be calculated for indices in the loop (and other related integers). 

For example:

#write function for calculating capture probabilities
  calc_prob <- nimbleFunction(
    run = function(nobs = double(0), 
                   nsize = double(0), 
                   hazard = double(2))
    {
      returnType(double(1))
     
      #create empty array
      p <- numeric(length = nsize, init = FALSE)

     
      #loop through sizes
      for(k in 1:nsize){
        p[k] <- 1-exp(-sum(hazard[1:nobs,k]))
      }
      return(p)
    },
    buildDerivs = list(run = list(ignore = c('k','nobs','nsize')))
  )

Reply all
Reply to author
Forward
0 new messages