Skip to content

ChiSquared can return NaN values when requesting the inverse CDF #361

@IceTDrinker

Description

@IceTDrinker

Hello,

First of all thanks a lot for the work on statrs, it is very nice to have such a library in rust !

I know computing some of the statistics are hard computational problems (as in dealing with floating point precision and writing the proper algorithms matching theoretical maths formulas).

I would like to report the following case which fails :

use statrs::distribution::{ChiSquared, ContinuousCDF};

pub fn main() {
    let alpha = 0.01;

    let df = 129757f64;
    let chi2 = ChiSquared::new(df).unwrap();
    let chi2_lower = chi2.inverse_cdf(alpha / 2.0);
    let chi2_upper = chi2.inverse_cdf(1.0 - alpha / 2.0);
    if !(chi2_lower.is_finite() && chi2_upper.is_finite()) {
        dbg!(chi2_lower);
        dbg!(chi2_upper);
        panic!("{df}");
    }
}

I am aware these values are extreme, chi2_upper produces a NaN.

I prefer reporting it because the function docstring does not indicate the possibility to encounter NaNs and other software like scipy will give a finite answer.

It's now possible to use scipy's special functions in rust using the wrapper around their xsf library :

It may be of interest to you for testing purposes.

I still think having rust code able to compute special math functions has value (to avoid dependency on a C++ compiler or tooling), I have no idea what the best course of action for statrs is.

I know there are other combinations of parameters which will make the inverse CDF fail for the ChiSquared distribution.

Cheers

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