# Excel's CHIDIST function in MATLAB for large values

This question is related to Excel's CHIDIST function in MATLAB

The minimum answer larger than zero I can get in Excel is about 2.2e-308 that correspond to `realmin` in MATLAB. However, in MATLAB the minimum answer is 2.2e-16, or `eps(1.0)`. I understand this is because I first calculate `chi2cdf` that is close to 1 for large values, and the precision in this case is `eps(1.0)`.

Is there any other way to reproduce Excel's CHIDIST and not to loose the precision? Do you know exactly how CHIDIST works?

EDIT:

I have to do this test multiple times, and in many cases the resulting p-values are close to zero. I need to rank the p-values, this is why I need high precision in p-value calculation.

-

I am assuming that this is merely out of curiousity, as I can't imagine a "real" example where something like this matters.  So if you are worried about this loss of precision for some real world problem, don't be.  It is still interesting to know these sorts of things though :) – probabilityislogic Aug 6 '11 at 18:25
I'm unfamiliar with Matlab, but I'm confident it can calculate an incomplete Gamma integral to very high precision.  See the 'upper' option at mathworks.com/help/techdoc/ref/gammainc.html. – whuber♦ Aug 6 '11 at 18:38
@probabilityislogic: Actually I do need this precision. Later I take log of p-values, so I get many Inf values, if p-value is 0. – yuk Aug 7 '11 at 2:23
– yuk Aug 7 '11 at 2:39
@yuk - You should really add this information to the question, that what you really require is a good approximation to the log of p-value.  In any event, I still struggle to see why not simply giving an arbitrarily big value whill not suffice.  Your minimum value is $-16\log(2.2)$, so just take $\log(0)=-20$ or bigger – probabilityislogic Aug 7 '11 at 3:45

## 1 Answer

A solution was developed in the comments following the question; the purpose of this post is to make that solution available as a reply so it can be searched, voted on, etc.

Chi-square distribution functions are scaled incomplete Gamma integrals. If you compute the integral from $0$ to $x$, you approach $1$ as $x$ gets large. Subtracting this value from $1$ loses ever more precision; with double-precision arithmetic, you're limited to results greater than about $10^{-16}$. The trick is to compute the integral from $x$ to $\infty$, which typically can be approximated accurately even for extremely small values.

The Matlab help for the incomplete Gamma integral indicates that this upper tail will be computed using its 'upper' option.

-

1
Thank a lot! To be more MATLAB specific, function `chi2cdf` calles `gamcdf`, which calles `gammainc`. In `gamcdf` I've changed the line `p = gammainc(z, a);` to `p = gammainc(z, a, 'upper');`, saved it under another name and changed the call in `chi2cdf`. Works great! – yuk Aug 8 '11 at 3:47
default
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 10, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9198353290557861, "perplexity_flag": "middle"}