registerDistributions Error

66 views
Skip to first unread message

Phd Student

unread,
Jul 8, 2021, 11:38:22 AM7/8/21
to nimble-users
Hi! 

I cant register a simulation function as nimble always seems to register my function as a density function. This doesn't just happen with my own function, but also with example from the help file. Am I doing something wrong here?

Here an example:

> rdirchmulti <- nimbleFunction(
+   run = function(n = integer(0), alpha = double(1), size = double(0)) {
+     returnType(double(1))
+     if(n != 1) print("rdirchmulti only allows n = 1; using n = 1.")
+     p <- rdirch(1, alpha)
+     return(rmulti(1, size = size, prob = p))
+   })
> registerDistributions("rdirchmulti")
Registering the following user-provided distributions: rdirchmulti
Error in FUN(X[[i]], ...) : 
  checkDistributionFunctions: density function for rdirchmulti has invalid or missing returnType, which must be 'double(0)' (or equivalently 'double()').


Thanks for helping me out with this. 

Perry de Valpine

unread,
Jul 8, 2021, 11:53:11 AM7/8/21
to Phd Student, nimble-users
I don't think it's possible or makes sense within nimble to register an r function without a corresponding d function.  When a distribution is used in a model, the model constructs code internally that will call the d function or the r function where needed in calculate or simulate methods, respectively.  For use by an algorithm such as MCMC, it is common that one needs the d function but not the r function.  In that case, one can provide a dummy r function, or one can let nimble provide a dummy r function automatically.  (If you don't need any customization of how a distribution is registered, it will be registered automatically by nimbleModel, and a dummy r function will be created if necessary.)   For example, in MCMC, the r function would only be needed for uninitialized nodes or posterior predictive nodes.  If you have an algorithm use case where you are sure only the r function is needed and the d function is not needed (i.e. calculate will never be called for the node), you could write a dummy d function and register them together as a distribution.  Registration is based on the d function's name, as you've seen.  I hope that makes sense.

It looks like you are reporting that the example doesn't work when you have modified it and run it without preceding code.  On my system, it does work as provided.  For follow-up clarifications, it would help if you can describe your use case in more detail.  We haven't seen a use case with the need you are asking for.

We welcome all questions and try to support all users on this list, but I can't recall if we've seen an anonymous question like this previously.  

-Perry

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/5222566b-1cca-430a-b392-be698b6ef8f2n%40googlegroups.com.

Perry de Valpine

unread,
Jul 9, 2021, 12:32:49 PM7/9/21
to Phd Student, nimble-users
For list readers: We followed up off-list on this issue.  When a distribution is registered with nimble, the "d" name is used and the "r" name is automatically also searched for.  In addition there was an issue making the r function and d function conform to each other in terms of argument types.
-Perry
Reply all
Reply to author
Forward
0 new messages