Wilcoxon rank sum test
the p-value of a two-sided Wilcoxon rank sum test.
p = ranksum(
the null hypothesis that data in
samples from continuous distributions with equal medians, against
the alternative that they are not. The test assumes that the two samples
have different lengths.
Test the hypothesis of equal medians for two independent unequal-sized samples.
Generate sample data.
rng('default') % for reproducibility x = unifrnd(0,1,10,1); y = unifrnd(0.25,1.25,15,1);
These samples come from populations with identical distributions except for a shift of 0.25 in the location.
Test the equality of medians of
p = ranksum(x,y)
p = 0.0375
The p-value of 0.0375 indicates that
the null hypothesis of equal medians at the default 5% significance
Obtain the statistics of the test for the equality of two population medians.
Load the sample data.
Test if the mileage per gallon is the same for the first and second type of cars.
[p,h,stats] = ranksum(mileage(:,1),mileage(:,2))
p = 0.0043 h = 1 stats = ranksum: 21.5000
Both the p-value, 0.043, and
1 indicate the rejection of the null hypothesis of equal medians at
the default 5% significance level. Because the sample sizes are small
ranksum calculates the p-value
using the exact method. The structure
only the value of the rank sum test statistic.
Test the hypothesis of an increase in the population median.
Navigate to a folder containing sample data.
Load the sample data.
The weather data shows the daily high temperatures taken in the same month in two consecutive years.
Perform a left-sided test to assess the increase in the median at the 1% significance level.
[p,h,stats] = ranksum(year1,year2,'alpha',0.01,... 'tail','left')
p = 0.1271 h = 0 stats = zval: -1.1403 ranksum: 837.5000
Both the p-value of 0.1271 and
0 indicate that there is not enough evidence to reject the null hypothesis
and conclude that there is a positive shift in the median of observed
high temperatures in the same month from year 1 to year 2 at the 1%
significance level. Notice that
ranksum uses the
approximate method to calculate the p-value due
to the large sample sizes.
Use the exact method to calculate the p-value.
[p,h,stats] = ranksum(year1,year2,'alpha',0.01,... 'tail','left','method','exact')
p = 0.1273 h = 0 stats = ranksum: 837.5000
The results of the approximate and exact methods are consistent with each other.
x— Sample data
Sample data, specified as a vector.
y— Sample data
Sample data, specified as a vector. The length of
not have to be the same as the length of
Specify optional comma-separated pairs of
Name is the argument
Value is the corresponding
Name must appear
inside single quotes (
You can specify several name and value pair
arguments in any order as
'alpha',0.01,'method','approximate','tail','right'specifies a right-tailed rank sum test with 1% significance level, which returns the approximate p-value.
'alpha'— Significance level
Significance level of the decision of a hypothesis test, specified
as the comma-separated pair consisting of
a scalar value in the range 0 to 1. The significance level of
'method'— Computation method of the p-value
Computation method of the p-value,
specified as the comma-separated pair consisting of
one of the following:
|Exact computation of the p-value, |
|Normal approximation while computing the p-value, |
unspecified, the default is:
'exact' if min(nx,ny)
< 10 and nx + ny <
nx and ny are
the sizes of the samples in
'tail'— Type of test
Type of test, specified as the comma-separated pair consisting
'tail' and one of the following:
|Two-sided hypothesis test, where the alternative hypothesis
states that |
|Right-tailed hypothesis test, where the alternative hypothesis
states that the median of |
|Left-tailed hypothesis test, where the alternative hypothesis
states that the median of |
p— p-value of the test
p-value of the test, returned as a positive
scalar from 0 to 1.
p is the probability of observing
a test statistic as or more extreme than the observed value under
the null hypothesis.
ranksum computes the two-sided p-value
by doubling the most significant one-sided value.
h— Result of the hypothesis test
Result of the hypothesis test, returned as a logical value.
h = 1, this indicates rejection
of the null hypothesis at the 100 *
h = 0, this indicates a failure
to reject the null hypothesis at the 100 *
stats— Test statistics
Test statistics, returned as a structure. The test statistics
ranksum : Value of the rank sum
zval: Value of the z-statistic (computed when
The Wilcoxon rank sum test is a nonparametric
test for two populations when samples are independent. If
independent samples with different sample sizes, the test statistic
ranksum returns is the rank sum of the first
The Wilcoxon rank sum test is equivalent to the Mann-Whitney
U-test. The Mann-Whitney U-test is a nonparametric test for equality
of population medians of two independent samples
The Mann-Whitney U-test statistic, U, is
the number of times a y precedes an x in
an ordered arrangement of the elements in the two independent samples
It is related to the Wilcoxon rank sum statistic in the following
X is a sample of size nX,
For large samples,
a z-statistic to compute the approximate p-value
of the test.
Y are two independent
samples of size nX and nY,
where nX < nY the z-statistic
with continuity correction and tie adjustment. Here tiescor is given by
= tiedrank(x,y) to obtain tie adjustments. The standard
normal distribution gives the p-value for this z-statistic.
missing values and ignores them.
For a two-sided test of medians with unequal sample sizes, the
test statistic that
ranksum returns is the rank
sum of the first sample.
 Gibbons, J. D., and S. Chakraborti. Nonparametric Statistical Inference, 5th Ed., Boca Raton, FL: Chapman & Hall/CRC Press, Taylor & Francis Group, 2011.
 Hollander, M., and D. A. Wolfe. Nonparametric Statistical Methods. Hoboken, NJ: John Wiley & Sons, Inc., 1999.