Chi-square goodness of fit and contingency tables
The question
A fair die rolled 120 times shows each face about 20 times. How uneven can the counts get before you doubt the die?
Judge three dice first, then change the counts yourself.
- 1 SeeJudge three dice, then change their counts.
- 2 TrySimulate dice, then test a two-way table.
- 3 ProveBuild the proof, line by line.
- 4 ApplySpinners, reminders and loan defaults.
Before you start · 3 quick checks on p-values, sums of squared normals and expected counts, about 3 min
Two ideas before you start
2 ideasThe hypothesis fixes the expected counts
Figure: six bars for the counts 25, 17, 22, 16, 19 and 21 of 120 rolls, with a dashed mark at the expected count of each face. It needs JavaScript.
Expected: E = 120 × 1/6 = 20 for every face
The bars are what happened in 120 rolls. The dashed marks are what the hypothesis expects: E = n p for each face. Change the hypothesis and the marks move; the bars stay.
Exact definition
Model. \(n\) independent trials each land in exactly one of \(k \ge 2\) cells. The null hypothesis \(H_0\) names the cell probabilities \(p_1, \dots, p_k\), each positive, with \(p_1 + \cdots + p_k = 1\). The observed count \(O_i\) is the number of trials in cell \(i\), and the expected count under \(H_0\) is
\[E_i = n p_i.\]For a fair die \(k = 6\) and every \(p_i = \tfrac{1}{6}\). The lesson on hypothesis tests sets up null hypotheses and rejection rules.
Square each gap, scale it, add
Figure: bars of the terms (O − E)²/E of the faces added so far, for the counts 25, 17, 22, 16, 19 and 21 and a fair die. It needs JavaScript.
Faces 1 to 2: 1.25 + 0.45 = 1.7
Each face adds (O − E)²/E. Squaring makes a shortfall count like an excess, and dividing by E measures each gap against the size of the count. The total over all faces is X².
Exact definition
Pearson's statistic. With observed counts \(O_i\) and expected counts \(E_i = n p_i\),
\[X^2 = \sum_{i=1}^{k} \frac{(O_i - E_i)^2}{E_i}.\]\(X^2 = 0\) exactly when every count equals its expected count, and it grows as the counts move away from them. For the counts on this card and a fair die, \(E_i = 20\) and the six terms add up to 2.8.
Fine print: names and the model
Many books write the statistic as \(\chi^2\), the same letter as the distribution it is compared with; this page keeps \(X^2\) for the statistic and \(\chi^2_m\) for the chi-square distribution with \(m\) degrees of freedom. The test is named after Karl Pearson, who proposed it in 1900.
Every statement on this page assumes independent trials. The p-values come from the chi-square curve, which is an approximation; the exact null distribution of \(X^2\) is discrete and depends on \(n\).
Three dice, one referee
- Read the three tables and make your prediction. The lab opens when you check it.
- Load a table with A, B or C, then change any face's count with − and +.
- The bars, the table of terms and the tiles follow every change. The two goals ask for the smallest change that flips the verdict.
| Die | 1 | 2 | 3 | 4 | 5 | 6 | Total |
|---|
How the page computes X² and the p-value
With \(n\) rolls in all, every face of a fair die has expected count \(E = n/6\), and \(X^2 = \sum_{i=1}^{6} (O_i - E)^2/E\). The p-value is \(P(\chi^2_5 \ge X^2)\), the chance that a chi-square variable with \(k - 1 = 5\) degrees of freedom is at least the observed \(X^2\). The test rejects a fair die at the 5% level when \(p \le 0.05\), which happens when \(X^2\) is at least about 11.07. Stage 2 checks the chi-square curve against simulated fair dice, and stage 3 explains where \(k - 1\) comes from.
The chi-square p-value is an approximation. It is close to the exact one when every expected count is at least about 5, and the lab says so when \(E\) is below 5.
Table A loaded.
| Face | O | E | O − E | (O − E)²/E |
|---|
Goals
More rolls, and a table with two ways to split
A quick guess first. The simulated dice open once you answer.
Explorer: 2,000 simulated tables
Each table is \(n\) rolls of a fair die, and the figure collects the \(X^2\) of 2,000 such tables. The tables come from the page's random generator with the fixed seed 1, so every visit shows the same tables.
A 2 × 2 table
In this test 200 visitors were split: 120 saw version A and 80 saw version B, and 50 of the 200 signed up. If signing up is independent of the version, each cell expects its row total times its column total, divided by 200. The explorer keeps all four totals and lets you choose how many of the 50 sign-ups came from version A. Treat the visitors as independent.
| Version | Signed up | Did not |
|---|
Goals
Why the average is k − 1
Assumptions. \(n \ge 1\) independent trials each land in exactly one of \(k \ge 2\) cells, cell \(i\) with probability \(p_i \gt 0\), and \(p_1 + \cdots + p_k = 1\). \(O_i\) is the number of trials in cell \(i\), \(E_i = n p_i\), and \(X^2 = \sum_{i=1}^{k} (O_i - E_i)^2/E_i\).
Conclusion. (a) For every \(n \ge 1\),
\[\operatorname{E}[X^2] = k - 1.\](b) If \(k = 2\), write \(p = p_1\) and \(Z = (O_1 - n p)/\sqrt{n p(1 - p)}\). Then \(X^2 = Z^2\) for every outcome.
Facts the proof uses.
- A count of successes in \(n\) independent trials, each a success with probability \(q\), has mean \(n q\) and variance \(n q(1 - q)\).
- For a random variable \(Y\) with finite variance, \(\operatorname{Var}(Y) = \operatorname{E}[(Y - \operatorname{E}[Y])^2]\).
- Linearity of expectation: for random variables with finite means and constants \(a, b\), \(\operatorname{E}[aY + b] = a\operatorname{E}[Y] + b\) and \[\begin{aligned} &\operatorname{E}[Y_1 + \cdots + Y_k] \\ &= \operatorname{E}[Y_1] + \cdots + \operatorname{E}[Y_k]. \end{aligned}\]
Each option is one step of the proof. Pick the one that is true for every \(n \ge 1\), every \(k \ge 2\) and all positive \(p_i\) that add up to 1; line 5 is about \(k = 2\).
Problems
For an even number of degrees of freedom \(2m\) the chi-square tail has a closed form:
\[\begin{aligned} &P(\chi^2_{2m} \ge x) \\ &= e^{-x/2}\Bigl(1 + \tfrac{x}{2} + \cdots \\ &\qquad + \tfrac{(x/2)^{m-1}}{(m-1)!}\Bigr). \end{aligned}\]So \(P(\chi^2_2 \ge x) = e^{-x/2}\), and for 4 degrees of freedom the tail beyond \(x\) is \(e^{-x/2}(1 + x/2)\). Each problem has hints and a full solution.
Related: the t distribution handles a mean when σ is unknown, and likelihood ratio tests lead to another statistic compared with a chi-square curve.
Further reading
- K. Pearson, "On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling," Philosophical Magazine, Series 5, 50 (1900), the paper that introduced the test.
- J. A. Rice, Mathematical Statistics and Data Analysis, 3rd ed. (Duxbury, 2007), chapter 9 on testing hypotheses and assessing goodness of fit and chapter 13 on the analysis of categorical data.
- A. Agresti, Categorical Data Analysis, 3rd ed. (Wiley, 2013), chapter 3 on inference for two-way contingency tables.