Mercurial > octave-nkf
annotate scripts/statistics/tests/welch_test.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | fd0a3ac60b0e |
children | bc924baa2c4e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1995-2012 Kurt Hornik |
3426 | 2 ## |
3922 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3454 | 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. | |
6754 | 24 ## Under the null, the test statistic @var{t} approximately follows a |
25 ## Student distribution with @var{df} degrees of freedom. | |
3200 | 26 ## |
3454 | 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 | |
3457 | 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. | |
3200 | 34 ## |
3454 | 35 ## The p-value of the test is returned in @var{pval}. |
3426 | 36 ## |
3200 | 37 ## If no output argument is given, the p-value of the test is displayed. |
3454 | 38 ## @end deftypefn |
3200 | 39 |
5428 | 40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 41 ## Description: Welch two-sample t test |
3200 | 42 |
43 function [pval, t, df] = welch_test (x, y, alt) | |
3426 | 44 |
3200 | 45 if ((nargin < 2) || (nargin > 3)) |
6046 | 46 print_usage (); |
3200 | 47 endif |
3426 | 48 |
4030 | 49 if (! (isvector (x) && isvector (y))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
50 error ("welch_test: both X and Y must be vectors"); |
3200 | 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)); | |
3792 | 58 v_y = sumsq (y - mu_y) / (n_y * (n_y - 1)); |
3200 | 59 c = v_x / (v_x + v_y); |
3457 | 60 df = 1 / (c^2 / (n_x - 1) + (1 - c)^2 / (n_y - 1)); |
3200 | 61 t = (mu_x - mu_y) / sqrt (v_x + v_y); |
10606
ec34c7acd057
Replace deprecated function calls in statistics tests
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
62 cdf = tcdf (t, df); |
3200 | 63 |
64 if (nargin == 2) | |
65 alt = "!="; | |
66 endif | |
3426 | 67 |
5443 | 68 if (! ischar (alt)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10606
diff
changeset
|
69 error ("welch_test: ALT must be a string"); |
3200 | 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 | |
3456 | 78 error ("welch_test: option %s not recognized", alt); |
3200 | 79 endif |
3426 | 80 |
3200 | 81 if (nargout == 0) |
3456 | 82 printf (" pval: %g\n", pval); |
3200 | 83 endif |
84 | |
85 endfunction |