Skip to content

How to Perform Levene’s Test of Equal Variance in Python

How to Perform Levene's Test of Equal Variance in Python Cover Image

Levene’s test is an important statistical test used to assess the equality of variance between different samples. In this tutorial, you’ll learn how to understand and compute Levene’s test of equal variance in Python, using the important scipy package. Since many statistical tests work under the assumption that groups have equal variances, we can use Levene’s test to determine if this assumption is satisfied.

By the end of this tutorial, you’ll have learned the following:

  • How to understand Levene’s test of equal variance
  • How to use SciPy to conduct Levene’s test in Python
  • How to understand the results of conducting Levene’s test in Python

Quick Answer: Levene’s Test in Python

How to conduct Levene’s test in Python

  1. Import required libraries

    Import the SciPy library for running Levene’s test. You may need to install it using either pip or conda, first:
    from scipy import stats
    import numpy as np

  2. Load your data

    If using your own data, skip this step. If you want to use custom data, use the code below:
    np.random.seed(42) # Set seed for reproducibility
    group1 = np.random.normal(loc=20, scale=5, size=50)
    group2 = np.random.normal(loc=22, scale=5, size=50)

  3. Perform Levene’s test

    Use scipy.stats.levene() to conduct Levene’s test on the generated data. Pass the datasets as arguments to the function.
    levene_stat, p_value = stats.levene(group1, group2)

  4. Interpret the results

    Evaluate the obtained test statistic and p-value to draw conclusions regarding the equality of variances among the groups. Use a predefined significance level (e.g., 0.05) to determine significance.
    if p_value < 0.05:
    print("Variances are significantly different.")
    else:
    print("Variances are likely similar.")

Understanding Levene’s Test of Equal Variance

Many statistical tests make the assumption that your data is normally distributed and has equal variance. Python makes it simple by providing many different ways to test for normality in your data. But what about testing for equal variances? That’s where Levene’s test of equal variances comes into play.

As the name implies, the test is used to test whether or not two or more groups have equal variances. It works by comparing the average differences between the scores in each group with the overall average score across all groups.

If the differences between group scores are pretty consistent and similar, Levene’s test will suggest that the groups have roughly equal variability. However, if the differences between group scores are significantly different, it indicates that the variability (how spread out the scores are) varies more between the groups. This might imply that something other than chance is influencing the differences between the groups’ scores.

The formula for Levene’s test is shown below:

Where:

  • W is the test statistic.
  • N is the total number of observations.
  • k is the number of groups.
  • ni​ is the number of observations in the i-th group.
  • Zˉi.​ is the mean of group i.
  • ˉ..Zˉ..​ is the overall mean of all observations.
  • Zij​ is the j-th observation in the i-th group.

Now that you have a good understanding of what Levene’s test is, let’s explore how to do it in Python.

How to Conduct Levene’s Test in Python

In Python, we can use the powerful SciPy package. The package was specifically built to handle complex mathematical calculations. In particular, we can use the levene function from the stats package.

Let’s first create a bit of random data that is modeled after the normal distribution. For this, we can use NumPy to create normally distributed data.

# Creating Normally Distributed Data with NumPy
import numpy as np
from scipy import stats

np.random.seed(42)  
group1 = np.random.normal(loc=20, scale=5, size=50)
group2 = np.random.normal(loc=22, scale=5, size=50)

In the code block above, we created two datasets with different means (the loc= parameter) and the same standard deviation (scale=). We can now use SciPy to run Levene’s test. Let’s take a look at what this looks like:

# Performing Levene's Test in Python
levene_stat, p_value = stats.levene(group1, group2)

print("Levene's test statistic:", levene_stat)
print("p-value:", p_value)

# Returns:
# Levene's test statistic: 0.2782715593829571
# p-value: 0.599028616451668

We can see that the function returns two values, a test statistic and a p-value. Let’s take a look at how we can interpret these in the next section.

How to Interpret Levene’s Test in Python

In the previous section, you learned how to perform Levene’s test in Python. Now, let’s take a look at how you can interpret the returned test statistic and p-value. The p-value tells us whether or not we have enough evidence to reject the idea that the variances of the groups are similar.

Typically, you might use a significance level of 0.05. Since our returned p-value of 0.599 is greater than that significance level, we find that there isn’t strong enough evidence to conclude that the variances differ significantly. Explained more plainly, this indicates that the variability among the groups is likely similar.

Inversely, if the p-value is less than your chosen level of significance, you might conclude that there’s enough evidence to say that the variances are significantly different.

We can code this into a Python conditional statement to make the result easier to understand:

# Writing a Conditional Statement to Interpret Levene's Test
if p_value < 0.05:
    print("There is enough evidence to suggest that the variances are significantly different.")
else:
    print("There is not enough evidence to suggest that the variances are significantly different. They are likely similar.")

This can be helpful to better understand how the results can be interpreted, especially if you are sharing a notebook with others.

Customizing Levene’s Test in Python

The typical implementation of Levene’s test doesn’t assume how your data are distributed. Depending on your data’s distribution, you can modify how the function calculates the center of the data.

Three variations of Levene’s test are possible. The possibilities and their recommended usages are:

  • ‘median’ : Recommended for skewed (non-normal) distributions
  • ‘mean’ : Recommended for symmetric, moderate-tailed distributions.
  • ‘trimmed’ : Recommended for heavy-tailed distributions.

By default, the function will use 'median', but can be customized depending on your distribution. In our previous example, this didn’t matter since our data was normally distributed.

It’s important to keep in mind the nuances of your data to ensure that the center is appropriately calculated. Because the median is less sensitive to outliers, it’s a great default. However, you may want to customize the behavior depending on your specific use case.

Conclusion

Understanding and implementing Levene’s test in Python is a valuable skill in statistical analysis, ensuring the validity of assumptions regarding variance equality among groups. This tutorial provided a comprehensive overview, from the fundamental principles behind Levene’s test to practical application using Python’s scipy library.

With a focus on interpreting results and conveying them through Python’s conditional statements, you’ve gained insights into assessing variance differences and making informed decisions regarding statistical analyses based on these findings. Moreover, considering the distribution of your data and customizing the test’s center calculation method enhances the accuracy of variance comparisons across diverse datasets.

For a deeper dive into this statistical tool and its functionalities, exploring the official documentation for the scipy.stats.levene() function will further enrich your understanding and application of Levene’s test in various analytical contexts. Incorporating this method into your analytical repertoire ensures robust and accurate assessments of variance equality, bolstering the reliability of subsequent statistical inferences.

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *