12
13
#include <stdlib.h>
15
double PointChi2 (double prob, double v);
16
double QuantileChi2 (double prob, double v);
17
#define PointGamma(prob,alpha,beta) PointChi2(prob,2.0*(alpha))/(2.0*(beta))
18
#define QuantileGamma(prob,alpha,beta) QuantileChi2(prob,2.0*(alpha))/(2.0*(beta))
18
19
#define CDFGamma(x,alpha,beta) IncompleteGamma((beta)*(x),alpha,LnGammaFunction(alpha))
19
20
#define CDFChi2(x,v) CDFGamma(x,(v)/2.0,0.5)
21
double PointNormal (double prob);
22
double QuantileNormal (double prob);
22
23
double CDFNormal (double x);
23
24
double LnGammaFunction (double alpha);
24
25
double IncompleteGamma (double x, double alpha, double ln_gamma_alpha);
26
27
int main(int argc, char*argv[])
28
int i,j, n=20, ndf=200, nprob=8, option=0; /* 0:table, 1:prob */
29
double df,chi2, d=1.0/n, prob[]={.005, .025, .1, .5, .90, .95, .99, .999};
29
int i,j, n=20, ndf=200, nprob=8, option=0;
30
double df, chi2, d=1.0/n, prob[]={.005, .025, .1, .5, .90, .95, .99, .999};
34
printf ("\nd.f. & Chi^2 value (Ctrl-c to break)? ");
35
scanf ("%lf%lf", &df, &chi2);
36
if(df<1 || chi2<0) break;
37
prob[0]=1-CDFChi2(chi2,df);
38
printf ("\nprob = %.9f = %.3e\n", prob[0],prob[0]);
32
if (argc!=2 && argc!=3) {
42
33
printf ("\n\nChi-square critical values\n");
43
34
for (i=0; i<ndf; i++) {
50
41
printf ("\n%3d ", i+1);
51
42
for (j=0; j<nprob; j++)
52
printf ("%9.4f", PointChi2(prob[j],(double)(i+1)));
43
printf ("%9.4f", QuantileChi2(prob[j],(double)(i+1)));
53
44
if (i%5==4) printf ("\n");
55
46
printf ("\nENTER for more, (q+ENTER) for quit... ");
53
printf ("\nd.f. & Chi^2 value (Ctrl-c to break)? ");
54
scanf ("%lf%lf", &df, &chi2);
55
if(df<1 || chi2<0) break;
56
prob[0] = 1-CDFChi2(chi2,df);
57
printf ("\ndf = %2.0f prob = %.9f = %.3e\n", df, prob[0], prob[0]);
64
printf("df = %d ch2 = %.4f invalid");
67
prob[0] = 1 - CDFChi2(chi2, df);
68
printf ("\ndf = %2.0f prob = %.9f = %.3e\n", df, prob[0], prob[0]);
64
double PointNormal (double prob)
74
double QuantileNormal (double prob)
66
76
/* returns z so that Prob{x<z}=prob where x ~ N(0,1) and (1e-12)<prob<1-(1e-12)
67
77
returns (-9999) if in error
193
double PointChi2 (double prob, double v)
203
double QuantileChi2 (double prob, double v)
195
205
/* returns z so that Prob{x<z}=prob where x is Chi2 distributed with df=v
196
206
returns -1 if in error. 0.000002<prob<0.999998
197
207
RATNEST FORTRAN by
198
Best DJ & Roberts DE (1975) The percentage points of the
208
Best DJ & Roberts DE (1975) The percentage Quantiles of the
199
209
Chi2 distribution. Applied Statistics 24: 385-388. (AS91)
200
210
Converted into C by Ziheng Yang, Oct. 1993.