Chi-square tests: goodness of fit, independence and the chi-square distribution
Ten problems on chi-square tests: the chi-square distribution as a sum of squared standard normals with its mean, variance and the 5% cutoff for two degrees of freedom, a coin whose chi-square statistic is the square of its z score, a goodness-of-fit test of a die rolled 120 times, a classifier's predicted label distribution against a target mix, a Poisson fit to hourly error counts with an estimated rate and a merged tail cell, a 2 × 2 A/B table whose chi-square equals the two-proportion z squared, a 3 × 2 table of model versions and user ratings, its Pearson residuals and Cramér's V, how the statistic grows with the sample size when the proportions stay fixed, and the G-test as twice N times a KL divergence, with worked solutions and the mistakes of running the test on percentages instead of counts, forgetting to subtract a degree of freedom for an estimated parameter, using rc − 1 degrees of freedom for a contingency table, dividing by the observed counts instead of the expected ones and reading a large p-value as proof that the model fits.
Before you start
A chi-square test asks whether counts in categories are close enough to the counts a model predicts. The model might say a die is fair, a classifier should predict labels in a target mix, server errors arrive as a Poisson process, or two variables in a table are independent. For each category, the gap between the observed count O and the expected count E is squared and divided by ,E, and the sum of these terms is compared with a chi-square distribution, the distribution of a sum of squared standard normals. The number of squared normals, the degrees of freedom, is the number of categories minus the constraints the expected counts were made to satisfy. These ten problems build the distribution, run goodness-of-fit and independence tests by hand, connect the 2×2 test to the z test of an A/B experiment and the G-test to the KL divergence, and show what the statistic does as the sample grows.
The distribution. If Z1,…,Zk are independent standard normals, Z12+⋯+Zk2 has a chi-square distribution with k degrees of freedom, .χk2. Its mean is k and its variance is .2k. For k=2 the upper tail is exactly .Pr(χ22>x)=e−x/2.
Cutoffs at the 5% level.3.841 for 1 degree of freedom, 5.991 for ,2,7.815 for 3 and 11.07 for .5. The test is one-sided: only large values of the statistic count against .H0.
Pearson's statistic.,X2=∑(O−E)2/E, summed over all categories, with O and E as counts, never percentages. The expected count of a category is N times its probability under .H0.
Goodness of fit. With k categories, ,df=k−1−m, where m is the number of parameters estimated from the same data to compute the expected counts. The −1 is there because the expected counts must add up to .N.
Independence. For an r×c table, E=(row total)(column total)/N and .df=(r−1)(c−1).
When the approximation holds. The chi-square distribution is a large-sample approximation. A common rule asks for every expected count to be at least ;5; merge sparse categories, chosen before looking at the data, to get there.
Effect size and residuals. Cramér's V=X2/(N(min(r,c)−1)) measures the strength of association on a 0 to 1 scale, whatever N is. The Pearson residual (O−E)/E shows which cells drive the statistic; their squares add up to .X2.
G-test.G=2∑Oln(O/E) is the likelihood-ratio statistic. It is close to X2 when the counts are large, and it has the same degrees of freedom.
Let ,Z1,,Z2,Z3 be independent standard normals and .Q=Z12+Z22+Z32. Find E[Q] and .Var(Q). Then, using ,Pr(χ22>x)=e−x/2, find the 5% cutoff for 2 degrees of freedom.
·
A coin lands heads 60 times in 100 flips. Compute Pearson's statistic for H0:p=21 with the two categories heads and tails, compare it with the z statistic for the same test, and find the p-value.
··
A die is rolled 120 times, with these counts:
facecount125217315423524616
Test whether the die is fair at the 5% level.
··
A classifier should predict the labels cat, dog and bird in the mix ,50%,30% and .20%. On 1000 inputs it predicts cat 450 times, dog 350 times and bird 200 times. Test whether its predicted mix matches the target, at the 5% level.
···
A service logs its errors per hour for 200 hours: 80 hours had 0 errors, 64 had ,1,36 had ,2,16 had 3 and 4 had ,4, with none higher. Test whether the counts follow a Poisson distribution, estimating its rate from the data, and merging the categories 3 and 4 or more so that every expected count is at least .5.
··
In an A/B test, 200 of 2000 users convert with the old checkout and 240 of 2000 with the new one. Arrange the data as a 2×2 table, run the chi-square test of independence and compare the statistic with the pooled two-proportion z statistic, .z≈2.021.
··
Three model versions are each shown to 100 users, who give a thumbs up or a thumbs down:
v1v2v3up607545down402555
Test whether the rating is independent of the version, at the 5% level.
··
For the table of Problem 7, find the Pearson residuals (O−E)/E of every cell and Cramér's .V. Which cells drive the result?
··
Keep the classifier's predicted proportions of Problem 4, ,45%,35% and ,20%, against the target ,50%,,30%,,20%, but vary the number of inputs .N. Write X2 as a function of ,N, find the smallest N that gives significance at ,5%, and find X2 at .N=100,000.
···
For the classifier of Problem 4, compute the G-statistic ,G=2∑Oln(O/E), show that it equals 2N times the KL divergence from the target proportions to the observed ones, and compare it with Pearson's .X2≈13.33.
Answers
,E[Q]=3,,Var(Q)=6, and the 5% cutoff for χ22 is 2ln20≈5.991
,X2=4=z2,,df=1,p=Pr(χ12>4)≈0.0455
X2=5<11.07 and :p≈0.416: no evidence that the die is unfair
,X2≈13.33>5.991,:p≈0.0013: the predicted mix differs from the target
X2≈2.79 with ,df=4−1−1=2, so :p≈0.248: the Poisson model is not rejected
,X2≈4.086,,df=1,;p≈0.043; and z2=2.0212≈4.086
X2=18.75 with ,df=(3−1)(2−1)=2, so :p=e−9.375≈8.5×10−5: the rating depends on the version
Residuals ;0,0;;1.94,−2.37;,−1.94,2.37, and V=18.75/300=0.25
;X2=0.01333N; the smallest significant N is ;450; at ,N=100,000,X2≈1333
,G≈13.08=2000⋅0.006541, close to ,X2≈13.33, with p=e−G/2≈0.0014
Worked solutions
Problem 1
Let ,Z1,,Z2,Z3 be independent standard normals and .Q=Z12+Z22+Z32. Find E[Q] and .Var(Q). Then, using ,Pr(χ22>x)=e−x/2, find the 5% cutoff for 2 degrees of freedom.
E[Z2]=Var(Z)+(E[Z])2=1A standard normal has mean 0 and variance .1.
,E[Z4]=3, so Var(Z2)=3−12=2The fourth moment of a standard normal is .3.
E[Q]=3 and Var(Q)=3⋅2=6Means add, and so do variances of independent terms.
,e−x/2=0.05, so x=2ln20Set the upper tail equal to 0.05 and take logs.
,E[Q]=3,,Var(Q)=6, and the 5% cutoff for χ22 is 2ln20≈5.991In general χk2 has mean k and variance ,2k, so a statistic close to its degrees of freedom is unremarkable.
Problem 2
A coin lands heads 60 times in 100 flips. Compute Pearson's statistic for H0:p=21 with the two categories heads and tails, compare it with the z statistic for the same test, and find the p-value.
E=50 for heads and 50 for tailsNp and .N(1−p).
X2=50102+50(−10)2=4Both gaps are ,10, in opposite directions.
z=100⋅0.2560−50=2The binomial has standard deviation .Np(1−p)=5.
,X2=z2, with df=2−1=1With two categories the two gaps are tied, so there is one squared normal, not two.
,X2=4=z2,,df=1,p=Pr(χ12>4)≈0.0455The same p-value as the two-sided z test, and the cutoff 3.841 is .1.962.
Problem 3
A die is rolled 120 times, with these counts:
facecount125217315423524616
Test whether the die is fair at the 5% level.
E=120/6=20 for every faceUnder H0 each face has probability .61.
O−E=5,−3,−5,3,4,−4The gaps add up to ,0, as they always must.
∑(O−E)2=25+9+25+9+16+16=100Square every gap.
,X2=100/20=5, with df=6−1=5All the expected counts are equal, so divide once.
X2=5<11.07 and :p≈0.416: no evidence that the die is unfairThe statistic is right at its mean of .5. That is not evidence that the die is fair, only that 120 rolls cannot tell (Mistake 5).
Problem 4
A classifier should predict the labels cat, dog and bird in the mix ,50%,30% and .20%. On 1000 inputs it predicts cat 450 times, dog 350 times and bird 200 times. Test whether its predicted mix matches the target, at the 5% level.
E=500,300,2001000 times each target proportion.
500(−50)2=5 and 300502≈8.333Cat and dog; bird matches exactly and adds .0.
X2=5+8.333+0≈13.33The same gap of 50 counts for more in the smaller category.
,df=3−1=2, and p=e−13.33/2≈0.0013The exact tail for 2 degrees of freedom.
,X2≈13.33>5.991,:p≈0.0013: the predicted mix differs from the targetThe classifier over-predicts dog. Running the test on the percentages 45,35,20 instead gives X2≈1.33 and the opposite conclusion (Mistake 1).
Problem 5
A service logs its errors per hour for 200 hours: 80 hours had 0 errors, 64 had ,1,36 had ,2,16 had 3 and 4 had ,4, with none higher. Test whether the counts follow a Poisson distribution, estimating its rate from the data, and merging the categories 3 and 4 or more so that every expected count is at least .5.
λ^=(64+72+48+16)/200=1The maximum-likelihood rate is the sample mean: total errors over hours.
E=200e−1(1,1,21)≈73.58,73.58,36.79200Pr(K=k) for ,k=0,1,2, with .Pr(K=k)=e−λλk/k!.
,E3+=200−183.94≈16.06, with O3+=20The cell 4 or more alone would expect only ,3.80, so it is merged with .3.
X2≈0.561+1.246+0.017+0.966≈2.79The four terms .(O−E)2/E.
X2≈2.79 with ,df=4−1−1=2, so :p≈0.248: the Poisson model is not rejectedOne degree of freedom goes to the total and one to the estimated .λ. Forgetting the second gives p≈0.425 (Mistake 2).
Problem 6
In an A/B test, 200 of 2000 users convert with the old checkout and 240 of 2000 with the new one. Arrange the data as a 2×2 table, run the chi-square test of independence and compare the statistic with the pooled two-proportion z statistic, .z≈2.021.
Rows old, new; columns converted, not: 200,1800 and 240,1760Row totals 2000 and ;2000; column totals 440 and .3560.
E=2000⋅440/4000=220 converted per row, 1780 notRow total times column total over .N.
∣O−E∣=20 in every cellIn a 2×2 table all four gaps have the same size.
X2=400(2202+17802)≈3.636+0.449Two cells divide by 220 and two by .1780.
,X2≈4.086,,df=1,;p≈0.043; and z2=2.0212≈4.086The chi-square test of a 2×2 table is exactly the two-sided pooled z test, squared, and gives the same p-value. Using rc−1=3 degrees of freedom gives p≈0.25 (Mistake 3).
Problem 7
Three model versions are each shown to 100 users, who give a thumbs up or a thumbs down:
v1v2v3up607545down402555
Test whether the rating is independent of the version, at the 5% level.
Column totals 180 up and 120 down, N=300Each row total is .100.
E=100⋅180/300=60 up and 40 down in every rowUnder independence each version gets the overall 60% rate.
v1 adds ;0; v2 and v3 each add 60152+40152v2 is 15 above in up, v3 15 below.
X2=2(3.75+5.625)=18.75225/60=3.75 and .225/40=5.625.
X2=18.75 with ,df=(3−1)(2−1)=2, so :p=e−9.375≈8.5×10−5: the rating depends on the versionFar beyond the cutoff .5.991.
Problem 8
For the table of Problem 7, find the Pearson residuals (O−E)/E of every cell and Cramér's .V. Which cells drive the result?
v1: 0 and 0The first version matches the overall rate exactly.
v2: 15/60≈1.94 up, −15/40≈−2.37 downPositive residuals mark more counts than independence predicts.
v3: −1.94 up, +2.37 downThe mirror image of v2.
1.942⋅2+2.372⋅2=7.5+11.25=18.75The squared residuals add up to .X2.
Residuals ;0,0;;1.94,−2.37;,−1.94,2.37, and V=18.75/300=0.25The min(r,c)−1 is 1 here. v2 is liked and v3 is disliked more than chance allows, and the down cells weigh more because their expected counts are smaller.
Problem 9
Keep the classifier's predicted proportions of Problem 4, ,45%,35% and ,20%, against the target ,50%,,30%,,20%, but vary the number of inputs .N. Write X2 as a function of ,N, find the smallest N that gives significance at ,5%, and find X2 at .N=100,000.
X2=∑Np(Np^−Np)2=N∑p(p^−p)2Every count is N times a proportion.
∑p(p^−p)2=0.50.0025+0.30.0025≈0.01333The bird term is .0.
0.01333N>5.991 when N>449.3The cutoff for 2 degrees of freedom.
N=450 gives ;p≈0.0498;N=449 gives p≈0.0501The same mismatch flips from not significant to significant.
;X2=0.01333N; the smallest significant N is ;450; at ,N=100,000,X2≈1333X2 grows like N while the effect size w=X2/N≈0.115 stays put. With enough data any mismatch, however small, is significant, so report the size of the effect, not only the p-value.
Problem 10
For the classifier of Problem 4, compute the G-statistic ,G=2∑Oln(O/E), show that it equals 2N times the KL divergence from the target proportions to the observed ones, and compare it with Pearson's .X2≈13.33.
KL(p^∥p)=∑p^ln(p^/p)≈0.006541Each term of G divided by ,2N, since O=Np^ and .O/E=p^/p.
,G≈13.08=2000⋅0.006541, close to ,X2≈13.33, with p=e−G/2≈0.0014G=2NKL(p^∥p) exactly, and X2 is its second-order approximation, so the two agree when the observed and expected proportions are close.
Where this goes wrong
1. Percentages instead of counts
The predicted mix is reported as percentages, and percentages look like the natural thing to compare.
Observed 45,35,20 and expected ,50,30,20, in percentRight so far as a description of Problem 4.
“.X2=5025+3025+0.”The statistic needs counts. Rescaling every count by c rescales X2 by ,c, so percentages behave as if N were .100.
,X2≈1.33,:p≈0.51: the classifier matches the targetThe counts out of 1000 give X2≈13.33 and .p≈0.0013. The percentages threw away a factor of 10 in the sample size.
2. Not subtracting a degree of freedom for an estimated parameter
The Poisson test of Problem 5 has four categories, and the rule "categories minus one" is remembered on its own.
X2≈2.79 over 4 categoriesRight so far: Problem 5.
“.df=4−1=3.”The rate λ^=1 was estimated from the same counts, which pulls the expected counts toward the observed ones and removes one more degree of freedom.
p=Pr(χ32>2.79)≈0.425The right reference is ,χ22, with p≈0.248 and cutoff 5.991 instead of .7.815. Too many degrees of freedom makes every fit look better than it is.
3. rc − 1 degrees of freedom for a contingency table
A 2×2 table has four cells, so four minus one looks like the count of free cells.
X2≈4.086 for the A/B tableRight so far: Problem 6.
“.df=4−1=3.”Under independence the expected counts must match both the row totals and the column totals. Once one cell of a 2×2 table is fixed with all totals known, the other three follow, so .df=(2−1)(2−1)=1.
,p=Pr(χ32>4.086)≈0.25, not significantAgainst the cutoff 7.815 the result vanishes. With 1 degree of freedom the cutoff is 3.841 and ,p≈0.043, the same as the z test.
4. Dividing by the observed counts
The formula is remembered as "squared difference over the count", and the observed count is the one on the page.
O−E=5,−3,−5,3,4,−4 for the dieRight so far: Problem 3.
“.X2=∑(O−E)2/O.”Pearson's statistic divides by :E: under H0 a count has variance about ,E, so (O−E)/E is roughly a standard normal. Dividing by O gives a different statistic, Neyman's, which only agrees with Pearson's when every count is large, and is infinite whenever a category has no observations.
X2=2525+179+1525+239+2416+1616≈5.25The right value is .100/20=5. The gap is small here because the counts are near ;20; with a sparse category it can be huge.
5. A large p-value read as proof that the model fits
The die of Problem 3 passed its test easily, so it is declared fair.
,X2=5,p≈0.416Right so far: Problem 3.
“The test shows the die is fair.”A test can only fail to find a difference; whether it would have found one depends on its power.
The die is fairA die that shows 1 a fifth of the time, and each other face 16% of the time, gives 120 rolls a noncentrality of only ,120∑(p1−p0)2/p0=0.96, and the test rejects it only about 10% of the time. Ninety percent of the time such a loaded die passes. Keeping H0 means "not shown to be wrong", not "shown to be right".