← SciSim / Statistics
Chi-Squared Test
📊 Tier: Standard Undergraduate
§1 Interactive Simulation
χ² statistic
df
p-value
α level
0.05
Critical value
Decision
Degrees of freedom 3
χ² observed value 5
α significance level 0.05
GoF test type
§2 The Idea, Step by Step

Imagine you suspect a board-game die of being "lucky." You roll it 60 times and tally how often each face shows up. A fair die should land on each face about 10 times — but you never get a clean 10, 10, 10, 10, 10, 10. There is always some wobble. The real question is whether that wobble is just ordinary luck, or whether the die is genuinely loaded. The chi-squared test is a fair-minded referee that measures the total wobble and decides whether it looks suspicious.

To put a number on it, call what you actually counted the observed values $O$, and what a fair die predicts the expected values $E$. For each face, see how far off you were, $O-E$, square it so that overshoots and undershoots both count as "off," and divide by $E$ so a gap of 5 matters more for a small category than for a big one. Then add up all six faces:

$$\chi^2=\sum\frac{(O-E)^2}{E}$$

Worked number: with the counts 8, 10, 15, 7, 12, 8 and $E=10$ for every face, the six pieces are $0.4+0+2.5+0.9+0.4+0.4=4.6$. A bigger total means the die looks more rigged; a total near zero means the data hug the fair-die prediction.

HOW BIG IS "TOO BIG"?

Under the null hypothesis $H_0$ (the die is fair), the statistic $\chi^2$ does not wander freely — it follows a known curve, the chi-squared distribution with $df=k-1$ degrees of freedom, where $k$ is the number of categories. Here $df=6-1=5$. One degree of freedom is "used up" because the counts are forced to add to the fixed total $n=60$. Compare your $4.6$ to the critical value $\chi^2_{5,\,0.05}=11.07$, or equivalently read the right-tail $p$-value $P(\chi^2_5>4.6)\approx0.47$. Since $4.6<11.07$ (and $p>0.05$), we fail to reject $H_0$: no real evidence the die is loaded. For a two-way contingency table the same recipe runs on $E_{ij}=n_{i\cdot}\,n_{\cdot j}/n$ with $df=(r-1)(c-1)$.

HOW THE SLIDERS MAP

In the simulation above, the Degrees of freedom slider sets the shape of the curve, χ² observed value slides the yellow line that marks your result, and the α significance level slider sets where the orange rejection region begins.

TRY THIS IN THE SIM ABOVE

(1) Drag $\alpha$ from $0.05$ down toward $0.001$ and watch the critical value climb — demanding stronger proof raises the bar. (2) Compare $df=1$ with $df=20$ and see the curve slide right and flatten as you add categories. (3) Click "Roll 60 dice" ten times in a row; roughly once you will see $p<0.05$ for a genuinely fair die — a Type I error happening in front of you, exactly $5\%$ of the time at $\alpha=0.05$.

§3 Mathematical Derivation

Pearson Chi-Squared Test — Karl Pearson, 1900

$$\boxed{\chi^2 = \sum_{i=1}^k \frac{(O_i - E_i)^2}{E_i} \overset{H_0}{\sim} \chi^2_{k-1}}$$

Goodness of Fit: $df=k-1$ (categories minus 1).
Test of Independence: $df=(r-1)(c-1)$ (rows-1 × cols-1).

Symbol Table

SymbolMeaningRangeNote
$O_i$Observed count in category $i$$\{0,1,2,...\}$Actual data counts
$E_i$Expected count under $H_0$$(0,\infty)$$E_i=n\cdot p_i$ for GoF; $n_{i\cdot}n_{\cdot j}/n$ for independence
$k$Number of categories$\{2,3,...\}$Goodness of fit
$r,c$Rows and columns$\{2,3,...\}$Contingency table dimensions
$df$Degrees of freedom$\mathbb{N}$$k-1$ (GoF) or $(r-1)(c-1)$ (independence)
$\chi^2_{df}$Chi-squared distribution$[0,\infty)$Sum of df squared standard normals
STEP 1 — Intuition

If $H_0$ is true (die is fair, variables are independent), the observed counts $O_i$ should be close to the expected counts $E_i$. The statistic $\chi^2=\sum(O_i-E_i)^2/E_i$ measures total discrepancy, normalized by expected size. Large $\chi^2$ → data is far from $H_0$.

STEP 2 — Distribution Under H₀

Under $H_0$, as $n\to\infty$: $\chi^2\overset{d}{\to}\chi^2_{k-1}$ (chi-squared with $k-1$ df). This holds because each $(O_i-E_i)/\sqrt{E_i}$ is approximately standard normal, and the sum of squares of $k$ standard normals has $\chi^2_k$ distribution — minus 1 df lost to the constraint $\sum O_i=n$.

STEP 3 — Expected Cell Counts (Independence)

For a contingency table, under independence: $E_{ij}=\frac{n_{i\cdot}\cdot n_{\cdot j}}{n}$, where $n_{i\cdot}$ is the row total and $n_{\cdot j}$ is the column total. This is the maximum likelihood estimate of expected frequency under independence.

STEP 4 — Assumption: E_i ≥ 5

The chi-squared approximation requires $E_i\ge5$ for all cells. When this fails: combine rare categories, use Fisher's exact test (2×2 tables), or use Monte Carlo permutation tests.

STEP 5 — p-value

$$p\text{-value} = P(\chi^2_{df} > \chi^2_{obs}) = 1 - F_{\chi^2_{df}}(\chi^2_{obs})$$

The chi-squared test is always right-tailed — large $\chi^2$ means large discrepancy from $H_0$.

Worked Example

Problem: Roll a die 60 times: observe frequencies 8,10,15,7,12,8 for faces 1–6. Test if die is fair ($\alpha=0.05$).

$H_0$: die is fair ($p_i=1/6$, so $E_i=10$ for each face).

$$\chi^2=\frac{(8-10)^2}{10}+\frac{(10-10)^2}{10}+\frac{(15-10)^2}{10}+\frac{(7-10)^2}{10}+\frac{(12-10)^2}{10}+\frac{(8-10)^2}{10}$$

$$=0.4+0+2.5+0.9+0.4+0.4=\mathbf{4.6},\quad df=5$$

Critical value: $\chi^2_{5,0.05}=11.07$. Since $4.6 < 11.07$, fail to reject $H_0$. p-value ≈ 0.47. No evidence die is unfair.

Casella & Berger — Statistical Inference, Ch. 10: "Asymptotic Evaluations"
Hogg, McKean & Craig — Introduction to Mathematical Statistics, Ch. 6
§4 FAQ
StatQuest — youtube.com/@statquest
Khan Academy — khanacademy.org/math/statistics-probability
§5 Misconceptions & Common Errors
❌ Misconception 1

"Chi-squared test can be used with any cell counts, even very small ones."
The chi-squared approximation requires E_i≥5 for all cells (some say ≥1 if no more than 20% below 5). With small expected counts, the chi-squared distribution is a poor approximation. Solutions: combine categories, use Fisher's exact test (for 2×2 tables), or use Monte Carlo/permutation versions of the chi-squared test.
📖 Casella & Berger — Ch. 10

❌ Misconception 2

"A significant chi-squared test tells us which categories deviate from expected."
The overall chi-squared test only detects that SOME deviation exists. To identify which cells contribute most, examine standardized residuals $(O_i-E_i)/\sqrt{E_i}$ — values beyond ±2 indicate problematic cells. Or use post-hoc pairwise comparisons with adjusted p-values.
📖 Agresti — Categorical Data Analysis, Ch. 3

❌ Misconception 3

"Chi-squared test of independence proves two variables are related."
Rejecting independence only shows statistical association, not the nature or direction of the relationship. The chi-squared test doesn't tell you how strongly the variables are associated. Use effect size measures like Cramér's V = $\sqrt{\chi^2/(n\cdot\min(r-1,c-1))}$ to quantify association strength (0=none, 1=perfect).
📖 Hogg, McKean & Craig — Ch. 6

❌ Error 1

Wrong degrees of freedom for goodness-of-fit test
✅ df = k-1 (number of categories minus 1), not k. One df is lost to the constraint $\sum O_i = \sum E_i = n$. If parameters are estimated from data (e.g., fitting a Poisson), subtract additional df: df = k - 1 - (number of estimated parameters).
🔍 Students use df=k instead of df=k-1, looking up wrong critical value.

❌ Error 2

Using frequencies instead of counts (proportions in O_i)
✅ Both $O_i$ and $E_i$ must be COUNTS (raw numbers), not proportions or percentages. Using proportions gives a chi-squared statistic that's off by a factor of n. Always ensure $\sum O_i = n$ (total sample size).
🔍 Students enter percentage data like O_i=0.35 instead of actual count O_i=35.

❌ Error 3

Wrong df for independence test: using (r-1)+(c-1) instead of (r-1)(c-1)
✅ For an r×c contingency table: df = (r-1)(c-1), NOT (r-1)+(c-1). Example: 3×4 table has df=(2)(3)=6, not 2+3=5. The multiplicative formula reflects that we lose one df per row margin and one per column margin, and their interaction.
🔍 Students add instead of multiply: "(3-1)+(4-1)=5" instead of "(3-1)(4-1)=6".

Casella & Berger — Statistical Inference, Ch. 10
Agresti — Categorical Data Analysis, Wiley, 3rd ed. 2013
Hogg, McKean & Craig — Introduction to Mathematical Statistics, Ch. 6