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...... }