Observed against expected
Before you start: read testing of hypothesis for \(H_0\), level of significance and critical values.
Throw a die 120 times and you expect each face 20 times. You will not get exactly 20 of each. The chi-square test asks whether the gap between what you observed and what you expected is small enough to be chance, or too big to believe \(H_0\).
\[\chi^2 = \sum \frac{(O - E)^2}{E}\]
\(O\) = observed frequency, \(E\) = expected frequency under \(H_0\). Reject \(H_0\) if \(\chi^2\) is more than the table value for the right degrees of freedom.
Each term is a squared gap scaled by what you expected. A gap of 5 matters a lot when you expected 10 and very little when you expected 500. The test is always right-tailed: only a large \(\chi^2\) counts against \(H_0\).
Conditions
- Every expected frequency should be at least 5. If some are smaller, merge neighbouring classes before you compute.
- The total frequency should be reasonably large (many books ask for at least 50).
- The observations must be independent, and you must use actual counts, not percentages.
Degrees of freedom and critical values
Goodness of fit: df \(= k - 1 - m\), where \(k\) is the number of classes after merging and \(m\) is the number of parameters you estimated from the data (0 if nothing was estimated, 1 for a Poisson mean, and so on).
Contingency table with \(r\) rows and \(c\) columns: df \(= (r - 1)(c - 1)\).
| df | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 5% | 3.841 | 5.991 | 7.815 | 9.488 | 11.070 | 12.592 |
| 1% | 6.635 | 9.210 | 11.345 | 13.277 | 15.086 | 16.812 |
Test of independence
For a table that classifies people two ways (say, study mode and result), \(H_0\) says the two attributes are independent. The expected count in each cell is \[E = \frac{\text{row total} \times \text{column total}}{\text{grand total}}\]
For a 2 × 2 table with cells \(a, b\) in the first row and \(c, d\) in the second, a shortcut gives the same value: \[\chi^2 = \frac{N(ad - bc)^2}{(a + b)(c + d)(a + c)(b + d)}\]
Some books apply Yates' correction to a 2 × 2 table, replacing \(|O - E|\) by \(|O - E| - 0.5\), especially when some expected counts are small.
Solved examples
Example 1: Is the die fair?
EasyA die thrown 120 times gives the faces 1 to 6 with frequencies 25, 17, 15, 23, 24, 16. Test at 5% whether the die is fair.
- \(H_0\): the die is fair, so \(E = 20\) for each face.
- \((O - E)^2\): 25, 9, 25, 9, 16, 16. Sum \(= 100\), so \(\chi^2 = \dfrac{100}{20} = 5.0\).
- df \(= 6 - 1 = 5\), table value 11.070. Since \(5.0 < 11.070\), accept \(H_0\).
Answer\(\chi^2 = 5.0\): no evidence that the die is unfair
Example 2: A 9 : 3 : 3 : 1 ratio
MediumIn a cross, theory predicts four kinds of plant in the ratio 9 : 3 : 3 : 1. Out of 160 plants, the counts are 95, 28, 27, 10. Does the data fit the theory at 5%?
- Expected: \(160 \times \tfrac{9}{16} = 90\), then 30, 30, 10.
- \(\chi^2 = \dfrac{25}{90} + \dfrac{4}{30} + \dfrac{9}{30} + 0 = 0.278 + 0.133 + 0.300 = 0.711\).
- df \(= 3\), table value 7.815. Accept \(H_0\).
Answer\(\chi^2 \approx 0.711\): the data fit the 9 : 3 : 3 : 1 ratio
Example 3: Equal preference
Medium200 buyers choose among four colours: 64, 46, 56, 34. Test at 5% whether the colours are equally popular.
- \(H_0\): equal preference, so \(E = 50\) each.
- \((O - E)^2\): 196, 16, 36, 256. Sum \(= 504\), \(\chi^2 = \dfrac{504}{50} = 10.08\).
- df \(= 3\), table value 7.815. Since \(10.08 > 7.815\), reject \(H_0\).
Answer\(\chi^2 = 10.08\): the colours are not equally popular
Example 4: Fitting a binomial distribution
Exam levelFour coins are tossed 160 times. The numbers of heads 0, 1, 2, 3, 4 occur 8, 34, 69, 37, 12 times. Test at 5% whether the coins are unbiased.
- \(H_0\): \(p = \tfrac{1}{2}\), so \(P(k) = \dbinom{4}{k}\dfrac{1}{16}\). Expected: \(160 \times \tfrac{1}{16},\ \tfrac{4}{16},\ \tfrac{6}{16},\ \tfrac{4}{16},\ \tfrac{1}{16}\) = 10, 40, 60, 40, 10.
- \(\chi^2 = \dfrac{4}{10} + \dfrac{36}{40} + \dfrac{81}{60} + \dfrac{9}{40} + \dfrac{4}{10}\).
- \(= 0.4 + 0.9 + 1.35 + 0.225 + 0.4 = 3.275\).
- No parameter was estimated (\(p\) was given by \(H_0\)), so df \(= 5 - 1 = 4\). Table value 9.488. Accept \(H_0\).
Answer\(\chi^2 = 3.275\): consistent with unbiased coins
Example 5: Fitting a Poisson distribution
Exam levelDefects found on 200 items: 0 defects on 70 items, 1 on 80, 2 on 33, 3 on 14, 4 on 3. Fit a Poisson distribution and test the fit at 5%.
- \(\sum xf = 0(70) + 1(80) + 2(33) + 3(14) + 4(3) = 200\), so the mean is \(\dfrac{200}{200} = 1\). Take \(\lambda = 1\).
- Expected: \(200e^{-1} = 73.58\) for 0 and for 1, \(200e^{-1}/2 = 36.79\) for 2. For 3 or more: \(200 - 73.58 - 73.58 - 36.79 = 16.06\). (Merging 3 and 4 keeps every \(E \ge 5\).)
- \(\chi^2 = \dfrac{3.58^2}{73.58} + \dfrac{6.42^2}{73.58} + \dfrac{3.79^2}{36.79} + \dfrac{0.94^2}{16.06}\).
- \(= 0.174 + 0.560 + 0.390 + 0.055 = 1.18\).
- 4 classes, 1 parameter estimated, so df \(= 4 - 1 - 1 = 2\). Table value 5.991. Accept \(H_0\).
Answer\(\chi^2 \approx 1.18\): the Poisson distribution fits well
Example 6: A 2 × 2 table
MediumOf 100 students who studied online, 60 passed and 40 failed. Of 100 who studied offline, 75 passed and 25 failed. Is the result independent of study mode? Test at 5%.
- Row totals 100, 100; column totals 135 (pass), 65 (fail); \(N = 200\).
- Expected: pass \(= \dfrac{100 \times 135}{200} = 67.5\) in each row, fail \(= 32.5\) in each row. Every \(|O - E| = 7.5\).
- \(\chi^2 = 56.25\left(\dfrac{2}{67.5} + \dfrac{2}{32.5}\right) = 1.667 + 3.462 = 5.128\). The shortcut gives \(\dfrac{200(1500 - 3000)^2}{100 \cdot 100 \cdot 135 \cdot 65}\), the same.
- df \(= (2 - 1)(2 - 1) = 1\), table value 3.841. Since \(5.128 > 3.841\), reject \(H_0\). (With Yates' correction, \(\chi^2 = 49\left(\dfrac{2}{67.5} + \dfrac{2}{32.5}\right) \approx 4.467\): still a rejection.)
AnswerResult and study mode are associated
Example 7: A 2 × 3 table
Exam level300 people by income group and opinion on a proposal. For: low 70, middle 70, high 40. Against: low 30, middle 50, high 40. Test at 5% whether opinion is independent of income.
- Row totals 180, 120; column totals 100, 120, 80; \(N = 300\).
- Expected, For row: \(\dfrac{180 \times 100}{300} = 60\), then 72, 48. Against row: 40, 48, 32.
- \(\chi^2 = \dfrac{100}{60} + \dfrac{4}{72} + \dfrac{64}{48} + \dfrac{100}{40} + \dfrac{4}{48} + \dfrac{64}{32}\).
- \(= 1.667 + 0.056 + 1.333 + 2.5 + 0.083 + 2 = 7.639\).
- df \(= (2 - 1)(3 - 1) = 2\), table value 5.991. Reject \(H_0\). (At 1% the value is 9.210, so it would be accepted there.)
Answer\(\chi^2 \approx 7.64\): opinion depends on income at the 5% level
Before computing a single \((O - E)^2\), check that your expected frequencies add up to the same total as the observed ones. If they don't, something in your \(E\) column is wrong, and everything after it will be wrong too. It takes ten seconds and I still do it every time.
Common mistakes
1. Forgetting to subtract estimated parameters
In Example 5 the mean was found from the data, so df \(= 4 - 1 - 1 = 2\), not 3. Using df 3 gives the table value 7.815 instead of 5.991.
2. Dividing by O instead of E
Each term is \(\dfrac{(O - E)^2}{E}\). In Example 3, \(\dfrac{196}{64}\) instead of \(\dfrac{196}{50}\) changes the answer.
3. Leaving tiny expected frequencies
In Example 5 the expected count for "4 or more" on its own is about 3.80. Merge such classes with a neighbour first, and count the classes after merging when you find df.
Practice questions
Try each one on paper before you open the answer.
Q1A coin tossed 100 times gives 60 heads. Is it fair, at 5%?
\(E = 50, 50\). \(\chi^2 = \dfrac{100}{50} + \dfrac{100}{50} = 4\). df 1, table value 3.841. Reject: the coin looks biased.
Q2A die thrown 60 times gives 8, 12, 9, 11, 13, 7. Test fairness at 5%.
\(E = 10\). \(\sum (O - E)^2 = 4 + 4 + 1 + 1 + 9 + 9 = 28\), \(\chi^2 = 2.8\).
df 5, table value 11.070. Accept: the die may be fair.
Q3Theory gives a 3 : 1 ratio. Out of 200, the counts are 140 and 60. Test at 5%.
\(E = 150, 50\). \(\chi^2 = \dfrac{100}{150} + \dfrac{100}{50} = 2.667\). df 1, 3.841. Accept.
Q4A 2 × 2 table has cells 30, 20 (first row) and 20, 30 (second row). Test independence at 5% without Yates' correction.
\(\chi^2 = \dfrac{100(900 - 400)^2}{50 \cdot 50 \cdot 50 \cdot 50} = \dfrac{25\,000\,000}{6\,250\,000} = 4\).
df 1, 3.841. Reject independence.
Q5Find the df and the 5% table value for (a) a 4 × 3 contingency table, (b) a Poisson fit with 6 classes after merging.
(a) \((4 - 1)(3 - 1) = 6\), table value 12.592.
(b) \(6 - 1 - 1 = 4\), table value 9.488.
Q6In a contingency table with grand total 200, a cell's row total is 80 and its column total is 50. Find its expected frequency.
\(E = \dfrac{80 \times 50}{200} = 20\).
Bring it to a free demo class and work through it with Vipul Sir. Book on WhatsApp.
Frequently asked questions
What does the chi-square test tell you?
Whether observed frequencies differ from expected ones by more than chance would explain. It is used for goodness of fit and for testing independence in contingency tables.
How do you find degrees of freedom for a chi-square test?
For goodness of fit, (number of classes) − 1 − (number of parameters estimated). For an \(r \times c\) contingency table, \((r - 1)(c - 1)\).
Why must expected frequencies be at least 5?
The table values come from an approximation that is poor when expected counts are small. Small \(E\) values can make \(\chi^2\) blow up, so merge classes first.
Is the chi-square test one-tailed or two-tailed?
It is right-tailed. \(\chi^2\) is never negative, and only a large value counts as evidence against \(H_0\).