KS Maths
Topic 4.11 · AI SL and HL

Chi-squared tests for independence and goodness of fit

How different is different enough?

Hypotheses, expected frequencies, degrees of freedom, and the point at which a difference stops being chance.

This video lesson is a free sample. The plans open the rest of the lessons in this course, each with a figure to move and questions that check your answer as you type. See the plans.
Words you need (10 key words in this lesson)

The maths words this lesson uses, in plain English. Open this before you start if English is not your first language.

independent
Not affecting each other.Two coin tosses are independent.
square
Multiply a number by itself. Also, a shape with four equal sides and four right angles.5 squared is 25.
expected value
The average result if we repeated many times.Expected heads in 10 tosses is 5.
frequency
How many times something happens or appears.Score 5 has a frequency of 3.
degree
The highest power of x in a polynomial. Also, the unit for measuring angles.The degree of x cubed plus x is 3.
decimal place
A digit after the decimal point. We count places from the point.3.14 has two decimal places.
hypothesis
An idea that we test using data.Our hypothesis is that the coin is fair.
decimal
A number with a decimal point. It shows parts smaller than one.0.5 is a decimal.
probability
How likely it is that something happens, from 0 to 1.The probability of heads is one half.
variable
A letter that stands for a number that can change.In y = 2x, x and y are variables.

Warm-up

Four quick ones from earlier work. Try each on paper before you open the answer; a slip here shows you what to watch for later in the lesson.

  1. Out of 200 people, 35% prefer tea. How many people is that?
    Answer

    0.35 × 200 = 70. A percentage of a total is the same arithmetic as a row total times a column proportion, which is where expected frequencies come from.

  2. Work out (18 − 15)² ÷ 15.
    Answer

    (18 − 15)² = 9, and 9 ÷ 15 = 0.6. Square the difference first, then divide by the 15: this is one cell of a χ² sum.

  3. Events A and B are independent, with P(A) = 0.4 and P(B) = 0.5. Find P(A and B).
    Answer

    0.4 × 0.5 = 0.2. For independent events the probabilities multiply. An expected frequency is exactly this, scaled up to the number of people.

  4. A test gives p = 0.03. Is that less than the significance level 0.05?
    Answer

    Yes: 0.03 < 0.05, so the result is significant at the 5% level. A small p-value means the data would be surprising if H₀ were true.

Words used in this lesson
observed frequency (O)
the number actually counted in a cell. For example: 20 students were counted in this cell.
expected frequency (E)
the number you would expect in a cell if H₀ were true. For example: if 35% of 200 people prefer tea, the expected number is 70.
independent
one variable tells you nothing about the other. For example: height and birthday month are independent.
association
a link between two variables. For example: an association between age group and phone type.
degrees of freedom
how many cell counts are free to vary once the totals are fixed. For example: a table with 4 rows and 5 columns has (4 − 1)(5 − 1) degrees of freedom.
critical value
the boundary: χ² above it means reject H₀. For example: a critical value of 7.81 for a test with 3 degrees of freedom at the 5% level.
significance level
how small p must be before you reject H₀. For example: a 1% significance level means p must be below 0.01.
p-value
the probability of a result at least this extreme if H₀ is true. For example: p = 0.2 means a result like this happens 1 time in 5 by chance.
reject
decide the evidence is strong enough to say H₀ is unlikely. For example: “reject H₀” is the conclusion when p is below the level.
insufficient
not enough. For example: “insufficient evidence” means the data did not give strong enough support.

Predict first. Each press of the shift slider moves the same number of people from one cell to another. If you shift 5 people and then 10 people, will the χ² statistic for 10 be twice the one for 5, or more than twice? Decide, and say why, before you look.

Then check

Now set the shift to 5 and read χ², then set it to 10 and read it again. How does the second compare with the first? If it is not what you predicted, look at the (O − E)² in the formula and ask what squaring does.

150 people, two groups, three preferences. Move the counts and watch χ² climb. The conclusion flips at one specific value, and nowhere else.

shift 10
16.667χ² statistic
5.991critical value, 5%
reject H₀conclusion

Grey is what you would expect if the two were unrelated. Green is what happened.

The hypotheses

H₀: the two variables are independent. H₁: they are not independent. For goodness of fit, H₀ is that the data fits the stated distribution.

H₀ is always the boring one: nothing going on, no association. You never prove it. You either have enough evidence to reject it or you do not, and "we accept H₀" is wording that loses marks. Say "there is insufficient evidence to reject H₀".

Expected frequencies

expected= row total × column totalgrand total what the cell would be if the two were unrelated row 1, column 1= 60 × 50150 = 20

Every expected frequency should be at least 5. If one is not, the test is not reliable and the usual fix is to combine categories. Checking this is worth a mark and almost nobody does it.

The statistic and the decision

χ²= Σ(O − E)²E divided by E, which is why a gap of 10 matters more in a small cell degrees of freedom= (rows − 1)(columns − 1) = (2 − 1)(3 − 1) = 2 for goodness of fit it is the number of categories minus 1
Decide byReject H₀ when
critical valueχ² is bigger than it
p-valuep is smaller than the significance level

The two tests always agree, and the inequalities point opposite ways, which is the thing people get backwards under pressure. Big statistic, small p, reject.

Goodness of fit

The same statistic tests whether data fit a stated distribution. This time the expected frequencies come from the model: expected = total × probability.

data a die rolled 120 times gives 22, 17, 20, 18, 23, 20 H₀ the die is fair, so each face has probability 1/6 expected= 120 × 1/6 = 20 for every face χ²= (2² + 3² + 0² + 2² + 3² + 0²) ÷ 20 = 26 ÷ 20 = 1.3 degrees of freedom= 6 − 1 = 5 six categories; the 5% critical value is 11.07

1.3 is less than 11.07, so there is insufficient evidence to reject H₀: the rolls are consistent with a fair die. When the model’s probabilities are not equal, multiply each one by the total. Probabilities 0.5, 0.3 and 0.2 with 200 observations give expected frequencies 100, 60 and 40.

Comparing two means: the t-test

The other test in this part of the course compares the means of two populations. Both populations are taken to be normal with the same variance, so the calculator’s pooled two-sample t-test is the one to run, and the decision is made with its p-value.

the claim a revision app raises quiz scores (out of 20) with the app 15, 18, 14, 17, 16, 19, 15, 18 mean 16.5 without it 14, 16, 13, 15, 17, 14, 15, 16 mean 15 hypotheses H₀: μA = μB, H₁: μA > μB one-tailed, because the claim has a direction calculator t = 1.93, p = 0.0374

0.0374 < 0.05, so reject H₀: there is evidence at the 5% level that the mean score is higher with the app. On the TI-Nspire this is Stat Tests → 2-Sample t Test with Pooled set to Yes; on the fx-CG50 it is TEST → t → 2-SAMPLE with Pooled set to On. Choose the direction of H₁ on the same screen.

Decide one tail or two before you look at the data. If the claim had been only “the app changes scores”, H₁ would be μA ≠ μB, the p-value would double to 0.0748, and the conclusion at the 5% level would reverse.

On the GDC: the chi-squared test

Enter the table as a matrix, run one test, and the expected frequencies come back with it.

When you may use it. Applications. A calculator is allowed in every paper.

TI-Nspire CX II

  1. Store the observed table as a matrix, for example a := [[20,30][30,20]]
  2. menu → Statistics → Stat Tests → χ² 2-way Test
  3. Read χ², PVal and df; the expected matrix is stored in stat.ExpMatrix

Casio fx-CG50

  1. MENU → Run-Matrix → F3 MAT/VCT and enter the table as Mat A
  2. MENU → Statistics → F3 TEST → F3 CHI → 2WAY
  3. On the 2WAY input screen set Observed to Mat A and Expected to Mat B before running it
  4. Read χ², p and df, then F6 ▸MAT and open Mat B

The mark people lose. Check the expected frequencies before you trust the verdict. Any expected value below about 5 and the test is not valid, however confident the p-value looks. The calculator will not refuse to run it.

A second example, finished by you

The same test on a new table. The first three steps are written out with the reason for each; the last is yours. Cover the reasons and see if you can say them first.

100 shoppers are asked which of three payment methods they prefer, by age group. Are the two independent at the 5% level?
Under 30: 24, 18, 8 (total 50)
30 and over: 6, 32, 12 (total 50)
Column totals: 30, 50, 20.

  1. H₀: payment method is independent of age group. H₁: it is not. df = (2 − 1)(3 − 1) = 2.State both hypotheses in context. The degrees of freedom come from the table’s dimensions, not from the number of people.
  2. E for row 1, column 1 = 50 × 30 ÷ 100 = 15. The expected frequencies for the columns are 15, 25 and 10, in both rows.Row total times column total over grand total. Both rows have the same total of 50, so both rows have the same expected frequencies. Every one is at least 5, so the test is valid.
  3. The first cell contributes (24 − 15)² ÷ 15 = 5.4.Square the difference, then divide by the expected frequency of that cell.
  4. Find the contribution of each of the other five cells in the same way and add all six, or enter the table as a matrix on your calculator.Your step. Write it out in full before you type the answer below.

Your answer. Type the χ² statistic, to 2 decimal places.

Check before you go on

One question that tells you whether the main idea has landed. Each wrong option is a different common slip, so the reply tells you which one you made.

A χ² test is run at the 1% significance level and gives p = 0.03. Which conclusion is correct?

A χ² test is run at the 1% significance level and gives p = 0.03. Which conclusion is correct?

Where it decides something

A school canteen is deciding whether to run separate menus for two year groups. Of 60 students in each year group, 38 in Year 10 and 22 in Year 11 bring a packed lunch; the rest buy lunch. So Year 10 has 38 packed and 22 buying, and Year 11 has 22 packed and 38 buying. Each expected frequency is 30.

Find the χ² statistic for this table, to 2 decimal places.

Your turn

1. A contingency table has 3 rows and 5 columns. How many degrees of freedom?

2. Row total 45, column total 80, grand total 200. What is the expected frequency?

3. A goodness of fit test with 5 categories gives χ² = 10.4, and the 5% critical value is 9.488. What do you conclude?

Where the marks go

Stating both hypotheses in context, not just as symbols. "H₀: preference is independent of group" rather than "H₀: independent".

The degrees of freedom. One number, often one mark, and easy to get wrong by forgetting to subtract.

The conclusion in context and with the comparison stated. "16.7 is greater than 5.99, so reject H₀: there is evidence that preference depends on group."

Something to think about

Take the table with counts 20, 30 in the top row and 30, 20 in the bottom row. Now halve every count, so the table has 50 people instead of 100 but exactly the same proportions. Predict what happens to the χ² statistic, the degrees of freedom and the conclusion at the 5% level (critical value 3.841). Then ask: what would happen if you doubled every count instead?

One way to think about it

The proportions have not changed, so what has? Work out the new expected frequencies and the new (O − E)² ÷ E for one cell, and compare it with the same cell in the original table. Look for a rule linking the size of the sample to the size of the statistic.

Climb to an exam question

Four steps, each one a little harder than the last. Every step is a piece of the exam question at the end, so by the top you have already done each part once.

Step 1 of 4: find an expected frequency. 100 people choose a cat or a dog as their preferred pet, and are grouped as adults or children. The results are:
Cat: 15 adults, 20 children (total 35)
Dog: 15 adults, 50 children (total 65)
Column totals: 30 adults, 70 children.
Assuming that choice of pet is independent of age group, find the expected frequency for cat and adults.

Step 2 of 4: one cell of the statistic. In the same survey, the observed frequency for cat and children is 20 and its expected frequency is 24.5. Find the contribution of this cell to the χ² statistic, to 2 decimal places.

Step 3 of 4: the whole statistic. Using the same observed frequencies as before, find the χ² statistic for the whole table, to 2 decimal places. You will need all four expected frequencies.

Step 4 of 4: state the conclusion. A different test of independence, on a 2 by 2 table, gives χ² = 2.9. At the 5% significance level the critical value for 1 degree of freedom is 3.841. Which conclusion is correct?

A different test of independence, on a 2 by 2 table, gives χ² = 2.9. At the 5% significance level the critical value for 1 degree of freedom is 3.841. Which conclusion is correct?

The exam question

Written for this site in the style of the real papers. Do it on paper first, as you would in the exam, and show your working: most of the marks are for method. Give answers to 3 significant figures unless the question says otherwise. Then check each part below.

Paper 2 style · calculator (GDC) allowed · 9 marks · about 10 minutes

A gym records, for 140 members, the time of day of their visit and their membership type. The results are:
Morning: 32 standard, 8 premium (total 40)
Afternoon: 26 standard, 14 premium (total 40)
Evening: 22 standard, 38 premium (total 60)
Column totals: 80 standard, 60 premium.
The gym manager tests, at the 5% significance level, whether time of visit and membership type are independent.

(a) State the null hypothesis and the alternative hypothesis. [1]

(b) Write down the number of degrees of freedom. [1]

(c) Find the expected frequency for members who visit in the evening and have a premium membership. Give your answer to one decimal place. [2]

(d) Find the χ² statistic for the data. Give your answer to two decimal places. [3]

(e) The critical value for this test at the 5% significance level is 5.991. Hence state the conclusion of the test, giving a reason. [2]

Check your working for (a)

Both hypotheses are written in context, naming the two variables. The null hypothesis is the claim of no link between them; the alternative says there is one.

Check (b). Type the number of degrees of freedom.

Check (c). Type the expected frequency for evening and premium, to 1 decimal place.

Check (d). Type the χ² statistic, to 2 decimal places.

Check (e). Which conclusion is correct?

Which conclusion is correct?

Want the rest of this topic?

This is a free sample lesson. The IB Diploma plan opens every other Diploma lesson, AA and AI, SL and HL, and every lesson from Year 7 for the refreshers. It covers every child in the family and includes all seven revision packs.

$99 for 12 months, or $149 for 2 years

One plan opens every Diploma lesson, AA and AI, SL and HL, for the whole family.

That is $8.25 a month for the whole family.

Student without a card? in one tap.

One payment. Nothing renews. Full refund within 30 days. Prices are in US$; Gumroad shows your local currency at checkout. Already paid? Type the email you paid with in the sign-in box, then click the link we send. See every price, including $19 revision packs.

Teaching this lesson?

Every lesson here has a teacher page beside it: worked answers, what each common wrong answer tells you about a student's thinking, and questions at three levels for the board. Every unit has a free sample lesson for students, and a plan opens every other lesson.

See the teacher pages