# Chi-squared tests: goodness of fit and independence

> IB Mathematics AI HL · IB Math AI HL
> Source: https://www.owlsprep.com/study/ib-math-ai-hl-u5-chi-squared-tests-goodness-of/

This module covers the two core chi-squared hypothesis tests for IB Math AI HL: goodness of fit (to test if data matches a claimed distribution) and the test for independence between two categorical variables. You will learn calculation methods and interpretation for exam questions.

**Prerequisites:** [Fundamentals of hypothesis testing](https://www.owlsprep.com/study/ib-math-ai-hl-u5-hypothesis-testing-basics/); [Categorical data classification](https://www.owlsprep.com/study/ib-math-ai-hl-u5-data-types/)

## Learning objectives

- State null and alternative hypotheses for chi-squared goodness of fit and independence tests
- Calculate expected frequencies and the chi-squared test statistic correctly
- Determine correct degrees of freedom for both test types
- Draw statistically valid conclusions in context for chi-squared tests

## Foundations of the Chi-Squared Goodness of Fit Test

**Chi-squared goodness of fit test** — A hypothesis test that assesses whether an observed frequency distribution matches a hypothesized theoretical distribution.

*Example:* Testing if a six-sided die is fair by comparing observed roll counts to expected uniform counts

All chi-squared tests use the same core formula for the test statistic, which quantifies how far observed values are from the values expected under the null hypothesis:

$$\chi^2 = \sum \frac{(O - E)^2}{E}$$

**Worked example:** A clinic claims blood type proportions are 0.45 (O), 0.40 (A), 0.11 (B), 0.04 (AB). A sample of 200 people gives observed counts: 92, 75, 24, 9. Calculate the chi-squared test statistic.

1. First calculate expected frequencies by multiplying total sample size by the hypothesized proportion for each category:
2. $$E_O = 200 \times 0.45 = 90,\quad E_A = 200 \times 0.40 = 80,\quad E_B = 200 \times 0.11 = 22,\quad E_{AB} = 200 \times 0.04 = 8$$
3. Calculate the $(O-E)^2/E$ term for each category:
4. $$\frac{(92-90)^2}{90} \approx 0.044,\quad \frac{(75-80)^2}{80} = 0.3125,\quad \frac{(24-22)^2}{22} \approx 0.1818,\quad \frac{(9-8)^2}{8} = 0.125$$
5. Sum all terms to get the final test statistic:
6. $$\chi^2 \approx 0.044 + 0.3125 + 0.1818 + 0.125 = 0.663$$

> **tip**
>
> Always check that the sum of expected frequencies equals the sum of observed frequencies to catch calculation errors early.

> **Exam tip:** If asked to show the chi-squared calculation, always show each individual $(O-E)^2/E$ term to get full marks.

## Degrees of Freedom and Validity Conditions

Degrees of freedom for a goodness of fit test depends on how many parameters of the hypothesized distribution were estimated from your sample data, which is a common exam trap.

**Degrees of freedom (goodness of fit)** — Calculated as $\nu = n - k - 1$, where $n$ is the number of categories, and $k$ is the number of distribution parameters estimated from the sample.

*Example:* For a normal distribution goodness of fit test with 8 categories, where mean and standard deviation are estimated, $k=2$ so $\nu = 8 - 2 - 1 = 5$

> **warning**
>
> The chi-squared approximation is only valid if all expected frequencies are at least 5. If any expected frequency is less than 5, you must combine adjacent categories before proceeding with the test.

**Worked example:** Find the correct degrees of freedom for a goodness of fit test of a uniform distribution with 6 categories, where no parameters are estimated from the sample.

1. Identify the values for $n$ (number of categories) and $k$ (number of estimated parameters):
2. $$n = 6,\quad k = 0$$
3. Apply the degrees of freedom formula:
4. $$\nu = 6 - 0 - 1 = 5$$

## Chi-Squared Test for Independence

The chi-squared test for independence assesses whether two categorical variables are associated (related) or independent, using data arranged in a contingency table.

**Test for independence** — A chi-squared test that tests the null hypothesis that two categorical variables from the same population are independent of each other.

For a contingency table with $r$ rows and $c$ columns, the expected frequency for each cell is calculated as:

$$E = \frac{\text{Row Total} \times \text{Column Total}}{\text{Grand Total}}$$

Degrees of freedom for a test of independence is always calculated as:

$$\nu = (r - 1)(c - 1)$$

**Worked example:** A 3×2 contingency table (3 rows, 2 columns) is used to test if age group is associated with preference for a product. Find the correct degrees of freedom for the test.

1. Identify the number of rows $r$ and columns $c$:
2. $$r = 3,\quad c = 2$$
3. Apply the degrees of freedom formula for independence:
4. $$\nu = (3 - 1)(2 - 1) = 2 \times 1 = 2$$

**Check your understanding**

Check your understanding:

1. What is the degrees of freedom for a chi-squared test of independence in a 3×3 contingency table?

   - 9
   - 4
   - 6
   - 3

   *Why:* Correct! $(r-1)(c-1) = (3-1)(3-1) = 2 \times 2 = 4$

## Hypotheses and Conclusion Writing

**Exam command terms**

Common exam command terms for chi-squared questions:

- **Carry out a complete chi-squared test** — You must complete all steps: state hypotheses, calculate test statistic, find df, compare to critical value/p-value, and write a conclusion in context *(You will lose 1-2 marks if you do not write your conclusion in the context of the question)*

For all chi-squared tests, the null hypothesis always states there is no effect: for goodness of fit, $H_0$ = the observed distribution matches the theoretical distribution. For independence, $H_0$ = the two variables are independent. The alternative hypothesis always states there is an effect (distribution does not match / variables are associated).

To draw your conclusion: reject $H_0$ if $\chi^2 > $ critical value, or $p < \alpha$ (usually 0.05). Otherwise, fail to reject $H_0$.

## Common pitfalls

- **Wrong:** Using $\nu = n-1$ for goodness of fit when parameters were estimated from the sample
  - Why it fails: This gives an incorrect degrees of freedom, leading to the wrong critical value and wrong conclusion
  - Correct: Always subtract the number of estimated parameters $k$ first: $\nu = n - k - 1$
- **Wrong:** Using $\nu = rc - 1$ instead of $(r-1)(c-1)$ for independence tests
  - Why it fails: This overestimates degrees of freedom, leading to an incorrect significance test result
  - Correct: Memorize the correct formula $\nu = (r-1)(c-1)$ for all contingency table tests
- **Wrong:** Leaving expected frequencies < 5 unadjusted
  - Why it fails: The chi-squared approximation is invalid for small expected frequencies, so your result will be unreliable
  - Correct: Combine adjacent categories to make all expected frequencies at least 5 before calculating the test statistic
- **Wrong:** Claiming you have proven independence when you fail to reject $H_0$
  - Why it fails: Failure to reject the null hypothesis only means there is insufficient evidence to reject it, not that it is proven true
  - Correct: Write 'there is insufficient evidence at the 5% significance level to conclude the variables are associated' instead of 'the variables are independent'
- **Wrong:** Forgetting to check that expected frequencies sum to the grand total
  - Why it fails: A mismatched sum is a clear sign of calculation error that will cost you marks
  - Correct: Always sum your expected frequencies and confirm they equal the grand total of observed frequencies

## Cheatsheet

| Item | Formula / Rule |
| --- | --- |
| Chi-squared test statistic | $\chi^2 = \sum \frac{(O-E)^2}{E}$ |
| Expected frequency (independence) | $\frac{\text{Row Total} \times \text{Column Total}}{\text{Grand Total}}$ |
| Goodness of fit df (k estimated parameters) | $\nu = n - k - 1$ |
| Independence test df (r×c table) | $\nu = (r-1)(c-1)$ |
| Goodness of fit $H_0$ | Observed distribution matches theoretical distribution |
| Independence test $H_0$ | Two categorical variables are independent |
| Validity condition | All expected frequencies $\geq 5$ |

## What's next

Chi-squared tests are one of the most frequently tested topics in IB Math AI HL statistics, appearing on both Paper 1 and Paper 2 almost every session. They are also widely used in real-world research across social sciences, medicine, and business, forming the foundation for more advanced categorical data analysis. Mastering this topic will also reinforce your understanding of core hypothesis testing logic that applies to all other tests you will study. Next, you will move on to hypothesis tests for continuous data, building on the framework you learned here.

- [t-tests and confidence intervals for means](https://www.owlsprep.com/study/ib-math-ai-hl-u5-t-tests-and-confidence-intervals/)
- [Spearman's rank correlation coefficient](https://www.owlsprep.com/study/ib-math-ai-hl-u5-spearman-s-rank-correlation-coefficient/)
- [Graph theory: basic concepts](https://www.owlsprep.com/study/ib-math-ai-hl-u5-graph-theory-basic-concepts/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/ib-math-ai-hl-u5-chi-squared-tests-goodness-of/
