Levene's Test for Homogeneity of Variances

Hypothesis TestingParametric TestsFree Lesson

Advertisement

Levene's Test

Levene's test checks whether k groups have equal population variances (homoscedasticity). Unlike the F-test, it doesn't assume normality.

W=(Nk)(k1)i=1kni(Zˉi.Zˉ..)2i=1kj=1ni(ZijZˉi.)2W = \frac{(N-k)}{(k-1)} \cdot \frac{\sum_{i=1}^k n_i (\bar{Z}_{i.} - \bar{Z}_{..})^2}{\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - \bar{Z}_{i.})^2}

where Zij=YijYˉi.Z_{ij} = |Y_{ij} - \bar{Y}_{i.}| (absolute deviations from group means).

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)")

# 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
Levene'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 in ANOVA
print("
If Levene's is significant (p < 0.05):")
print("→ Use Welch's ANOVA (one-way): stats.f_oneway is not appropriate")
print("→ Or use Kruskal-Wallis nonparametric test")

Key Takeaways

  1. Levene's test uses absolute deviations, making it robust to non-normality
  2. Brown-Forsythe uses median deviations — even more robust
  3. p > 0.05 → fail to reject equal variances → proceed with standard ANOVA
  4. p < 0.05 → use Welch's ANOVA or nonparametric alternative
  5. ANOVA is fairly robust to moderate variance inequality when group sizes are equal

Advertisement

Need Expert Statistics Help?

Get personalized tutoring, dissertation support, or statistical consulting.

Advertisement