home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / welch_test.m < prev    next >
Text File  |  1997-06-01  |  3KB  |  83 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, t, df] = welch_test (x, y [, alt])
  18. ##
  19. ## For two samples x and y from normal distributions with unknown means
  20. ## and unknown and not necessarily equal variances, perform a Welch test
  21. ## of the null hypothesis of equal means.
  22. ## Under the null, the test statistic t approximately follows a Student
  23. ## distribution with df 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 mean(x) != m.
  29. ## If alt is ">", the one-sided alternative mean(x) > m is considered,
  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:  Welch two-sample t test
  39.  
  40. function [pval, t, df] = welch_test (x, y, alt)
  41.   
  42.   if ((nargin < 2) || (nargin > 3))
  43.     usage ("[pval, t, df] = welch_test (x, y [, alt])");
  44.   endif
  45.     
  46.   if (! (is_vector (x) && is_vector (y)))
  47.     error ("welch_test:  both x and y must be vectors");
  48.   endif
  49.  
  50.   n_x  = length (x);
  51.   n_y  = length (y);
  52.   mu_x = sum (x) / n_x;
  53.   mu_y = sum (y) / n_y;
  54.   v_x  = sumsq (x - mu_x) / (n_x * (n_x - 1));
  55.   v_y  = sumsq (x - mu_y) / (n_y * (n_y - 1));
  56.   c    = v_x / (v_x + v_y);
  57.   df   = 1 / ( c^2 / (n_x - 1) + (1 - c)^2 / (n_y - 1));
  58.   t    = (mu_x - mu_y) / sqrt (v_x + v_y);
  59.   cdf  = t_cdf (t, df);
  60.  
  61.   if (nargin == 2)
  62.     alt  = "!=";
  63.   endif
  64.   
  65.   if (! isstr (alt))
  66.     error ("welch_test:  alt must be a string");
  67.   endif
  68.   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
  69.     pval = 2 * min (cdf, 1 - cdf);
  70.   elseif (strcmp (alt, ">"))
  71.     pval = 1 - cdf;
  72.   elseif (strcmp (alt, "<"))
  73.     pval = cdf;
  74.   else
  75.     error (sprintf ("welch_test:  option %s not recognized", alt));
  76.   endif
  77.   
  78.   if (nargout == 0)
  79.     printf ("  pval:  %g\n", pval);
  80.   endif
  81.  
  82. endfunction
  83.