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