🧠 Introduction
In real-world statistics, we often want to know whether our observed data follow a theoretical or expected pattern.
For example:
- Does a die give all faces with equal probability?
- Are colors chosen by customers equally preferred?
- Do the grades of students follow a normal or expected distribution?
To answer such questions, statisticians use the Chi-Squared Goodness of Fit Test, one of the most important non-parametric hypothesis tests.
📘 What is the Chi-Squared Goodness of Fit Test?
The Goodness of Fit test checks whether the observed frequency distribution of a categorical variable matches an expected (theoretical) frequency distribution.
It’s based on the Chi-Squared (χ²) distribution and measures the discrepancy between what you observe and what you expect.
🧮 Formula
χ2=∑(Oi−Ei)2Ei\chi^2 = \sum \frac{(O_i – E_i)^2}{E_i}χ2=∑Ei(Oi−Ei)2
where:
- OiO_iOi: Observed frequency in category i
- EiE_iEi: Expected frequency in category i
The greater the difference between OiO_iOi and EiE_iEi, the larger the χ² statistic, indicating a poorer fit to the expected model.
🎯 Hypotheses
- Null hypothesis (H₀): The observed data follow the expected distribution.
- Alternative hypothesis (H₁): The observed data do not follow the expected distribution.
⚙️ Steps to Perform the Test
- State hypotheses.
- Collect observed data (O).
- Determine expected frequencies (E).
- Based on a known or theoretical probability model.
- Compute the test statistic: χ2=∑(O−E)2E\chi^2 = \sum \frac{(O – E)^2}{E}χ2=∑E(O−E)2
- Find degrees of freedom: df=k−1−mdf = k – 1 – mdf=k−1−m where:
- k = number of categories
- m = number of estimated parameters (often 0 for simple models)
- Find the p-value or compare χ² with the critical value from the Chi-Squared table.
- Make a decision:
- If χcalc2>χcrit2\chi^2_{calc} > \chi^2_{crit}χcalc2>χcrit2, reject H₀.
- Otherwise, fail to reject H₀.
📊 Example 1: Testing a Fair Die
A die is rolled 60 times, and results are recorded as:
| Face | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Observed (O) | 8 | 10 | 9 | 11 | 12 | 10 |
Step 1:
Expected frequency per face E=60/6=10E = 60 / 6 = 10E=60/6=10.
Step 2:
Compute (O−E)2/E(O – E)^2 / E(O−E)2/E:
| Face | O | E | (O−E)²/E |
|---|---|---|---|
| 1 | 8 | 10 | 0.4 |
| 2 | 10 | 10 | 0.0 |
| 3 | 9 | 10 | 0.1 |
| 4 | 11 | 10 | 0.1 |
| 5 | 12 | 10 | 0.4 |
| 6 | 10 | 10 | 0.0 |
| Total | 1.0 |
Step 3:
χ2=1.0\chi^2 = 1.0χ2=1.0 df=6−1=5df = 6 – 1 = 5df=6−1=5
At 5% significance level, χcrit2(5,0.05)=11.07\chi^2_{crit}(5, 0.05) = 11.07χcrit2(5,0.05)=11.07.
✅ Since 1.0 < 11.07 → Fail to reject H₀.
There is no significant difference — the die appears fair.
📈 Example 2: Unequal Expected Probabilities
Suppose a company claims the proportion of customers choosing four product colors are:
- Red: 40%
- Blue: 30%
- Green: 20%
- Yellow: 10%
Out of 200 customers, the observed data are:
| Color | Observed (O) | Expected % | Expected (E) | (O−E)²/E |
|---|---|---|---|---|
| Red | 90 | 40% | 80 | 1.25 |
| Blue | 60 | 30% | 60 | 0.00 |
| Green | 30 | 20% | 40 | 2.5 |
| Yellow | 20 | 10% | 20 | 0.00 |
| Total | 200 | 100% | 200 | 3.75 |
χ2=3.75,df=4−1=3\chi^2 = 3.75, \quad df = 4 – 1 = 3χ2=3.75,df=4−1=3
At α=0.05\alpha = 0.05α=0.05, critical χ² = 7.815.
✅ Since 3.75 < 7.815, we fail to reject H₀.
Observed distribution fits the expected model.
🧾 Assumptions
- Data are frequencies (not percentages or continuous values).
- Observations are independent.
- Expected frequency ≥ 5 in each category.
- Categorical data only.
If any expected frequency < 5, merge categories or use Fisher’s Exact Test.
💻 Chi-Squared Goodness of Fit in Python
import numpy as np
from scipy.stats import chisquare
Observed frequencies
observed = np.array([8, 10, 9, 11, 12, 10])
Expected frequencies
expected = np.array([10, 10, 10, 10, 10, 10])
Perform test
chi2, p = chisquare(observed, expected)
print(“Chi-Squared Statistic:”, round(chi2, 3))
print(“P-value:”, round(p, 4))
Output:
Chi-Squared Statistic: 1.0
P-value: 0.9616
✅ Since p > 0.05, we fail to reject H₀.
There is no significant difference between observed and expected frequencies.
⚠️ Common Mistakes
🚫 Using percentages instead of raw frequencies
🚫 Ignoring small expected frequencies (< 5)
🚫 Applying the test to continuous or correlated data
🚫 Misinterpreting p-values (small p-value → evidence against H₀)
📚 Real-World Applications
- Quality control: Checking if defect rates follow expected proportions.
- Elections: Do votes align with expected voter share?
- Manufacturing: Are production defects evenly distributed across machines?
- Marketing: Are customer color or product preferences as predicted?
- Education: Do grade distributions match the expected curve?
🧭 Final Thoughts
The Chi-Squared Goodness of Fit Test provides a powerful, simple way to test how well data match a theoretical expectation.
It’s a cornerstone of inferential statistics, especially when analyzing categorical or frequency data.
For students and analysts, understanding this test not only strengthens your grasp of hypothesis testing but also builds a foundation for more advanced tools like Chi-Squared Tests of Independence, Regression Residual Analysis, and Model Fit Diagnostics.


