Skip to content

NegativeBinomial::pmf(0) is NaN when p is 1 #463

Description

@DRMacIver
use statrs::distribution::{Discrete, DiscreteCDF, NegativeBinomial};

fn main() {
    for (r, p) in [(1.0, 1.0), (2.5, 1.0), (1.0, 0.999)] {
        let nb = NegativeBinomial::new(r, p).unwrap();
        println!("r = {r}, p = {p}: pmf(0) = {:?}, cdf(0) = {:?}", nb.pmf(0), nb.cdf(0));
    }
}

Output:

r = 1, p = 1: pmf(0) = NaN, cdf(0) = 1.0
r = 2.5, p = 1: pmf(0) = NaN, cdf(0) = 1.0
r = 1, p = 0.999: pmf(0) = 0.9990000000000004, cdf(0) = 0.999

With p = 1 every trial succeeds, so all the mass is at 0 and pmf(0) should be 1 — which is what cdf(0) reports. new accepts p = 1, and p = 0.999 works.

Tested on statrs 0.19.1 and current main (50fcf4d).

(This was another bug found using hegel.)

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions