Skip to content

Chi-square tests

Add up the squared gaps between counts seen and counts expected, each scaled by its expected count; a fair die rarely adds up to much.

Helps to know firstp-values

Helps to know first
This topic

First, the picture

Roll a die 60 times. Each face should come up about 10 times. Watch the gaps between seen and expected add up on a dial.

Is this die fair?

60 rolls of a die against the 10 per face a fair die expects (seed 8400).

A fair die, rolled 60 times: each face is expected 10 times. Each square is a bar's squared gap to 10, divided by 10.

rolls
0
needle χ²
–
zone from
11.07

Predict first. Roll a truly fair die 60 times. How often will the needle reach the shaded zone?

0.00 / 9.29 s
Describe this picture

Six bars, one per face, fill as 60 seeded rolls land (seed 8400), against a dashed level line at 10, the expected count. Beside the dial, a needle on a half-circle from 0 to 20 shows the sum of six areas; a shaded zone on the dial starts at 11.07. Before the clip a question asks how often a truly fair die, rolled 60 times, sends the needle into the shaded zone: never, about one time in twenty, or about half the time. A tap picks one and starts the clip. In the clip the rolls land one by one; when all 60 are in, a square appears beside each bar whose area is the squared gap to the line divided by 10, and the squares add into the needle as it swings. The counts are 13, 13, 11, 8, 7 and 8, and the needle stops at 3.60. Readouts give the rolls, the needle and the start of the zone. After the clip a switch, “Die”, reads the same 60 draws through a loaded die, with a six coming up a quarter of the time and each other face 0.15 of the time: counts 12, 12, 7, 10, 7 and 12, needle 3.00.

Squares on the bars

Before the clip you guessed. A fair die sends the needle into the shaded zone about one time in twenty: that is how the zone was drawn. The clip’s 60 rolls gave counts 13, 13, 11, 8, 7 and 8, and the needle stopped at 3.60, far from the zone.

The needle shows the chi-square statistic. For counts nkn_k in KK categories, with npknp_k the counts expected if the die is fair (here 60×16=1060\times\tfrac16=10 each),

χ2=∑k=1K(nk−npk)2npk.\chi^2=\sum_{k=1}^{K}\frac{(n_k-np_k)^2}{np_k}.

Each gap is squared, so a shortfall counts as much as an excess, and scaled by its expected count, so a gap of 4 counts for more where 10 were expected than where 1000 were. A fair die gives small gaps and a small total; a loaded one tends to give a large one.

Where the curve comes from

Each scaled gap (nk−npk)/npk(n_k-np_k)/\sqrt{np_k} is roughly a standard bell when the expected count is not too small. A sum of squares of df\mathrm{df} independent standard bells follows the chi-square law with df\mathrm{df} degrees of freedom, χdf2\chi^2_\text{df}. It has mean df\mathrm{df} and variance 2 df2\,\mathrm{df}.

The six gaps of the die are not independent: they add up to 0, because the counts add up to 60. That costs one degree of freedom, so the die’s statistic follows χ52\chi^2_5, with mean 5 and standard deviation 10=3.16\sqrt{10}=3.16. In general a goodness-of-fit test with KK categories has K−1K-1 degrees of freedom.

The shaded zone starts at the 95 % point of χ52\chi^2_5, 11.0711.07. The 5 % points for 1, 2, 3 and 5 degrees of freedom are 3.8413.841, 5.9915.991, 7.8157.815 and 11.07011.070. With 2 degrees of freedom the tail has a simple formula, e−x/2e^{-x/2}, so its 5 % point is 2ln⁡20=5.9912\ln20=5.991.

Testing a die

Two fair-looking rolls. Counts of 6, 8, 9, 11, 12, 14 have gaps −4,−2,−1,1,2,4-4,-2,-1,1,2,4, whose squares add to 42, so χ2=4.2\chi^2=4.2: with 5 degrees of freedom, pval=0.52p_\text{val}=0.52. Nothing odd. Counts of 4, 6, 8, 9, 12, 21 have squares adding to 182, so χ2=18.2\chi^2=18.2 and pval=0.003p_\text{val}=0.003: the 21 sixes stand out.

A loaded die. After the clip, switch to the loaded die. Its sixes are only a little too common, and 60 rolls catch it about one time in five. With 300 rolls the test catches it about four times in five.

A thousand tests

The needle of 1000 repeated tests, each of n rolls, over the χ² curve for 5 degrees of freedom.

Repeat the test 1000 times: roll a fair die 60 times and read the needle each time.

rolls per test
60
die
fair
past 11.07
–
0.00 / 8.60 s
Describe this picture

A histogram of 1000 repeats (seed 8401) of “roll the die 60 times and compute the needle”, drawn over the χ52\chi^2_5 curve, dashed, with the stretch beyond 11.07 shaded and a readout of the share of repeats that land there. In the clip the repeats pour in with the fair die; then the stretch is shaded: 43 of 1000, 4.3 %. After the clip a slider sets the number of rolls per test from 30 to 600 in steps of 30, with the loaded die for any value but 60. As the rolls grow, the histogram slides to the right and more of it lands beyond 11.07: 11.7 % at 30 rolls, 21.8 % at 60, 82.5 % at 300 and 99.1 % at 600. Past 120 rolls the axis widens to 70, and the last bar holds everything beyond.

In the clip’s 1000 tests of a fair die, 43 landed past 11.07: 4.3 %, close to the promised 5 %.

With the fair die, about 5 % of repeats land in the shaded zone, give or take 0.7 %. With the loaded die, the share grows with the number of rolls: more data, more power, as in Significance tests.

Fifty patients get a treatment and 50 do not. Did more of the treated improve?

GroupImprovedNot improvedTotal
Treatment302050
Control203050
Total5050100

If improving had nothing to do with the treatment, each cell would hold its row total times its column total over the grand total: here 50×50/100=2550\times50/100=25 in every cell. In general, with npqn_{pq} the count in row pp and column qq, and np+n_{p+}, n+qn_{+q} the row and column totals, the expected count is np+n+q/nn_{p+}n_{+q}/n.

Each cell is 5 away from 25, so χ2=4×25/25=4.0\chi^2=4\times25/25=4.0. A table with rr rows and cc columns has (r−1)(c−1)(r-1)(c-1) degrees of freedom, here 1, and pval=0.0455p_\text{val}=0.0455.

With one degree of freedom the chi-square statistic is the square of a z statistic. Here the two shares improved are 0.6 and 0.4; their difference over its standard error is z=0.2/0.1=2z=0.2/0.1=2, and 2(1−Φ(2))=0.04552(1-\Phi(2))=0.0455, the same test.

Some software applies a continuity correction to 2×22\times2 tables, shrinking each gap by 0.5 first: χ2=4×4.52/25=3.24\chi^2=4\times4.5^2/25=3.24 and pval=0.0719p_\text{val}=0.0719. Say which one you used.

When it applies, and what it measures

The bell approximation behind the chi-square curve needs enough counts. A common rule: every expected count at least 5. Rolling a die 6 times, with 1 expected per face, is too few.

Watch out

A small pvalp_\text{val} measures evidence, not the strength of a relation, and it does not say which cell is off. Double every count in the treatment table and χ2\chi^2 doubles to 8.0, with pval=0.0047p_\text{val}=0.0047; but the shares improved are still 0.6 and 0.4. A measure of strength for a 2×22\times2 table, ϕ=χ2/n\phi=\sqrt{\chi^2/n}, stays 0.20 both times.

Here ϕ\phi is a strength coefficient; signal processing pages use the same letter for a phase.

Worked example

1. The die. Counts 6, 8, 9, 11, 12, 14: χ2=4.2\chi^2=4.2, pval=0.52p_\text{val}=0.52. Counts 4, 6, 8, 9, 12, 21: χ2=18.2\chi^2=18.2, pval=0.003p_\text{val}=0.003.

2. A 2×32\times3 table. Two groups answer a three-choice question: (20,30,50)(20,30,50) and (30,30,40)(30,30,40). The column totals are 50, 60 and 90, so the expected rows are both (25,30,45)(25,30,45). The six scaled squares are 1, 0, 0.5556, 1, 0, 0.55561,\ 0,\ 0.5556,\ 1,\ 0,\ 0.5556, so χ2=3.1111\chi^2=3.1111 with (2−1)(3−1)=2(2-1)(3-1)=2 degrees of freedom, and pval=e−3.1111/2=0.2110p_\text{val}=e^{-3.1111/2}=0.2110.

3. Mendel’s peas. Of 556 pea plants, Mendel counted 315, 108, 101 and 32 in four classes that Mendel’s theory put at 9 : 3 : 3 : 1. The expected counts are 312.75, 104.25, 104.25 and 34.75. The scaled squares are 0.0160.016, 0.1350.135, 0.1010.101 and 0.2180.218, adding to χ2=0.470\chi^2=0.470 with 3 degrees of freedom: pval=0.925p_\text{val}=0.925, a very good fit.

Try it yourself. Counts 5 and 15 against 10 and 10: χ2=(25+25)/10=5\chi^2=(25+25)/10=5. A 3×43\times4 table has 2×3=62\times3=6 degrees of freedom. With 2 degrees of freedom, the chance beyond 5.991 is e−2.9957=0.05e^{-2.9957}=0.05. Under H0H_0, the die’s statistic averages 5, its degrees of freedom.

Where you’ll meet this

Testing randomness. Random-number generators and hash functions are checked by sorting many outputs into bins and testing whether the counts are even.

Genetics. Ratios of inherited traits, like Mendel’s, are tested against their predicted proportions.

Surveys. Whether an answer depends on age group, region or product is a test of independence on a table of counts.

Model checking. Whether a histogram of data fits a model’s curve is a goodness-of-fit test, with the model’s expected count in each bin.

In signal processing · the chi-square law in a spectrum

For white Gaussian noise, each bin of the periodogram is a sum of two squared bell-shaped parts: a chi-square variable with 2 degrees of freedom, scaled. That is why its scatter does not shrink with more data, as The periodogram in the signal processing course shows.

Reference card

QuantityFormulaNotes
Statisticχ2=∑k(nk−npk)2/(npk)\chi^2=\sum_k(n_k-np_k)^2/(np_k)squared, scaled gaps
Chi-square lawχdf2\chi^2_\text{df}: mean df\mathrm{df}, variance 2 df2\,\mathrm{df}sum of squared standard bells
Goodness of fitK−1K-1 degrees of freedomKK categories
Tableexpected np+n+q/nn_{p+}n_{+q}/n; (r−1)(c−1)(r-1)(c-1) degrees of freedom
5 % points3.841, 5.991, 7.815, 11.070 for 1, 2, 3, 5 degrees of freedom2 degrees: tail e−x/2e^{-x/2}
2×22\times2 tableχ2=z2\chi^2=z^2say whether corrected
Conditionsevery expected count at least 5
Strengthϕ=χ2/n\phi=\sqrt{\chi^2/n} for a 2×22\times2 tablethe p-value is evidence, not strength

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look