Introduction
Many questions are about counts in categories. A die is rolled 60 times: are the six faces equally common? A survey records gender and preferred phone brand: does the preference depend on gender? The tests of the previous lessons worked with means and proportions. The chi-square test, written χ², works directly with a table of counts and is one of the most used tests in science, medicine and business.
How it works
Every chi-square test compares observed counts O with the expected counts E that H₀ predicts, using one statistic:
χ² = Σ (O − E)² ÷ E
Each category adds its squared gap scaled by what was expected, so a gap of 5 matters more when only 10 were expected than when 1,000 were. If H₀ is true, χ² tends to be small; large values are evidence against H₀.
How large is too large depends on the degrees of freedom df, the number of counts that are free to vary once the totals are fixed. Under H₀, χ² follows a known curve for each df, and statisticians tabulate the critical value that is exceeded only 5% of the time:
| df | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 5% critical value | 3.841 | 5.991 | 7.815 | 9.488 | 11.070 | 12.592 | 14.067 | 15.507 | 16.919 | 18.307 |
If χ² is above the critical value, reject H₀ at the 5% level.
Goodness of fit. H₀ gives the share of each of k categories, for example "a fair die: 1/6 each". Expected count = total × share, and df = k − 1, because the last count is fixed by the total.
Independence. For a table with r rows and c columns, H₀ says the row variable and the column variable are unrelated. Then the expected count in a cell is
E = row total × column total ÷ grand total
and df = (r − 1)(c − 1).
The test is approximate: it is reliable when every expected count is at least about 5.
Example
observed = [8, 9, 12, 11, 6, 14]
expected = sum(observed) / len(observed)
chi2 = 0
for o in observed:
chi2 += (o - expected) ** 2 / expected
df = len(observed) - 1
critical = 11.070
print("expected per face:", expected)
print("chi-square =", round(chi2, 2), " df =", df)
print("reject 'fair die' at 0.05?", chi2 > critical)Line by line
observedholds how many times each face came up in 60 rolls.- A fair die expects 60 ÷ 6 = 10 of each face.
- The loop adds (O − E)² ÷ E for every face: 0.4 + 0.1 + 0.4 + 0.1 + 1.6 + 1.6.
- With 6 faces, df = 5, and the 5% critical value is 11.070.
Expected result
expected per face: 10.0
chi-square = 4.2 df = 5
reject 'fair die' at 0.05? False
4.2 is well below 11.070, so these rolls give no evidence that the die is unfair.
In the real world
Geneticists check whether offspring follow the 3 : 1 ratios Mendel predicted. Election analysts test whether voting choice depends on age group. Casinos and game makers test that dice, cards and random number generators produce every outcome equally often. Marketing teams test whether the choice of product depends on region.
Common mistakes
- Using percentages instead of counts; the statistic only works on raw counts.
- Getting the degrees of freedom wrong, for example using k instead of k − 1.
- Running the test with tiny expected counts, where the approximation fails.
- Reading a significant result as a cause: dependence in a table, like correlation, does not say why.
Best practices
- Write the expected table next to the observed one before computing χ².
- Check that every expected count is at least about 5, and merge small categories if not.
- Report χ², df and the conclusion together, for example "χ² = 4.2, df = 5, not significant".
Summary
A chi-square test compares observed counts with the counts H₀ expects, using χ² = Σ (O − E)² ÷ E. For goodness of fit, E comes from the stated shares and df = k − 1; for independence in a table, E = row total × column total ÷ grand total and df = (r − 1)(c − 1). When χ² is over the critical value for its df, H₀ is rejected.
Quick check
Key takeaways
- Chi-square compares counts, never percentages or means.
- Goodness of fit uses k − 1 degrees of freedom; independence uses (rows − 1)(columns − 1).
- Expected counts in a table are row total × column total ÷ grand total.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.