wanna mathMath ain't scary (not anymore)
4-Tier Textbook Index • Vol → Mod → Ch → Lab

Engineering Mathematics Volumes I–VI

6 Volumes • 20 Modules • 85 ChaptersFull Matrix →
4-Tier Statistics Index • Vol S → Mod → Ch → Lab

Interactive Statistics (S.1–S.6)

6 Modules • 18 ChaptersStatistics Index →
CHAPTER S.5.2 • 2DPublished Interactive Labs~30 min study

Chi-Square Goodness-of-Fit, Contingency Tables & Simpson's Paradox

Compare observed counts against expected independence—and witness how combining two departments can completely reverse a winning rate.

Engineering & Physical Application Context

Loaded die / Benford fraud checks, A/B categorical conversions, and confounding bias in observational studies.

Governing Mathematical Formulation
χ2=∑(O−E)2E,df=(r−1)(c−1)\chi^2 = \sum \frac{(O - E)^2}{E}, \qquad \text{df} = (r-1)(c-1)
Volume S • Module S.51 Interactive Lab(s)

The Question You Should Ask

How does Pearson's χ2=∑(O−E)2/E\chi^2 = \sum (O - E)^2/E quantify deviation from categorical independence—and how can pooling subgroups flip every comparison upside down in Simpson's Paradox?

In Simple Words & Math

Under independence inside an r×cr\times c contingency table, expected cell counts obey Eij=RiCj/NE_{ij} = R_i C_j / N, and normalized squared residuals follow χ2\chi^2 with (r−1)(c−1)(r-1)(c-1) degrees of freedom.

Yet even a statistically clear aggregate table can invert true subgroup proportions whenever an unmodeled confounding variable shifts weights unevenly across categories—requiring stratified analysis.

4-Step Pedagogical Chapter Rhythm • Geometric Intuition First
Ch. S.5.2
STEP 1 • GEOMETRIC QUESTION

How can Treatment A beat Treatment B inside BOTH mild and severe patient subgroups, yet lose to Treatment B when the hospital combines everyone together?

STEP 2 • INTERACTIVE LAB EXPLORATION

Edit Observed vs. Expected contingency bars to watch Pearson χ² residuals grow, then switch to the Simpson's Paradox Reverser and slide subgroup sizes to flip the aggregated winner.

STEP 3 • SYMBOLIC FORMALIZATION
χ2=∑i,j(Oij−Eij)2Eij,Eij=RiCjN\chi^2 = \sum_{i,j} \frac{(O_{ij} - E_{ij})^2}{E_{ij}}, \qquad E_{ij} = \frac{R_i C_j}{N}
STEP 4 • EDGE CASES & NEXT BRIDGE

Observational totals without stratification hide confounding variables! Next, Chapter S.5.3 compares 3 or more continuous groups at once via One-Way ANOVA.

Interactive Laboratories in Chapter S.5.2
FIRST-TIMER GUIDE • CATEGORICAL COUNTS & CONFOUNDING

Comparing Real Counts Against a Fair Baseline—And Catching Simpson's Paradox Red-Handed

Imagine rolling a board-game die 120 times and getting thirty 6s instead of the expected twenty. Is the die rigged, or just lucky? Karl Pearson's Chi-Square (χ2\chi^2) test scores every category by how far Observed (OO) counts drift from Expected (EE) counts—measured relative to EE. Even more eye-opening: in real hospitals or admissions offices, pooling two unequal sub-groups into one 2×22 \times 2 table can completely reverse which option wins—that optical illusion is Simpson's Paradox!

Initializing Interactive Laboratory
LOADING MODULE

Preparing interactive canvas & numerical solver…

wanna math drafting viewport

The Chi-Square Formula, Expected Independence Counts & Confounding Traps

1. PEARSON'S CHI-SQUARE STATISTIC
χ2=∑(O−E)2E\chi^2 = \sum \frac{(O - E)^2}{E}

Why divide by EE? Being off by 5 counts out of E=10E = 10 is huge, whereas being off by 5 counts out of E=1,000E = 1{,}000 is tiny! Degrees of freedom are (r−1)(c−1)(r - 1)(c - 1) for a table or k−1k - 1 for goodness-of-fit.

2. EXPECTED COUNTS UNDER INDEPENDENCE
Eij=Row Totali×Col TotaljNE_{ij} = \frac{\text{Row Total}_i \times \text{Col Total}_j}{N}

From S.2.1, two events are independent when P(A∩B)=P(A)P(B)P(A \cap B) = P(A)P(B). Multiplying by NN gives the exact counts we expect if row & column are unrelated!

3. TINY WORKED GOODNESS-OF-FIT CHECK

Toss a coin 100 times (E=50E = 50 Heads, E=50E = 50 Tails) and observe 60 Heads, 40 Tails:

χ2=(60−50)250+(40−50)250=2+2=4.00\chi^2 = \frac{(60 - 50)^2}{50} + \frac{(40 - 50)^2}{50} = 2 + 2 = 4.00

With df=1\text{df} = 1, χ2=4.00>3.84\chi^2 = 4.00 > 3.84, giving p≈0.0455p \approx 0.0455!

TRAP 1 & 2 • PERCENTAGES LIE & SIMPSON'S PARADOX

Never feed percentages into χ2\chi^2—always feed raw counts (and switch to Fisher's Exact Test whenever any Eij<5E_{ij} < 5). Second, always inspect sub-group strata: Treatment A can lose overall only because doctors gave it to harder cases!

BRIDGE TO S.5.3 • COMPARING 3+ NUMERICAL GROUPS AT ONCE

Chi-Square compares counts across multiple categories. What if you want to compare numerical averages across 3 or 4 treatments simultaneously without triggering false alarms? Welcome to One-Way ANOVA in S.5.3!

Categorical Inference & Confounding • Ch. S.5.2

Expected Independence Derivation, Constraint Degrees of Freedom & Simpson's Reversal

Open Chi-Square Formulas →
1. WHY E_ij = R_i C_j / N & df = (r−1)(c−1)
Eij=N⋅P^(Row i)P^(Col j)=NRiNCjN=RiCjNE_{ij} = N \cdot \hat{P}(\text{Row } i)\hat{P}(\text{Col } j) = N\frac{R_i}{N}\frac{C_j}{N} = \frac{R_i C_j}{N}

Once marginal row totals RiR_i and column totals CjC_j are fixed from the table, filling an (r−1)×(c−1)(r-1)\times(c-1) upper-left block determines all remaining border cells uniquely!

2. TWO-BY-TWO EQUIVALENCE TO Z²
χ12=∑i=12∑j=12(Oij−Eij)2Eij=(p^1−p^2p^(1−p^)(1/n1+1/n2))2\chi^2_{1} = \sum_{i=1}^{2}\sum_{j=1}^{2}\frac{(O_{ij}-E_{ij})^2}{E_{ij}} = \left(\frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}(1-\hat{p})(1/n_1 + 1/n_2)}}\right)^2

For any 2×22\times 2 contingency table, Pearson's χ2\chi^2 with df=1\text{df}=1 matches the squared two-proportion ZZ-statistic identically—with expected counts Eij≥5E_{ij} \ge 5 guarding continuity.

3. ALGEBRA OF SIMPSON'S PARADOX
a1b1>c1d1   and   a2b2>c2d2  ̸ ⁣ ⁣  ⟹  a1+a2b1+b2>c1+c2d1+d2\frac{a_1}{b_1} > \frac{c_1}{d_1} \;\text{ and }\; \frac{a_2}{b_2} > \frac{c_2}{d_2} \;\not\!\!\implies \frac{a_1+a_2}{b_1+b_2} > \frac{c_1+c_2}{d_1+d_2}

Weighted averages depend on subgroup denominators! When Treatment A gets assigned mostly to severe cases (low survival), its pooled average looks worse even while beating Treatment B inside every stratum.

Chapter S.5.2 Laboratory Specifications

1 Interactive Lab(s)
LAB S.5.2.1Component: ChiSquareContingencySimpsonLab

Chi-Square Contingency Cells & Simpson's Paradox Reverser

Inspect (O − E)²/E cell contributions and dynamically reverse aggregated rates in Simpson's Paradox via subgroup weighting.

χ2=∑(Oij−Eij)2Eij\chi^2 = \sum \frac{(O_{ij} - E_{ij})^2}{E_{ij}}