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:
where is the absolute deviation of observation in group from the group center, is the mean of for group , is the overall mean, is the size of group , is the number of groups, and 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 ifcenter
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.2 | 2.5 | 1.9 |
2.3 | 2.7 | 2.0 |
2.1 | 2.8 | 2.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:
statistic | pvalue |
---|---|
0.877 | 0.463 |
Example 2: Mean Center
Inputs:
samples | ||
---|---|---|
1.2 | 2.5 | 1.9 |
2.3 | 2.7 | 2.0 |
2.1 | 2.8 | 2.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:
statistic | pvalue |
---|---|
5.882 | 0.039 |
Example 3: Trimmed Center (0.1 Proportion)
Inputs:
samples | ||
---|---|---|
1.2 | 2.5 | 1.9 |
2.3 | 2.7 | 2.0 |
2.1 | 2.8 | 2.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:
statistic | pvalue |
---|---|
5.882 | 0.039 |
Example 4: Different Values (Median Center)
Inputs:
samples | ||
---|---|---|
1.0 | 2.0 | 3.0 |
1.1 | 2.1 | 3.1 |
1.2 | 2.2 | 3.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:
statistic | pvalue |
---|---|
0.000 | 1.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]]