Re: P-value from chi-square value: source code
- From: iandjmsmith@xxxxxxx (Ian Smith)
- Date: 11 Apr 2005 02:40:00 -0700
"Reef Fish" <Large_Nassau_Grouper@xxxxxxxxx> wrote in message news:<1113074906.743917.66500@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
> DZ wrote:
> > P.S. The p-value is 1-chi_cdf(ChiSquare, df)
>
> If so, why do you need any ChiSquare software, let along coding it?
> Why wouldn't any standard normal software suffice?
> >
> > DZ wrote:
> > > Dhruv <bhaveshrb@xxxxxxxxx> wrote:
> > >> Ian Smith wrote:
> > >>> "Dhruv" <bhaveshrb@xxxxxxxxx> wrote
> > >>> > Could anyone send me a link to the source code in C++ for
> > >>> > calculating the P-value from a chi-square value.
> > >>> You are looking for code to evaluate the upper tail probability
> of a
> > >>> chi-squared distribution.
> > >>> http://odin.mdacc.tmc.edu/anonftp/#DCDFLIB
> > >>
> > >> I actually tried the code and am having some problems with it. I
> am
> > >> sort of a newbie to programming and though I have managed to write
> > >> some good code, I am unable to figure out the problem at present.
> > >> What I need basically is a function which takes in Chi-square
> value
> > >> and returns the P-value. Please help me on this.
> > >
> > > You need to compile the library and use the function "cdfchi" ,
> > > passing parameters as addresses, for example:
> > >
> > > double chi_cdf (double ChiSquare, double df) {
> > > double q, p, bound;
> > > int which=1, status;
> > > cdfchi(&which, &p, &q, &ChiSquare, &df, &status, &bound);
> > > return p;
> > > }
>
> And Ian Smith gave two links and said,
>
> IS> both have C code which will do it and there is nothing better out
> IS> there that I know of.
>
> Nothing better? Use either or both those links and let us know
> that the p-values (1-tail will do) for observed 1 df Chisquare
> values of 1, 10, and 100, to at least 8 significant figures,
> and then I'll tell you how many better ones there are. :-)
>
> -- Bob.
For Gnumeric, the figures (rounded to 15 figures) you requested were
3.17310507862914E-01, 1.56540225800255E-03 & 1.52397060483210E-23
I believe the 3rd one should be 1.52397060483211E-23
However, I do not think an algorithm for a chi-squared distribution
for 1 degree of freedom (i.e. a simple transformation of the normal)
is of much interest.
Both DCDFLIB and Gnumeric use more general methods for the cdf of
gamma distribution and define the cdf of the chi-squared in terms of
it.
What you need to evaluate the cdf of the gamma distribution is a
method for calculating the left tail for moderate sized shape
parameters, a method for calculating the right tail for moderate sized
shape parameters, a method for calculating the left tail for small
sized shape parameters, a method for calculating the right tail for
small sized shape parameters, a method for calculating the left and
right tails for large sized shape parameters.
What makes these algorithms good is the fact that they have all these
various methods of calculating both tails of the gamma distribution
and work pretty accurately regardless of what values are input.
There may be methods of calculating both tails of the gamma
distribution with fewer more robust methods but I have never seen
them. If you would care to show me some source code which does this, I
would be happy to test it for you.
In my opinion, most statistical software available is pretty poor. I
do not mind if a routine comes back with an error message or even does
not come back. At least I have not got an inaccurate answer with no
warning about possible problems. Usually you do not have to do much
more than look at the source code to spot horrific "1-" cancellation
error problems, use of log-gamma functions which guarantee inaccuracy
with large values, use of algorithms which cannot possibly deal with
all the cases...
It would be nice when people recommend software if they included some
details of why. Admittedly, my explanation was perhaps too brief, but
at least I do know the software. I have looked at several software
packages in a variety of languages and I stick by what I said before.
There is nothing better out there that I know of.
If you wish to get a better idea of the accuracy of these methods try
the Chi-squared calculator at
http://members.aol.com/iandjmsmith/EXAMPLES.HTM (my own software is
not in C or I would have recommended it) or download Gnumeric and give
it a go!
Ian Smith
.
- Follow-Ups:
- Re: P-value from chi-square value: source code
- From: Reef Fish
- Re: P-value from chi-square value: source code
- From: G Robin Edwards
- Re: P-value from chi-square value: source code
- References:
- P-value from chi-square value: source code
- From: Dhruv
- Re: P-value from chi-square value: source code
- From: Ian Smith
- Re: P-value from chi-square value: source code
- From: Dhruv
- Re: P-value from chi-square value: source code
- From: Reef Fish
- P-value from chi-square value: source code
- Prev by Date: Re: Comparing success/failure rates in two experiments: PLEASE HELP!!!e
- Next by Date: Adding unknown independent distributions
- Previous by thread: Re: P-value from chi-square value: source code
- Next by thread: Re: P-value from chi-square value: source code
- Index(es):
Relevant Pages
|