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