home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / bartlett_test.m < prev    next >
Text File  |  1997-02-26  |  2KB  |  63 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  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.
  7. ## 
  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
  11. ## General Public License for more details. 
  12. ## 
  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.  
  17. ## usage:  [pval, chisq, df] = bartlett_test (x1, ...)
  18. ##
  19. ## Performs a Bartlett test for the homogeneity of variances in the data
  20. ## vectors x1, x2, ..., xk, where k > 1.
  21. ##
  22. ## Under the null of equal variances, the test statistic chisq
  23. ## approximately ollows a chi-square distribution with df degrees of
  24. ## freedom; pval is the p-value (1 minus the CDF of this distribution at
  25. ## chisq) of the test.
  26. ##
  27. ## If no output argument is given, the p-value is displayed.
  28.  
  29. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  30. ## Description:  Bartlett test for homogeneity of variances
  31.  
  32. function [pval, chisq, df] = bartlett_test (...)
  33.  
  34.   k = nargin;
  35.   if (k < 2)
  36.     usage ("[pval, chisq, df] = bartlett_test (x1, ...)");
  37.   endif
  38.  
  39.   f = zeros (k, 1);
  40.   v = zeros (k, 1);
  41.   va_start ();
  42.   for i = 1 : k;
  43.     x = va_arg ();
  44.     if (! is_vector (x))
  45.       error ("bartlett_test:  all arguments must be vectors");
  46.     endif
  47.     f(i) = length (x) - 1;
  48.     v(i) = var (x);
  49.   endfor
  50.  
  51.   f_tot = sum (f);
  52.   v_tot = sum (f .* v) / f_tot;
  53.   c     = 1 + (sum (1 ./ f) - 1 / f_tot) / (3 * (k - 1));
  54.   chisq = (f_tot * log (v_tot) - sum (f .* log (v))) / c;
  55.   df    = k;
  56.   pval  = 1 - chisquare_cdf (chisq, df);
  57.   
  58.   if (nargout == 0)
  59.     printf("  pval:  %g\n", pval);
  60.   endif
  61.  
  62. endfunction
  63.