changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/tests/bartlett_test.m

changeset 10289: 4b124317dc38
parent:93c65f2a5668
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (46 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
2## Kurt Hornik
3##
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {[@var{pval}, @var{chisq}, @var{df}] =} bartlett_test (@var{x1}, @dots{})
22## Perform a Bartlett test for the homogeneity of variances in the data
23## vectors @var{x1}, @var{x2}, @dots{}, @var{xk}, where @var{k} > 1.
24##
25## Under the null of equal variances, the test statistic @var{chisq}
26## approximately follows a chi-square distribution with @var{df} degrees of
27## freedom.
28##
29## The p-value (1 minus the CDF of this distribution at @var{chisq}) is
30## returned in @var{pval}.
31##
32## If no output argument is given, the p-value is displayed.
33## @end deftypefn
34
35## Author: KH <Kurt.Hornik@wu-wien.ac.at>
36## Description: Bartlett test for homogeneity of variances
37
38function [pval, chisq, df] = bartlett_test (varargin)
39
40 k = nargin;
41 if (k < 2)
42 print_usage ();
43 endif
44
45 f = zeros (k, 1);
46 v = zeros (k, 1);
47
48 for i = 1 : k;
49 x = varargin{i};
50 if (! isvector (x))
51 error ("bartlett_test: all arguments must be vectors");
52 endif
53 f(i) = length (x) - 1;
54 v(i) = var (x);
55 endfor
56
57 f_tot = sum (f);
58 v_tot = sum (f .* v) / f_tot;
59 c = 1 + (sum (1 ./ f) - 1 / f_tot) / (3 * (k - 1));
60 chisq = (f_tot * log (v_tot) - sum (f .* log (v))) / c;
61 df = k;
62 pval = 1 - chisquare_cdf (chisq, df);
63
64 if (nargout == 0)
65 printf(" pval: %g\n", pval);
66 endif
67
68endfunction