How to Perform Bartlettís Test in Python?

Many statistical tests and procedures assume that data is normally distributed and has equal variances. Bartlett's test is a statistical hypothesis test that determines whether two or more samples have equal variances. This test is essential for validating assumptions before applying parametric statistical methods like ANOVA.

What is Bartlett's Test?

Bartlett's test examines whether samples from different groups have statistically equal variances (homoscedasticity). It tests the null hypothesis that all population variances are equal against the alternative hypothesis that at least one variance differs significantly.

The test is commonly used as a preliminary check before performing ANOVA. If variances are equal, standard ANOVA can be applied. If variances are unequal, alternative methods like Welch's ANOVA should be used instead.

Assumptions

  • Data follows a normal distribution

  • Samples are independent

  • Each sample contains at least 3 observations

Syntax

from scipy.stats import bartlett
stat, p_value = bartlett(*samples)

Parameters

*samples: Variable number of 1-dimensional arrays containing sample data. At least two samples are required.

Return Values

stat: Bartlett's test statistic (follows chi-square distribution)
p_value: The p-value for the hypothesis test

Example

Let's perform Bartlett's test on three different samples ?

from scipy.stats import bartlett
import numpy as np

# Create three sample datasets
group1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
group2 = [-0.208, 0.696, 0.928, -1.148, -0.213, 0.229, 0.137, 0.269, -0.870, -1.204]
group3 = [1.2, 1.8, 0.9, 1.1, 1.5, 0.8, 1.3, 1.0, 1.4, 1.6]

# Perform Bartlett's test
stat, p_value = bartlett(group1, group2, group3)

print(f"Test statistic: {stat:.4f}")
print(f"p-value: {p_value:.6f}")

# Interpret results at ? = 0.05
alpha = 0.05
if p_value > alpha:
    print("Fail to reject null hypothesis: Variances are equal")
else:
    print("Reject null hypothesis: Variances are significantly different")
    
# Calculate individual variances for comparison
print(f"\nVariances:")
print(f"Group 1: {np.var(group1, ddof=1):.4f}")
print(f"Group 2: {np.var(group2, ddof=1):.4f}")
print(f"Group 3: {np.var(group3, ddof=1):.4f}")
Test statistic: 3.7352
p-value: 0.154425
Fail to reject null hypothesis: Variances are equal

Variances:
Group 1: 1.8859
Group 2: 0.4806
Group 3: 0.0933

How It Works

Bartlett's test calculates a test statistic that follows a chi-square distribution with (k-1) degrees of freedom, where k is the number of groups. The test statistic is computed using the pooled variance and individual group variances ?

Bartlett's Test Process Calculate Sample Variances Compute Test Statistic Compare with Critical Value

When to Use Bartlett's Test

Use Bartlett's test when:

  • Data is normally distributed

  • You need to test homogeneity of variances

  • Preparing for ANOVA analysis

Consider alternatives when:

  • Data is non-normal (use Levene's test instead)

  • Sample sizes are very small

Conclusion

Bartlett's test is essential for validating equal variance assumptions in statistical analysis. Use it before ANOVA to ensure appropriate test selection, but remember it requires normally distributed data for reliable results.

Updated on: 2026-03-26T23:26:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements