7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007 |
|
2 ## Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_num}, @var{df_den}] =} var_test (@var{x}, @var{y}, @var{alt}) |
|
22 ## For two samples @var{x} and @var{y} from normal distributions with |
|
23 ## unknown means and unknown variances, perform an F-test of the null |
6754
|
24 ## hypothesis of equal variances. Under the null, the test statistic |
|
25 ## @var{f} follows an F-distribution with @var{df_num} and @var{df_den} |
|
26 ## degrees of freedom. |
3200
|
27 ## |
3454
|
28 ## With the optional argument string @var{alt}, the alternative of |
|
29 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
30 ## @code{"<>"}, the null is tested against the two-sided alternative |
|
31 ## @code{var (@var{x}) != var (@var{y})}. If @var{alt} is @code{">"}, |
|
32 ## the one-sided alternative @code{var (@var{x}) > var (@var{y})} is |
3457
|
33 ## used. Similarly for "<", the one-sided alternative @code{var |
|
34 ## (@var{x}) > var (@var{y})} is used. The default is the two-sided |
|
35 ## case. |
3200
|
36 ## |
3454
|
37 ## The p-value of the test is returned in @var{pval}. |
3426
|
38 ## |
3200
|
39 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
40 ## @end deftypefn |
3200
|
41 |
5428
|
42 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
43 ## Description: F test to compare two variances |
3200
|
44 |
|
45 function [pval, f, df_num, df_den] = var_test (x, y, alt) |
3426
|
46 |
3200
|
47 if ((nargin < 2) || (nargin > 3)) |
6046
|
48 print_usage (); |
3200
|
49 endif |
3426
|
50 |
4030
|
51 if (! (isvector (x) && isvector (y))) |
3456
|
52 error ("var_test: both x and y must be vectors"); |
3200
|
53 endif |
|
54 |
|
55 df_num = length (x) - 1; |
|
56 df_den = length (y) - 1; |
|
57 f = var (x) / var (y); |
|
58 cdf = f_cdf (f, df_num, df_den); |
3426
|
59 |
3200
|
60 if (nargin == 2) |
|
61 alt = "!="; |
|
62 endif |
3426
|
63 |
5443
|
64 if (! ischar (alt)) |
3456
|
65 error ("var_test: alt must be a string"); |
3200
|
66 endif |
|
67 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
68 pval = 2 * min (cdf, 1 - cdf); |
|
69 elseif (strcmp (alt, ">")) |
|
70 pval = 1 - cdf; |
|
71 elseif (strcmp (alt, "<")) |
|
72 pval = cdf; |
|
73 else |
3456
|
74 error ("var_test: option %s not recognized", alt); |
3200
|
75 endif |
3426
|
76 |
3200
|
77 if (nargout == 0) |
3456
|
78 printf ("pval: %g\n", pval); |
3200
|
79 endif |
|
80 |
|
81 endfunction |