Analyst Dojo
Lesson 13 of 13 11–14% of the exam

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.

8 min cram, 36 min full read. Based on the 2027 CFA® Program Level I curriculum, Quantitative Methods, Learning Module 7. Sitting in November 2026? The 2026 version of this lesson is still available.

Tailor this to you: take the self-assessment

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.

Cram mode: the one-screen version of this lesson. Switch to Full for the explanations, worked examples and interactives.

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.

Which test of independence?
Are two characteristics linked?choose by the type of data
  • 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 .

Expected frequency Memorize
Eij=(row i total)×(column j total)grand totalE_{ij} = \frac{(\text{row } i \text{ total}) \times (\text{column } j \text{ total})}{\text{grand total}}

Turned around, the formula can find a missing total.

Rearrange itExpected frequency in a cell: solve for any one
The question asks for
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

On your calculator
  1. 80×50÷200=
  1. 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.

Chi-square statistic Memorize
χ2=∑i,j(Oij−Eij)2Eij\chi^2 = \sum_{i,j} \frac{(O_{ij} - E_{ij})^2}{E_{ij}}
Degrees of freedom Memorize
df=(r−1)(c−1)df = (r - 1)(c - 1)
Rearrange itDegrees of freedom of a contingency table: solve for any one
The question asks for
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

On your calculator
  1. (2−1)×(3−1)=
  1. 2ENTER1−3ENTER1−×

ENTER enter (on the HP, puts the number on the stack)

InteractiveContingency table: how far from independence?

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
Observed counts O (type to edit)
TechIndustrialsUtilitiesTotal
High ESG80
Low ESG120
Total507080200
Expected E and each cell's (O − E)² ÷ E (darker = bigger penalty)
TechIndustrialsUtilities
High ESG5.000E 20 · O 300.321E 28 · O 251.531E 32 · O 25
Low ESG3.333E 30 · O 200.214E 42 · O 451.021E 48 · O 55

Significance level α (right tail)

χ² = Σ(O − E)² ÷ E11.42
df = (r − 1)(c − 1)(2 − 1)(3 − 1) = 2
Critical value (5%)5.991
p-value0.0033
DecisionReject independence

χ² = 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₀.

Expected count and χ² on your calculator
  1. 80×50÷200=E for High ESG, Tech = 20
  2. ((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

  1. 80ENTER50×200÷E for High ESG, Tech = 20
  2. 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.

Largest standardized residual on your calculator
  1. 30−20=÷20√x=2.2361

√x square root

  1. 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.

Each cell adds a penalty for being far from what independence predicts.

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 residual Memorize
standardized residual=Oij−EijEij\text{standardized residual} = \frac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}}

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

ItemRule
Null hypothesisthe two classifications are independent
Expected countE=row×column÷grand totalE = \text{row} \times \text{column} \div \text{grand total}
Statisticχ2=∑(O−E)2/E\chi^2 = \sum (O-E)^2 / E
Degrees of freedom(r−1)(c−1)(r-1)(c-1)
Tailright tail only: large χ2\chi^2 → reject
Standardized residual(O−E)/E(O-E)/\sqrt{E}; 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 χ2>\chi^2 > critical value → reject independence. Expected counts below about 5 make the test unreliable.

Which test of independence?
Are two characteristics linked?choose by the type of data
  • 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
Expected frequency Memorize
Eij=(row i total)×(column j total)grand total=(row i share)×(column j share)×NE_{ij} = \dfrac{(\text{row } i \text{ total}) \times (\text{column } j \text{ total})}{\text{grand total}} = (\text{row } i \text{ share}) \times (\text{column } j \text{ share}) \times N
Quick example

Row total 90, column total 60, grand total 300. E = 90 × 60 ÷ 300 = 18. In shares: 0.30 × 0.20 × 300 = 18.

On your calculator
  1. 90×60÷300= 18
  2. 0.3×0.2×300= 18
  1. 90ENTER60×300÷ 18
  2. 0.3ENTER0.2×300× 18

ENTER enter (on the HP, puts the number on the stack)

Chi-square statistic Memorize
χ2=∑i,j(Oij−Eij)2Eij\chi^2 = \sum_{i,j} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}
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.

On your calculator
  1. (40−50)x²÷50×4= 8

x² square

  1. 40ENTER50−ENTER×50÷4× 8

ENTER enter (on the HP, puts the number on the stack)

Degrees of freedom Memorize
df=(r−1)(c−1)df = (r - 1)(c - 1)
Quick example

A table with 3 rows and 4 columns. df = 2 × 3 = 6.

On your calculator
  1. (3−1)×(4−1)= 6
  1. 3ENTER1−4ENTER1−× 6

ENTER enter (on the HP, puts the number on the stack)

Standardized residual Memorize
standardized residual=Oij−EijEij\text{standardized residual} = \dfrac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}}
Quick example

A cell with O = 26 and E = 16. (26 − 16) ÷ √16 = +2.5: beyond ±2, so well above what independence predicts.

On your calculator
  1. 26−16=÷16√x= 2.5

√x square root

  1. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  1. 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:

  2. 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:

  3. 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:

  4. 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:

  5. 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.
Expected frequency Memorize
Eij=(row i total)×(column j total)grand total=(row i share)×(column j share)×NE_{ij} = \dfrac{(\text{row } i \text{ total}) \times (\text{column } j \text{ total})}{\text{grand total}} = (\text{row } i \text{ share}) \times (\text{column } j \text{ share}) \times N
Quick example

Row total 90, column total 60, grand total 300. E = 90 × 60 ÷ 300 = 18. In shares: 0.30 × 0.20 × 300 = 18.

On your calculator
  1. 90×60÷300= 18
  2. 0.3×0.2×300= 18
  1. 90ENTER60×300÷ 18
  2. 0.3ENTER0.2×300× 18

ENTER enter (on the HP, puts the number on the stack)

Chi-square statistic Memorize
χ2=∑i,j(Oij−Eij)2Eij\chi^2 = \sum_{i,j} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}
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.

On your calculator
  1. (40−50)x²÷50×4= 8

x² square

  1. 40ENTER50−ENTER×50÷4× 8

ENTER enter (on the HP, puts the number on the stack)

Degrees of freedom Memorize
df=(r−1)(c−1)df = (r - 1)(c - 1)
Quick example

A table with 3 rows and 4 columns. df = 2 × 3 = 6.

On your calculator
  1. (3−1)×(4−1)= 6
  1. 3ENTER1−4ENTER1−× 6

ENTER enter (on the HP, puts the number on the stack)

Standardized residual Memorize
standardized residual=Oij−EijEij\text{standardized residual} = \dfrac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}}
Quick example

A cell with O = 26 and E = 16. (26 − 16) ÷ √16 = +2.5: beyond ±2, so well above what independence predicts.

On your calculator
  1. 26−16=÷16√x= 2.5

√x square root

  1. 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

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.

  1. The central limit theorem and the standard error of the mean
  2. Confidence intervals for a mean return
  3. Value at risk from the normal distribution
  4. Sampling methods and sampling error
  5. How a hypothesis test works: four steps from claim to decision
  6. Type I and Type II errors and the power of a test
  7. t-tests of a mean, a difference in means and paired differences
  8. Chi-square and F-tests of variance
  9. Is a correlation significant? The t-test of correlation
  10. P-values, p-hacking and critical values from software
  11. Parametric vs non-parametric tests
  12. Spearman rank correlation: the non-parametric test
  13. Chi-square test of independence with a contingency table

Ask Sensei Listen instead

Questions on Chi-square test of independence with a contingency table

Learn with Sensei, out loud The voice tutor, in early access

Sensei teaches the lesson aloud and stops when you interrupt: "wait, why is the geometric mean lower?" It answers with the lesson's own example, then quizzes you by voice. Every account can try Sensei on one module, with one voice question.

USD 149 a month founding price (regular USD 199), kept for as long as you stay subscribed. Everything in Plus included.

In active development and improving every day. Send feedback · Plans and pricing

Analyst Dojo Plus

Your free review list holds 10 items.

Plus keeps everything you save, for USD 19 a month. To make room on the free plan, remove something from your review list.

See Plus

Lessons, checkpoints and the lesson questions in practice stay free. Compare plans