Skip to Content

LEVENE

Overview

The LEVENE function performs Levene’s test for equality of variances across multiple sample groups. This test is robust to departures from normality and can compare more than two samples, making it preferable to parametric tests like the F-test when normality cannot be assumed. The test statistic is computed by measuring the absolute deviations from a central tendency (mean, median, or trimmed mean) and comparing the variances of these deviations across groups. The null hypothesis is that all groups have equal variances.

The test statistic is:

W=(Nk)(k1)i=1kni(ZiZ)2i=1kj=1ni(ZijZi)2W = \frac{(N - k)}{(k - 1)} \cdot \frac{\sum_{i=1}^k n_i (Z_{i\cdot} - Z_{\cdot\cdot})^2}{\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - Z_{i\cdot})^2}

where ZijZ_{ij} is the absolute deviation of observation jj in group ii from the group center, ZiZ_{i\cdot} is the mean of ZijZ_{ij} for group ii, ZZ_{\cdot\cdot} is the overall mean, nin_i is the size of group ii, kk is the number of groups, and NN is the total number of observations. For more details, see the scipy.stats.levene documentation.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=LEVENE(samples, [center], [trimmed_proportion])
  • samples (2D list, required): Table where each column is a sample group and each row is an observation. Must have at least two columns and two rows per column.
  • center (string, optional, default='median'): Central tendency function to use. One of 'mean', 'median', or 'trimmed'.
  • trimmed_proportion (float, optional, required only if center is 'trimmed', default=None): Proportion to cut from each end when using trimmed mean. Must be between 0 and 0.5.

The function returns a single-row, two-column table: [statistic, pvalue], or an error message (string) if the input is invalid. The statistic is the Levene test statistic, and the p-value indicates the probability of observing the data if the null hypothesis of equal variances is true.

Examples

Example 1: Basic Case (Median Center)

Inputs:

samples
1.22.51.9
2.32.72.0
2.12.82.2
center
median

Excel formula:

=LEVENE({1.2,2.5,1.9;2.3,2.7,2.0;2.1,2.8,2.2}, "median")

Expected output:

statisticpvalue
0.8770.463

Example 2: Mean Center

Inputs:

samples
1.22.51.9
2.32.72.0
2.12.82.2
center
mean

Excel formula:

=LEVENE({1.2,2.5,1.9;2.3,2.7,2.0;2.1,2.8,2.2}, "mean")

Expected output:

statisticpvalue
5.8820.039

Example 3: Trimmed Center (0.1 Proportion)

Inputs:

samples
1.22.51.9
2.32.72.0
2.12.82.2
center
trimmed
trimmed_proportion
0.1

Excel formula:

=LEVENE({1.2,2.5,1.9;2.3,2.7,2.0;2.1,2.8,2.2}, "trimmed", 0.1)

Expected output:

statisticpvalue
5.8820.039

Example 4: Different Values (Median Center)

Inputs:

samples
1.02.03.0
1.12.13.1
1.22.23.2
center
median

Excel formula:

=LEVENE({1.0,2.0,3.0;1.1,2.1,3.1;1.2,2.2,3.2}, "median")

Expected output:

statisticpvalue
0.0001.000

Python Code

from scipy.stats import levene as scipy_levene from typing import List, Optional, Union def levene(samples: List[List[float]], center: str = 'median', trimmed_proportion: Optional[float] = None) -> Union[List[List[float]], str]: """ Performs the Levene test for equality of variances across multiple samples. Args: samples: 2D list of float values. Each column represents a sample group. center: Function of central tendency to use ('mean', 'median', 'trimmed'). Default is 'median'. trimmed_proportion: Proportion to cut from each end when center is 'trimmed'. Optional. Returns: 2D list with a single row: [statistic, pvalue], or an error message (str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate samples if not isinstance(samples, list) or len(samples) < 2: return "Invalid input: samples must be a 2D list with at least two columns (groups)." if any(not isinstance(col, list) or len(col) < 2 for col in samples): return "Invalid input: each sample group must be a list with at least two values." # Validate center if center not in ('mean', 'median', 'trimmed'): return "Invalid input: center must be 'mean', 'median', or 'trimmed'." # Validate trimmed_proportion if center == 'trimmed': if trimmed_proportion is None: return "Invalid input: trimmed_proportion must be specified when center is 'trimmed'." try: trimmed_proportion = float(trimmed_proportion) except Exception: return "Invalid input: trimmed_proportion must be a float." if not (0 <= trimmed_proportion < 0.5): return "Invalid input: trimmed_proportion must be between 0 and 0.5." else: trimmed_proportion = None # Convert all values to float and check for errors try: samples_float = [] for col in samples: col_float = [float(x) for x in col] samples_float.append(col_float) except Exception: return "Invalid input: all sample values must be numeric." # Run Levene test try: if center == 'trimmed': stat, pvalue = scipy_levene(*samples_float, center=center, proportiontocut=trimmed_proportion) else: stat, pvalue = scipy_levene(*samples_float, center=center) except Exception as e: return f"scipy.stats.levene error: {e}" # Convert numpy types to native float try: stat = float(stat) pvalue = float(pvalue) except Exception: return "Invalid output: could not convert result to float." # Check for nan/inf if any([isinstance(x, float) and (x != x or x in [float('inf'), float('-inf')]) for x in [stat, pvalue]]): return "Invalid output: statistic or pvalue is NaN or infinite." return [[stat, pvalue]]

Example Workbook

Link to Workbook

Last updated on