196. Chi-Square Goodness of Fit

Compares an observed categorical distribution to a theoretical categorical distribution.

Χ2=𝑖=1𝑘(𝑜𝑖𝑒𝑖)2𝑒𝑖df=𝑘1
Example

Your professor gave a hard test. He said 45% got A’s, 20% B’s, 15% C’s, 10% D’s and 10% F’s. You don’t believe him. You take a simple random sample of 60 students and find 20 got A’s, 15 B’s, 8 O’s, 12 D’s and 5 F’s. At 𝛼=0.05, test.

CategoryABCDFTotal
Expected % (𝑂𝑖)45%20%15%10%10%100%
Observed # (𝐸𝑖)2015812560
Expected #600.45=27600.20=12600.15=9600.10=6600.10=660
(𝑂𝐸)2𝐸1.8150.7500.1116.0000.167
  1. Hypothesis
𝐻0:𝑝𝐴=0.45,𝑝𝐵=0.20,𝑝𝐶=0.15,𝑝𝐷=0.10,𝑝𝐹=0.10(the professor's claimed distribution is correct)𝐻𝑎:at least one 𝑝𝑖differs from its claimed value
  1. Test Statistic & P-Value
𝜒2=𝑖=1𝑛(𝑜𝑖𝑒𝑖)2𝑒𝑖=𝑧𝐴2+𝑧𝐵2+𝑧𝐶2+𝑧𝐷2+𝑧𝐹2=(2027)227+(1512)212+(89)29+(126)26+(56)26=1.81+0.75+0.11+6.00+0.17=8.84𝑝-value=0.0652𝛼=0.05critical value=𝜒0.05,42=9.49𝜒2=8.84
  1. Decision

Since 𝜒2=8.84<9.49 (equivalently 𝑝=0.0652>𝛼=0.05), we fail to reject 𝐻0.

  1. Conclusion

At the 5% significance level there is not enough evidence to conclude that the professor’s claimed grade distribution differs from the true one.

Example

Complaints by Quarter (testing for a uniform distribution)

Q1Q2Q3Q4
𝑜𝑖43525440189

Under 𝐻0 all four quarters are equally likely, so 𝑒𝑖=𝑁/𝑘=189/4 for every 𝑖.

Q1Q2Q3Q4
𝑜𝑖43525440189
𝑒𝑖1894189418941894189
Q1Q2Q3Q4
𝑜𝑖43525440189
𝑒𝑖47.2547.2547.2547.25189
(4347.25)247.25(5247.25)247.25(5447.25)247.25(4047.25)247.25
0.380.480.961.11𝟐.𝟗𝟒
Χ2=0.38+0.48+0.96+1.11=2.94

df=𝑘1=3, and Χ0.05,32=7.815. Since 2.94<7.815 we fail to reject 𝐻0 (𝑝0.40): the complaints are consistent with a uniform distribution across quarters.

chi2_got.py
from scipy import stats
f_obs = np.array([43, 52, 54, 40])
f_exp = np.array([47, 47, 47, 47])
stats.chisquare(f_obs=f_obs, f_exp=f_exp)