Hypothesis tests

Chi-square goodness-of-fit test calculator

Assess whether observed frequencies follow a specified theoretical distribution.

Calculator

Enter the observed frequencies and the theoretical probabilities separated by commas. If you don't provide probabilities, a uniform distribution is assumed.

Result pending…
Expected frequencies pending…

Explanation

The chi-square goodness-of-fit test lets you assess whether the empirical distribution of a categorical variable is compatible with a previously specified theoretical distribution. The question it answers is: are the counts observed in each category compatible with the probabilities postulated by the model, or is there evidence that the actual distribution differs?

The core idea is to compare the observed frequencies with the expected frequencies that would occur if the null hypothesis were true. If the discrepancies are too large to attribute to sampling chance, the proposed theoretical model is rejected.

For example, a fair die should produce each face with probability 1/6. After 120 rolls, you compare whether the observed counts are compatible with those probabilities. Another common use is checking whether the proportion of defects by type on a production line matches the expected historical pattern.

The test statistic aggregates the weighted discrepancies across all categories and follows a chi-square distribution under the null hypothesis. The degrees of freedom are \(k - 1\), where \(k\) is the number of categories, minus the number of additional parameters estimated from the sample.

Hypotheses and test statistic

\(H_0\): the observed distribution matches the theoretical one

\(H_1\): the observed distribution does not match the theoretical one

\(\chi^2 = \sum_{i=1}^{k}\frac{(O_i - E_i)^2}{E_i}\),   with \(E_i = n \cdot p_i\)

Quick check

The chi-square statistic sums, for each category, the squared difference between observed and expected frequency, divided by the expected frequency. Under the null hypothesis that the data follow the theoretical distribution, this statistic approximately follows a \(\chi^2\) distribution with \(k - 1\) degrees of freedom:

\(\chi^2 = \sum_{i=1}^{k} \frac{(O_i - E_i)^2}{E_i} \sim \chi^2_{k-1}\)

Expected frequencies are computed from the sample size \(n\) and the theoretical probabilities \(p_i\):

\(E_i = n \cdot p_i\)

where \(n = \sum_{i=1}^{k} O_i\) is the total number of observations. If the theoretical probabilities are equal across all categories (uniform distribution), \(E_i = n / k\) for every \(i\).

For the chi-square approximation to be reliable, all expected frequencies should be greater than or equal to 5. If any category has very low expected frequencies, consider grouping categories before applying the test.

Worked example

A fair die is rolled 60 times and the observed frequencies are recorded for each face: face 1 → 8 times; face 2 → 12; face 3 → 9; face 4 → 11; face 5 → 10; face 6 → 10. Is there statistical evidence, at \(\alpha = 0.05\), that the die is not fair?

Under \(H_0\) (fair die), the probability of each face is \(1/6\), so the expected frequency for each category is \(E_i = 60 \times \tfrac{1}{6} = 10\) rolls.

The chi-square goodness-of-fit statistic is computed as:

\( \chi^2 = \sum_{i=1}^{k} \frac{(O_i - E_i)^2}{E_i} = \frac{(8-10)^2}{10} + \frac{(12-10)^2}{10} + \frac{(9-10)^2}{10} + \frac{(11-10)^2}{10} + \frac{(10-10)^2}{10} + \frac{(10-10)^2}{10} \)

\( \chi^2 = 0.4 + 0.4 + 0.1 + 0.1 + 0 + 0 = 1.0 \)

The degrees of freedom are \(df = k - 1 = 6 - 1 = 5\). The critical value for \(\alpha = 0.05\) and \(df = 5\) is \(\chi^2_{0.05;\,5} \approx 11.07\), and the p-value associated with \(\chi^2 = 1.0\) at 5 degrees of freedom is \(p \approx 0.96\).

Since \(\chi^2 = 1.0 \ll 11.07\) (and \(p \gg 0.05\)), \(H_0\) is not rejected. All expected frequencies equal 10, well above the recommended minimum of 5, so the test is valid in this case.

Conclusion: there is no statistical evidence that the die is loaded. The observed deviations from the expected frequencies are perfectly compatible with the random variability of a fair die over 60 rolls.

How to interpret the result

Rejecting \(H_0\) (p-value < \(\alpha\)) means that the observed frequencies differ from those expected under the theoretical model by more than chance can explain. In practice, the proposed distributional model (uniform, binomial, Poisson, etc.) does not adequately describe the data. However, overall significance does not reveal where the discrepancy lies: to locate it, check the standardized residuals \((O_i - E_i)/\sqrt{E_i}\) for each category. Absolute values greater than 2 flag the categories that most drive the statistic away from what is expected under \(H_0\).

Failing to reject \(H_0\) (p-value ≥ \(\alpha\)) does not prove that the data "follow" the theoretical model; it only indicates that they are compatible with it at the chosen significance level. With small samples the power of the test is limited, and moderate deviations can go unnoticed. With very large samples, on the other hand, small systematic deviations of little practical relevance can turn out to be significant.

The chi-square statistic \(\chi^2 = \sum_i (O_i - E_i)^2 / E_i\) aggregates the squared discrepancies across all categories, weighted by the expected frequency. In the chart, the green zone is the non-rejection region and the red zone is the right-tailed critical region (the goodness-of-fit test is always right-tailed, since \(\chi^2 \geq 0\)); the amber line marks the observed statistic. Before interpreting the result, check that all expected frequencies are ≥ 5; if not, group categories or use an exact test.

Frequently asked questions

  • When is the chi-square goodness-of-fit test used? When you want to know whether the counts observed in mutually exclusive categories are compatible with a previously specified theoretical distribution.
  • How many degrees of freedom does the statistic have? The degrees of freedom are \(k - 1\), where \(k\) is the number of categories. If additional model parameters have been estimated from the sample itself, one degree of freedom is subtracted for each estimated parameter.
  • What condition must the expected frequencies meet? The approximation is adequate when all expected frequencies are ≥ 5. If any is lower, consider grouping infrequent categories.
  • What's the difference from the chi-square test of independence? The goodness-of-fit test checks whether a variable follows a specific theoretical distribution. The independence test checks whether two variables are associated with each other, based on a contingency table.
  • Do the theoretical probabilities need to add up to exactly 1? Yes. The tool checks that the entered probabilities add up to 1 (within numerical tolerance) and warns if that condition is not met.
  • How does sample size affect the result? With very large samples, the test can detect very small deviations from the theoretical model that are not practically relevant. In those cases it is a good idea to complement the p-value with a measure of effect size.
  • What is the minimum detectable effect (MDE) in this test? It is the smallest deviation from the theoretical model that the test can detect with the power and \(\alpha\) set. If the actual discrepancy is smaller, the test may fail to reject \(H_0\) even though a small difference exists.

Reference: Chi-square goodness-of-fit test — Wikipedia