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