Wilcoxon Signed-Rank Test

The Wilcoxon Signed-Rank Test of Wilcoxon (1945) compares two samples paired by date, through the ranks of their differences. It tests the null hypothesis that the differences are distributed symmetrically around zero against the alternative that one sample tends to be larger on the same dates. Pairing removes what both samples share, such as a common market move, which makes it more powerful than an unpaired test for two correlated assets.

The formula for the Wilcoxon Signed-Rank Test takes the differences \(d_{t}\) on the dates both samples have, drops the zeros, ranks their absolute values and sums the ranks of the positive differences into \(W_{\text{pos}}\):

\[d_{t} = a_{t} - b_{t}\]

The statistic is standardised with its mean and a variance corrected for ties:

\[\sigma_{W}^{2} = \frac{m \cdot (m + 1) \cdot (2 \cdot m + 1)}{24} - \frac{\sum \left(t^{3} - t\right)}{48}\] \[z_{W} = \frac{W_{\text{pos}} - \frac{m \cdot (m + 1)}{4}}{\sigma_{W}}\]

Where \(a_{t}\) and \(b_{t}\) are the two samples on date t, m the number of non-zero differences and t the size of every group of tied absolute differences. \(z_{W}\) is standard normal under the null: it is rejected when the two-sided p-value is below the significance level (0.05 for the 5% level), with \(W_{\text{pos}}\) above its mean meaning the first sample tends to be the larger. Dropping zero differences and ties lowers its power for data with many equal values.

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: signed-rank test, paired Wilcoxon test.

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

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 Wilcoxon, F. (1945). “Individual Comparisons by Ranking Methods.” Biometrics Bulletin, 1(6), 80-83.
  • Library Referenced against scipy’s wilcoxon (Wilcoxon Signed-Rank Test)
Share