Kolmogorov-Smirnov Test

The Kolmogorov-Smirnov Test, in its two-sample form of Smirnov (1948), compares the whole distributions of two samples. It tests the null hypothesis that the two samples come from the same continuous distribution against the alternative that the distributions differ anywhere, in location, spread or shape.

The formula for the Kolmogorov-Smirnov Test takes the largest gap between the empirical distribution functions of the two samples:

\[D = \max\left(\left\lvert F_{a}(x) - F_{b}(x)\right\rvert\right)\]

Where \(F_{a} (x)\) and \(F_{b} (x)\) are the shares of each sample at or below x. D lies between 0 and 1: the larger the gap, the stronger the evidence that the distributions differ. Its p-value comes from the Kolmogorov distribution for \(n_{a} \cdot n_{b} / (n_{a} + n_{b})\) observations, with \(n_{a}\) and \(n_{b}\) the sizes of the samples, and the null is rejected when it is below the significance level (0.05 for the 5% level). The test is most sensitive near the middle of the distributions and has little power against differences in the tails, where returns differ most.

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: KS test, two-sample Kolmogorov-Smirnov test, Smirnov test.

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

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 Smirnov, N. (1948). “Table for Estimating the Goodness of Fit of Empirical Distributions.” Annals of Mathematical Statistics, 19(2), 279-281.
  • Library Referenced against scipy’s ks_2samp (Kolmogorov-Smirnov Test)
Share