You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: permit location of 0/negative_infinity() in neg_binomial_2(_log)_lpmf #3302
It is currently possible to have location parameter of 0 in poisson_lpmf and negative_infinity() in poisson_log_lpmf; I've also verified this works with the _glm functions:
model {
real works =poisson_lpmf(1 | 0),
works2 =poisson_log_lpmf(1 | negative_infinity());
}
However, this is not possible with neg_binomial_2 functions:
model {
real fails =neg_binomial_2_lpmf(1 | 0, 1),
fails2 =neg_binomial_2_log_lpmf(1 | negative_infinity(), 1);
}
which errors out with:
Exception: neg_binomial_2_lpmf: Location parameter is 0, but must be positive finite!
I think it would be reasonable to permit similar behaviour as for the Poisson functions?
I think that would be OK. May I ask why you care about these boundary cases? Do they show up in real-world models you're fitting?
Also, we need to work out what the gradients are to avoid blow up if the 0 is an autodiff variable. The negative infinity case is harder for gradients.
In occupancy models where sites $i \in 1:I$ are surveyed over surveys $j \in 1:J$, I use the proportion of the maximum survey length $\Delta$ as offsets for each survey. Sometimes, sites "fail" to survey when for example a camera trap was malfunctioning. So in my models, I have to loop over every survey $j$ for each site $i$ to check if the offset was 0:
for (i in1:I) {
for (j in1:J) {
if (!is_inf(log_Delta[j, i])) {
target+= ...
}
}
}
Ideally, I could just vectorise over all surveys $j$ and handle the 0/negative_infinity() inside the _lpmf functions. Note that when $\Delta_{ij} = 0$, the linear predictor is $-\infty$.
An alternative way to code that now would be to use long form rather than wide form for (i, j). It will be more efficient if there are a lot of zeros, otherwise finagling the likelihood to ignore it will be more efficient. It does require the observations to also be zero or it'll just blow up and reject.
Activity
bob-carpenter commentedon Apr 14, 2026
I think that would be OK. May I ask why you care about these boundary cases? Do they show up in real-world models you're fitting?
Also, we need to work out what the gradients are to avoid blow up if the 0 is an autodiff variable. The negative infinity case is harder for gradients.
mhollanders commentedon Apr 14, 2026
In occupancy models where sites$i \in 1:I$ are surveyed over surveys $j \in 1:J$ , I use the proportion of the maximum survey length $\Delta$ as offsets for each survey. Sometimes, sites "fail" to survey when for example a camera trap was malfunctioning. So in my models, I have to loop over every survey $j$ for each site $i$ to check if the offset was 0:
Ideally, I could just vectorise over all surveys$j$ and handle the $\Delta_{ij} = 0$ , the linear predictor is $-\infty$ .
0/negative_infinity()inside the_lpmffunctions. Note that whenbob-carpenter commentedon Apr 15, 2026
Thanks---that's a really useful example.
An alternative way to code that now would be to use long form rather than wide form for
(i, j). It will be more efficient if there are a lot of zeros, otherwise finagling the likelihood to ignore it will be more efficient. It does require the observations to also be zero or it'll just blow up and reject.