3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3200
|
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. |
3426
|
7 ## |
3200
|
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 |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3200
|
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 |
3454
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{pval}, @var{ks}] =} kolmogorov_smirnov_test_2 (@var{x}, @var{y}, @var{alt}) |
|
19 ## Perform a 2-sample Kolmogorov-Smirnov test of the null hypothesis |
|
20 ## that the samples @var{x} and @var{y} come from the same (continuous) |
|
21 ## distribution. I.e., if F and G are the CDFs corresponding to the |
|
22 ## @var{x} and @var{y} samples, respectively, then the null is that F == |
|
23 ## G. |
3200
|
24 ## |
3454
|
25 ## With the optional argument string @var{alt}, the alternative of |
|
26 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
27 ## @code{"<>"}, the null is tested against the two-sided alternative F |
|
28 ## != G. In this case, the test statistic @var{ks} follows a two-sided |
|
29 ## Kolmogorov-Smirnov distribution. If @var{alt} is @code{">"}, the |
|
30 ## one-sided alternative F > G is considered, similarly for @code{"<"}. |
|
31 ## In this case, the test statistic @var{ks} has a one-sided |
|
32 ## Kolmogorov-Smirnov distribution. The default is the two-sided case. |
3200
|
33 ## |
3454
|
34 ## The p-value of the test is returned in @var{pval}. |
3200
|
35 ## |
3426
|
36 ## If no output argument is given, the p-value is displayed. |
3454
|
37 ## @end deftypefn |
3200
|
38 |
3456
|
39 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
40 ## Description: Two-sample Kolmogorov-Smirnov test |
3200
|
41 |
|
42 function [pval, ks] = kolmogorov_smirnov_test_2 (x, y, alt) |
3426
|
43 |
3200
|
44 if (nargin < 2 || nargin > 3) |
3456
|
45 usage ("[pval, ks] = kolmogorov_smirnov_test_2 (x, y, tol)"); |
3200
|
46 endif |
|
47 |
|
48 if !( is_vector (x) && is_vector (y)) |
3456
|
49 error ("kolmogorov_smirnov_test_2: both x and y must be vectors."); |
3200
|
50 endif |
|
51 |
|
52 if (nargin == 2) |
|
53 alt = "!="; |
3426
|
54 else |
3200
|
55 if (! isstr (alt)) |
3456
|
56 error ("kolmogorov_smirnov_test_2: alt must be a string."); |
3200
|
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 |
3456
|
78 error ("kolmogorov_smirnov_test_2: option %s not recognized", alt); |
3200
|
79 endif |
3426
|
80 |
3200
|
81 if (nargout == 0) |
3456
|
82 printf (" pval: %g\n", pval); |
3200
|
83 endif |
|
84 |
|
85 endfunction |