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