Bartlett Test

The Bartlett Test of Bartlett (1937) asks whether two samples have equal variances by comparing the log of the pooled variance with the logs of the separate ones, the likelihood ratio test of equal variances for normal data. It is the most powerful of the tests of equal variances when the data are normal.

The formula for the Bartlett Test compares the pooled variance \(s_{p}^{2}\) with the sample variances \(s_{i}^{2}\), corrected by C for small samples:

\[C = 1 + \frac{\sum_{i=1}^{2} \frac{1}{n_{i} - 1} - \frac{1}{n - 2}}{3}\] \[T = \frac{(n - 2) \cdot \ln\left(s_{p}^{2}\right) - \sum_{i=1}^{2} (n_{i} - 1) \cdot \ln\left(s_{i}^{2}\right)}{C}\]

Where \(n_{i}\) is the size of sample i and \(n = n_{1} + n_{2}\). T is chi-squared distributed with 1 degree of freedom under the null: equal variances are rejected when the p-value is below the significance level (0.05 for the 5% level). It assumes normal data and rejects far too often for fat-tailed returns, where the Levene Test or the Brown-Forsythe Test is the safer choice.

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: Bartlett’s test, homogeneity of variances test.

In the Finance Toolkit this is get_two_sample_t_test with method="bartlett", 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="bartlett")

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 Bartlett, M.S. (1937). “Properties of Sufficiency and Statistical Tests.” Proceedings of the Royal Society A, 160(901), 268-282.
  • Library Referenced against scipy’s bartlett (Bartlett Test)
Share