'**************************************************** '* Program to demonstrate the chi-square * '* cumulative distribution * '* ------------------------------------------------ * '* Reference: BASIC Scientific Subroutines, Vol. II * '* by F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1].* '* ------------------------------------------------ * '* SAMPLE RUN: * '* How many degrees of freedom: 100 * '* What is the range (X1,X2): * '* X1: 50 * '* X2: 150 * '* What is the table step size: 5 * '* Summation truncation error bound: 1e-6 * '* * '* X Chi-Square CDF * '* ------------------------ * '* 50 0.00001 * '* 55 0.00007 * '* 60 0.00052 * '* 65 0.00261 * '* 70 0.00985 * '* 75 0.02918 * '* 80 0.07034 * '* 85 0.14206 * '* 90 0.24680 * '* 95 0.37742 * '* 100 0.51881 * '* 105 0.65350 * '* 110 0.76780 * '* 115 0.85507 * '* 120 0.91559 * '* 125 0.95401 * '* 130 0.97649 * '* 135 0.98869 * '* 140 0.99486 * '* 145 0.99779 * '* 150 0.99910 * '* * '**************************************************** DEFINT I-N DEFDBL A-H, O-Z CLS PRINT INPUT " How many degrees of freedom: ", m PRINT " What is the range (X1,X2):" INPUT " X1: ", x1 INPUT " X2: ", x2 INPUT " What is the table step size: ", x3 INPUT " Summation truncation error bound: ", e CLS PRINT PRINT " X Chi-Square CDF " PRINT " -------------------------" FOR x = x1 TO x2 STEP x3 GOSUB 3000 PRINT USING " ### #.#####"; x; y NEXT END '*************************************************** '* Series approximation subroutine LN(X!) * '* Accuracy better then 6 places for x>=3 * '* Accuracy better than 12 places for x>10 * '* Advantage is that very large values of the * '* argument can be used without fear of over flow. * '* x is the input, y is the output. * '* ----------------------------------------------- * '* Reference: CRC Math Tables. * '*************************************************** 1000 x1 = 1# / (x * x) y = (x + .5#) * LOG(x) - x * (1# - x1 / 12# + x1 * x1 / 360# - x1 * x1 * x1 / 1260# + x1 * x1 * x1 * x1 / 1680#) y = y + .918938533205# RETURN '****************************************************** '* Chi-square function subroutine. This program takes * '* a given degree of freedom, m and value, x, and * '* calculates the chi-square density distribution * '* function value, y. * '* -------------------------------------------------- * '* Reference: Texas Instruments SR-51 owners Manual, * '* 1974. * '* -------------------------------------------------- * '* Subroutine used: LN(X!). * '****************************************************** 2000 'Save X xm1 = x 'Perform calculation x = m / 2 - 1 'Call LN(X!) subroutine GOSUB 1000 x = xm1 c = -x / 2 + (m / 2 - 1) * LOG(x) - (m / 2) * LOG(2) - y y = EXP(c) RETURN '****************************************************************** '* Chi-square cumulative distribution subroutine * '* The program is fairly accurate and calls upon the chi-square * '* probability density function subroutine. The input parameter * '* is m, the number of degrees of freedom. Also required is the * '* ordinate value, x. The subroutine returns y, the cummulative * '* distribution integral from 0 to x. This program also requires * '* an accuracy parameter, e, to determine the level of summation. * '* -------------------------------------------------------------- * '* Reference: Hewlett-Packard statistics programs, 1974. * '****************************************************************** 3000 y1 = 1: x2 = x: m2 = m + 2 x2 = x2 / m2 3100 y1 = y1 + x2 IF x2 < e THEN GOTO 3200 m2 = m2 + 2 'This form is used to avoid overflow x2 = x2 * x / m2 'Loop to continue sum GOTO 3100 'Obtain y, the probability density function 3200 GOSUB 2000 y = y1 * y * 2 * x / m RETURN 'End of file chi-sq.bas