Mann-Whitney U Test

The Mann-Whitney U Test of Mann and Whitney (1947), equivalent to the Wilcoxon rank-sum test, compares the two samples through their ranks instead of their values. It tests the null hypothesis that the two samples come from the same distribution against the alternative that one tends to give larger values than the other. Using ranks makes it robust to outliers and fat tails, where the t-test loses power.

The formula for the Mann-Whitney U Test ranks the two samples together, ties at their average rank, and counts how often a value of the first sample exceeds one of the second through the sum of its ranks \(R_{a}\):

\[U = R_{a} - \frac{n_{a} \cdot (n_{a} + 1)}{2}\]

The statistic is standardised with its mean and a variance corrected for ties, with a continuity correction of 0.5 towards the mean:

\[\sigma_{U}^{2} = \frac{n_{a} \cdot n_{b}}{12} \cdot \left((n + 1) - \frac{\sum \left(t^{3} - t\right)}{n \cdot (n - 1)}\right)\] \[z_{U} = \frac{U - \frac{n_{a} \cdot n_{b}}{2} - 0.5 \cdot \operatorname{sign}\left(U - \frac{n_{a} \cdot n_{b}}{2}\right)}{\sigma_{U}}\]

Where \(n_{a}\) and \(n_{b}\) are the sizes of the two samples, \(n = n_{a} + n_{b}\) and t the size of every group of tied values. \(z_{U}\) 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 U above its mean \(n_{a} \cdot n_{b} / 2\) meaning the first sample tends to the larger values. It detects a shift in location, not a difference in spread: two samples with the same median but different volatility can pass it.

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: Mann-Whitney test, Wilcoxon rank-sum test, U test.

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

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 Mann, H.B. and Whitney, D.R. (1947). “On a Test of Whether One of Two Random Variables is Stochastically Larger than the Other.” Annals of Mathematical Statistics, 18(1), 50-60.
  • Library Referenced against scipy’s mannwhitneyu (Mann-Whitney U Test)
Share