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