changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/tests/sign_test.m

changeset 10289: 4b124317dc38
parent:93c65f2a5668
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (70 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
2## Kurt Hornik
3##
4## This file is part of Octave.
5##
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.
10##
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.
15##
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/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {[@var{pval}, @var{b}, @var{n}] =} sign_test (@var{x}, @var{y}, @var{alt})
22## For two matched-pair samples @var{x} and @var{y}, perform a sign test
23## of the null hypothesis PROB (@var{x} > @var{y}) == PROB (@var{x} <
24## @var{y}) == 1/2. Under the null, the test statistic @var{b} roughly
25## follows a binomial distribution with parameters @code{@var{n} = sum
26## (@var{x} != @var{y})} and @var{p} = 1/2.
27##
28## With the optional argument @code{alt}, the alternative of interest
29## can be selected. If @var{alt} is @code{"!="} or @code{"<>"}, the
30## null hypothesis is tested against the two-sided alternative PROB
31## (@var{x} < @var{y}) != 1/2. If @var{alt} is @code{">"}, the
32## one-sided alternative PROB (@var{x} > @var{y}) > 1/2 ("x is
33## stochastically greater than y") is considered. Similarly for
34## @code{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) < 1/2
35## ("x is stochastically less than y") is considered. The default is
36## the two-sided case.
37##
38## The p-value of the test is returned in @var{pval}.
39##
40## If no output argument is given, the p-value of the test is displayed.
41## @end deftypefn
42
43## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44## Description: Sign test
45
46function [pval, b, n] = sign_test (x, y, alt)
47
48 if ((nargin < 2) || (nargin > 3))
49 print_usage ();
50 endif
51
52 if (! (isvector (x) && isvector (y) && (length (x) == length (y))))
53 error ("sign_test: x and y must be vectors of the same length");
54 endif
55
56 n = length (x);
57 x = reshape (x, 1, n);
58 y = reshape (y, 1, n);
59 n = sum (x != y);
60 b = sum (x > y);
61 cdf = binomial_cdf (b, n, 1/2);
62
63 if (nargin == 2)
64 alt = "!=";
65 endif
66
67 if (! ischar (alt))
68 error ("sign_test: alt must be a string");
69 endif
70 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
71 pval = 2 * min (cdf, 1 - cdf);
72 elseif strcmp (alt, ">")
73 pval = 1 - cdf;
74 elseif strcmp (alt, "<")
75 pval = cdf;
76 else
77 error ("sign_test: option %s not recognized", alt);
78 endif
79
80 if (nargout == 0)
81 printf (" pval: %g\n", pval);
82 endif
83
84endfunction