nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

`registerDistributions` type-checking does not allow default values for `x` #1345

Closed danielturek closed 9 months ago

danielturek commented 11 months ago

When defining the density function of a custom distribution, the type-checking that takes place in registerDistributions does not allow a default value for x. During the type-checking, we require an identical to pass for the return type defined by the density function (in the dist <- nimbleFunction(x = ...) of the argument list), and the returnType statement of the generation function. If you provide a default value of x in the density function, this identical check fails. Example below:

library(nimble)

ddist <- nimbleFunction(
    run = function(x = double(0, default = 0), log = integer(default = 1)) {
        returnType(double())
        return(0)
    }
)

rdist <- nimbleFunction(
    run = function(n = integer()) {
        returnType(double())
        return(0)
    }
)

registerDistributions('ddist')

## Error in FUN(X[[i]], ...) : 
##   checkDistributionFunctions: random generation function `rdist` is missing `returnType` or `returnType` 
##   does not match the type of the `x` argument to the corresponding density function.
paciorek commented 10 months ago

I think I would argue that instead we should not allow default arguments for user-defined distributions (by analogy with standard distributions).

@danielturek what do you think about me adding a check that errors out if there is a default?

danielturek commented 10 months ago

@paciorek Yes, that makes more sense, stepping back and looking at this again. I'm on board with your suggestion.

paciorek commented 10 months ago

Ok, I've added an error trap in checkDistributionFunctions in branch fix_1345.

paciorek commented 9 months ago

Fixed by PR #1371