Skip to content

Hypergeometric::pmf returns 0.0 or NaN for large populations #426

Description

@samoht

pmf divides three binomial coefficients held in f64. Once one of them passes f64::MAX the division happens between infinities, and nothing is signalled. ln_pmf, cdf and mean on the same object are correct.

let d = Hypergeometric::new(1030, 1, 515).unwrap();
d.pmf(0); // 0.0, exact value 1/2
d.pmf(1); // 0.0, exact value 1/2

let d = Hypergeometric::new(20000, 200, 300).unwrap();
d.pmf(0); // NaN, exact value 0.047931510683835526
d.pmf(3); // NaN, exact value 0.22687643066364876

C(1030, 515) exceeds f64::MAX while C(1029, 515) does not, so the first case overflows only the denominator and gives 0.0. Both coefficients overflow in the second, giving NaN.

The expected values above, computed with arbitrary-precision integers:

python3 -c "from math import comb; from fractions import Fraction as F; h=lambda N,K,n,x: F(comb(K,x)*comb(N-K,n-x),comb(N,n)); print([float(h(*a)) for a in [(1030,1,515,0),(1030,1,515,1),(20000,200,300,0),(20000,200,300,3)]])"

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