Statistics

Chi-Square Goodness-of-Fit Test

Enter your observed counts and the expected counts under the null distribution. The test statistic χ² = Σ (O − E)²/E is compared against the chi-square distribution with df = k − 1, giving the p-value and the reject / fail-to-reject conclusion at your chosen α.

Chi-Square Goodness-of-Fit Test

Compare observed and expected frequencies — χ², df, p-value.

Try:
Answerχ² = 7.5, df = 4, p = 0.111709, fail to reject H₀
  1. Observed15, 15, 20, 30, 20
  2. Expected20, 20, 20, 20, 20
  3. Per-cell terms(15−20)²/20 = 1.25; (15−20)²/20 = 1.25; (20−20)²/20 = 0; (30−20)²/20 = 5; (20−20)²/20 = 0
  4. Test statisticχ² = Σ (O − E)²/E = 7.5
  5. Degrees of freedomdf = k − 1 = 4
  6. p-valueP(χ² > 7.5 | df = 4) = 0.111709
  7. Critical valueχ²_crit at α = 0.05, df = 4: 9.48773
  8. Conclusionχ² ≤ χ²_crit (p ≥ α) — fail to reject H₀; the data are consistent with the expected distribution.

Measuring the gap between observed and expected

A goodness-of-fit test asks whether counts falling into categories are consistent with a distribution you proposed in advance. Are the six faces of this die appearing equally often? Do customer arrivals match the split the model predicts? The counts will never match exactly, so the real question is whether the mismatch is larger than chance would produce.

This calculator takes the observed and expected counts, computes the chi-square statistic, and compares it against the chi-square distribution to return degrees of freedom, a critical value, a p-value and a conclusion at your chosen significance level.

The statistic aggregates the discrepancy across every category into a single number, weighting each squared difference by how large the expected count was. A shortfall of five where twenty were expected is far more surprising than the same shortfall where two hundred were expected, and dividing by E is what encodes that.

Because the per-category contributions are listed individually, the tool also shows where the misfit lives. A large statistic driven almost entirely by one category is a different finding from one spread evenly, and only the itemised list distinguishes them.

How to use this calculator

  1. Enter the observed counts As a comma-separated list, in category order. These are raw counts of items, not percentages or proportions — converting to percentages first destroys the sample size the test depends on.
  2. Enter the expected counts in the same order The list must have the same length as the observed one, and every value must be positive. For a uniform hypothesis these are all the total divided by the number of categories.
  3. Set the significance level α A value strictly between 0 and 1, conventionally 0.05. This is the false-positive rate you are willing to accept, and it must be chosen before seeing the data if the test is to mean anything.
  4. Read the contributions before the verdict Each category's term is shown separately. Scanning them tells you which categories drove the result, which is usually more informative than the reject decision on its own.

The formula, and where it comes from

χ² = Σ (O − E)² / E, with df = k − 1

For each category the difference between observed and expected is squared, removing the sign so that shortfalls and excesses both count as misfit, and then divided by the expected count to scale it. Summing across categories gives a statistic that grows as the data depart from the hypothesis in either direction.

The degrees of freedom are the number of categories minus one. One is subtracted because the counts must add up to the sample total: once the first k − 1 category counts are known, the last is fixed, so only k − 1 of them are free to vary.

What each input means

O Observed counts — form field “Observed counts (comma-separated)”
What you actually counted in each category, as whole numbers of items. At least two categories are required. Units: counts.
E Expected counts — form field “Expected counts (comma-separated)”
What the null hypothesis predicts for each category, in the same order and the same units. Every value must be strictly positive, since the formula divides by it. These may be fractional even though the observed counts are not. Units: counts.
α Significance level — form field “Significance level α”
The threshold for rejecting the null hypothesis, strictly between 0 and 1. It sets the critical value the statistic is compared against.
df Degrees of freedom
Computed here as k − 1, where k is the number of categories. If you estimated any parameters from the same data, the correct df is lower and this tool does not adjust for that.

Worked examples

Every number below is produced by the same calculation engine the tool above runs. Nothing here is typed by hand, so the walkthrough cannot drift from what you get when you enter the same values yourself.

A moderate departure from uniform

A hundred observations across five categories, expected to be evenly split at twenty each. The observed counts are 15, 15, 20, 30, 20 — one category clearly over, two under.

Inputs Observed counts (comma-separated) = 15, 15, 20, 30, 20, Expected counts (comma-separated) = 20, 20, 20, 20, 20, Significance level α = 0.05

  1. Observed 15, 15, 20, 30, 20
  2. Expected 20, 20, 20, 20, 20
  3. Per-cell terms (15−20)²/20 = 1.25; (15−20)²/20 = 1.25; (20−20)²/20 = 0; (30−20)²/20 = 5; (20−20)²/20 = 0
  4. Test statistic χ² = Σ (O − E)²/E = 7.5
  5. Degrees of freedom df = k − 1 = 4
  6. p-value P(χ² > 7.5 | df = 4) = 0.111709
  7. Critical value χ²_crit at α = 0.05, df = 4: 9.48773
  8. Conclusion χ² ≤ χ²_crit (p ≥ α) — fail to reject H₀; the data are consistent with the expected distribution.

Result χ² = 7.5, df = 4, p = 0.111709, fail to reject H₀

The per-category contributions show where the statistic comes from: the two shortfalls of five contribute 1.25 each, the excess of ten contributes 5, and the two exact matches contribute nothing. The single over-represented category supplies most of the total on its own.

The resulting statistic is not large enough to clear the critical value at α = 0.05, so the test fails to reject the uniform hypothesis. That is not the same as confirming it: with only a hundred observations the test simply lacks the power to detect a departure of this size.

A departure large enough to reject

The same five categories and the same expected split, but with the imbalance pushed further: 12, 10, 18, 40, 20.

Inputs Observed counts (comma-separated) = 12, 10, 18, 40, 20, Expected counts (comma-separated) = 20, 20, 20, 20, 20, Significance level α = 0.05

  1. Observed 12, 10, 18, 40, 20
  2. Expected 20, 20, 20, 20, 20
  3. Per-cell terms (12−20)²/20 = 3.2; (10−20)²/20 = 5; (18−20)²/20 = 0.2; (40−20)²/20 = 20; (20−20)²/20 = 0
  4. Test statistic χ² = Σ (O − E)²/E = 28.4
  5. Degrees of freedom df = k − 1 = 4
  6. p-value P(χ² > 28.4 | df = 4) = 0.0000103481
  7. Critical value χ²_crit at α = 0.05, df = 4: 9.48773
  8. Conclusion χ² > χ²_crit (p < α) — reject H₀ that the observed and expected distributions agree.

Result χ² = 28.4, df = 4, p = 0.0000103481, reject H₀

The excess in the fourth category has doubled to twenty, and because the difference is squared its contribution grows fourfold rather than twofold. This is why chi-square is so sensitive to a single badly-fitting category.

The p-value now falls below α, so the null hypothesis is rejected. The correct reading is that the data are unlikely under a uniform model — not that any particular alternative has been proved.

Reading the result

What rejection does and does not establish

Rejecting the null means the counts would be unusual if the proposed distribution held. It does not identify the true distribution, nor say the effect is large: with a big enough sample, a trivial departure rejects.

Failing to reject is not confirmation

A p-value above α means the data are compatible with the hypothesis, not that the hypothesis is true. Small samples fail to reject almost everything, so the result is only as strong as the sample size behind it.

Reading the p-value

The p-value is the probability of a statistic at least this extreme when the null hypothesis holds. It is not the probability that the hypothesis is true, and a p-value of 0.03 does not mean a 3% chance of being wrong.

When you would use this

Testing a die or generator for fairness

Roll counts against a uniform expectation is the textbook case, and the same structure checks whether a random number generator spreads evenly across buckets.

Comparing a sample against a known population split

When the breakdown of a population is known — say the regional split of customers — the test checks whether a sample matches it, which is the standard way to detect a biased recruitment or response process.

Assumptions and limitations

What this calculator assumes

  • The expected counts come from a hypothesis chosen before the data, not fitted to them; df = k − 1 assumes no parameters were estimated from the same sample.
  • Every expected count is positive. The chi-square approximation is generally considered reliable when each expected count is at least 5.

Where it stops being the right tool

  • Goodness-of-fit only. A test of independence between two categorical variables uses a contingency table and different degrees of freedom.
  • Degrees of freedom are always k − 1. If you estimated parameters from the data, subtract one per parameter yourself; the tool will not do it.

Common mistakes

Entering percentages instead of counts

Why it happens. Category breakdowns are usually reported as percentages, and a list of percentages looks exactly like a list of counts to the calculator.

How to avoid it. Multiply the percentages by the sample size first. The statistic scales with the total, so entering percentages effectively claims a sample of 100 and understates the evidence.

Choosing α after seeing the p-value

Why it happens. When a p-value lands at 0.06, moving α to 0.10 is tempting and takes only a moment, since the field is right there.

How to avoid it. Fix α before running the test. Adjusting it afterwards means the reported false-positive rate no longer describes the procedure you actually followed.

Using k − 1 when parameters were estimated from the data

Why it happens. The k − 1 rule is the one that gets memorised, and the exception for fitted parameters is easy to forget when the expected counts were derived from the same sample.

How to avoid it. Subtract one further degree of freedom per estimated parameter. Fitting a Poisson mean to the data, for instance, gives df = k − 2, and using k − 1 makes the test too permissive.

Frequently asked questions

What does the null hypothesis say here?

That the observed counts are consistent with the expected distribution — in other words that the proposed model fits. The test looks for evidence against it and never proves it correct.

How small can the expected counts be?

The usual rule of thumb is that every expected count should be at least 5. Below that the chi-square approximation to the sampling distribution degrades and the p-value becomes unreliable, so an exact test is preferable.

How are the degrees of freedom determined?

As k − 1, where k is the number of categories, because the counts must total the sample size and so only k − 1 are free. If you estimated parameters from the same data, subtract one more for each — this tool always uses k − 1.

Can the expected counts be fractional?

Yes. Splitting 100 observations across three categories gives expected counts near 33.3, which is entirely valid. Only the observed counts must be whole numbers, since they are counts of real items.