Skip to content

Chi-Squared Test of a Dataset's Class Balance

Pearson's chi-squared statistic adds up, over categories, the squared gap between observed and expected counts divided by the expected count. The SciPy documentation for scipy.stats.chisquare begins "Perform Pearson's chi-squared test." and describes it as "a goodness-of-fit test for a multinomial distribution with given probabilities." For each category j with observed count O_j and expected count E_j, the statistic is the sum of (O_j − E_j)² / E_j. A large value means the observed counts sit far from what the stated probabilities predict.

The documentation also states when to distrust the result. "This test is invalid when the observed or expected frequencies in each category are too small," and "A typical rule is that all of the observed and expected frequencies should be at least 5." About degrees of freedom it says "The default degrees of freedom, k-1, are for the case when no parameters of the distribution are estimated." With k categories and nothing fitted from the data, the reference distribution is chi-squared with k − 1 degrees of freedom.

Try it by hand. Suppose a labeling team tags 60 examples into six classes, and the plan says the classes should be evenly balanced, so each category should hold 10. The observed counts are 5, 8, 9, 8, 10, and 20. The terms (O − E)² / E are 25/10 = 2.5, 4/10 = 0.4, 1/10 = 0.1, 0.4, 0, and 100/10 = 10. Their sum is 13.4, exactly 67/5. With six categories and no fitted parameters there are five degrees of freedom, and the p-value is about 0.020. The one class with 20 examples contributes 10 of the 13.4. The rule of thumb holds here: every expected count is 10, and the smallest observed count is 5. I computed the sum in exact fractions and checked it against SciPy's chisquare, which returns 13.4 and about 0.0199, and the data are invented. Now divide by the squared expected count instead, which is the slip to avoid. Each term shrinks by a factor of 10 and the total becomes 67/50 = 1.34, small enough to look harmless. Why divide by E at all? A gap of 5 against an expected 10 adds 2.5, but the same gap of 5 against an expected 1,000 adds 0.025. The statistic measures the gap relative to the expected count. And if every count is multiplied by 10 with the proportions unchanged, the statistic becomes 134, because the same relative departure is stronger evidence from more data.

The trap: using the wrong denominator, or trusting the p-value when its assumptions fail. The expected count goes in the denominator, not its square. The documentation's definition rests on independent sampling of the observations, so duplicated or clustered examples can make the nominal p-value misleading, and a small p-value says the proportions differ from the plan without saying why.

Prof Rod's rule: divide each squared gap by its expected count, use k − 1 degrees of freedom when nothing was fitted, and check that every expected count is at least 5 before trusting the p-value.

SciPy documentation, scipy.stats.chisquare