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.m < prev    next >
Text File  |  1997-06-01  |  3KB  |  98 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:  
  18. ##   [pval, ks] = kolmogorov_smirnov_test (x, dist [, params] [, alt])
  19. ##
  20. ##
  21. ## Performs a Kolmogorov-Smirnov test of the null hypothesis that the
  22. ## sample x comes from the (continuous) distribution dist. I.e., if F
  23. ## and G are the CDFs corresponding to the sample and dist, respectively,
  24. ## then the null is that F == G.
  25. ##
  26. ## The optional argument params contains a list of parameters of dist.
  27. ## E.g., to test whether a sample x comes from a uniform distribution on
  28. ## [2,4], use `kolmogorov_smirnov_test(x, "uniform", 2, 4)'.
  29. ##
  30. ## With the optional argument string alt, the alternative of interest
  31. ## can be selected. If alt is "!=" or "<>", the null is tested against
  32. ## the two-sided alternative F != G.  In this case, the test statistic
  33. ## ks follows a two-sided Kolmogorov-Smirnov distribution.
  34. ## If alt is ">", the one-sided alternative F > G is considered,
  35. ## similarly for "<".  In this case, the test statistic ks has a
  36. ## one-sided Kolmogorov-Smirnov distribution.
  37. ## The default is the two-sided case.
  38. ##
  39. ## pval is the p-value of the test.
  40. ##
  41. ## If no output argument is given, the p-value is displayed.
  42.  
  43. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  44. ## Description:  One-sample Kolmogorov-Smirnov test
  45.  
  46. function [pval, ks] = kolmogorov_smirnov_test (x, dist, ...)
  47.   
  48.   if (nargin < 2)
  49.     error (sprintf (["usage:\n\t", ...
  50.     "[pval, ks] = ", ...
  51.     "kolmogorov_smirnov_test (x, dist, [, params] [, alt])"]));
  52.   endif
  53.  
  54.   if (! is_vector (x))
  55.     error ("kolmogorov_smirnov_test:  x must be a vector.");
  56.   endif
  57.  
  58.   n = length (x);
  59.   s = sort (x);
  60.   f = sprintf ("%s_cdf", dist);
  61.  
  62.   alt  = "!=";
  63.   
  64.   if (nargin == 2)
  65.     z = reshape (feval (f, s), 1, n);
  66.   else
  67.     args = "";
  68.     for k = 1 : (nargin-2);
  69.       tmp  = va_arg ();
  70.       if isstr (tmp)
  71.     alt = tmp;
  72.       else
  73.     args = sprintf ("%s, %g", args, tmp);
  74.       endif
  75.     endfor
  76.     z = reshape (eval (sprintf ("%s(s%s);", f, args)), 1, n);
  77.   endif    
  78.  
  79.   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
  80.     ks   = sqrt(n) * max(max([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)]));
  81.     pval = 1 - kolmogorov_smirnov_cdf (ks);
  82.   elseif (strcmp (alt, ">"))
  83.     ks   = sqrt(n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n]));
  84.     pval = exp(- 2 * ks^2);
  85.   elseif (strcmp (alt, "<"))
  86.     ks   = - sqrt(n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n]));
  87.     pval = exp(- 2 * ks^2);
  88.   else
  89.     error (sprintf (["kolmogorov_smirnov_test:  ", ...
  90.     "alternative %s not recognized"], alt));
  91.   endif
  92.     
  93.   if (nargout == 0)
  94.     printf ("pval:  %g\n", pval);
  95.   endif
  96.   
  97. endfunction
  98.