Skip to content

Feature request: permit location of 0/negative_infinity() in neg_binomial_2(_log)_lpmf #3302

Description

@mhollanders

Good day,

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?

Activity

  1. bob-carpenter commented on Apr 14, 2026

    @bob-carpenter
    Member

    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.

  2. mhollanders commented on Apr 14, 2026

    @mhollanders
    Author

    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 in 1:I) {
      for (j in 1: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$.

  3. bob-carpenter commented on Apr 15, 2026

    @bob-carpenter
    Member

    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.

  4. transferred this issue fromstan-dev/stanon Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    distributionsIssues that deal with distribution functions: pdf, pmf, cdfgood first issuenumericsNumerical issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions