nimble-dev / nimble

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

posterior_predictive MCMC samplers don't check for proper "r" generation function #1145

Open danielturek opened 3 years ago

danielturek commented 3 years ago

Neither the posterior_predictive nor the posterior_predictive_branch samplers check for the existence of functional "r" random generation functions. This causes a problem in the case of user-defined distributions, when the "r" function is not provided, and thus we auto-generate a placeholder "r" function, which itself generates an error if called. When nodes following such a distribution are assigned either form of the posterior_predictive MCMC sampler, the posterior_predictive sampler will call the "r" function thus resulting in an error.

Bad news, this is an instance of our default MCMC sampler assignment producing an error at MCMC runtime. Good news, this produces a hard-stop error, and there's no chance of any "silent failure".

@perrydv @paciorek I'm not sure the best approach for fixing this. I don't have many great ideas:

paciorek commented 3 years ago

@danielturek Hmm, is a run-time error so bad given that the error message is quite clear: Error: user-defined distribution dZIP provided without random generation function.

Perhaps there is some elegance in detecting this at configuration or build time, but given other priorities, my initial reaction is that maybe we don't do anything.

Per another discussion, I am planning to add a note about user-defined 'r' being needed for initialization and posterior_predictive sampling in the manual and the user-defined distribution example.

Curious what @perrydv thinks.

danielturek commented 2 years ago

Yes, this is a legitimate concern @paciorek . Maybe it would be best if we inserted some checking into the R code which does sampler assignments. If a posterior_predictive_branch sampler is under consideration, then before assignment the necessary r functions are checked for (in the enclosing environment? in the global environment? exactly how to handle this would be a question for me). The sampler is then only assigned if all necessary r functions are found.

Does that sound reasonable?

paciorek commented 2 years ago

@danielturek I don't feel strongly. If you think we should do this and want to do it, that's fine with me. Given other release work I'm doing, I probably wouldn't do it myself for the upcoming release.

danielturek commented 2 years ago

@paciorek Agreed and understood. I'll put it on my "get to it when I can" list, which won't be for this upcoming release.

paciorek commented 1 year ago

@danielturek given post. pred. revamp, could you check if this can be closed?

danielturek commented 1 year ago

@paciorek This case is still not handled by the posterior_predictive sampler, but I'm ok with that for now. It feels like a fairly rare case to me.

If we agree this is something to prioritize, then I'm happy to address it, but I'd prefer if that wasn't for this upcoming release.

paciorek commented 1 year ago

Fine by me.