/******************************************************** * Program to demonstrate Chi-square Statistic * * ----------------------------------------------------- * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * * * * C++ version by J-P Moreau, Paris * * (www.jpmoreau.fr) * * ----------------------------------------------------- * * SAMPLE RUN: * * * * CHI-SQUARE CUMULATIVE DISTRIBUTION APPROXIMATION * * * * P(X) X2 REAL X2 * * ----------------------------- * * 0.05 124.5347 124.3 * * 0.10 118.6326 118.5 * * 0.15 114.5999 * * 0.20 111.4342 * * 0.25 108.7913 109.1 * * 0.30 106.5055 * * 0.35 104.4821 * * 0.40 102.6610 * * 0.45 101.0018 * * 0.50 99.1944 99.3 * * 0.55 97.6863 * * 0.60 96.0812 * * 0.65 94.3595 * * 0.70 92.4934 * * 0.75 90.4428 90.1 * * 0.80 88.1446 * * 0.85 85.4893 * * 0.90 82.2522 82.4 * * 0.95 77.7867 77.9 * * 1.00 0.0000 * * ----------------------------- * * * ********************************************************/ #include #include double x,y; int i,m; char *x1[21]; /*************************************************** * Chi-square cumulative distribution approximation * * Good for m > 100. * * Reference: Statistics Manual, Crow, Maxfield and * * Davis (Dover, 1960). * * The input value is y, the probability, the output* * value is the corresponding Chi-square statistic. * ***************************************************/ void Chi_Square() { //Labels: e100, e200, e210 double z; x = y; //Guard against zero discontinuity if (x <= 0) x = exp(-100); if (x > 0.5) goto e100; x = -log(x); //Regressed table correction z = -0.803 + 1.312 * x - 0.2118 * x * x + 0.016 * x * x * x; goto e200; e100: x = 1.0 - x; //Guard against zero discontinuity if (x <= 0) goto e210; x = -log(x); z = 0.803 - 1.312 * x + 0.2118 * x * x - 0.016 * x * x * x ; e200: x = 2.0 / (9.0 * m); x = 1.0 - x + z * sqrt(x); e210: x = m * x * x * x; } void main() { for (i=0; i<21; i++) x1[i] = ""; x1[1] = "124.3"; x1[2] = "118.5"; x1[5] = "109.1"; x1[10] = " 99.3"; x1[15] = " 90.1"; x1[18] = " 82.4"; x1[19] = " 77.9"; printf(" CHI-SQUARE CUMULATIVE DISTRIBUTION APPROXIMATION\n"); printf(" P(X) X2 REAL X2 \n"); printf(" ---------------------------- \n"); m = 100; i = 0; y = 0.05; while (y < 1.01) { i++; Chi_Square(); printf("%5.2f %8.4f %s\n",y,x,x1[i]); y += 0.05; } printf(" ---------------------------- \n"); } // End of file chisqa.cpp