# What's the proper function to calculate confidence percentages for Chi Squares?

In the book Practical BASIC Programs by Lon Poole (Osborne/McGraw-Hill, 1980) (~10M pdf), the sample program listing has a number of discrepancies and some outright incorrect statements. On page 143 of the book (page 156 of the pdf), we are presented with the formulas for calculating chi-square and chi-square with Yates' correction, respectively, as:

$$\chi^2 = \sum_{i=1}^{K}\frac{(O_i - E_i)^2}{E_i}$$

$$\chi^2 = \sum_{i=1}^{K}\left(\frac{|O_i - E_i|-.5}{E_i}\right)^2$$

The program listing implements the two chi-square formulas as:

370 S=S+(ABS(X-Y)^2)/Y
390 T=T+((ABS(X-Y)-.5)^2)/Y


Line 370 will provide the correct result, even with the ABS() function, but I think that the code used on line 390 to compute Yates' corrected chi-square is incorrect--line 390 only squares the numerator in the summation, not the entire fraction.

Which is correct for the Yates' corrected version, the mathematical function or the program listing?

Later in the program listing the 5% and 95% confidence levels are computed. Four different methods are used for (n > 101), (n == 101), (30 < n < 101), and (n < 31). For (n < 31) and (n == 101) a lookup table is used. For (30 < n < 101) the program uses lines 1400 and 1410 to compute the values of 5% and 95% confidence levels, respectively ('N' is the total number of samples so '(N-1)' gives degrees of freedom).

1400 C=(N-1)*(1-2/(9*(N-1))+1.6449*SQR(2/(9*(N-1)))^3
1410 D=(N-1)*(1-2/(9*(N-1))-1.6449*SQR(2/(9*(N-1)))^3


For (n > 101) the program uses lines 1600 and 1610 to compute the values of 5% and 95% confidence levels, respectively.

1600 C=.5*(1.6449+SQR(2*(N-1)-1)))^2
1610 D=.5*(SQR(2/(9*(N-1))-1.6449)^2


I don't know where these formulas came from; several searches on the 'net provided nothing that I recognized as being mathematically equivalent. Also, as you can see, all four lines have mismatched parentheses, so even if the original equations used to created these lines of code are sound, I still don't know where to place the missing parentheses.

Can someone shed some light on the four lines of code, i.e. what process are these trying to implement? Also, is there a general process that can be used to compute any confidence level percentage for any number of degrees of freedom?

-
Note that if the degrees of freedom are high enough, one usually uses a normal approximation for $\chi^2$. See for instance formula 26.4.17 in Abramowitz and Stegun (which seems to have been used for lines 1400 and 1410). This explains the mysterious constant. – J. M. Apr 12 '11 at 2:32
@J.M.: Thanks, those links are perfect. In addition to formula 26.4.17 that you pointed out, it looks like formula 26.4.16 was used for lines 1600 and 1610. If you'd be willing to submit your comment as an answer I'd happily selected it as the answer. – oosterwal Apr 12 '11 at 13:25

I don't have my handbooks with me, so I can't answer regarding Yates's $\chi^2$ correction (maybe I will edit this answer later).
For those $\chi^2$ quantiles, on the other hand, it would seem that they used the normal distribution approximations embodied in formulae 26.4.16 and 26.4.17 in Abramowitz and Stegun's handbook. 26.4.17 is referred to as the Wilson-Hilferty approximation, while 26.4.16 is the Fisher approximation. The 1.6449 in the formula is the quantile for the normal distribution corresponding to 95% confidence.
Line 390 in your code is correct for Yates' correction for continuity, while the second stated formula you quote for $\chi^2$ is wrong.
$$\chi_\text{Yates}^2 = \sum_{i=1}^{K}\frac{(|O_i - E_i|-.5)^2}{E_i}$$