nimble icon indicating copy to clipboard operation
nimble copied to clipboard

On-the-fly custom distribution registration: scoping of r-function

Open danielturek opened this issue 7 months ago • 2 comments

Briefly: when using a custom-defined distribution, and making use of the automatic registration (at the time of model building) of the distribution, the automatic registration process will (very conveniently) create a placeholder random generation function (r-function) for you, if one doesn't exist.

However, when this process takes place stemming from nimbleMCMC, the creation of the r-function goes somewhere, but not somewhere that the ensuing compilation cannot locate it.

@paciorek Since I believe you authored the "automatic generation of the r-function" code, would you mind taking a look of the scoping for where this function is created? The relevant code is essentially lines 505-507 of distributions_processInputList.R

Reproducible example below:

library(nimble)

dNorm <- nimbleFunction(
    run = function(x = double(), mean = double(), sd = double(), Mean = double(), SD = double(), log=integer(0, default = 0)) {
        ####y <- Rrnorm(mean=Mean,sd=SD)
        y <- rnorm(1, Mean, SD)
        ####pdf <- Rdnorm(y,mean=mean,sd=sd,log=log)
        lp <- dnorm(y, mean, sd, log = 1)
        returnType(double(0))
        return(lp)
    })

code <- nimbleCode({
    for(i in 1:n) {
        x[i] ~ dNorm(mean=mu, sd=sigma, Mean=Mu, SD=Sigma)
    }
    # priors
    mu ~ dunif(-10, 10)    ## dunif(-10*Mu, 10*Mu)
    sigma ~ dunif(0, 10*Sigma)
})

Mu <- 0
Sigma <- 1
n <- 10
x <- rep(0,n)

constants <- list(n=n, Mu=Mu, Sigma=Sigma)
data <- list(x=x)
inits <- list(mu=Mu, sigma=Sigma)

mcmc.out <- nimbleMCMC(code=code,   ## ERROR
                       constants=constants,
                       data=data,
                       inits=inits,
                       niter=1000,
                       summary=TRUE,
                       monitors=c("mu","sigma"))

danielturek avatar May 09 '25 14:05 danielturek

yes, it's on my to-do list for 1.4.0. Do we need a branch with a fix quickly? If not, it will probably be some weeks.

paciorek avatar May 09 '25 14:05 paciorek

No quick fix necessary. Thanks, Chris.

danielturek avatar May 09 '25 15:05 danielturek