1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {[@var{pval}, @var{z}] =} wilcoxon_test (@var{x}, @var{y}, @var{alt})
22## For two matched-pair sample vectors @var{x} and @var{y}, perform a
23## Wilcoxon signed-rank test of the null hypothesis PROB (@var{x} >
24## @var{y}) == 1/2. Under the null, the test statistic @var{z}
25## approximately follows a standard normal distribution when @var{n} > 25.
27## @strong{Warning}: This function assumes a normal distribution for @var{z}
28## and thus is invalid for @var{n} <= 25.
30## With the optional argument string @var{alt}, the alternative of
31## interest can be selected. If @var{alt} is @code{"!="} or
32## @code{"<>"}, the null is tested against the two-sided alternative
33## PROB (@var{x} > @var{y}) != 1/2. If alt is @code{">"}, the one-sided
34## alternative PROB (@var{x} > @var{y}) > 1/2 is considered. Similarly
35## for @code{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) <
36## 1/2 is considered. The default is the two-sided case.
38## The p-value of the test is returned in @var{pval}.
40## If no output argument is given, the p-value of the test is displayed.
43## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44## Description: Wilcoxon signed-rank test
46function [pval, z] = wilcoxon_test (x, y, alt)
48 if (nargin < 2 || nargin > 3)
52 if (! (isvector (x) && isvector (y) && (length (x) == length (y))))
53 error ("wilcoxon_test: x and y must be vectors of the same length");
57 x = reshape (x, 1, n);
58 y = reshape (y, 1, n);
60 d = d (find (d != 0));
64 z = sum (r (find (d > 0)));
65 z = ((z - n * (n + 1) / 4) / sqrt (n * (n + 1) * (2 * n + 1) / 24));
67 error ("wilcoxon_test: implementation requires more than 25 different pairs");
70 cdf = stdnormal_cdf (z);
77 error("wilcoxon_test: alt must be a string");
78 elseif (strcmp (alt, "!=") || strcmp (alt, "<>"))
79 pval = 2 * min (cdf, 1 - cdf);
80 elseif (strcmp (alt, ">"))
82 elseif (strcmp (alt, "<"))
85 error ("wilcoxon_test: option %s not recognized", alt);
89 printf (" pval: %g\n", pval);