Chi-Square Goodness-of-Fit Test
Hypothesis Testing
Does Your Data Match the Theory?
The chi-square goodness-of-fit test evaluates whether observed frequencies match expected theoretical frequencies. It is essential for validating distributional assumptions and testing genetic models.
- Genetics β Testing whether offspring ratios match Mendelian predictions
- Manufacturing β Verifying that product characteristics follow specified distributions
- Marketing β Analyzing whether customer preferences match expected market models
The goodness-of-fit test is the first step in validating any theoretical model.
Tests whether observed frequencies match a set of expected (theoretical) frequencies.
Worked Example: Are Die Rolls Fair?
Visualization
# Visualize
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
faces = [f'Face {i}' for i in range(1, 7)]
x = np.arange(6)
axes[0].bar(x - 0.2, observed, 0.4, label='Observed', color='steelblue', alpha=0.8)
axes[0].bar(x + 0.2, expected, 0.4, label='Expected', color='coral', alpha=0.8)
axes[0].set_xticks(x)
axes[0].set_xticklabels(faces)
axes[0].set_title('Observed vs Expected Frequencies')
axes[0].legend()
# Chi-square distribution
x_chi = np.linspace(0, 20, 500)
axes[1].plot(x_chi, stats.chi2.pdf(x_chi, df=df), 'b-', linewidth=2)
axes[1].fill_between(x_chi, stats.chi2.pdf(x_chi, df=df), where=x_chi >= chi2, alpha=0.4, color='red')
axes[1].axvline(chi2, color='red', linewidth=2, linestyle='--', label=f'ΟΒ²={chi2:.3f}')
axes[1].axvline(stats.chi2.ppf(0.95, df=df), color='black', linewidth=1.5, linestyle=':',
label=f'Critical value={stats.chi2.ppf(0.95,df=df):.3f}')
axes[1].set_title(f'ΟΒ²({df}) Distribution (p={p_value:.3f})')
axes[1].legend()
plt.tight_layout()
plt.savefig('chi_square_gof.png', dpi=150)
plt.show()