Levene's Test for Equality of Variances Explained (with Python Examples) (2024)

In this tutorial we will explore the Levene’s test for equality of variances and its application in Python.

[the_ad id=”3031″]

Table of Contents

  • Introduction
  • Sample data
  • Levene’s test explained
    • Levene’s test hypotheses
    • Levene’s test statistic
  • Levene’s test example in Python
  • Conclusion


A lot of statistical tests and procedures assume normality of the data and equal variances.

These conditions often constitute whether a researcher can use a parametric or non-parametric test, formulate their hypotheses in certain ways, and much more.

Levene’s test is one of the popular tests in inferential statistics that addresses the data drawn from non-normal distribution.

If the data follows a normal distribution, please consider using the Bartlett’s test.

What is Levene’s test?

Levene’s test is used to test for equality of variances of a variable calculated for two or more groups (samples).

How do you interpret Levene’s test?

If the p-value of Levene’s test is less than the significance level (for example 0.05), the variances of at least two groups are not equal.

To continue following this tutorial we will need the following Python libraries: pandas and scipy.

If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:

pip install pandaspip install scipy

Sample data

To perform the calculations mentioned in this tutorial in the example section and the Python implementation section, you will need some data to work with.

Throughout the examples in this tutorial, the data from the .csv file available below is used.


The data contains 80 observations of reactions to new treatment across three groups: ‘control’, ‘treatment1’, and ‘treatment2’.

The data for afterfollows a non-normal distribution.

Levene’s test explained

As mentioned earlier, the assumption of equality of variances is important in statistical analysis and often shapes the researcher’s procedures when working on measuring the outcomes of experiments and data analysis.

Levene’s test hypotheses

The null hypothesis of Levene’s test is that all groups have equal variances.

The alternative hypothesis of Levene’s test is that at least one pair of groups has unequal variances.

Given a variable \(Y\) of the size \(N\), which is split into \(k\) groups:

$$H_0 : \sigma_1^2 = \sigma_2^2 = … = \sigma_k^2$$

$$H_1 : \sigma_i^2 \neq \sigma_j^2$$


  • \(k\) : total number of groups (\(\geq\)2)
  • \(i\) : one of the \(k\) groups
  • \(j\) : one of the \(k\) groups
  • \((i, j)\) : a pair of groups from \(k\) groups
  • \(i \neq j\) : two groups are not the same group

Levene’s test statistic

The Levene’s test statistic is given by:

$$W = \frac{N-k}{k-1} \times \frac{\sum_{i=1}^k n_i \times (\bar{Z}_{i.} – \bar{Z}_{..})^2}{\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} – \bar{Z}_{i.})}$$


  • \(N\) : total number of observations
  • \(n_i\) : number of observations in \(i\)-th group
  • \(k\) : number of groups
  • \(Z_{ij}\) : one of the three forms below:
    • \(Z_{ij} = |Y_{ij} – \bar{Y}_{i.}|\)
    • \(Z_{ij} = |Y_{ij} – \tilde{Y}_{i.}|\)
    • \(Z_{ij} = |Y_{ij} – \bar{Y}_{i.}’|\)
      • \({Y}_{ij}\) : the \(j\)-th observation of the \(i\)-th group
      • \(\bar{Y}_{i.}\) : mean of \(i\)-th group
      • \(\tilde{Y}_{i.}\) : median of \(i\)-th group
      • \(\bar{Y}_{i.}’\) : 10% trimmed mean of \(i\)-th group
  • \(\bar{Z}_{i.}\) : the mean of \(Z_{ij}\) of the \(i\)-th group
  • \(\bar{Z}_{..}\) : the mean of all \(Z_{ij}\)

The original paper only proposes the use of the mean calculation (\(\bar{Y}_{i.}\)) of the \(Z_{ij}\) values, and further extensions of the Lavene’s test propose the use of median (\(\tilde{Y}_{i.}\)) and trimmed mean (\(\bar{Y}_{i.}’\)) when calculating the \(Z_{ij}\) values.

After the Levene’s test statistic (\(W\)) is calculated, it should be compared against the upper critical value given by:

$$F_{\alpha, k-1, N-k}$$

Therefore, we reject the null hypotheses of equal variances when:

$$W > F_{\alpha, k-1, N-k}$$

Levene’s test example in Python

In order to see Levene’s test in practice and its application in Python, we will use the sample data file mentioned in one of the previous sections.

First, import the required dependencies:

import pandas as pdfrom scipy.stats import levene

Then read the .csv file provided into a Pandas DataFrame and print first few rows:

df = pd.read_csv('data_levenes_test.csv')print(df.head())

And you should get:

 group before_treatment after_treatment0 control 27.9 33.81 control 16.8 9.32 control 27.2 23.43 control 12.5 19.94 control 14.4 16.0

You can compute some group summary statistics to understand the data better:

df_agg = ( df.groupby("group") .agg( avg_bef_treat=("before_treatment", "mean"), var_bef_treat=("before_treatment", "var"), avg_aft_treat=("after_treatment", "mean"), var_aft_treat=("after_treatment", "var"), ) .reset_index())print(df_agg)

And you should get:

 group avg_bef_tr var_bef_tr avg_aft_tr var_aft_tr0 control 20.145 18.878436 19.825 28.8255131 treatment1 19.210 17.007263 15.475 4.6493422 treatment2 21.510 19.673579 20.315 15.141458

Here you can clearly see the difference between “var_bef_tr” (variance before treatment) across 3 groups is not that different: 18.88, 17.01, 19.67.

However the difference between “var_aft_tr” (variance after treatment) across 3 groups is quite different: 28.83, 4.65, 15.14.

The difference in variance after treatment across three groups is large enough for us to be almost sure that it is significantly different, but to check it statistically we will perform the Levene’s test in Python!

We will need to create the variables which will store observations relevant to a particular group:

control_group = df[df['group']=='control']['after_treatment']treatment1_group = df[df['group']=='treatment1']['after_treatment']treatment2_group = df[df['group']=='treatment2']['after_treatment']

Finally perform the Levene’s test in Python:

stat, p_value = levene(control_group, treatment1_group, treatment2_group)print(f"Lavene's test statistic: {stat}")print(f"P-value: {p_value}")

You should get:

Lavene's test statistic: 4.240957881271611P-value: 0.017895111992486838

Since the p-value is less than 0.05, we reject the null hypothesis and conclude that at least one pair of groups has unequal variances.


By default, the levene() function computes the absolute residuals using the median method. However, as mentioned in the explanation section, you can also use mean and trimmed mean. The official documentation shows how to change the function used in the test.


In this article we discussed how to perform the Levene’s test for equality of variances and its application in Python using scipy library.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Statistics articles.

Levene's Test for Equality of Variances Explained (with Python Examples) (2024)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6015

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.