Chi-squared test in this visualization
Roll a die with faces times, each roll independent of the others, and count how often each face comes up: the observed counts . If the die is fair, each face is expected times. The chi-squared statistic adds up each face’s squared difference from , measured against . For a fair die and enough rolls, approximately follows the chi-squared distribution with degrees of freedom, and the p-value is the area under that curve to the right of the observed . Here the counts are yellow columns with as an ink line, and the curve sits below them.
Why each term is divided by E
An observed count that exceeds the expected count by 9 is a big surprise when and hardly noticeable when . Chance variation in a count grows with the count: for a fair die each count has variance , close to . Dividing the squared difference by puts every face on that common scale, so a term near 1 is an ordinary amount of chance. Squaring lets one far-off face dominate: in the six-sided example, nineteen sixes contribute 8.1 of .
Why k − 1 degrees of freedom
The counts must add up to , so the differences always add up to 0: once of them are known, the last one is fixed. For a fair die the average of is exactly , so the same means less when there are more faces: 11.2 is past the 5% point for a six-sided die (p ≈ 0.048) and ordinary for a twelve-sided one (p ≈ 0.43). What a p-value does and does not tell you is the subject of hypothesis testing and p-values.
When the curve can be trusted
With a fair die, can take only certain values, so its true distribution is a set of separate spikes; the chi-squared curve is the smooth shape they approach as grows. Rerunning the rolls with a fair die estimates the true tail probability directly. With an expected count of 10 per face the estimate and the curve agree closely; with only 6 rolls they do not. A common rule of thumb asks for expected counts of at least about 5. More rolls work the other way too: multiplying every count by ten multiplies by ten, so with enough independent rolls even a slight lean becomes significant. measures the evidence against a fair die, not how unfair the die is, and a large p only means these counts are not enough to show it is unfair.
Things to notice
- When the hypothesis gives the categories unequal probabilities , the expected counts are and everything else stays the same.
Controls
- Press or drag above a face to set its count; pressing its number under the axis only selects it.