Skip to content

Commit

Permalink
allow edge case of negative binomial with 0 mean
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed Feb 13, 2023
1 parent f6a0334 commit 600227f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions inst/stan/functions/observation_model.stan
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ int[] report_rng(vector reports, real[] rep_phi, int model_type) {
}

for (s in 1:t) {
// defer to poisson if phi is large, to avoid overflow
if (sqrt_phi > 1e4) {
sampled_reports[s] = poisson_rng(reports[s] > 1e8 ? 1e8 : reports[s]);
if (reports[s] < 1e-8) {
sampled_reports[s] = 0;
} else {
sampled_reports[s] = neg_binomial_2_rng(reports[s] > 1e8 ? 1e8 : reports[s], sqrt_phi);
// defer to poisson if phi is large, to avoid overflow
if (sqrt_phi > 1e4) {
sampled_reports[s] = poisson_rng(reports[s] > 1e8 ? 1e8 : reports[s]);
} else {
sampled_reports[s] = neg_binomial_2_rng(reports[s] > 1e8 ? 1e8 : reports[s], sqrt_phi);
}
}
}
return(sampled_reports);
Expand Down

0 comments on commit 600227f

Please sign in to comment.