Hexa Scholar

Chi-square goodness of fit and contingency tables

Six gold bins of slightly different heights, each filled with gold beads, stand in a row on a navy workbench under a level brass rod; some bins rise above the rod and some fall short.
Six bins, some above a level rod and some below.

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. 1 SeeJudge three dice, then change their counts.
  2. 2 TrySimulate dice, then test a two-way table.
  3. 3 ProveBuild the proof, line by line.
  4. 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 ideas

The 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.

Hypothesis:

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 added:

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\).

1 See

Three dice, one referee

Counts of each face in 120 rolls of three dice.
Die123456Total
2 Try

More rolls, and a table with two ways to split

A quick guess first. The simulated dice open once you answer.

A 2 × 2 table

3 Prove

Why the average is k − 1

Theorem

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.

  1. A count of successes in \(n\) independent trials, each a success with probability \(q\), has mean \(n q\) and variance \(n q(1 - q)\).
  2. For a random variable \(Y\) with finite variance, \(\operatorname{Var}(Y) = \operatorname{E}[(Y - \operatorname{E}[Y])^2]\).
  3. 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\).

What else follows
4 Apply

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.

Key point To test counts against a hypothesis, compute \(E_i = n p_i\) and \(X^2 = \sum (O_i - E_i)^2/E_i\), and compare \(X^2\) with the chi-square distribution with \(k - 1\) degrees of freedom. For an \(r \times c\) table use the row total times the column total, divided by \(N\), as each expected count, and \((r - 1)(c - 1)\) degrees of freedom. Check that the observations are independent and the expected counts are not too small.
Level Intermediate Prerequisites hypothesis tests and the chi-square distribution; p-values and expected counts are refreshed in Before you start Time ~40 min

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