Levene Test

The Levene Test of Levene (1960) asks whether two samples are equally dispersed: the null hypothesis is equal variances, the alternative that one sample is more volatile than the other. It runs an analysis of variance on the absolute deviations from each sample’s mean, which makes it less sensitive to non-normal data than the Bartlett Test.

The formula for the Levene Test takes the absolute deviations \(z_{i , j}\) of every value from the mean of its sample:

\[z_{i,j} = \left\lvert x_{i,j} - \bar{x}_{i}\right\rvert\]

It then compares the means of these deviations between the samples with their spread within them:

\[W = \frac{(n - 2) \cdot \sum_{i=1}^{2} n_{i} \cdot (\bar{z}_{i} - \bar{z})^{2}}{\sum (z_{i,j} - \bar{z}_{i})^{2}}\]

Where \(x_{i , j}\) is value j of sample i, bar(x_i) the mean of sample i, \(n_{i}\) its size, \(n = n_{1} + n_{2}\), bar(z_i) the mean of the deviations in sample i and bar(z) their overall mean. W is F(1, n - 2) distributed under the null: equal variances are rejected when the p-value is below the significance level (0.05 for the 5% level). With fat tails the mean is a poor centre, and the test then rejects equal variances too often; the Brown-Forsythe Test corrects this.

A two-sample test asks whether two samples, such as the daily returns of two assets, could come from the same distribution, or whether they differ by more than chance explains. The tests differ in what they compare: the means, the whole distribution, the differences paired by date, or the variances, and in what they assume about the shape of the returns, whose fat tails break the normality that the classical tests rest on.

Also known as: Levene’s test, equal variance test.

In the Finance Toolkit this is get_two_sample_t_test with method="levene", the same function as Two-Sample t-Test.

Example · MSFT, ORCL, SAP · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_two_sample_t_test:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["MSFT", "ORCL", "SAP"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_two_sample_t_test(method="levene")

Which returns:

Using Your Own Data

get_two_sample_t_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_two_sample_t_test()

References

  • Paper Levene, H. (1960). “Robust Tests for Equality of Variances.” In Contributions to Probability and Statistics, Stanford University Press, 278-292.
  • Library Referenced against scipy’s levene (Levene Test, Brown-Forsythe Test)
Share