annotate scripts/statistics/tests/hotelling_test_2.m @ 23084:ef4d915df748

maint: Merge stable to default.
author John W. Eaton <jwe@octave.org>
date Mon, 23 Jan 2017 14:27:48 -0500
parents 3a2b891d0b33 e9a0469dedd9
children 092078913d54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 21634
diff changeset
1 ## Copyright (C) 1996-2016 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
6 ## under the terms of the GNU General Public License as published by
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
8 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19697
diff changeset
20 ## @deftypefn {} {[@var{pval}, @var{tsq}] =} hotelling_test_2 (@var{x}, @var{y})
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
21 ## For two samples @var{x} from multivariate normal distributions with
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
22 ## the same number of variables (columns), unknown means and unknown
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
23 ## equal covariance matrices, test the null hypothesis @code{mean
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
24 ## (@var{x}) == mean (@var{y})}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
25 ##
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
26 ## Hotelling's two-sample @math{T^2} is returned in @var{tsq}. Under the null,
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
27 ## @tex
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
28 ## $$
18679
a142f35f3cb6 doc: Fix unbalanced parentheses in documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## {(n_x+n_y-p-1) T^2 \over p(n_x+n_y-2)}
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
30 ## $$
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
31 ## @end tex
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
32 ## @ifnottex
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
33 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
34 ## @example
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
35 ## (n_x+n_y-p-1) T^2 / (p(n_x+n_y-2))
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
36 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
37 ##
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
38 ## @end ifnottex
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
39 ## @noindent
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
40 ## has an F distribution with @math{p} and @math{n_x+n_y-p-1} degrees of
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
41 ## freedom, where @math{n_x} and @math{n_y} are the sample sizes and
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
42 ## @math{p} is the number of variables.
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
43 ##
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
44 ## The p-value of the test is returned in @var{pval}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
45 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
46 ## If no output argument is given, the p-value of the test is displayed.
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
47 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
48
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
50 ## Description: Compare means of two multivariate normals
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
51
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
52 function [pval, Tsq] = hotelling_test_2 (x, y)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
53
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
54 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
55 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
56 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
57
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3922
diff changeset
58 if (isvector (x))
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
59 n_x = length (x);
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3922
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
62 else
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
63 n_y = length (y);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
64 p = 1;
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65 endif
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3922
diff changeset
66 elseif (ismatrix (x))
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
67 [n_x, p] = size (x);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
68 [n_y, q] = size (y);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
71 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
74 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
75
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
76 d = mean (x) - mean (y);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
77 S = ((n_x - 1) * cov (x) + (n_y - 1) * cov (y)) / (n_x + n_y - 2);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
80 p, n_x + n_y - p - 1);
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
81
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
82 if (nargout == 0)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
83 printf (" pval: %g\n", pval);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
84 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
85
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
86 endfunction