What the goodness-of-fit test measures
The chi-square goodness of fit test compares observed category frequencies with the frequencies expected under a hypothesized distribution. It answers one question: do the observed counts match the expected pattern? You have one sample, one categorical variable with k categories, and a distribution you want to test against. The output is a chi-square statistic, the degrees of freedom for the design, and a p-value that tells you whether the discrepancy between observed and expected is larger than chance alone would explain under the null hypothesis.
Common uses include testing whether a die is fair, whether observed genetic ratios match Mendelian predictions, whether weekly customer traffic is evenly distributed across the days of the week, and whether a categorical survey response follows a known population breakdown. In every case the structure is the same: observed counts, expected counts, chi-square statistic, p-value.
Formula and degrees of freedom
The Pearson chi-square goodness of fit formula is χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ, where the sum runs over every category, Oᵢ is the observed count in category i, and Eᵢ is the expected count under the null hypothesis. Each term (Oᵢ − Eᵢ)² / Eᵢ is a contribution; the statistic is their sum. Larger discrepancies produce larger contributions, scaled by how many observations were expected in that category.
Degrees of freedom for a standard goodness-of-fit test with k categories is df = k − 1. If any parameter of the hypothesized distribution was estimated from the same data — for example, using the sample mean to fit a Poisson model — subtract one degree of freedom per estimated parameter. A chi-square value interpreted with the wrong df produces a meaningless p-value, which is why the df adjustment matters.
The p-value is the upper-tail probability P(X² ≥ χ²) under the chi-square distribution with the given df. The critical value χ²crit is the threshold at which that probability equals α. Reject the null if p < α, or equivalently if χ² > χ²crit. Both rules always give the same answer.
Worked example: is a die fair?
Roll a six-sided die 100 times and record each face. The null hypothesis is that all six faces are equally likely, so the expected count for each is 100 / 6 ≈ 16.67. Suppose the observed counts are 18, 14, 12, 20, 16, 20. Each contribution is (O − E)² / E:
- (18 − 16.67)² / 16.67 ≈ 0.106
- (14 − 16.67)² / 16.67 ≈ 0.427
- (12 − 16.67)² / 16.67 ≈ 1.306
- (20 − 16.67)² / 16.67 ≈ 0.667
- (16 − 16.67)² / 16.67 ≈ 0.027
- (20 − 16.67)² / 16.67 ≈ 0.667
Summing the contributions gives χ² ≈ 3.20. With k = 6 categories and no estimated parameters, df = 5. The upper-tail p-value is P(X² ≥ 3.20) with df = 5, which is about 0.67. At α = 0.05 the data are consistent with a fair die — the observed counts are not far enough from the expected to reject the null.
Worked example: Mendelian ratio
Suppose you expect a 9:3:3:1 ratio of four phenotypes in 160 offspring. The expected counts are 90, 30, 30, 10. You observe 86, 32, 29, 13. Each contribution is (O − E)² / E, and the sum gives χ². With k = 4 categories and no estimated parameters, df = 3. Compare the p-value to your chosen α for the decision. The structure is identical to the die example — only the expected counts and df change.
Assumptions and diagnostics
Three assumptions matter for the chi-square approximation to be reliable:
- Independent observations. Each observation falls into exactly one category and does not influence any other. Repeated measures, matched pairs, and clustered sampling break this and call for a different method.
- Mutually exclusive and exhaustive categories. Every observation belongs to one and only one category, and the categories together cover every possible outcome.
- Sufficiently large expected counts. A common rule of thumb is at least 5 in every expected cell.
The calculator flags two situations. If any expected count is below 5, the asymptotic approximation may be unreliable — consider combining sparse categories (if scientifically defensible) or using an exact method. If only two categories are present, the test is mathematically related to the exact binomial, which is often preferable for small samples.
Goodness of fit vs test of independence
These are the two most common uses of the chi-square statistic and they are easy to confuse.
- Goodness of fit — one sample, one categorical variable, one hypothesized distribution. df = k − 1 (or fewer if parameters were estimated). This is what this calculator handles.
- Test of independence — one sample, two categorical variables arranged in an r × c contingency table. df = (r − 1)(c − 1). Use a dedicated independence calculator for that workflow.
Both use the Pearson χ² form. What changes is the design and the degrees of freedom. Mixing them up produces a statistic with the wrong df and a meaningless p-value.
Tips and limits
- Convert percentages to counts. The chi-square statistic is not scale-invariant. If you have percentages, multiply by N first, or use the calculator's proportion mode.
- Do not forget the df reduction when parameters were estimated. Fitting a distribution to the data costs one df per estimated parameter.
- Do not read p > α as proof of the null. Failing to reject H₀ means the data did not provide sufficient evidence at the chosen α. The sample may simply be too small.
- The contribution table is a diagnostic, not a formal test. The omnibus test tells you whether the overall distribution matches; it does not identify which categories differ. Formal follow-up comparisons require a separate analysis that controls the familywise error rate.
- This calculator handles the one-sample goodness-of-fit case only. It does not run tests of independence, homogeneity, Fisher's exact test, McNemar's test, or any of the small-sample alternatives. It does not compute power or required sample size for goodness-of-fit tests.