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

  1. ## Copyright (C) 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, Tsq] = hotelling_test_2 (x, y)
  18. ##
  19. ## For two samples x from multivariate normal distributions with the
  20. ## same number of variables (columns), unknown means and unknown equal
  21. ## covariance matrices, test the null hypothesis mean (x) == mean (y).
  22. ##
  23. ## Tsq is Hotelling's two-sample T^2.  Under the null, 
  24. ##    (n_x+n_y-p-1) T^2 / (p(n_x+n_y-2))
  25. ## has an F distribution with p and n_x+n_y-p-1 degrees of freedom,
  26. ## where n_x and n_y are the sample sizes and p is the number of
  27. ## variables.
  28. ##
  29. ## pval is the p-value of the test.
  30. ##
  31. ## If no output argument is given, the p-value of the test is displayed.
  32.  
  33. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  34. ## Description:  Compare means of two multivariate normals
  35.   
  36. function [pval, Tsq] = hotelling_test_2 (x, y)
  37.   
  38.   if (nargin != 2)
  39.     usage ("hotelling_test_2 (x, y)");
  40.   endif
  41.   
  42.   if (is_vector (x))
  43.     n_x = length (x);
  44.     if (! is_vector (y))
  45.       error ("hotelling_test_2:  If x is a vector, y must be too.");
  46.     else
  47.       n_y = length (y);
  48.       p   = 1;
  49.     endif
  50.   elseif (is_matrix (x))
  51.     [n_x, p] = size (x);
  52.     [n_y, q] = size (y);
  53.     if (p != q)
  54.       error (strcat ("hotelling_test_2:  ",
  55.              "x and y must have the same number of columns"));
  56.     endif
  57.   else
  58.     error ("hotelling_test_2:  x and y must be matrices (or vectors)");
  59.   endif
  60.   
  61.   d    = mean (x) - mean (y);
  62.   S    = ((n_x - 1) * cov (x) + (n_y - 1) * cov (y)) / (n_x + n_y - 2);
  63.   Tsq  = (n_x * n_y / (n_x + n_y)) * d * (S \ d');
  64.   pval = 1 - f_cdf ((n_x + n_y - p - 1) * Tsq / (p * (n_x + n_y - 2)),
  65.             p, n_x + n_y - p - 1);
  66.           
  67.   if (nargout == 0)
  68.     printf ("  pval:  %g\n", pval);
  69.   endif
  70.   
  71. endfunction