1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{pval}, @var{z}] =} u_test (@var{x}, @var{y}, @var{alt})
21 ## For two samples @var{x} and @var{y}, perform a Mann-Whitney U-test of
22 ## the null hypothesis PROB (@var{x} > @var{y}) == 1/2 == PROB (@var{x}
23 ## < @var{y}). Under the null, the test statistic @var{z} approximately
24 ## follows a standard normal distribution. Note that this test is
25 ## equivalent to the Wilcoxon rank-sum test.
27 ## With the optional argument string @var{alt}, the alternative of
28 ## interest can be selected. If @var{alt} is @code{"!="} or
29 ## @code{"<>"}, the null is tested against the two-sided alternative
30 ## PROB (@var{x} > @var{y}) != 1/2. If @var{alt} is @code{">"}, the
31 ## one-sided alternative PROB (@var{x} > @var{y}) > 1/2 is considered.
32 ## Similarly for @code{"<"}, the one-sided alternative PROB (@var{x} >
33 ## @var{y}) < 1/2 is considered. The default is the two-sided case.
35 ## The p-value of the test is returned in @var{pval}.
37 ## If no output argument is given, the p-value of the test is displayed.
40 ## This implementation is still incomplete---for small sample sizes,
41 ## the normal approximation is rather bad ...
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Description: Mann-Whitney U-test
46 function [pval, z] = u_test (x, y, alt)
48 if ((nargin < 2) || (nargin > 3))
52 if (! (isvector (x) && isvector (y)))
53 error ("u_test: both X and Y must be vectors");
58 r = ranks ([(reshape (x, 1, n_x)), (reshape (y, 1, n_y))]);
59 z = (sum (r(1 : n_x)) - n_x * (n_x + n_y + 1) / 2) ...
60 / sqrt (n_x * n_y * (n_x + n_y + 1) / 12);
62 cdf = stdnormal_cdf (z);
69 error("u_test: ALT must be a string");
71 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
72 pval = 2 * min (cdf, 1 - cdf);
73 elseif (strcmp (alt, ">"))
75 elseif (strcmp (alt, "<"))
78 error ("u_test: option %s not recognized", alt);
82 printf (" pval: %g\n", pval);