# [SciPy-dev] Bug/Error with chi-squared distribution and df<1

Kevin Dunn kgdunn@gmail....
Tue Sep 22 16:48:57 CDT 2009

```> ti, 2009-09-22 kello 14:27 -0400, josef.pktd@gmail.com kirjoitti:
> [clip]
>> Taking the hint with incomplete gamma, the following looks good. This
>> would mean until Pauli fixes scipy.special if it your fix works, we
>> could also use gammainc directly. I don't know the differences between
>> the various implementations well enough to see whether we buy some
>> other problems with this
>
> The C function `igam` called by `chdtr` is the same as what is exposed
> as `gammainc`.

Thanks again to all the helpful replies on this list.  I'm using this
relationship between the chi-squared distribution and the gamma
function to evaluate the CDF and inverse CDF.

As an example for future readers of this list, let our value be x = 3
with 0.6 degrees of freedom,

Chi-square CDF
----------------
* In MATLAB: chi2cdf(3, 0.6)   % 0.957890536704110
* In R: pchisq(3, 0.6)  # 0.9578905
* And temporarily in SciPy:  scipy.special.gammainc(0.6/2.0, 3/2.0)
# note the order of parameters is flipped; result =
0.95789053670411062
* Or another temporary possibility: scipy.stats.gamma.cdf(3/2.0,
0.6/2.0) # this time the parameters are in the expected order

But ideally in SciPy: scipy.stats.chi2.cdf(3, 0.6)  # currently
returns a NaN, but should return the same result above

Inverse Chi-square CDF
----------------
* In MATLAB: chi2inv(0.95789, 0.6)  % 2.999980885343446
* In R: qchisq(0.95789, 0.6)    #  2.999981
* And temporarily in SciPy: scipy.special.gammaincinv(0.6/2.0,
0.95789) * 2.0  # again we reorder the parameters; result =
2.9999808853434109
* Or another temporary possibility: scipy.stats.gamma.ppf(0.95789,
0.6/2.0)*2 # 2.9999808853434109

But ideally in SciPy: scipy.stats.chi2.ppf(0.95789, 0.6) # currently
returns a NaN, but should return the same result above

Hope that helps in the mean time,
Kevin

> --
> Pauli Virtanen
```