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.
Predict first. Roll a truly fair die 60 times. How often will the needle reach the shaded zone?
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 in categories, with the counts expected if the die is fair (here each),
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 is roughly a standard bell when the expected count is not too small. A sum of squares of independent standard bells follows the chi-square law with degrees of freedom, . It has mean and variance .
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 , with mean 5 and standard deviation . In general a goodness-of-fit test with categories has degrees of freedom.
The shaded zone starts at the 95 % point of , . The 5 % points for 1, 2, 3 and 5 degrees of freedom are , , and . With 2 degrees of freedom the tail has a simple formula, , so its 5 % point is .
Testing a die
Two fair-looking rolls. Counts of 6, 8, 9, 11, 12, 14 have gaps , whose squares add to 42, so : with 5 degrees of freedom, . Nothing odd. Counts of 4, 6, 8, 9, 12, 21 have squares adding to 182, so and : 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.
Describe this picture
A histogram of 1000 repeats (seed 8401) of “roll the die 60 times and compute the needle”, drawn over the 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.
Tables: is one thing related to another?
Fifty patients get a treatment and 50 do not. Did more of the treated improve?
| Group | Improved | Not improved | Total |
|---|---|---|---|
| Treatment | 30 | 20 | 50 |
| Control | 20 | 30 | 50 |
| Total | 50 | 50 | 100 |
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 in every cell. In general, with the count in row and column , and , the row and column totals, the expected count is .
Each cell is 5 away from 25, so . A table with rows and columns has degrees of freedom, here 1, and .
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 , and , the same test.
Some software applies a continuity correction to tables, shrinking each gap by 0.5 first: and . 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 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 doubles to 8.0, with ; but the shares improved are still 0.6 and 0.4. A measure of strength for a table, , stays 0.20 both times.
Here 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: , . Counts 4, 6, 8, 9, 12, 21: , .
2. A table. Two groups answer a three-choice question: and . The column totals are 50, 60 and 90, so the expected rows are both . The six scaled squares are , so with degrees of freedom, and .
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 , , and , adding to with 3 degrees of freedom: , a very good fit.
Try it yourself. Counts 5 and 15 against 10 and 10: . A table has degrees of freedom. With 2 degrees of freedom, the chance beyond 5.991 is . Under , 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
| Quantity | Formula | Notes |
|---|---|---|
| Statistic | squared, scaled gaps | |
| Chi-square law | : mean , variance | sum of squared standard bells |
| Goodness of fit | degrees of freedom | categories |
| Table | expected ; degrees of freedom | |
| 5 % points | 3.841, 5.991, 7.815, 11.070 for 1, 2, 3, 5 degrees of freedom | 2 degrees: tail |
| table | say whether corrected | |
| Conditions | every expected count at least 5 | |
| Strength | for a table | the p-value is evidence, not strength |