home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / u_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, z] = u_test (x, y [, alt])
  18. ##
  19. ## For two samples x and y, perform a Mann-Whitney U-test of the null
  20. ## hypothesis PROB(x > y) == 1/2 == PROB(x < y).  Under the null, the
  21. ## test statistic z approximately follows a standard normal
  22. ## distribution.  Note that this test is equivalent to the Wilcoxon
  23. ## rank-sum test.
  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 PROB(x > y) != 1/2.
  29. ## If alt is ">", the one-sided alternative PROB(x > y) > 1/2 is
  30. ## considered, 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. ## This implementation is still incomplete---for small sample sizes,
  38. ## the normal approximation is rather bad ...
  39.  
  40. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  41. ## Description:  Mann-Whitney U-test
  42.  
  43. function [pval, z] = u_test (x, y, alt)
  44.   
  45.   if ((nargin < 2) || (nargin > 3))
  46.     usage ("[pval, z] = u_test (x, y [, alt])");
  47.   endif
  48.     
  49.   if (! (is_vector (x) && is_vector (y)))
  50.     error ("u_test:  both x and y must be vectors");
  51.   endif
  52.  
  53.   n_x  = length (x);
  54.   n_y  = length (y);
  55.   r    = ranks ([reshape (x, 1, n_x), reshape (y, 1, n_y)]);
  56.   z    = (sum (r(1 : n_x)) - n_x * (n_x + n_y + 1) / 2) ...
  57.            / sqrt (n_x * n_y * (n_x + n_y + 1) / 12);  
  58.  
  59.   cdf  = stdnormal_cdf (z);
  60.   
  61.   if (nargin == 2)
  62.     alt  = "!=";
  63.   endif
  64.  
  65.   if (! isstr (alt))
  66.     error("u_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 = cdf;
  72.   elseif (strcmp (alt, "<"))
  73.     pval = 1 - cdf;
  74.   else
  75.     error (sprintf ("u_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.