Negative Binomial Overflow during warmup

Hello everyone,

I am fitting Negative Binomial count models and generating Negative Binomial draws in the generated quantities block. While the sampling is fine, I get an error because there is an overflow in the random number generator:

"Error in sampler$call_sampler(args_list[[i]]) : "                                                                                               
[52] "  Exception: gamma_rng: Inverse scale parameter is inf, but must be finite!  (in 'model2152242e4a15e1_CBDmodelnew' at line 106)"

For info, the corresponding line is as follows:

mufor[pos] = gamma_rng(phi,phi/exp(k));

After analyzing the sampling, it turns out that in the first 20-50 iterations, k takes relatively high values before converging. To solve this issue, I would then have two questions:

  1. Is it possible to define a conditional statement in the generated quantities block which depends on the iteration development, such that for e.g. for the first 100 iterations, the output is zero ?
  2. More generally, is it possible to define a generated quantities block which would be evaluated only during sampling and not during warmup?

A temporary solution would be to define a conditional statement and say that if k is too high, then the output is zero but I guess there must be a more elegant and general solution.

This topic was partly discussed by @bgoodri in this post and @Bob_Carpenter in this discussion but I couldn’t find the answer to my questions.

Thanks for your help!

1 Like

No, or at least some Stan developers have strongly resisted having a Stan program be stateful

Just call stan with save_warmup = FALSE.


No, but if you are OK with branching, it IMHO makes a bit more sense to branch on the value of k.

Good luck!

1 Like

Thanks both for your suggestions. I will try to fit the model with the option save_warmup = FALSE and see if I still get the error. Otherwise, I will include a conditional statement on the value of k, for e.g. outputs 0 if k greater than 15.

Just for information in case a future user has the same problem: I still got the error when using save_warmup = FALSE as option because it does not prevent from calculating the gamma random numbers.

However, the problem was solved by conditioning on the value of k :

if ( fabs(k)>15){
mufor[pos] = 0;
} else {
mufor[pos] = gamma_rng(phi,phi/exp(k));