Chi-squared test calculator
Paste a contingency table (one row per line) for a test of independence, or a list of observed frequencies for goodness of fit. Every expected value is shown, so you can check your own table.
- χ² statistic
- 4.38
- Degrees of freedom
- 2
- p-value
- 0.112
- Critical value (5%)
- 5.99
Show the working
H0: the two variables are independent. H1: they are not independent.
Expected frequencies: E = (row total × column total) ÷ grand total, with grand total 110.
| Column 1 | Column 2 | Column 3 | |
|---|---|---|---|
| Row 1 | 18 (13.6) | 22 (22.7) | 10 (13.6) |
| Row 2 | 12 (16.4) | 28 (27.3) | 20 (16.4) |
χ²calc = Σ (O − E)² ÷ E = 4.38
Degrees of freedom = (rows − 1)(columns − 1) = (2 − 1)(3 − 1) = 2
p-value = P(χ²2 ≥ 4.38) = 0.112; critical value at 5% = 5.99
p = 0.112 ≥ 0.05, so there is not enough evidence to reject H0 at the 5% level. (Equivalently, χ²calc ≤ the critical value.)
Answers are rounded to 3 significant figures, which is what IB papers ask for unless a question says otherwise.
How to do it by hand
- State the hypotheses. Independence: H₀ the variables are independent. Goodness of fit: H₀ the data follow the given distribution.
- Work out each expected frequency: (row total × column total) ÷ grand total for a contingency table, or probability × total for goodness of fit. Combine categories if any expected frequency is below 5.
- χ² = Σ (O − E)² ÷ E. Degrees of freedom: (rows − 1)(columns − 1), or categories − 1 − parameters estimated.
- Compare the p-value with the significance level (or χ² with the critical value) and give the conclusion in context.
Worked example
A school asks 150 students which of three lunch options they prefer, split by year group. Test at the 5% level whether preference is independent of year group. Year 12: 30, 25, 20. Year 13: 20, 35, 20.
Solution
H0: the two variables are independent. H1: they are not independent.
Expected frequencies: E = (row total × column total) ÷ grand total, with grand total 150.
| Column 1 | Column 2 | Column 3 | |
|---|---|---|---|
| Row 1 | 30 (25.0) | 25 (30.0) | 20 (20.0) |
| Row 2 | 20 (25.0) | 35 (30.0) | 20 (20.0) |
χ²calc = Σ (O − E)² ÷ E = 3.67
Degrees of freedom = (rows − 1)(columns − 1) = (2 − 1)(3 − 1) = 2
p-value = P(χ²2 ≥ 3.67) = 0.160; critical value at 5% = 5.99
p = 0.160 ≥ 0.05, so there is not enough evidence to reject H0 at the 5% level. (Equivalently, χ²calc ≤ the critical value.)
Answer: χ² = 3.67, df = 2, p = 0.160
Every number in this example is worked out by the same code as the tool above, and the code is tested against independent results from Python's SciPy library.
Where you need it
| AA SL | Not in the syllabus |
|---|---|
| AA HL | Not in the syllabus |
| AI SL | χ² tests for independence and goodness of fit. |
| AI HL | χ² tests for independence and goodness of fit. |
On your calculator
In the exam you use your own calculator, so practise it too. Our guides give the exact keystrokes for the Casio fx-CG50, TI-Nspire CX II and TI-84 Plus CE, with a worked example to check against:
Common mistakes
- Using the observed table where the expected one is needed: expected frequencies are almost never whole numbers, so do not round them.
- Wrong degrees of freedom. A 3 × 4 table has (3 − 1)(4 − 1) = 6, not 12 − 1.
- Running the test with expected frequencies below 5. Combine rows or columns first, then recount the degrees of freedom.
- Saying “H₀ is true” or “accept H₀”. Either there is evidence to reject H₀ or there is not enough evidence.
Notes and practice
Questions students ask
What is the difference between a test for independence and a goodness of fit test?
Independence uses a two-way table to ask whether two variables are related. Goodness of fit uses one list of counts to ask whether they follow a given distribution, such as equally likely outcomes.
How do I find the degrees of freedom?
For an r × c contingency table it is (r − 1)(c − 1). For goodness of fit with k categories it is k − 1, minus one more for each parameter you estimated from the data.
Does this tool apply Yates' continuity correction?
No. For 2 × 2 tables some textbooks use Yates' correction; graphic calculators do not, and this tool matches them. If your course uses it, the χ² value will be slightly smaller.