1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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.
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.
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/>.
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.
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.
35 ## The p-value of the test is returned in @var{pval}.
37 ## If no output argument is given, the p-value of the test is displayed.
40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
41 ## Description: Welch two-sample t test
43 function [pval, t, df] = welch_test (x, y, alt)
45 if ((nargin < 2) || (nargin > 3))
49 if (! (isvector (x) && isvector (y)))
50 error ("welch_test: both X and Y must be vectors");
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);
69 error ("welch_test: ALT must be a string");
71 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
72 pval = 2 * min (cdf, 1 - cdf);
73 elseif (strcmp (alt, ">"))
75 elseif (strcmp (alt, "<"))
78 error ("welch_test: option %s not recognized", alt);
82 printf (" pval: %g\n", pval);