Chi-square test of independence with a contingency table
Lay the counts out in a contingency table. If the two characteristics were independent, each cell would hold (row total × column total) ÷ grand total. The chi-square statistic, Σ(O − E)² ÷ E with (r − 1)(c − 1) degrees of freedom, measures how far observed counts stray from that. A large value rejects independence.
Before this: Parametric vs non-parametric tests
On the exam
- Compare and contrast parametric and non-parametric tests, describe situations in which each is the more appropriate type of test, construct appropriate hypothesis tests, and interpret the results
Explain tests of independence based on contingency table data, and determine the result at a given level. Expect to find an expected count, one cell's contribution or the degrees of freedom, then compare a given statistic with a critical value.
How deeply to learn each part
- Know the idea
- Understand it in general. It frames other topics, but you will not calculate with it.
- Use it
- Understand it well enough to solve a question with it. Recognize when it applies.
- Memorize
- Learn it by heart. The exam will not give it to you.
You know this one. The full lesson is folded away. The takeaways, formulas and checkpoint below are your quick refresher, and the checkpoint is a fair test of whether you still do.
You tested strong here. The full lesson is folded away. The takeaways, formulas and checkpoint below are your quick refresher, and the checkpoint is a fair test of whether you still know it.
When do you use a chi-square test of independence?
When both characteristics are labels, not numbers. Sector, ESG (environmental, social and governance) rating, region and credit grade are categorical data : you cannot compute a correlation, but you can count how many companies fall in each combination.
Those counts go in a contingency table , with one characteristic across the rows and the other down the columns. A test of independence asks whether the pattern of counts is what you would get if the two characteristics had nothing to do with each other. It is a non-parametric test: it assumes no distribution for the data, only counts in categories.
- Two numerical variables Memorize returns, prices, ratios
- Parametric t-test of correlation roughly normal data
- Spearman rank test ranks, outliers, non-normal
-
- Two categorical variables Memorize sector, rating, region
- Chi-square test of independence counts in a contingency table
- Degrees of freedom (r − 1)(c − 1) right-tailed
-
The null hypothesis is always that the two classifications are independent.
How do you find the expected counts?
Under independence, each cell gets its share of the grand total according to its row and column. The counts you actually see are the observed frequency ; the counts independence predicts are the expected frequency .
Turned around, the formula can find a missing total.
In a contingency table the row total is 80, the column total is 50 and the grand total is 200. Under independence, the expected frequency of the cell is closest to:
80 × 50 ÷ 200 = 20
Expected frequency, E20
- 80×50÷200=
- 80ENTER50×200÷
ENTER enter (on the HP, puts the number on the stack)
How do you compute the statistic and decide?
For every cell, square the gap between observed and expected, divide by the expected count and add up all the cells. The chi-square distribution then gives the critical value.
A contingency table has 2 rows and 3 columns. The chi-square test of independence has how many degrees of freedom? The answer is closest to:
(2 − 1) × (3 − 1) = 2
Degrees of freedom2
- (2−1)×(3−1)=
- 2ENTER1−3ENTER1−×
ENTER enter (on the HP, puts the number on the stack)
Try this 0 of 4
- Press “Make independent” and read χ²
- Keep 200 companies but edit counts until you fail to reject at 5%
- Load the worked example and double every count
- Find the cell with a standardized residual beyond 2
| Tech | Industrials | Utilities | Total | |
|---|---|---|---|---|
| High ESG | 80 | |||
| Low ESG | 120 | |||
| Total | 50 | 70 | 80 | 200 |
| Tech | Industrials | Utilities | |
|---|---|---|---|
| High ESG | 5.000E 20 · O 30 | 0.321E 28 · O 25 | 1.531E 32 · O 25 |
| Low ESG | 3.333E 30 · O 20 | 0.214E 42 · O 45 | 1.021E 48 · O 55 |
Significance level α (right tail)
χ² = 11.42 is above the 5% critical value of 5.991: reject independence. ESG rating and sector are related in this sample.
Show working
H₀: ESG rating and sector are independent. Hₐ: they are not. The test is right-tailed: any departure from E raises χ².
E = row total × column total ÷ grand total. For High ESG, Tech: 80 × 50 ÷ 200 = 20.
Expected: High ESG 20, 28, 32; Low ESG 30, 42, 48.
χ² = (30 − 20)² ÷ 20 + (25 − 28)² ÷ 28 + (25 − 32)² ÷ 32 + (20 − 30)² ÷ 30 + (45 − 42)² ÷ 42 + (55 − 48)² ÷ 48
= 5.000 + 0.321 + 1.531 + 3.333 + 0.214 + 1.021 = 11.421.
df = (rows − 1)(columns − 1) = (2 − 1)(3 − 1) = 2. Critical value at 5%: 5.991. 11.421 > 5.991: reject H₀.
- 80×50÷200=E for High ESG, Tech = 20
- ((30−20)x²÷20)+((25−28)x²÷28)+((25−32)x²÷32)+((20−30)x²÷30)+((45−42)x²÷42)+((55−48)x²÷48)=χ² = 11.421
x² square
- 80ENTER50×200÷E for High ESG, Tech = 20
- 30ENTER20−ENTER×20÷25ENTER28−ENTER×28÷+25ENTER32−ENTER×32÷+20ENTER30−ENTER×30÷+45ENTER42−ENTER×42÷+55ENTER48−ENTER×48÷+χ² = 11.421
ENTER enter (on the HP, puts the number on the stack)
Standardized residual = (O − E) ÷ √E, approximately standard normal, so a cell beyond about ±2 (±1.96 at 95%) departs significantly from independence. Largest here, High ESG with Tech: (30 − 20) ÷ √20 = +2.24, beyond ±2. That is the only cell beyond ±2: the relationship comes mainly from it.
- 30−20=÷20√x=2.2361
√x square root
- 30ENTER20−20g√x÷2.2361
ENTER enter (on the HP, puts the number on the stack) · g blue shift key · √x square root
Divide by E, never by O; and df comes from the table's shape, not from the number of companies.
Exam tip The expected count uses the row and column totals. If you divide the grand total by the number of cells you ignore the row and column totals and get the wrong E.
Which cells drive the result?
Memorize
A rejection says the characteristics are related, not where. The standardized residual (also called the Pearson residual) of each cell answers that: positive means more companies than independence predicts, negative means fewer.
Standardized residuals are approximately standard normal, so a cell beyond about ±2 (±1.96 at 95%) departs significantly from independence.
Go deeper When the test is unreliable
The chi-square distribution is only an approximation, and it is poor when expected counts are small. As a rule of thumb, every expected count should be at least about 5. If many are smaller, combine neighboring categories (for example, merge two thin sectors) or collect more data before running the test. Observed counts can be small without trouble, since the rule concerns the expected ones.
Memorize this
MemorizeChi-square test of independence
| Item | Rule |
|---|---|
| Null hypothesis | the two classifications are independent |
| Expected count | |
| Statistic | |
| Degrees of freedom | |
| Tail | right tail only: large → reject |
| Standardized residual | ; beyond about ±2 → significant cell |
Memory hook "Row times column over total." Divide by E, not O: each cell is judged against what independence predicts. If O = E everywhere, χ² = 0.
Know it cold critical value → reject independence. Expected counts below about 5 make the test unreliable.
- Two numerical variables Memorize returns, prices, ratios
- Parametric t-test of correlation roughly normal data
- Spearman rank test ranks, outliers, non-normal
-
- Two categorical variables Memorize sector, rating, region
- Chi-square test of independence counts in a contingency table
- Degrees of freedom (r − 1)(c − 1) right-tailed
-
Quick example
Row total 90, column total 60, grand total 300. E = 90 × 60 ÷ 300 = 18. In shares: 0.30 × 0.20 × 300 = 18.
- 90×60÷300= 18
- 0.3×0.2×300= 18
- 90ENTER60×300÷ 18
- 0.3ENTER0.2×300× 18
ENTER enter (on the HP, puts the number on the stack)
Quick example
A 2 × 2 table with observed 40, 60, 60, 40 and every expected count 50. Each cell adds 100 ÷ 50 = 2, so χ² = 8.0.
- (40−50)x²÷50×4= 8
x² square
- 40ENTER50−ENTER×50÷4× 8
ENTER enter (on the HP, puts the number on the stack)
Quick example
A table with 3 rows and 4 columns. df = 2 × 3 = 6.
- (3−1)×(4−1)= 6
- 3ENTER1−4ENTER1−× 6
ENTER enter (on the HP, puts the number on the stack)
Quick example
A cell with O = 26 and E = 16. (26 − 16) ÷ √16 = +2.5: beyond ±2, so well above what independence predicts.
- 26−16=÷16√x= 2.5
√x square root
- 26ENTER16−16g√x÷ 2.5
ENTER enter (on the HP, puts the number on the stack) · g blue shift key · √x square root
How it is asked
-
Expected count for one cell
2 × 2 table of 160 loans: first row 24 and 16, second row 36 and 84. Expected top-left count?
40 × 60 ÷ 160 = 15. The observed 24 and grand total ÷ cells (40) are the traps.
-
Degrees of freedom
Same table, 2 rows and 2 columns. df?
(2 − 1)(2 − 1) = 1. Using r × c (4) or cells − 1 (3) is wrong.
-
Decision from the statistic
3 × 2 table, χ² = 8.42, 5% critical value 5.991 (df 2).
8.42 > 5.991: reject independence, the classifications are related.
-
Which cell drives it?
O = 64, E = 49. Standardized residual?
15 ÷ 7 = 2.14, beyond ±2. Trap: 4.59, the χ² contribution.
Exam traps
- Using df = r × c, or n − 1, instead of (r − 1)(c − 1).
- Dividing by the observed count O instead of the expected count E.
- Computing expected counts as grand total ÷ number of cells, ignoring the row and column totals.
- Reading a rejection as causation: it says the characteristics are related, not why.
Checkpoint
5 exam-style questions. About 90 seconds each.
1A 2 × 2 table of 100 bond issues shows counts of 18 and 12 in the first row and 22 and 48 in the second. Under independence, the expected count for the top-left cell is closest to:
2A contingency table sorts 100 bond issues by two characteristics, each with two categories. The degrees of freedom of the chi-square test of independence are:
3A chi-square test of independence on a 2 × 2 contingency table gives a statistic of 7.14. With a 5% critical value of 3.841, the analyst should most appropriately conclude that the two classifications are:
4In a contingency table, a cell has an observed frequency of 36 and an expected frequency of 30. Its contribution to the chi-square statistic is closest to:
5In a contingency table, a cell has an observed count of 45 and an expected count of 36. Its standardized residual and the best reading of it are:
Key takeaways
- E = row total × column total ÷ grand total.
- χ² = Σ(O − E)² ÷ E, with (r − 1)(c − 1) degrees of freedom.
- A large χ² rejects independence; the test is right-tailed.
Quick example
Row total 90, column total 60, grand total 300. E = 90 × 60 ÷ 300 = 18. In shares: 0.30 × 0.20 × 300 = 18.
- 90×60÷300= 18
- 0.3×0.2×300= 18
- 90ENTER60×300÷ 18
- 0.3ENTER0.2×300× 18
ENTER enter (on the HP, puts the number on the stack)
Quick example
A 2 × 2 table with observed 40, 60, 60, 40 and every expected count 50. Each cell adds 100 ÷ 50 = 2, so χ² = 8.0.
- (40−50)x²÷50×4= 8
x² square
- 40ENTER50−ENTER×50÷4× 8
ENTER enter (on the HP, puts the number on the stack)
Quick example
A table with 3 rows and 4 columns. df = 2 × 3 = 6.
- (3−1)×(4−1)= 6
- 3ENTER1−4ENTER1−× 6
ENTER enter (on the HP, puts the number on the stack)
Quick example
A cell with O = 26 and E = 16. (26 − 16) ÷ √16 = +2.5: beyond ±2, so well above what independence predicts.
- 26−16=÷16√x= 2.5
√x square root
- 26ENTER16−16g√x÷ 2.5
ENTER enter (on the HP, puts the number on the stack) · g blue shift key · √x square root
All Quantitative Methods formulas, every lesson
Practice exam-style questions on Estimation and Hypothesis Testing
Questions about this lesson?
Where this comes back
- Portfolio Return and Risk Quantitative Methods
- Simple Linear Regression in Finance Quantitative Methods
Questions candidates ask
How do you calculate expected frequencies in a contingency table?
Multiply the cell's row total by its column total and divide by the grand total. This is the count you would expect if the two characteristics were independent. For a row total of 80, a column total of 50 and 200 observations, E = 80 × 50 ÷ 200 = 20.
What are the degrees of freedom for a chi-square test of independence?
(r − 1)(c − 1), where r is the number of rows and c the number of columns. A 2 × 3 table has (2 − 1)(3 − 1) = 2 degrees of freedom. It is not r × c and it is not the number of observations minus 1.
What does the chi-square test of independence tell you?
It tells you whether two categorical characteristics, such as sector and ESG rating, are related in the sample. A statistic above the critical value rejects independence. It does not measure the strength of the link or show which characteristic drives the other.
Spotted an error? Tell us and we will fix it.