When two yes/no traits travel together, χ² puts a number on the suspicion.
Probability asked how likely is tomorrow? and the rest of Stage 34 squeezed a pile of numbers into a center, a spread, a line. But some data are not numbers at all — smokes or doesn't; tired in class or alert; left-handed or right-handed. These are categorical, and the only natural summary is a count. Pair two such traits and the counts fall into a little grid — a contingency table — and the question becomes: do the two traits travel together, or are they unrelated? This lesson builds one honest number, the chi-square statistic χ², that turns "those splits look different" into a verdict you can defend.
A categorical variable sorts each individual into a named box, not onto a number line. "Eye color," "favorite sport," and the yes/no answer "Did you sleep fewer than six hours?" are all categorical. You cannot take their mean; you can only count how many landed in each box.
Pair two categorical traits and you cross-tabulate. Suppose we ask 100 students two questions — "Do you use a screen after midnight on school nights?" and "Do you feel tired in first period?" — and tally the answers into a 2×2 grid. That grid is the contingency table (or two-way table): rows for one trait, columns for the other, and a count in every cell.
| Tired | Alert | Row total | |
|---|---|---|---|
| Late screen | 34 | 16 | 50 |
| No late screen | 18 | 32 | 50 |
| Column total | 52 | 48 | 100 |
The inside four numbers are the cell counts; the margins are the row totals and column totals; the corner is the grand total n = 100. Everything we do flows from these counts — never from percentages someone already rounded off.
A contingency table is just counts arranged by two categorical traits at once. The totals on the edges (the marginal counts) are the bookkeeping that the rest of the test leans on.
Raw counts can mislead because the row totals may differ. The fair comparison is a conditional rate: within each row, what fraction is tired? Among the 50 late-screen students, 34 are tired — a rate of 3450 = 68%. Among the 50 who avoid late screens, only 18 are tired — 1850 = 36%.
Those two rates are very different, so we suspect a link. The picture that shows this at a glance is the mosaic: one full-height bar per row, sliced by the column rates. If the two traits were unrelated, both bars would slice at the same height. When the slice lines sit at clearly different heights — as below — that mismatch is the visible face of association.
Different conditional rates make you suspect a link — but a small sample can produce different rates by pure luck. "The mosaic looks lopsided" is a hunch, not a verdict. We need a number that accounts for how big the sample is. That number is χ².
Step the four cell counts. The table and the mosaic redraw, and the readout reports the two conditional "tired" rates and whether they differ.
To judge the gap fairly we ask a sharp question: if the two traits were truly unrelated (statisticians say independent), how many students would we expect in each cell? Independence means the "tired" rate is the same in both rows — namely the overall rate 52100 = 52%. So among the 50 late-screen students we would expect 50 × 0.52 = 26 to be tired.
That arithmetic packs into one clean rule. The expected count for a cell is
E = (row total) × (column total)grand total
Apply it to every cell. Row total 50, column total 52, grand total 100 gives E = 26; and so on for all four:
| Tired | Alert | |
|---|---|---|
| Late screen | 26 | 24 |
| No late screen | 26 | 24 |
The expected table has the same margins as the observed one (the row totals are still 50 and 50, the columns still 52 and 48) — it just spreads the counts as if the traits ignored each other. The closer the observed counts hug these expected ones, the less reason there is to claim a link.
Expected counts are not a guess — they are the counts forced by the margins and the assumption of independence. Every expected cell is rowSum · colSum / total, computed by GX.chiExpected.
Now we need one number for "how far is observed from expected?" Cell by cell, take the gap O − E, square it (so over- and under-counts both count, and big misses count extra), and divide by E (so a gap of 8 means more in a cell of 5 than in a cell of 500). Add the four pieces:
χ² = Σ (O − E)²E
Each cell's contribution (O−E)²E:
Late + Tired: (34−26)²26 = 6426 = 2.462
Late + Alert: (16−24)²24 = 6424 = 2.667
No-late + Tired: (18−26)²26 = 2.462
No-late + Alert: (32−24)²24 = 2.667
Sum: χ² = 2.462 + 2.667 + 2.462 + 2.667 = 10.256.
The 2×2 shortcut agrees exactly: χ² = n(ad − bc)²(a+b)(c+d)(a+c)(b+d) = 100·(34·32 − 16·18)²50·50·52·48 = 10.256.
A χ² of 0 would mean observed equalled expected in every cell — perfect independence. The bigger the gaps, the bigger χ² climbs. So we have built a meter; next we calibrate it.
Choose a table. The left grid is your observed counts; the right grid is the expected counts from GX.chiExpected, each cell showing its (O−E)²/E. The readout sums them to χ² and confirms the 2×2 shortcut matches.
How big is "big"? Even with truly independent traits, random sampling makes χ² wobble above 0. Statisticians worked out how far it typically wanders, and set a critical value as the line of reasonable doubt. For a 2×2 table the table has 1 degree of freedom (df = (rows−1)(cols−1) = 1·1 = 1), and at the 5% significance level the critical value is
χ²crit = 3.841 (df = 1, α = 0.05)
The rule is a single comparison:
| If… | Verdict |
|---|---|
| χ² is greater than 3.841 | the traits are associated |
| χ² is at most 3.841 | no evidence of association |
Our screen table gave χ² = 10.256, well past 3.841 — so we conclude the late-screen habit and feeling tired are associated. (For a bigger table you would read a different critical value for its df: at df=2 the 5% line is 5.991, at df=3 it is 7.815, and so on.)
Notice what we just did: we measured a gap, compared it against a threshold, and reached a verdict — "associated" or "not." That move from evidence to a defensible yes/no is exactly the reasoning that Stage 35 · Logic will make formal, with hypotheses, claims, and proof. The chi-square test is the bridge from describing data to deciding with it.
The slider shifts counts toward the diagonal — making the two rows split more and more differently. The gauge shows χ² rising past the dashed 3.841 verdict line.
Association is not causation. A big χ² says the two traits move together; it does not say one causes the other, nor which direction it runs. (A lurking variable — say, having a heavy course load — might drive both late screens and tiredness.)
It needs counts, not percentages. Feed χ² the raw cell counts; the same percentages from 20 people and from 2000 people carry very different weight.
Expected counts should be at least 5. When an expected cell is tiny, the χ² approximation gets shaky — gather more data or merge categories.
Size matters more than significance. With a huge sample even a trivial difference can push χ² past 3.841. "Statistically associated" is not the same as "a difference big enough to care about."
| Step | What you do | Tool |
|---|---|---|
| 1 · Tabulate | Cross two categorical traits into a 2×2 (or r×c) count table with row/column totals. | contingency table |
| 2 · Compare rates | Within each row, the conditional rate; a mosaic shows the splits. | GX.mosaic |
| 3 · Expect | E = rowSum · colSum / total in every cell (independence). | GX.chiExpected |
| 4 · Measure | χ² = Σ (O−E)²/E; 2×2 shortcut agrees. | GX.chiSq · GX.chiSq2x2 |
| 5 · Decide | df = (r−1)(c−1); compare χ² to the critical value (3.841 at df=1, α=0.05). Above ⇒ associated. | verdict |
Remember the three guards: counts not percentages, expected ≥ 5, and association ≠ causation.
Six questions to lock it in. Tap the answer you think is right.
This lesson develops CCSS HSS-ID.B.5 — summarizing categorical data in two-way frequency tables and reading joint, marginal, and conditional relative frequencies to recognize possible associations. The chi-square test of independence itself (expected counts, the χ² = Σ(O−E)²/E statistic, degrees of freedom, and the critical-value verdict) is an AP Statistics topic that formalizes B.5 — here it is introduced concretely on a 2×2 table. The "reach a verdict from evidence" move foreshadows Stage 35 · Logic. Throughout, every expected count, χ² value, and conditional rate is computed by the shared GX helpers (chiExpected, chiSq, chiSq2x2, mosaic), never read off a picture — and we stress the three classic cautions: association is not causation, χ² needs counts not percentages, and expected counts should be at least 5.