3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3454
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{pval}, @var{ks}] =} kolmogorov_smirnov_test (@var{x}, @var{dist}, @var{params}, @var{alt}) |
|
21 ## Perform a Kolmogorov-Smirnov test of the null hypothesis that the |
|
22 ## sample @var{x} comes from the (continuous) distribution dist. I.e., |
|
23 ## if F and G are the CDFs corresponding to the sample and dist, |
|
24 ## respectively, then the null is that F == G. |
3200
|
25 ## |
3454
|
26 ## The optional argument @var{params} contains a list of parameters of |
|
27 ## @var{dist}. For example, to test whether a sample @var{x} comes from |
|
28 ## a uniform distribution on [2,4], use |
3200
|
29 ## |
3454
|
30 ## @example |
|
31 ## kolmogorov_smirnov_test(x, "uniform", 2, 4) |
|
32 ## @end example |
3200
|
33 ## |
6754
|
34 ## @noindent |
|
35 ## @var{dist} can be any string for which a function @var{dist_cdf} |
|
36 ## that calculates the CDF of distribution @var{dist} exists. |
|
37 ## |
3454
|
38 ## With the optional argument string @var{alt}, the alternative of |
|
39 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
40 ## @code{"<>"}, the null is tested against the two-sided alternative F |
|
41 ## != G. In this case, the test statistic @var{ks} follows a two-sided |
|
42 ## Kolmogorov-Smirnov distribution. If @var{alt} is @code{">"}, the |
3457
|
43 ## one-sided alternative F > G is considered. Similarly for @code{"<"}, |
|
44 ## the one-sided alternative F > G is considered. In this case, the |
|
45 ## test statistic @var{ks} has a one-sided Kolmogorov-Smirnov |
|
46 ## distribution. The default is the two-sided case. |
3200
|
47 ## |
3454
|
48 ## The p-value of the test is returned in @var{pval}. |
3200
|
49 ## |
|
50 ## If no output argument is given, the p-value is displayed. |
3454
|
51 ## @end deftypefn |
3200
|
52 |
5428
|
53 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
54 ## Description: One-sample Kolmogorov-Smirnov test |
3200
|
55 |
3979
|
56 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin) |
3426
|
57 |
3200
|
58 if (nargin < 2) |
6046
|
59 print_usage (); |
3200
|
60 endif |
|
61 |
4030
|
62 if (! isvector (x)) |
3458
|
63 error ("kolmogorov_smirnov_test: x must be a vector"); |
3200
|
64 endif |
|
65 |
|
66 n = length (x); |
|
67 s = sort (x); |
4348
|
68 f = str2func (sprintf ("%s_cdf", dist)); |
3200
|
69 |
|
70 alt = "!="; |
3426
|
71 |
3200
|
72 if (nargin == 2) |
|
73 z = reshape (feval (f, s), 1, n); |
|
74 else |
|
75 args = ""; |
|
76 for k = 1 : (nargin-2); |
3979
|
77 tmp = varargin{k}; |
5443
|
78 if ischar (tmp) |
3426
|
79 alt = tmp; |
3200
|
80 else |
3426
|
81 args = sprintf ("%s, %g", args, tmp); |
3200
|
82 endif |
|
83 endfor |
4900
|
84 z = reshape (eval (sprintf ("%s(s%s);", func2str (f), args)), 1, n); |
3426
|
85 endif |
3200
|
86 |
|
87 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
3456
|
88 ks = sqrt (n) * max (max ([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)])); |
3200
|
89 pval = 1 - kolmogorov_smirnov_cdf (ks); |
|
90 elseif (strcmp (alt, ">")) |
3456
|
91 ks = sqrt (n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n])); |
|
92 pval = exp (- 2 * ks^2); |
3200
|
93 elseif (strcmp (alt, "<")) |
3456
|
94 ks = - sqrt (n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n])); |
|
95 pval = exp (- 2 * ks^2); |
3200
|
96 else |
3456
|
97 error ("kolmogorov_smirnov_test: alternative %s not recognized", alt); |
3200
|
98 endif |
3426
|
99 |
3200
|
100 if (nargout == 0) |
3456
|
101 printf ("pval: %g\n", pval); |
3200
|
102 endif |
3426
|
103 |
3200
|
104 endfunction |