Levene's Test
Hypothesis Testing
The Robust Alternative to the F-Test
Levene's test checks for equal variances across groups without assuming normality, making it more reliable than the F-test in practice. It is the standard method for verifying homoscedasticity before ANOVA.
- ANOVA Preparation β Checking assumptions before running analysis of variance
- Quality Engineering β Assessing process stability across multiple production lines
- Psychology Research β Verifying variance homogeneity in experimental designs
When normality is uncertain, Levene's test provides reliable variance checking.
Levene's test checks whether k groups have equal population variances (homoscedasticity). Unlike the F-test, it doesn't assume normality.
Levene's Test Statistic
Here,
- =Levene's test statistic
- =Total number of observations
- =Number of groups
- =Number of observations in group i
- =|Y_{ij} β Θ²_{i.}| (absolute deviations from group means)
Python Implementation
Levene's Test in Python
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
np.random.seed(42)
# Compare 3 groups for ANOVA β check variance assumption first
group_a = np.random.normal(50, 5, 30)
group_b = np.random.normal(55, 5, 28)
group_c = np.random.normal(48, 8, 32) # larger variance
# Levene's test
stat, p = stats.levene(group_a, group_b, group_c)
print(f"Levene's Test for 3 Groups:")
print(f"W = {stat:.4f}, p = {p:.4f}")
print(f"Groups have {'equal' if p > 0.05 else 'UNEQUAL'} variances (Ξ±=0.05)")
Visualization
# Visualize variance differences
fig, ax = plt.subplots(figsize=(8, 5))
ax.boxplot([group_a, group_b, group_c], labels=['Group A', 'Group B', 'Group C'],
patch_artist=True)
ax.set_title(f"Group Variance Comparison\nLevene's: W={stat:.3f}, p={p:.4f}")
ax.set_ylabel('Values')
for i, (g, label) in enumerate(zip([group_a, group_b, group_c], ['A','B','C']), 1):
ax.text(i, g.max()+0.5, f'SD={g.std(ddof=1):.2f}', ha='center', fontsize=9)
plt.tight_layout()
plt.savefig('levenes_test.png', dpi=150)
plt.show()
What To Do If Variances Are Unequal
If Levene's is Significant (p < 0.05)
- Use Welch's ANOVA (one-way): stats.f_oneway is not appropriate
- Or use Kruskal-Wallis nonparametric test
Key Takeaways
Summary: Levene's Test
- Levene's test uses absolute deviations, making it robust to non-normality
- Brown-Forsythe uses median deviations β even more robust
- p greater than 0.05 -> fail to reject equal variances -> proceed with standard ANOVA
- p less than 0.05 -> use Welch's ANOVA or nonparametric alternative
- ANOVA is fairly robust to moderate variance inequality when group sizes are equal