1 ## Copyright (C) 1996-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{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})}.
26 ## Hotelling's two-sample @math{T^2} is returned in @var{tsq}. Under the null,
29 ## {n_x+n_y-p-1) T^2 \over p(n_x+n_y-2)}
35 ## (n_x+n_y-p-1) T^2 / (p(n_x+n_y-2))
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.
44 ## The p-value of the test is returned in @var{pval}.
46 ## If no output argument is given, the p-value of the test is displayed.
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
50 ## Description: Compare means of two multivariate normals
52 function [pval, Tsq] = hotelling_test_2 (x, y)
61 error ("hotelling_test_2: if X is a vector, Y must also be a vector");
70 error ("hotelling_test_2: X and Y must have the same number of columns");
73 error ("hotelling_test_2: X and Y must be matrices (or vectors)");
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);
83 printf (" pval: %g\n", pval);