home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / var_test.m < prev    next >
Text File  |  1997-06-01  |  2KB  |  77 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, f, df_num, df_den] = var_test (x, y [, alt])
  18. ##
  19. ## For two samples x and y from normal distributions with unknown 
  20. ## means and unknown variances, perform an F-test of the null
  21. ## hypothesis of equal variances.
  22. ## Under the null, the test statistic f follows an F-distribution
  23. ## with df_num and df_den degrees of freedom.
  24. ##
  25. ## With the optional argument string alt, the alternative of interest
  26. ## can be selected.  
  27. ## If alt is "!=" or "<>", the null is tested against the two-sided
  28. ## alternative var(x) != var(y).
  29. ## If alt is ">", the one-sided alternative var(x) > var(y) is used,
  30. ## similarly for "<".  
  31. ## The default is the two-sided case.
  32. ##
  33. ## pval is the p-value of the test.
  34. ##  
  35. ## If no output argument is given, the p-value of the test is displayed.
  36.  
  37. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  38. ## Description:  F test to compare two variances
  39.  
  40. function [pval, f, df_num, df_den] = var_test (x, y, alt)
  41.   
  42.   if ((nargin < 2) || (nargin > 3))
  43.     usage ("[pval, f, df_num, df_den] = var_test (x, y [, alt])");
  44.   endif
  45.     
  46.   if (! (is_vector (x) && is_vector (y)))
  47.     error ("var_test:  both x and y must be vectors");
  48.   endif
  49.  
  50.   df_num = length (x) - 1;
  51.   df_den = length (y) - 1;
  52.   f      = var (x) / var (y);
  53.   cdf    = f_cdf (f, df_num, df_den);
  54.   
  55.   if (nargin == 2)
  56.     alt  = "!=";
  57.   endif
  58.     
  59.   if (! isstr (alt))
  60.     error ("var_test:  alt must be a string");
  61.   endif
  62.   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
  63.     pval = 2 * min (cdf, 1 - cdf);
  64.   elseif (strcmp (alt, ">"))
  65.     pval = 1 - cdf;
  66.   elseif (strcmp (alt, "<"))
  67.     pval = cdf;
  68.   else
  69.     error (sprintf ("var_test:  option %s not recognized", alt));
  70.   endif
  71.   
  72.   if (nargout == 0)
  73.     printf ("pval:  %g\n", pval);
  74.   endif
  75.  
  76. endfunction
  77.