wilcoxonRankSum

Performs a Wilcoxon rank sum test for comparing the medians of two populations.

Synopsis

wilcoxonRankSum (x, y)

Required Arguments

float x[] (Input)
Array of length nx containing the first sample.
float y[] (Input)
Array of length ny containing the second sample.

Return Value

The two-sided p-value for the Wilcoxon rank sum statistic computed with average ranks used in the case of ties.

Optional Arguments

fuzz, float (Input)

Nonnegative constant used to determine ties in computing ranks in the combined samples. A tie is declared when two observations in the combined sample are within fuzz of each other.

Default: fuzz = 100 × machine(4) × \(\max\{|x_{i1}|,|x_{j2}|\}\)

stat (Output)
An array of length 10 containing the following statistics:
Row Statistics
0 Wilcoxon W statistic (the sum of the ranks of the x observations) adjusted for ties in such a manner that W is as small as possible.
1 \(2\times E(W)-W\), where \(E(W)\) is the expected value of W.
2 Probability of obtaining a statistic less than or equal to \(\min\{ W,2\times E(W)-W \}\).
3 W statistic adjusted for ties in such a manner that W is as large as possible.
4 \(2\times E(W)-W\), where \(E(W)\) is the expected value of W, adjusted for ties in such a manner that W is as large as possible.
5 probability of obtaining a statistic less than or equal to \(\min\{ W,2\times E(W)-W \}\), adjusted for ties in such a manner that W is as large as possible.
6 W statistic with average ranks used in case of ties.
7 Estimated standard error of stat [6] under the null hypothesis of no difference.
8 Standard normal score associated with stat [6].
9 Two-sided p-value associated with stat[8].

Description

Function wilcoxonRankSum conducts the Wilcoxon rank sum test for identical population distribution functions. The Wilcoxon test and the Mann-Whitney U test are equivalent. If the difference between the two populations can be attributed solely to a difference in location, then the Wilcoxon test becomes a test of equality of the population means (or medians) and is the nonparametric equivalent of the two-sample t-test. Function wilcoxonRankSum obtains ranks in the combined sample after first eliminating missing values from the data. The rank sum statistic is then computed as the sum of the ranks in the x sample. Three methods for handling ties are used. (A tie is counted when two observations are within fuzz of each other.) Method 1 uses the largest possible rank for tied observations in the smallest sample, while Method 2 uses the smallest possible rank for these observations. Thus, the range of possible rank sums is obtained.

Method 3 uses the average rank of the tied observations for handling tied observations between samples. Asymptotic standard normal scores are computed for the W score (based on a variance that has been adjusted for ties) when average ranks are used (see Conover 1980, p. 217), and the probability associated with the two-sided alternative is computed.

The p-value returned in stat[9] is the two-sided p-value calculated using the normal approximation with the normal score returned in stat[8]. The p-value returned by this routine is either the approximate or exact two-sided p-value depending upon the number of observations.

Hypothesis Tests

In each of the following tests, the first line gives the hypothesis (and its alternative) under the assumptions 1 to 3 below, while the second line gives the hypothesis when assumption 4 is also true. The rejection region is the same for both hypotheses and is given in terms of Method 3 for handling ties. Another output statistic should be used, (stat[0] or stat[3]), if another method for handling ties is desired.

Test Null and Alternative Hypothesis Action
1

\(H_0 : Pr(x<y)=0.5\) vs \(H_1 : Pr(x<y)\neq 0.5\) or

\(H_0 : E(x)=E(y)\) vs \(H_1 : E(x)\neq E(y)\)

Reject if pValue is less than the user’s significance level of the test.
2

\(H_0 : Pr(x<y)\leq 0.5\) vs \(H_1 : Pr(x<y)>0.5\)

or

\(H_0 : E(x)\geq E(y)\) vs \(H_1 : E(x)<E(y)\)

Reject if stat[6] is too small or if p[0] is less than the user’s significance level of the test
3

\(H_0\):Pr(x < y) ≥ 0.5 vs \(H_1\):Pr(x < y) < 0.5

or

\(H_0\):E(x) ≤ E(y)) vs \(H_1\):E(x) > E(y)

Reject if stat[6] is too large or if p[1] is less than the user’s significance level of the test

Assumptions

  1. Arguments x and y contain random samples from their respective populations.
  2. All observations are mutually independent.
  3. The measurement scale is at least ordinal (i.e., an ordering less than, greater than, or equal to exists among the observations).
  4. If \(f(x)\) and \(g(y)\) are the distribution functions of x and y, then \(g(y)=f(x+c)\) for some constant c (i.e., the distribution of y is, at worst, a translation of the distribution of x).

The p-values are calculated using either the large-sample normal approximation or the exact probability calculations. This approximate calculation is usually considered adequate when the size of one or both samples is greater than 50.

Example

The following example is taken from Conover (1980, p. 224). It involves the mixing time of two mixing machines using a total of 10 batches of a certain kind of batter, five batches for each machine. The null hypothesis is not rejected at the 5-percent level of significance. The warning error is always printed when one or more ties are detected.

The statistics are output in the array stat.

from numpy import *
from pyimsl.stat.wilcoxonRankSum import wilcoxonRankSum
from pyimsl.stat.writeMatrix import writeMatrix

x1 = array([7.3, 6.9, 7.2, 7.8, 7.2])
x2 = array([7.4, 6.8, 6.9, 6.7, 7.1])
stat = []
labels = ["Wilcoxon W statistic ......................",
          "2*E(W) - W ................................",
          "p-value ...................................",
          "Adjusted Wilcoxon statistic ...............",
          "Adjusted 2*E(W) - W .......................",
          "Adjusted p-value ..........................",
          "W statistics for averaged ranks............",
          "Standard error of W (averaged ranks) ......",
          "Standard normal score of W (averaged ranks)",
          "Two-sided p-value of W (averaged ranks) ..."]

wilcoxonRankSum(x1, x2, stat=stat)

writeMatrix("statistics", stat, rowLabels=labels,
            writeFormat="%7.3f", column=True)

Output

***
*** Warning error issued from IMSL function wilcoxonRankSum:
*** At least one tie is detected between the samples.
***
 
                     statistics
Wilcoxon W statistic ......................   34.000
2*E(W) - W ................................   21.000
p-value ...................................    0.110
Adjusted Wilcoxon statistic ...............   35.000
Adjusted 2*E(W) - W .......................   20.000
Adjusted p-value ..........................    0.075
W statistics for averaged ranks............   34.500
Standard error of W (averaged ranks) ......    4.758
Standard normal score of W (averaged ranks)    1.471
Two-sided p-value of W (averaged ranks) ...    0.141

Warning Errors

IMSLS_AT_LEAST_ONE_TIE At least one tie is detected between the samples.

Fatal Errors

IMSLS_ALL_X_Y_MISSING Each element of x and/or y is a missing (NaN, Not a Number) value.