home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / z_test_2.m < prev    next >
Text File  |  1997-06-01  |  3KB  |  86 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] = z_test_2 (x, y, v_x, v_y [, alt])
  18. ##
  19. ## For two samples x and y from normal distributions with unknown
  20. ## means and known variances v_x and v_y, perform a Z-test of the
  21. ## hypothesis of equal means.
  22. ## Under the null, the test statistic z follows a standard normal
  23. ## distribution.
  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
  30. ## used, 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. ## along with some information.
  37.  
  38. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  39. ## Description:  Compare means of two normal samples with known variances
  40.  
  41. function [pval, z] = z_test_2 (x, y, v_x, v_y, alt)
  42.   
  43.   if ((nargin < 4) || (nargin > 5))
  44.     usage ("[pval, z] = z_test_2 (x, y, v_x, v_y [, alt])");
  45.   endif
  46.     
  47.   if (! (is_vector (x) && is_vector (y)))
  48.     error("z_test_2:  both x and y must be vectors");
  49.   elseif (! (is_scalar (v_x) && (v_x > 0)
  50.          && is_scalar (v_y) && (v_y > 0)))
  51.     error ("z_test_2:  both v_x and v_y must be positive scalars.");
  52.   endif
  53.   
  54.   n_x  = length (x);
  55.   n_y  = length (y);
  56.   mu_x = sum (x) / n_x;
  57.   mu_y = sum (y) / n_y;
  58.   z    = (mu_x - mu_y) / sqrt (v_x / n_x + v_y / n_y);
  59.   cdf  = stdnormal_cdf (z);
  60.   
  61.   if (nargin == 4)
  62.     alt = "!=";
  63.   endif
  64.  
  65.   if (! isstr (alt))
  66.     error ("z_test_2:  alt must be a string");
  67.   elseif (strcmp (alt, "!=") || strcmp (alt, "<>"))
  68.     pval = 2 * min (cdf, 1 - cdf);
  69.   elseif (strcmp (alt, ">"))
  70.     pval = 1 - cdf;
  71.   elseif (strcmp (alt, "<"))
  72.     pval = cdf;
  73.   else
  74.     error (sprintf ("z_test_2:  option %s not recognized", alt));
  75.   endif
  76.   
  77.   if (nargout == 0)
  78.     s = strcat ("Two-sample Z-test of mean(x) == mean(y) against ",
  79.         "mean(x) %s mean(y),\n",
  80.         "with known var(x) == %g and var(y) == %g:\n",
  81.         "  pval = %g\n");
  82.     printf (s, alt, v_x, v_y, pval);
  83.   endif
  84.  
  85. endfunction
  86.