]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/kolmogorov_smirnov_test_2.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / kolmogorov_smirnov_test_2.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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 ==
25 ## G.
26 ##
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.
36 ##
37 ## The p-value of the test is returned in @var{pval}.
38 ##
39 ## The third returned value, @var{d}, is the test statistic, the maximum
40 ## vertical distance between the two cumulative distribution functions.
41 ##
42 ## If no output argument is given, the p-value is displayed.
43 ## @end deftypefn
44
45 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
46 ## Description: Two-sample Kolmogorov-Smirnov test
47
48 function [pval, ks, d] = kolmogorov_smirnov_test_2 (x, y, alt)
49
50   if (nargin < 2 || nargin > 3)
51     print_usage ();
52   endif
53
54   if (! (isvector (x) && isvector (y)))
55     error ("kolmogorov_smirnov_test_2: both X and Y must be vectors");
56   endif
57
58   if (nargin == 2)
59     alt = "!=";
60   else
61     if (! ischar (alt))
62       error ("kolmogorov_smirnov_test_2: ALT must be a string");
63     endif
64   endif
65
66   n_x = length (x);
67   n_y = length (y);
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;
74
75   z = cumsum (count);
76   ds = diff (s);
77   if (any (ds == 0))
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];
81     z = z(elems);
82   endif
83
84   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
85     d    = max (abs (z));
86     ks   = sqrt (n) * d;
87     pval = 1 - kolmogorov_smirnov_cdf (ks);
88   elseif (strcmp (alt, ">"))
89     d    = max (z);
90     ks   = sqrt (n) * d;
91     pval = exp (-2 * ks^2);
92   elseif (strcmp (alt, "<"))
93     d    = min (z);
94     ks   = -sqrt (n) * d;
95     pval = exp (-2 * ks^2);
96   else
97     error ("kolmogorov_smirnov_test_2: option %s not recognized", alt);
98   endif
99
100   if (nargout == 0)
101     printf ("  pval: %g\n", pval);
102   endif
103
104 endfunction