]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/welch_test.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / welch_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{t}, @var{df}] =} welch_test (@var{x}, @var{y}, @var{alt})
21 ## For two samples @var{x} and @var{y} from normal distributions with
22 ## unknown means and unknown and not necessarily equal variances,
23 ## perform a Welch test of the null hypothesis of equal means.
24 ## Under the null, the test statistic @var{t} approximately follows a
25 ## Student distribution with @var{df} degrees of freedom.
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 ## @code{mean (@var{x}) != @var{m}}.  If @var{alt} is @code{">"}, the
31 ## one-sided alternative mean(x) > @var{m} is considered.  Similarly for
32 ## @code{"<"}, the one-sided alternative mean(x) < @var{m} is
33 ## 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 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
41 ## Description: Welch two-sample t test
42
43 function [pval, t, df] = welch_test (x, y, alt)
44
45   if ((nargin < 2) || (nargin > 3))
46     print_usage ();
47   endif
48
49   if (! (isvector (x) && isvector (y)))
50     error ("welch_test: both X and Y must be vectors");
51   endif
52
53   n_x  = length (x);
54   n_y  = length (y);
55   mu_x = sum (x) / n_x;
56   mu_y = sum (y) / n_y;
57   v_x  = sumsq (x - mu_x) / (n_x * (n_x - 1));
58   v_y  = sumsq (y - mu_y) / (n_y * (n_y - 1));
59   c    = v_x / (v_x + v_y);
60   df   = 1 / (c^2 / (n_x - 1) + (1 - c)^2 / (n_y - 1));
61   t    = (mu_x - mu_y) / sqrt (v_x + v_y);
62   cdf  = tcdf (t, df);
63
64   if (nargin == 2)
65     alt  = "!=";
66   endif
67
68   if (! ischar (alt))
69     error ("welch_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 = 1 - cdf;
75   elseif (strcmp (alt, "<"))
76     pval = cdf;
77   else
78     error ("welch_test: option %s not recognized", alt);
79   endif
80
81   if (nargout == 0)
82     printf ("  pval: %g\n", pval);
83   endif
84
85 endfunction