nimble icon indicating copy to clipboard operation
nimble copied to clipboard

scoping issue with user-defined distributions and nimbleMCMC

Open paciorek opened this issue 8 months ago • 2 comments

This must have worked at one point as it involves a manual example. Probably caused by me making some lookup/scoping more strict at some point. I can take a look before next release.

dmyexp <- nimbleFunction(
    run = function(x = double(0), rate = double(0, default = 1), 
        log = integer(0, default = 0)) {
        returnType(double(0))
        logProb <- log(rate) - x*rate
        if(log) return(logProb)
        else return(exp(logProb)) 
    })

code <- nimbleCode({
    y ~ dmyexp(x)

    # priors
    x ~ dunif(0,1)
  })

  constants <- list()
  data <- list(y=0)
  initial_values <- list(x=0.5)

  nimbleMCMC(code=code,
                         constants=constants,
                         data=data,
                         inits=initial_values)
Error: In sizeAssignAfterRecursing: 'rmyexp' is not available or its output type is unknown. This may occur if a user-defined function name is the same as the name of a function in a package that `nimble` uses.
 This occurred for: model_y[1] <<- rmyexp(=1,rate=model_x[1])
 This was part of the call:  {
  model_y[1] <<- rmyexp(=1,rate=model_x[1])
}
Execution halted

paciorek avatar Apr 08 '25 13:04 paciorek

@paciorek Noting that there does not exist a manual example which would fail, on account of this.

In "Section 12.2 User-defined distributions", both dmyexp and rmyexp are defined (as well as pmyexp and qmyexp) before being used in a model.

In "Section 12.2.1 Using registerDistributions for alternative parameterizations and providing other information", there's an explicit call to registerDistributions for the distribution dmyexp.

Each of these are distinct from the present case, which only defines dmyexp, relies on automatic registration, and only creates the model via nimbleMCMC (I'm not sure whether that detail is relevant or not)

danielturek avatar Apr 08 '25 17:04 danielturek

Right - I was being hasty. My thinking was that the manual example without the r function does fail.

paciorek avatar Apr 08 '25 18:04 paciorek

Same issue as #1545, fixed by #1554 .

paciorek avatar Jun 20 '25 22:06 paciorek