Chi-Square Goodness-of-Fit Test
Inputs
| Observed counts | 50, 30, 20 |
|---|---|
| Expected counts | 40, 40, 20 |
Chi-Square Goodness-of-Fit Test
Run a Pearson chi-square goodness-of-fit test. Enter observed category counts and the counts expected under your null hypothesis to get the chi-square statistic, degrees of freedom, and p-value.
Inputs
Counts
Results
Enter a value to see results.
Details
Chi-Square Goodness-of-Fit Test
The chi-square goodness-of-fit test asks a single question: do a set of observed category counts match the counts a hypothesis predicts? It is the standard tool for comparing one categorical sample against a theoretical distribution — checking whether a die is fair, whether genetic crosses follow a 9:3:3:1 ratio, or whether the colors in a bag of candy appear in the advertised proportions. You enter the counts you actually saw in each category and the counts expected under the null hypothesis, and the test returns a statistic, its degrees of freedom, and a p-value.
The statistic
The test summarizes the gap between observation and expectation with the Pearson statistic
where is the observed count in category , is the expected count, and is the number of categories. Each term measures how far one category strayed from its prediction. The deviation is squared so that surpluses and shortfalls both count as evidence against the hypothesis rather than cancelling out, and so that larger absolute gaps weigh more heavily. Dividing by normalizes each squared deviation: a miss of ten counts is a serious discrepancy when only five were expected but trivial when five thousand were expected, and dividing by the expected count puts every category on a comparable scale. Summing across categories gives a single number that grows as the data drift away from the hypothesis. When the observed counts equal the expected counts exactly, every term is zero and .
Degrees of freedom and the p-value
The statistic is compared against a chi-square distribution whose shape is fixed by its degrees of freedom. For a simple goodness-of-fit test,
One degree of freedom is spent because the category counts must add up to a known total: once the first k − 1 counts are fixed, the last is determined, so only k − 1 of them are free to vary. (If the expected distribution is itself estimated from the data using extra parameters, subtract one more degree of freedom per estimated parameter.)
The p-value is the upper-tail probability — the chance of seeing a statistic at least as large as the one observed if the null hypothesis were true. Goodness-of-fit is inherently a one-sided, upper-tail test: only large values of signal a poor fit, while small values mean the data sit close to the prediction. You compare the p-value against a significance level (commonly five percent). When , the discrepancy is larger than sampling noise comfortably explains, and you reject the hypothesis that the data follow the expected distribution. When , the data are consistent with the hypothesis — which is not the same as proving it true.
Worked example
Suppose a store expects its three checkout lanes to handle traffic in a 40:40:20 split across 100 customers, and one afternoon it records 50, 30, and 20 customers. The expected counts are 40, 40, and 20. Then
χ2=40(50−40)2+40(30−40)2+20(20−20)2=40100+40100+0=2.5+2.5+0=5.With three categories the test has two degrees of freedom. The upper-tail p-value for at two degrees of freedom is about . Since , the observed split is consistent with the expected 40:40:20 distribution at the 5% level: the apparent imbalance between the first two lanes is within ordinary sampling variation.
Assumptions
The chi-square approximation rests on a few conditions. Expected counts should be reasonably large — a common rule of thumb requires every expected count to be at least 5; when several are smaller, combine sparse categories or use an exact test instead. Observations must be independent, each unit falling into exactly one category. And the test works on raw frequencies, not percentages or proportions: if a hypothesis is stated as proportions, multiply each by the total sample size to get expected counts before running the test, and confirm that the expected counts sum to the same total as the observed counts.
For comparing the means of two groups rather than categorical frequencies, see the Two-Sample t-Test Calculator (Welch); for turning a z-statistic into a tail probability, see P-Value from Z-Score Calculator.
Frequently Asked Questions (FAQ)
What is a goodness-of-fit test?
A goodness-of-fit test checks whether a set of observed category counts is consistent with a hypothesized distribution. The Pearson chi-square version compares the counts you observed in each category against the counts you would expect under the null hypothesis, using χ² = Σ(O−E)²/E. A small statistic means the data look like the hypothesis; a large one means they depart from it.
Common uses include testing whether a die is fair, whether outcomes match Mendelian ratios, or whether a sample matches known population proportions.
What is the difference between observed and expected counts?
Observed counts are the raw frequencies you actually recorded in each category. Expected counts are what the null hypothesis predicts: for a fair six-sided die rolled 120 times, each face is expected 20 times. Expected counts must be frequencies, not proportions or percentages — if your hypothesis is a set of proportions, multiply each by the total sample size first. The expected counts should sum to the same total as the observed counts.
Why are the degrees of freedom one less than the number of categories?
For a simple goodness-of-fit test the degrees of freedom equal k − 1, where k is the number of categories. One degree of freedom is lost because the counts must sum to a fixed total: once you know the counts in the first k − 1 categories, the last one is determined. If the expected distribution is estimated with additional parameters from the same data, you subtract one more degree of freedom for each estimated parameter.
Why should expected counts be at least 5?
The chi-square statistic only approximately follows a chi-square distribution, and the approximation is most reliable when the expected count in every category is at least 5 (a widely used rule of thumb). With smaller expected counts the p-value can be misleading. In that situation, combine sparse categories, collect more data, or use an exact test such as the multinomial or Fisher exact test instead.
Recommended Next
Two-Sample t-Test Calculator (Welch)
Run a Welch's two-sample t-test from summary statistics. Enter the mean, standard deviation, and sample size of two independent groups to get the t statistic, degrees of freedom, and two-tailed p-value.
P-Value from Z-Score Calculator
Convert a z-statistic into a p-value using the standard normal distribution. Supports two-tailed and one-tailed (left or right) tests.