[SciPy-User] discrepancy in chi square cumulative distribution and gamma functions?

Chad Fulton chadfulton at gmail.com
Wed Mar 25 20:08:35 EDT 2015


On Wed, Mar 25, 2015 at 4:43 PM, James Jensen <jdjensen at eng.ucsd.edu> wrote:
> From what I understand, the cdf of a chi-square distribution with k degrees
> of freedom and a test statistic x is
>
> lower_incomplete_gamma(k/2, x/2) / gamma(k/2)
>
> So you would expect the following two lines of code to yield the same
> result:
>
> cdf1 = scipy.stats.chi2.cdf(x, k)
> cdf2 = scipy.special.gammainc(0.5 * k, 0.5 * x) / scipy.special.gamma(0.5 *
> k)
>

It appears to be simply a naming/expectation issue. What is being
calculated with scipy.special.gammainc is not what e.g. Wikipedia
calls the lower incomplete gamma function but instead is what it calls
the regularized gamma function, see [1]. MATLAB's naming is consistent
with Scipy's (not with Wikipedia's), see [2].

In fact, both scipy.stats.chi2.cdf and scipy.special.gammainc call the
exact same underlying C function, it's just that scipy.stats.chi2.cdf
divides the arguments by 2 first.

You'll notice that the discrepancy-ratios you give are just equal to
scipy.special.gamma(0.5 * k), k = 1, 2, ..., 20.

[1] http://en.wikipedia.org/wiki/Incomplete_gamma_function#Regularized_Gamma_functions_and_Poisson_random_variables
[2] http://www.mathworks.com/help/matlab/ref/gammainc.html

Cheers,
Chad



More information about the SciPy-User mailing list