This program comes from(or was inspired by, rather) the chi-square test described within The Art Of Computer Programming, Vol. 2, Seminumerical Algorithms(TAOCP, hereafter). The test is important for random number generators (for testing them, anyway), so I figured I'd make a program to calculate the chi-square test. The only thing that needs to be done by the users of this (beside giving the function some parameters) is to fill in the table and table look-up methods for the test. I left this out for several reasons: (a)it is rather long, and I thought it may distract from the main purpose of the code, (b)there are a variety of tables that could be chosen; what is presented in TAOCP is hardly complete, or even close. Therefore, I leave it open so that it can easily accomodate any data source that you provide. The code is here, and documents itself with comments:
sub chi_sqr {
# We take n *independant* observations, each of which
#fall into one of k
# categories; $p[$s] is the probability that each
#observation falls into
# category s, and $y[$s] is the number of observations
#that actually *do*
# fall into category s
$params = shift;
$n = $params->{n};
$k = $params->{k};
$p = $params->{p};
$y = $params->{y};
for $s(0..$k-1) { $V += ($y->[$s] ** 2 / $p->[$s]) - $n }
$V = 1/$n * $V; ## Eq. (8), pp 43. Vol. 2. TAOCP.
$v = $k - 1; ## We have $v degrees of freedom
return get_val( $v, $V, $p );
}
sub get_val {
# Returns likelihood that $V is less than or equal to
#the value in row
# $v with the value $V
($v,$V) = @_;
$p = -- 0..6 --> 1%, 5%, 25%, ...
(from Table 1, as below) --
@x = qw(-2.33 -1.64 -.674 0.00 0.674 1.64 2.33);
## ^ From table 1, pp 44.
## Vol. 2. TAOCP
$chsqr = sub { $v+sqrt(2*$v)*$x[$p]+2/3*
$x[$p]**2-2/3+1/sqrt($v) };
## Ditto ^
@table = ...Insert table here...
.....perform table look-up; OR, if row $v isn't in table,
and $v is more than 30, perform $chsqr closure && return
the result of whichever method was performed......
}