# An Introduction toStatistics with Klong

## χ2 Tests and Contingency Tables

The `nstat` module has built-in support for χ2 tests. Given a data set of observations (O) and a data set of corresponding expectations (E), the `X2` function can be used to compute the resulting χ2 statistic and, subsequently, the improbability of the statistic given the null hypothesis O=E.

 ``` E::[10 10 10 10 10 10] O::[ 7 13 12 8 15 5] X2(E;O) `7.6` x2.cdf(5;it) `0.820298060986649229` ```

The expectation of a contingency table is computed by the `ct.exp` function (results in the output have been rounded to one decimal place):

 ``` CT::[[47 23 51 27] [52 28 45 25] [20 37 21 23]] ct.exp(CT) ```[[44.1 32.6 43.4 27.8] [44.7 33.1 44.0 28.2] [30.1 22.3 29.6 19.0]]``` ```

The corresponding χ2 statistic is computed by `ct.X2`:

 ``` ct.X2(CT) `23.2250278064831061` ```

For the curious:

 ``` x2.cdf(6;ct.X2(CT)) `0.999275490338781315` ```

So getting results (at least) as far removed from the expectation as in above table is pretty unlikely.