Performs a one-way Pearson's chi-square goodness of fit test between a range of observed and a range of expected values. This is a useful statistical test for testing whether a set of observations fits a discrete distribution.

Return Value

Type: TestRes

A TestRes of the chi-square statistic and the P-value for the alternative hypothesis that observed is not a sample from expected against the null that observed is a sample from expected.

Notes: By default, expected is assumed to be a range of expected proportions. These proportions are automatically normalized, and can sum to any number. By passing Expected.count in as the last parameter, calculating expected counts will be skipped, and expected will assume to already be properly normalized. This is slightly faster, but more importantly allows input ranges to be used.

The chi-square test relies on asymptotic statistical properties and is therefore not considered valid, as a rule of thumb, when expected counts are below 5. However, this rule is likely to be unnecessarily stringent in most cases.


1 // Test to see whether a set of categorical observations differs
2 // statistically from a discrete uniform distribution.
4 uint[] observed = [980, 1028, 1001, 964, 1102];
5 auto expected = repeat(1.0);
6 auto res2 = chiSquareFit(observed, expected);
7 assert(approxEqual(res2, 0.0207));
8 assert(approxEqual(res2.testStat, 11.59));