📘 CodingMarble Learn

Chi-Square Test for Independence and Homogeneity

A chi-square (χ²) test checks if counts in a table are too far from what we would expect by chance. For a contingency table: E = row total × column total ÷ grand total, χ² = Σ (O − E)² ÷ E, df = (r − 1)(c − 1). If χ² is bigger than the critical value (or p < significance level), reject H0 of no association. For 2×2 tables, Yates' correction uses (|O − E| − 0.5)².

🎬 Step-by-step story

  1. Here is a table of counts. 200 people chose tea or coffee, in two cities. The blue and orange bars are the observed counts, O.
  2. If the city did not matter, each cell would have an expected count, E. The grey glass boxes show E = 50 in every cell.
  3. The red piece is the gap between O and E. For each cell we work out (O − E)² ÷ E. Here each piece is 2.
  4. Stack the pieces. Their total is chi-square: χ² = 8. The degrees of freedom are (2 − 1) × (2 − 1) = 1.
  5. Compare 8 with the critical value 3.84. Our value is bigger, so we reject 'no link'. City and drink are associated.
  6. Your turn. Move the slider to change the tea count in City A. Watch E, χ² and the verdict change.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why do we divide by E?

A gap of 10 is big if you expected 20 but small if you expected 2,000. Dividing by E makes each gap fair for the size of the cell.

Why do we square O − E?

Some gaps are positive and some negative; in our table they add to zero. Squaring makes all pieces positive so they stack up.

Where does the expected count come from?

If city does not matter, City A (half the people) should get half of all tea drinkers: 100 × 100 ÷ 200 = 50.

Why is df only 1 for a 2×2 table?

Once you know the totals, filling one cell fixes the other three. Only one cell is free.

Does rejecting H0 tell us which cell is different?

Not directly. Look for the biggest pieces (O − E)² ÷ E. Try the slider and watch which red pieces grow.

Contingency tables: counting in two ways

A contingency table counts people (or things) sorted by two categories at once. Rows are one category (city). Columns are another (drink). Each box is a cell.

We only use counts. Never use percentages or means in a chi-square test.

TeaCoffeeTotal
City A6040100
City B4060100
Total100100200

Hypotheses: independence or homogeneity

Test of independence (association): one sample, two variables. H0: the two variables are independent (no association). H1: they are associated.

Test of homogeneity: several separate samples, one variable. H0: the distribution is the same in every group. H1: at least one group is different.

The arithmetic is the same. Only the way the data was collected and the wording of H0 change.

Goodness of fit is the one-row cousin: it checks whether counts match a given model (for example, a fair die). There df = k − 1 (k categories), minus 1 more for each parameter you estimate from the data.

Expected counts and the χ² statistic

If H0 is true, each cell should get its fair share:

E = (row total × column total) ÷ grand total

Then for every cell compute (O − E)² ÷ E and add them:

χ² = Σ (O − E)² ÷ E

Degrees of freedom: df = (rows − 1) × (columns − 1).

Conditions

Deciding: critical value, p-value and conclusion

A large χ² means the data are far from what H0 predicts. Use a table or calculator:

Common 5% critical values: df 1 → 3.841, df 2 → 5.991, df 3 → 7.815, df 4 → 9.488.

Write the conclusion in context: "There is evidence at the 5% level that drink choice is associated with city." A test shows association, not cause. Look at the cells with the biggest (O − E)² ÷ E to say where the difference is.

Yates' continuity correction (2×2 tables)

χ² is a smooth curve, but counts jump in whole numbers. For a 2×2 table (df = 1) some courses use Yates' correction:

χ²(Yates) = Σ (|O − E| − 0.5)² ÷ E

It makes χ² a little smaller, so the test is a bit more careful. In our example: 4 × (10 − 0.5)² ÷ 50 = 7.22, still above 3.841.

Try it at home

Ask 20 friends or family: "Do you prefer mornings or evenings?" and note if they are under 18 or 18+. Build a 2×2 table, work out E for each cell, then χ². Is it bigger than 3.84? (With only 20 people some E may be under 5: that is the lesson about conditions!)

Key formulas and definitions

Worked examples

1. In a 2×2 table the row totals are 30 and 70 and the column totals are 40 and 60. Find the expected count for row 1, column 1.

Grand total = 100. E = 30 × 40 ÷ 100 = 12.

2. Find df for a table with 3 rows and 4 columns.

df = (3 − 1)(4 − 1) = 2 × 3 = 6.

3. For one cell O = 18 and E = 12. Find its contribution to χ².

(18 − 12)² ÷ 12 = 36 ÷ 12 = 3.

4. City A: 60 tea, 40 coffee. City B: 40 tea, 60 coffee. Test at 5% if drink choice depends on city.

H0: independent. Totals: rows 100, 100; columns 100, 100; n = 200. Every E = 50. Each (O − E)² ÷ E = 100 ÷ 50 = 2. χ² = 8. df = 1, critical = 3.841. 8 > 3.841, so reject H0: evidence of association.

5. Apply Yates' correction to the table above.

|O − E| = 10 in every cell. (10 − 0.5)² ÷ 50 = 90.25 ÷ 50 = 1.805. χ² = 4 × 1.805 = 7.22 > 3.841. Still reject H0.

6. Three schools (samples of 100) report favourite sport: School P: cricket 50, football 50; School Q: cricket 40, football 60; School R: cricket 30, football 70. Test homogeneity at 5%.

Column totals: cricket 120, football 180, n = 300. E(cricket) = 100 × 120 ÷ 300 = 40 in each school; E(football) = 60. Pieces: P: 100/40 + 100/60 = 2.5 + 1.667; Q: 0 + 0; R: 2.5 + 1.667. χ² = 8.33. df = (3 − 1)(2 − 1) = 2, critical 5.991. 8.33 > 5.991, reject H0: the sport preferences are not the same in all schools (School P likes cricket more, R less).

Common mistakes

Practice quiz

1. The expected count in a cell is:
2. χ² is:
3. df for a 2×3 table is:
4. If χ² = 2.1 with df = 1 at 5%, you should:
5. Yates' correction is used for:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a chi-square test in simple words?

It checks if the counts in a table are too far from what we would expect if there were no link between the categories.

When should I use Yates' correction?

For 2×2 tables (df = 1), especially with smallish counts, if your course or teacher asks for it. It makes χ² slightly smaller.

What is the difference between independence and homogeneity tests?

Independence uses one sample with two variables. Homogeneity compares several separate samples on one variable. The calculation is the same.

Where this is taught

England (GCSE, A level)Year 12Optional application 2 Statistics (part 1)
England (GCSE, A level)Year 13Optional application 2 Statistics (part 2)
USA (Common Core, NGSS, AP)Grade 12Inference for Categorical Data: Proportions
China高三Ch.8 Paired data

Learn first

Related lessons

All Maths lessons