Chi-Squared Test
Visualize the Chi-Squared goodness-of-fit test by comparing observed and expected frequencies.
By Riano · Published · Updated · Report a correction
Chi-Squared Test
A goodness-of-fit test compares category counts with the counts predicted by a specified probability model. This explorer uses equal probabilities, so every expected count is the total divided by the number of categories. Taller gaps matter more because the Pearson statistic squares each gap before dividing by its expected count.
Ei = n / k
With no distribution parameters estimated from the observations, the reference distribution has k − 1 degrees of freedom. A larger statistic represents a larger discrepancy from the specified model, but its interpretation also depends on those degrees of freedom.
Thirty counts, three equal expectations
Set three categories, a sample size of thirty and Distribution Skew to −3. The deterministic illustration produces counts 15, 9 and 6. Under equal category probabilities, each expected count is 30 / 3 = 10. Calculate the statistic before checking the display:
| Category | Observed O | Expected E | O − E | (O − E)2 / E |
|---|---|---|---|---|
| 1 | 15 | 10 | 5 | 25 / 10 = 2.5 |
| 2 | 9 | 10 | −1 | 1 / 10 = 0.1 |
| 3 | 6 | 10 | −4 | 16 / 10 = 1.6 |
- Add the contributions: 2.5 + 0.1 + 1.6 = 4.2. The statistic reads 4.200.
- There are 3 − 1 = 2 degrees of freedom.
- For two degrees of freedom, the chi-square reference upper tail is exp(−x / 2). At x = 4.2 this gives exp(−2.1) ≈ 0.122456, displayed as 0.1225.
At a five-percent significance level, this example fails to reject equal probabilities. At twenty percent it rejects them, while the evidence remains unchanged. This comparison illustrates different decision rules: choose the significance level before examining data. The p-value is a reference-tail probability under the null model, not the probability that the null hypothesis is true.
Keep the evidence fixed, change the rule
Open the Chi-Squared Test explorer and use its four sliders:
- Set Number of Categories (k) to 3, Sample Size (n) to 30, Distribution Skew to −3 and Significance Level (α) to 0.05. Check expected count 10.00, statistic 4.200, two degrees of freedom and p-value 0.1225. Hover the bars to inspect their counts.
- Change only Significance Level (α) to 0.20. The critical value falls and the conclusion changes to Reject H0. Bar heights, statistic and p-value stay fixed; the third observed bar changes color because the display threshold also depends on α.
- Return α to 0.05, then set Distribution Skew to 0. The generated counts become 10, 10 and 10: the statistic is 0.000 and the reference p-value is 1.0000. Perfect agreement in these observed counts does not prove that population probabilities are equal.
- For a second check, use the default values k = 4, n = 100, skew = 0 and α = 0.05. Counts 28, 22, 22 and 28 each differ from their expectation 25 by three in magnitude. Four contributions of 9 / 25 sum to 1.440, with three degrees of freedom and displayed p-value 0.6962.
The legend occupies its own space above the plot. Gray bars show expectations; indigo and red bars show generated observations. Red means a contribution exceeds the displayed critical value divided by k. It is a visual heuristic, not a separate hypothesis test for that category. On a narrow screen, scroll the controls panel to reach the statistics.
What this illustration can establish
Read these boundaries alongside the statistic: generated counts, a reference distribution and a chosen decision rule answer different questions.
- Counts come from seeded noisy weights rounded to an exact total. They are a reproducible illustration, not independent multinomial simulation draws or observations from a real population. Changing k, n or skew changes the seed as well; increasing n does not append observations to a fixed sample.
- A real goodness-of-fit application needs independent observations, mutually exclusive categories and appropriate expected probabilities. Estimating model parameters can reduce the reference degrees of freedom further than the k − 1 used here.
- The chi-square reference is an approximation for categorical counts. The legal setting n = 20, k = 10 has expected count 2 in each category, where the approximation can be poor. The explorer still displays its numerical illustration; it does not provide an exact small-sample test.
- The displayed critical value uses a Wilson–Hilferty and inverse-normal approximation, separately from the gamma-CDF calculation of the p-value. Near a cutoff, these numerical routes can give different decisions. Very small displayed p-values are marked below 0.001, which does not mean their mathematical value is zero.
- Failure to reject is not proof of the null hypothesis. A rejection does not identify a cause for the discrepancy. The significance level should be chosen before inspecting the result.
Sources for the calculation and interpretation
These NIST handbook sections support the test formula, its reference distribution and the interpretation of a preselected significance level. The seeded numerical examples above are calculated for this explorer.
- NIST: Chi-Square Goodness-of-Fit Test gives the Pearson statistic, expected-count model and approximate reference degrees of freedom.
- NIST: Chi-Square Distribution gives the gamma density and cumulative distribution; its two-degree density yields the exponential tail used above.
- NIST: Critical Values and p Values explains the conditional tail interpretation and choosing a cutoff before testing.
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Chi-Squared Test module.
Try Chi-Squared Test on Riano →