Chalk−1

Math · Introductory statistics · Concept

The chi-square goodness-of-fit test

A chi-square goodness-of-fit test asks whether counts in categories match a claimed distribution, such as a fair die or a 3:1 genetic ratio. Each category adds (observed−expected)2/⁢expected to the statistic χ2. When the claim is right, χ2 follows a chi-square distribution with k−1 degrees of freedom, and a small p-value means the counts fit the claim poorly.

A concrete case

Do customers favor one lane?

A store has four checkout lanes, and the manager believes customers pick them evenly. Of 80 customers one afternoon, 26 used lane 1, 18 lane 2, 21 lane 3 and 15 lane 4. An even split would put 20 in each lane. Is lane 1 a favorite, or is this ordinary chance?

The misses from 20 are +6, −2, +1 and −5. They add to 0, and they always will, because the counts and the expected counts both total 80. Squaring each miss stops the signs from canceling, which gives 36, 4, 1 and 25.

A miss of 6 means more when 20 are expected than when 2,000 are, so each square is divided by its expected count. The total is 66⁢/20=3.3, and that total is the chi-square statistic. If customers really choose evenly, four lanes give a total of 3.3 or more about 35⁢% of the time. This afternoon is no evidence of a favorite lane.

62+(−2)2+12+(−5)220=3.3

The rule

Hypotheses and expected counts

H0 states the claimed distribution, a share pi for each category i. Ha says that at least one share differs. Under H0, category i is expected to hold Ei=n⋅pi of the n observations, the binomial mean n⁢p one category at a time. Expected counts need not be whole numbers. For the four lanes each pi is 1⁢/4, so each Ei is 80⋅1⁢/4=20.

Ei=npi

The statistic

Oi is the observed count in category i and Ei its expected count. Each of the k categories contributes its squared miss divided by its expected count, and χ2 adds the contributions. χ2 is never negative. It is 0 only when every count matches exactly, and it grows as the counts stray from the claim.

χ2=∑(Oi−Ei)2Ei

Degrees of freedom and the p-value

With k categories there are k−1 degrees of freedom. Once k−1 counts are known, the total fixes the last one. The p-value is the area to the right of χ2 under the chi-square curve with k−1 degrees of freedom. Only the right tail counts, because a poor fit makes χ2 large. A table of critical values shows how large χ2 must be for p to fall below 0.05.

Critical values of χ2 for a right tail of 0.05
Degrees of freedomχ2
13.841
25.991
37.815
49.488
511.070
612.592

The decision

As in every test, compare p with a level α chosen in advance. A p-value at or below α is evidence that the counts do not fit the claimed distribution. A larger p-value says they are consistent with it, which does not prove the claim.

Why it works

Why (Oi−Ei)2/Ei, and why k−1

Under H0, the count Oi is binomial, with mean Ei=npi and variance npi(1−pi). The average of (Oi−Ei)2 over many samples is that variance. Dividing by Ei, each term of χ2 averages 1−pi.

Add the k terms. χ2 averages k−(p1+p2+…+pk), and the shares add to 1, so χ2 averages k−1. The degrees of freedom are the size of χ2 to expect when the claim is right. The die example has 5 degrees of freedom and χ2=5.0, right on target.

A heuristic, not a proof, explains the curve. Dividing a miss by Ei puts it on roughly a z-score scale, and each term is that z-score squared. When the expected counts are large, those z-scores are close to normal, and their squared sum approximately follows the chi-square curve. Small expected counts spoil the approximation, which is why each Ei should be at least 5.

npi(1−pi)npi=1−pi

The method

A goodness-of-fit test in six steps

The table runs the test on the die from the first worked example, rolled 120 times with face counts 25, 17, 15, 23, 24 and 16. Lay the counts out in columns for Oi, Ei and (Oi−Ei)2/Ei, and the last column’s total is χ2.

Testing a die rolled 120 times
StepWhat to doFor the die
1State H0, Ha and α.H0: each face has probability 1⁢/6. Ha: some face does not. α=0.05.
2Find each expected count, and check that all are at least 5.120 ×1⁢/6 =20 for every face.
3Find each contribution (Oi−Ei)2/Ei.25⁢/20, 9⁢/20, 25⁢/20, 9⁢/20, 16⁢/20 and 16⁢/20.
4Add them to get χ2.100⁢/20 =5.
5Find k−1 and the right-tail p-value.5 degrees of freedom, p ≈0.416.
6Conclude in context.The counts are consistent with a fair die.

Where it breaks

Where it breaks

The test needs counts, not percentages. 60⁢% and 40⁢% of 50 people are 30 and 20, which give χ2=2 against an even split. The same percentages of 500 people give χ2=20. Percentages hide the sample size, and the sample size is the evidence.

Each observation must be independent and counted in exactly one category. Ten rolls by each of 12 people are fine. Counting one person’s answers to several questions as separate observations is not.

Every expected count should be at least 5. A category expected to hold 1.6 makes its term swing wildly, and the chi-square curve no longer describes χ2. Combine small categories before testing, which also lowers k.

A goodness-of-fit test compares one variable’s counts with a claimed distribution. A two-way table, such as smokers and nonsmokers against two diseases, asks whether two variables are related. Its expected counts are row total×column total ÷grand total, with (r−1)⁢(c−1) degrees of freedom.

The test says that something is off, not which category. Read the largest contributions afterward. A large p-value never proves the claimed distribution, because small biases need very many observations to show.

Check your answer

Check your answer

A chi-square sum is built from parts that must balance, and its size should match what the table says.

The expected counts add to n, the same total as the observed counts, and the misses Oi−Ei add to 0. For the die, 6×20=120, and 5−3−5+3+4−4 =0.

χ2 sits near its degrees of freedom when the fit is good. A χ2 above the table’s critical value means p<0.05, and one below it means p>0.05. For the die, 5.0 is far below 11.070, so p is well above 0.05.

The largest term names the category that drives the result. If χ2 is large, read the terms before writing the conclusion.

To have Chalk check the work, write each step as arithmetic in a Math box. Write 120⁢/6=20 for an expected count, then one row per contribution, such as (25−20)2/20=1.25, then their sum. The Checker marks each row ✓, or ✗ when a number is wrong. A row that names the statistic, χ2=1.25+0.45+…=5, has its arithmetic checked but shows “?”, because Chalk has no formula for χ2 to check. Chalk does not compute the p-value, so read it from Statistics.

A 2×2 table tested for independence and a four-category goodness-of-fit test each give χ2=5. Do they give the same p-value?

No. Both have four cells, but the two-way table has (2−1)⁢(2−1)=1 degree of freedom, and the goodness-of-fit test has 4−1=3. Once one count of the table is set, its totals fix the other three. That gives p≈0.025 for the table and p≈0.17 for the other test. The same χ2 is stronger evidence when fewer counts are free.

Too good a fit, and other uses of χ2

Karl Pearson introduced the statistic in 1900, one of the first significance tests. Its chi-square curve is an approximation that improves as the expected counts grow. For tiny counts, an exact multinomial test adds up the probabilities directly.

A fit can also be too good. A χ2 far into the left tail means the counts sit closer to the claim than chance usually allows. In 1936 Ronald Fisher argued that Mendel’s pea counts fit his ratios suspiciously well, a question historians still debate.

The same statistic tests a two-way table for independence, with expected counts built from the margins. When a model’s parameters are estimated from the data, such as an allele frequency estimated from the same plants, each estimate costs one more degree of freedom.

Common mistakes

  • Using percentages instead of counts. 60⁢% and 40⁢% against an even split give (60−50)2/50 +(40−50)2/50 =4 whatever the sample size. Convert to counts first, 30 and 20 for 50 people, which gives χ2=2.
  • Dividing by the observed count instead of the expected count. For the 3:1 cross, (140−150)2/140 +(60−50)2/60 ≈2.38. The model sets the scale, so divide by 150 and 50 to get 2.67.
  • Using k instead of k−1 degrees of freedom. A die has 6 faces but 5 degrees of freedom, because the total fixes the last count. With 6, the p-value for χ2=5 comes out 0.544 instead of 0.416.
  • Running the test with an expected count below 5. Cornflowers and lupines expected at 4.4 and 1.6 among 40 seedlings are too sparse. Combine them into one category expected to hold 6, then compute χ2.
  • Forgetting to square the misses. The die’s misses 5, −3, −5, 3, 4 and −4 add to 0, which says nothing. Their squares add to 100, and 100⁢/20=5 is χ2.
  • Taking a large p-value as proof that the model is right. p≈0.42 for the die says only that 120 rolls show no clear bias. A slightly loaded die could easily give counts like these.

Key terms

Chi-square goodness-of-fit test
A test of whether observed counts in categories match the counts a model expects, using χ2=Σ⁢(observed−expected)2/expected. It needs expected counts that aren’t too small, usually at least 5 each.
Chi-square distribution
A right-skewed distribution of nonnegative values, set by its degrees of freedom. It is used for chi-square tests on counts and for inference about variances.
Expected count
The count a model predicts on average, such as total×category probability. It doesn’t have to be a whole number, and a real sample will usually differ from it.
Degrees of freedom
The number of values free to vary once estimates have been fixed, such as n−1 for one sample’s standard deviation. It sets the shape of the t, chi-square and F distributions.
p-value
The probability, assuming the null hypothesis is true, of a result at least as extreme as the one observed. A small p-value is evidence against the null. It is not the probability that the null is true.
Null hypothesis
H0, the claim a test assumes so it can compute probabilities, usually “no effect” or a specific value such as μ=50. Failing to reject it doesn’t prove it true.

Work through an example

A die is rolled 120 times, giving 25 ones, 17 twos, 15 threes, 23 fours, 24 fives and 16 sixes. Is this consistent with a fair die at α=0.05?

Test whether a die is fair →

Test a 3:1 genetic ratio →

Sources and scope

Authored study material. Tool results depend on the stated inputs and model assumptions.

After reading

Try it, then study it

Open the example in its tool to check steps or change values, then review the key terms as flashcards.

Find the p-value in Statistics Check χ2 in Math Study the 6 key terms Open worked example on a board Statistics formulas in Math Reference

Your existing work stays on this device. Examples open as editable copies.