# Chi-squared inverse function and incomplete gamma function

1. Jun 24, 2011

### onanox

is there a way to explicitly express the chi-squared inverse function?

when programming it, I have had to resort to a guessing system where I find a chi value that is too low and too high, and evaluate the chi-squared CDF to reset the high and low points iteratively until it is within a preset tolerance to the correct value.

However, this a clunky and inefficient method.

Additionally, for large degrees of freedom (50,000 or so) I can't even calculate the chi-squared CDF because the values are too high. I noticed that this function can be expressed as a ratio of the gamma function and incomplete gamma function, so It occured to me to express the value as a difference of logs of each piece. However, I have only found expressions of the incomplete gamma function as sums, which are difficult (impossible?) to log. is there any way to express the incomplete gamma function as a product?

2. Jun 25, 2011

### SW VandeCarr

I think the scaled inverse chi square distribution is what you're after. The scaling factor is the population variance. The characteristic function of the incomplete gamma is expressed as a product (as are all characteristic functions). This link may help:

http://jacki.wikidoc.org/index.php/Scale-inverse-chi-square_distribution [Broken]

Last edited by a moderator: May 5, 2017
3. Jun 25, 2011

### onanox

well isnt the characteristic function for the incomplete gamma function an integral? which is a sum?

and just to clarify (and I might be wrong here) but I am not talking about the inverse chi-squared distribution, I am talking about the inverse function for the chi-squared function.

4. Jun 26, 2011

### bpet

To invert the gamma cdf, two different approaches are described in the documentation for gaminv in MATLAB and qgamma in R.

5. Jun 27, 2011

### onanox

in MATLAB, they just numerically solve the integral, however, with large degrees of freedom (50,000 or so) the program will crash because the values are just too large.
I guess this question is better suited for a programming board, and directed at larger number data types rather than better mathematical methods.

6. Jun 27, 2011

### sfs01

If you just want the quantile (inverse CDF) of the chi squared distribution, that can be approximated with the normal distribution inverse CDF for large numbers of degrees of freedom.

That isn't known analytically either, but matlab etc. have good approximations.

Or do you want something else? I have to admit I'm having trouble relating this to your question about the characteristic fn of a gamma function.

7. Jun 28, 2011

### bpet

MATLAB has no problems with large dof, e.g.

>>chi2inv(0.75,1e6) % returns 1.001e6

8. Jun 28, 2011

### onanox

I meant, matlab's documentation does not give any real info except that they solve it numerically, yet when I try to use a similar method in AS3, the program errors on high values.
I apologize for my ambiguity.

9. Jun 28, 2011

### Stephen Tashi

10. Jun 29, 2011

### bpet

Here are the references for GAMMAINCINV which inverts the regularized incomplete gamma function (i.e. the gamma cdf with scale parameter 1) using Newton's method:

% References:
% [1] Abramowitz & Stegun, Handbook of Mathematical Functions,
% Sec. 6.5, especially 6.5.29 and 26.5.31.
% [2] Knuesel, L. (1986) "Computation of the Chi-square and Poisson
% Distribution", SIAM J. Sci. Stat. Comput., 7(3):1022-1036.

For the Newton iterations, to compute the derivative of gammainc (the gamma pdf) you should use exp(gammaln(...)*...) to avoid underflow/overflow.

11. Jun 29, 2011

### onanox

12. Jun 29, 2011

### Stephen Tashi

I don't know what Q is. I think $P(x_p)$ is probably the cumulative distribution of the chi-square as defined on page 262.

In browsing the online book, I notice that the bottom of some of the pages are cut off, such as page 936. I wonder if that's worth reporting to someone, or does everybody already know that? I have a hard copy of this book packed away somewhere.

A more modern reference is the Digital Library Of Mathematical Functions. They define a P and Q on this page: http://dlmf.nist.gov/8

The bad thing about this reference is that they have little explanation of how their special functions are used in probabiity theory.

I notice the Wikipedia article on the inverse chi-square ( http://en.wikipedia.org/wiki/Inverse-chi-square_distribution) says there are two different distributions by that name. Do we have the one you want?

13. Jun 29, 2011

### onanox

once again, I am NOT looking for an "inverse chi squared" distribution (which is really a reciprocal chi squared distribution)
I am looking for an inverse function for the chi squared distribution.

after reading that link, it becomes apparent that the chi squared CDF is equivalent to the normalized incomplete gamma function.
the GNU Scientific Library defines a function for that, so I am currently checking that documentation.

Last edited: Jun 29, 2011