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

In reply to The Chi Square Test by dimmesdale

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.