Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Chi-squared inverse function and incomplete gamma function

  1. Jun 24, 2011 #1
    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. jcsd
  3. Jun 25, 2011 #2
    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
  4. Jun 25, 2011 #3
    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.
  5. Jun 26, 2011 #4
    To invert the gamma cdf, two different approaches are described in the documentation for gaminv in MATLAB and qgamma in R.
  6. Jun 27, 2011 #5
    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.
  7. Jun 27, 2011 #6
    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.
  8. Jun 28, 2011 #7
    MATLAB has no problems with large dof, e.g.

    >>chi2inv(0.75,1e6) % returns 1.001e6
  9. Jun 28, 2011 #8
    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.
  10. Jun 28, 2011 #9

    Stephen Tashi

    User Avatar
    Science Advisor

  11. Jun 29, 2011 #10
    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.
  12. Jun 29, 2011 #11
  13. Jun 29, 2011 #12

    Stephen Tashi

    User Avatar
    Science Advisor

    I don't know what Q is. I think [itex] P(x_p) [/itex] 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?
  14. Jun 29, 2011 #13
    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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook