home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / kolmogorov_smirnov_test_2.m < prev    next >
Text File  |  1997-06-01  |  3KB  |  87 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, ks] = kolmogorov_smirnov_test_2 (x, y [, alt])
  18. ##
  19. ## Performs a 2-sample Kolmogorov-Smirnov test of the null hypothesis
  20. ## that the samples x and y come from the same (continuous) distribution.
  21. ## I.e., if F and G are the CDFs corresponding to the x and y samples,
  22. ## respectively, then the null is that F == G.
  23. ##
  24. ## With the optional argument string alt, the alternative of interest
  25. ## can be selected.  
  26. ## If alt is "!=" or "<>", the null is tested against the two-sided
  27. ## alternative F != G.  In this case, the test statistic ks follows a
  28. ## two-sided Kolmogorov-Smirnov distribution.
  29. ## If alt is ">", the one-sided alternative F > G is considered,
  30. ## similarly for "<".  In this case, the test statistic ks has a
  31. ## one-sided Kolmogorov-Smirnov distribution.
  32. ## The default is the two-sided case.
  33. ##
  34. ## pval is the p-value of the test.
  35. ##
  36. ## If no output argument is given, the p-value is displayed.  
  37.  
  38. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  39. ## Description:  Two-sample Kolmogorov-Smirnov test
  40.  
  41. function [pval, ks] = kolmogorov_smirnov_test_2 (x, y, alt)
  42.   
  43.   if (nargin < 2 || nargin > 3)
  44.     usage (strcat ("[pval, ks] = ",
  45.            "kolmogorov_smirnov_test_2 (x, y [, tol])"));
  46.   endif
  47.  
  48.   if !( is_vector (x) && is_vector (y))
  49.     error ("kolmogorov_smirnov_test_2:  both x and y must be vectors.");
  50.   endif
  51.  
  52.   if (nargin == 2)
  53.     alt = "!=";
  54.   else 
  55.     if (! isstr (alt))
  56.       error ("kolmogorov_smirnov_test_2:  alt must be a string.");
  57.     endif
  58.   endif
  59.  
  60.   n_x = length (x);
  61.   n_y = length (y);
  62.   n   = n_x * n_y / (n_x + n_y);
  63.   x   = reshape (x, n_x, 1);
  64.   y   = reshape (y, n_y, 1);
  65.   [s, i] = sort ([x; y]);
  66.   count (find (i <= n_x)) = 1 / n_x;
  67.   count (find (i > n_x)) = - 1 / n_y;
  68.   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
  69.     ks   = sqrt (n) * max (abs (cumsum (count)));
  70.     pval = 1 - kolmogorov_smirnov_cdf (ks);
  71.   elseif (strcmp (alt, ">"))
  72.     ks   = sqrt (n) * max (cumsum (count));
  73.     pval = exp(- 2 * ks^2);
  74.   elseif (strcmp(alt, "<"))
  75.     ks   = - sqrt (n) * min (cumsum (count));
  76.     pval = exp(- 2 * ks^2);
  77.   else
  78.     error (sprintf (["kolmogorov_smirnov_test_2:  ", ...
  79.              "option %s not recognized"], alt));
  80.   endif
  81.     
  82.   if (nargout == 0)
  83.     printf ("  pval:  %g\n", pval);
  84.   endif
  85.  
  86. endfunction
  87.