]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/u_test.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / u_test.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
26 ##
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.
34 ##
35 ## The p-value of the test is returned in @var{pval}.
36 ##
37 ## If no output argument is given, the p-value of the test is displayed.
38 ## @end deftypefn
39
40 ## This implementation is still incomplete---for small sample sizes,
41 ## the normal approximation is rather bad ...
42
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44 ## Description: Mann-Whitney U-test
45
46 function [pval, z] = u_test (x, y, alt)
47
48   if ((nargin < 2) || (nargin > 3))
49     print_usage ();
50   endif
51
52   if (! (isvector (x) && isvector (y)))
53     error ("u_test: both X and Y must be vectors");
54   endif
55
56   n_x  = length (x);
57   n_y  = length (y);
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);
61
62   cdf  = stdnormal_cdf (z);
63
64   if (nargin == 2)
65     alt  = "!=";
66   endif
67
68   if (! ischar (alt))
69     error("u_test: ALT must be a string");
70   endif
71   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
72     pval = 2 * min (cdf, 1 - cdf);
73   elseif (strcmp (alt, ">"))
74     pval = cdf;
75   elseif (strcmp (alt, "<"))
76     pval = 1 - cdf;
77   else
78     error ("u_test: option %s not recognized", alt);
79   endif
80
81   if (nargout == 0)
82     printf ("  pval: %g\n", pval);
83   endif
84
85 endfunction