Mercurial > hg > octave-lyh
annotate scripts/statistics/tests/hotelling_test_2.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | 1c89599167a6 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13752
diff
changeset
|
1 ## Copyright (C) 1996-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 -*- |
3499 | 20 ## @deftypefn {Function File} {[@var{pval}, @var{tsq}] =} hotelling_test_2 (@var{x}, @var{y}) |
3454 | 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})}. | |
3200 | 25 ## |
6754 | 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 | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
33 ## |
3454 | 34 ## @example |
35 ## (n_x+n_y-p-1) T^2 / (p(n_x+n_y-2)) | |
36 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
37 ## |
6754 | 38 ## @end ifnottex |
3454 | 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}. | |
3200 | 45 ## |
46 ## If no output argument is given, the p-value of the test is displayed. | |
3454 | 47 ## @end deftypefn |
3200 | 48 |
5428 | 49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 50 ## Description: Compare means of two multivariate normals |
3426 | 51 |
3200 | 52 function [pval, Tsq] = hotelling_test_2 (x, y) |
3426 | 53 |
3200 | 54 if (nargin != 2) |
6046 | 55 print_usage (); |
3200 | 56 endif |
3426 | 57 |
4030 | 58 if (isvector (x)) |
3200 | 59 n_x = length (x); |
4030 | 60 if (! 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:
10821
diff
changeset
|
61 error ("hotelling_test_2: if X is a vector, Y must also be a vector"); |
3200 | 62 else |
63 n_y = length (y); | |
64 p = 1; | |
65 endif | |
4030 | 66 elseif (ismatrix (x)) |
3200 | 67 [n_x, p] = size (x); |
68 [n_y, q] = size (y); | |
69 if (p != q) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
70 error ("hotelling_test_2: X and Y must have the same number of columns"); |
3200 | 71 endif |
72 else | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
73 error ("hotelling_test_2: X and Y must be matrices (or vectors)"); |
3200 | 74 endif |
3426 | 75 |
3200 | 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'); | |
13752
6f068e3f3f9c
Change f_cdf references to fcdf in statistics/test directory (Bug #34628)
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
79 pval = 1 - fcdf ((n_x + n_y - p - 1) * Tsq / (p * (n_x + n_y - 2)), |
3426 | 80 p, n_x + n_y - p - 1); |
81 | |
3200 | 82 if (nargout == 0) |
3456 | 83 printf (" pval: %g\n", pval); |
3200 | 84 endif |
3426 | 85 |
6754 | 86 endfunction |
17346
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
87 |