1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{pval}, @var{ks}, @var{d}] =} kolmogorov_smirnov_test_2 (@var{x}, @var{y}, @var{alt})
21 ## Perform a 2-sample Kolmogorov-Smirnov test of the null hypothesis
22 ## that the samples @var{x} and @var{y} come from the same (continuous)
23 ## distribution. I.e., if F and G are the CDFs corresponding to the
24 ## @var{x} and @var{y} samples, respectively, then the null is that F ==
27 ## With the optional argument string @var{alt}, the alternative of
28 ## interest can be selected. If @var{alt} is @code{"!="} or
29 ## @code{"<>"}, the null is tested against the two-sided alternative F
30 ## != G@. In this case, the test statistic @var{ks} follows a two-sided
31 ## Kolmogorov-Smirnov distribution. If @var{alt} is @code{">"}, the
32 ## one-sided alternative F > G is considered. Similarly for @code{"<"},
33 ## the one-sided alternative F < G is considered. In this case, the
34 ## test statistic @var{ks} has a one-sided Kolmogorov-Smirnov
35 ## distribution. The default is the two-sided case.
37 ## The p-value of the test is returned in @var{pval}.
39 ## The third returned value, @var{d}, is the test statistic, the maximum
40 ## vertical distance between the two cumulative distribution functions.
42 ## If no output argument is given, the p-value is displayed.
45 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
46 ## Description: Two-sample Kolmogorov-Smirnov test
48 function [pval, ks, d] = kolmogorov_smirnov_test_2 (x, y, alt)
50 if (nargin < 2 || nargin > 3)
54 if (! (isvector (x) && isvector (y)))
55 error ("kolmogorov_smirnov_test_2: both X and Y must be vectors");
62 error ("kolmogorov_smirnov_test_2: ALT must be a string");
68 n = n_x * n_y / (n_x + n_y);
69 x = reshape (x, n_x, 1);
70 y = reshape (y, n_y, 1);
71 [s, i] = sort ([x; y]);
72 count (find (i <= n_x)) = 1 / n_x;
73 count (find (i > n_x)) = - 1 / n_y;
78 ## There are some ties, so keep only those changes.
79 warning ("cannot compute correct p-values with ties");
80 elems = [find(ds); n_x+n_y];
84 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
87 pval = 1 - kolmogorov_smirnov_cdf (ks);
88 elseif (strcmp (alt, ">"))
91 pval = exp (-2 * ks^2);
92 elseif (strcmp (alt, "<"))
95 pval = exp (-2 * ks^2);
97 error ("kolmogorov_smirnov_test_2: option %s not recognized", alt);
101 printf (" pval: %g\n", pval);