]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/bartlett_test.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / bartlett_test.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{pval}, @var{chisq}, @var{df}] =} bartlett_test (@var{x1}, @dots{})
21 ## Perform a Bartlett test for the homogeneity of variances in the data
22 ## vectors @var{x1}, @var{x2}, @dots{}, @var{xk}, where @var{k} > 1.
23 ##
24 ## Under the null of equal variances, the test statistic @var{chisq}
25 ## approximately follows a chi-square distribution with @var{df} degrees of
26 ## freedom.
27 ##
28 ## The p-value (1 minus the CDF of this distribution at @var{chisq}) is
29 ## returned in @var{pval}.
30 ##
31 ## If no output argument is given, the p-value is displayed.
32 ## @end deftypefn
33
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Description: Bartlett test for homogeneity of variances
36
37 function [pval, chisq, df] = bartlett_test (varargin)
38
39   k = nargin;
40   if (k < 2)
41     print_usage ();
42   endif
43
44   f = zeros (k, 1);
45   v = zeros (k, 1);
46
47   for i = 1 : k;
48     x = varargin{i};
49     if (! isvector (x))
50       error ("bartlett_test: all arguments must be vectors");
51     endif
52     f(i) = length (x) - 1;
53     v(i) = var (x);
54   endfor
55
56   f_tot = sum (f);
57   v_tot = sum (f .* v) / f_tot;
58   c     = 1 + (sum (1 ./ f) - 1 / f_tot) / (3 * (k - 1));
59   chisq = (f_tot * log (v_tot) - sum (f .* log (v))) / c;
60   df    = k;
61   pval  = 1 - chi2cdf (chisq, df);
62
63   if (nargout == 0)
64     printf("  pval: %g\n", pval);
65   endif
66
67 endfunction