1%% Copyright (C) 1995, 1996, 1997  Kurt Hornik
2%%               2006, 2011 Alois Schloegl
3%%
4%% This file is part of Octave.
5%%
6%% biosig 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, or (at your option)
9%% any later version.
10%%
11%% Biosig 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.
15%%
16%% You should have received a copy of the GNU General Public License
17%% along with Octave; see the file COPYING.  If not, write to the Free
18%% Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19%% 02110-1301, USA.
20
21%% -*- texinfo -*-
22%% @deftypefn {Function File} {[@var{pval}, @var{b}, @var{n}] =} signtest (@var{x}, @var{y}, @var{alt})
23%% For two matched-pair samples @var{x} and @var{y}, perform a sign test
24%% of the null hypothesis PROB (@var{x} > @var{y}) == PROB (@var{x} <
25%% @var{y}) == 1/2.  Under the null, the test statistic @var{b} roughly
26%% follows a binomial distribution with parameters @code{@var{n} = sum
27%% (@var{x} ~= @var{y})} and @var{p} = 1/2.
28%%
29%% With the optional argument @code{alt}, the alternative of interest
30%% can be selected.  If @var{alt} is @code{'~='} or @code{'<>'}, the
31%% null hypothesis is tested against the two-sided alternative PROB
32%% (@var{x} < @var{y}) ~= 1/2.  If @var{alt} is @code{'>'}, the
33%% one-sided alternative PROB (@var{x} > @var{y}) > 1/2 ('x is
34%% stochastically greater than y') is considered.  Similarly for
35%% @code{'<'}, the one-sided alternative PROB (@var{x} > @var{y}) < 1/2
36%% ('x is stochastically less than y') is considered.  The default is
37%% the two-sided case.
38%% If @var{x} and @var{y} are matrices (must have same size), the test
39%% is applied to each column.
40%%
41%% The p-value of the test is returned in @var{pval}.
42%%
43%% If no output argument is given, the p-value of the test is displayed.
44%% @end deftypefn
45
46%% Author: KH <Kurt.Hornik@wu-wien.ac.at>
47%% Description: Sign test
48%% Adapted for the use with M*tlab by AS <alois.schloegl@gmail.com> Dec 2006
49
50function [pval, b, n] = signtest (x, y, alpha, alt)
51
52  if ((nargin < 2) || (nargin > 4))
53    error;
54  end
55  if nargin<3,
56  	alpha=.05;
57  end;
58
59  d   = x - y;
60  n   = sumskipnan(d,1);
61  n   = sum ( d~=0 & ~isnan(d));
62  b   = sum (x > y);
63  cdf = binocdf (b, n, 1/2);
64
65  if (nargin == 2)
66    alt  = '~=';
67  end
68
69  if (strcmp (alt, '~=') || strcmp (alt, '<>') || (alt==0))
70    pval = 2 * min (cdf, 1 - cdf);
71  elseif (strcmp (alt, '>') || (alt==1))
72    pval = 1 - cdf;
73  elseif (strcmp (alt, '<') || (alt==-1))
74    pval = cdf;
75  else
76    error ('sign_test: option %s not recognized', alt);
77  end
78
79  if (nargout == 0)
80    printf ('  pval: %g\n', pval);
81  elseif nargout > 1,
82    b = (pval<alpha);
83  end
84
85end
86