]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/hotelling_test_2.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / hotelling_test_2.m
1 ## Copyright (C) 1996-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{tsq}] =} hotelling_test_2 (@var{x}, @var{y})
21 ## For two samples @var{x} from multivariate normal distributions with
22 ## the same number of variables (columns), unknown means and unknown
23 ## equal covariance matrices, test the null hypothesis @code{mean
24 ## (@var{x}) == mean (@var{y})}.
25 ##
26 ## Hotelling's two-sample @math{T^2} is returned in @var{tsq}.  Under the null,
27 ## @tex
28 ## $$
29 ## {n_x+n_y-p-1) T^2 \over p(n_x+n_y-2)}
30 ## $$
31 ## @end tex
32 ## @ifnottex
33 ##
34 ## @example
35 ## (n_x+n_y-p-1) T^2 / (p(n_x+n_y-2))
36 ## @end example
37 ##
38 ## @end ifnottex
39 ## @noindent
40 ## has an F distribution with @math{p} and @math{n_x+n_y-p-1} degrees of
41 ## freedom, where @math{n_x} and @math{n_y} are the sample sizes and
42 ## @math{p} is the number of variables.
43 ##
44 ## The p-value of the test is returned in @var{pval}.
45 ##
46 ## If no output argument is given, the p-value of the test is displayed.
47 ## @end deftypefn
48
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
50 ## Description: Compare means of two multivariate normals
51
52 function [pval, Tsq] = hotelling_test_2 (x, y)
53
54   if (nargin != 2)
55     print_usage ();
56   endif
57
58   if (isvector (x))
59     n_x = length (x);
60     if (! isvector (y))
61       error ("hotelling_test_2: if X is a vector, Y must also be a vector");
62     else
63       n_y = length (y);
64       p   = 1;
65     endif
66   elseif (ismatrix (x))
67     [n_x, p] = size (x);
68     [n_y, q] = size (y);
69     if (p != q)
70       error ("hotelling_test_2: X and Y must have the same number of columns");
71     endif
72   else
73     error ("hotelling_test_2: X and Y must be matrices (or vectors)");
74   endif
75
76   d    = mean (x) - mean (y);
77   S    = ((n_x - 1) * cov (x) + (n_y - 1) * cov (y)) / (n_x + n_y - 2);
78   Tsq  = (n_x * n_y / (n_x + n_y)) * d * (S \ d');
79   pval = 1 - fcdf ((n_x + n_y - p - 1) * Tsq / (p * (n_x + n_y - 2)),
80                     p, n_x + n_y - p - 1);
81
82   if (nargout == 0)
83     printf ("  pval: %g\n", pval);
84   endif
85
86 endfunction