Statistics & Probability · Grades 11, 12

Chi-Square Tests: Goodness of Fit and Independence

Quick answer

A chi-square test asks whether observed counts differ from expected counts by more than chance allows. The statistic χ² = Σ(O − E)²/E adds up the squared gaps, each scaled by its expected count. A goodness-of-fit test compares one variable with a claimed distribution, using k − 1 degrees of freedom for k categories. A test of independence compares a two-way table with the counts expected if the variables were unrelated, using (rows − 1)(columns − 1) degrees of freedom.

What you'll learn

  • Compute expected counts and the χ² statistic
  • Run a goodness-of-fit test
  • Run a test of independence on a two-way table
  • Find degrees of freedom and interpret the p-value

Observed against expected

A die is rolled 6060 times. If it is fair, each face should come up about 1010 times. The counts are:

Face123456
Observed OO5588998810102020
Expected EE101010101010101010101010

Is the die loaded, or is this ordinary luck? The chi-square statistic measures the total mismatch:

χ2=∑(O−E)2E=25+4+1+4+0+10010=13.4\chi^2 = \sum \frac{(O - E)^2}{E} = \frac{25 + 4 + 1 + 4 + 0 + 100}{10} = 13.4

Why the gaps are squared and scaled

Squaring makes every gap count as positive, so a surplus in one category cannot cancel a shortage in another, and it weights big gaps more heavily. Dividing by EE judges each gap against the size of its category: being 1010 off where 2020 were expected is a lot, where 1,0001{,}000 were expected it is noise. Under the null hypothesis, each term behaves like the square of a standardized gap, so χ2\chi^2 is a sum of squared z-scores, and its sampling distribution, the chi-square distribution, is known. Its shape depends on the degrees of freedom: for kk categories, k−1k - 1, since the counts must add to the total and the last one is not free.

The chi-square distribution with 5 degrees of freedom A right-skewed curve starting at 0, peaking near 3 and trailing off to the right. The small area under the curve beyond χ² = 13.4 is shaded; it is about 0.02, the p-value for the die. 5101520χ²y
  • chi-square, 5 df
The chi-square distribution with 5 degrees of freedom

For the die, χ2=13.4\chi^2 = 13.4 with 55 degrees of freedom gives a p-value of about 0.0200.020. At α=0.05\alpha = 0.05 (critical value 11.0711.07), we reject the hypothesis that the die is fair: six came up far too often.

Worked examples

Common mistakes

Practice problems

  1. A coin lands heads 4545 times in 100100 tosses. Compute χ2\chi^2 for the fair-coin hypothesis.

    Answer

    11

    Full solution

    (45−50)250+(55−50)250=0.5+0.5\tfrac{(45 - 50)^2}{50} + \tfrac{(55 - 50)^2}{50} = 0.5 + 0.5. With 11 degree of freedom the p-value is about 0.320.32: no evidence of bias.

  2. A bag of 100100 candies of 44 colors, claimed equally common, has counts 2222, 1818, 3030, 3030. Find χ2\chi^2 and decide at α=0.05\alpha = 0.05 (critical value 7.817.81).

    Answer

    χ2=4.32\chi^2 = 4.32; fail to reject

    Full solution

    9+49+25+2525=4.32<7.81\tfrac{9 + 49 + 25 + 25}{25} = 4.32 < 7.81. The p-value is about 0.230.23.

  3. How many degrees of freedom does a test of independence on a 3×43 \times 4 table have?

    Answer

    66

    Full solution

    (3−1)(4−1)(3 - 1)(4 - 1).

  4. In a two-way table, a row total is 4040, a column total is 3030 and the grand total is 120120. Find that cell’s expected count.

    Answer

    1010

    Full solution

    40×30120\tfrac{40 \times 30}{120}.

  5. Why is the χ² statistic never negative?

    Answer

    It is a sum of squares divided by positive expected counts.

    Full solution

    Each term (O−E)2E\tfrac{(O - E)^2}{E} is at least 00, and it is 00 only when every observed count equals its expected count.

  6. In Example 2, what would the observed counts be if the treatments worked equally well, with the same totals?

    Answer

    22.522.5 and 27.527.5 in each row, the expected counts

    Full solution

    Equal success rates of 45100\tfrac{45}{100} in both groups give 0.45×50=22.50.45 \times 50 = 22.5 improved in each. Then χ2=0\chi^2 = 0.

  7. A goodness-of-fit test gives χ2=2.1\chi^2 = 2.1 with 33 degrees of freedom. Using the critical value 7.817.81, what do you conclude at α=0.05\alpha = 0.05?

    Answer

    Fail to reject the null hypothesis.

    Full solution

    2.12.1 is well below the critical value, so the data are consistent with the claimed distribution.

  8. A student computes χ2\chi^2 for the die using (O−E)2O\tfrac{(O - E)^2}{O}. What is wrong?

    Answer

    The denominator must be the expected count EE.

    Full solution

    The test asks how unusual the observations are under the null hypothesis, so gaps are scaled by what the null predicts, EE.

Frequently asked questions

What does a chi-square test measure?

How far observed counts are from the counts expected under a null hypothesis, through χ² = Σ(O − E)²/E. The bigger χ², the stronger the evidence against the null.

What is a goodness-of-fit test?

A test of whether one categorical variable follows a claimed distribution, such as a fair die landing on each face 1/6 of the time. It has k − 1 degrees of freedom for k categories.

What is a chi-square test of independence?

A test of whether two categorical variables in a two-way table are related. The expected count for each cell is (row total × column total)/grand total, and there are (r − 1)(c − 1) degrees of freedom.

Why divide by the expected count?

A gap of 10 matters more when 20 were expected than when 1,000 were. Dividing by E scales each gap to the size of its category.

When is the chi-square test valid?

When the data are counts from a random sample and every expected count is at least 5.

Standards alignment

This lesson covers the following Common Core State Standards for Mathematics.

  • CCSS.MATH.CONTENT.HSS.IC.B.5Making Inferences and Justifying ConclusionsUse data from a randomized experiment to compare two treatments; use simulations to decide if differences between parameters are significant.
  • CCSS.MATH.CONTENT.HSS.CP.A.4Conditional Probability and the Rules of ProbabilityConstruct and interpret two-way frequency tables of data when two categories are associated with each object being classified. Use the two-way table as a sample space to decide if events are independent and to approximate conditional probabilities.