3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
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 ## |
3191
|
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 ## |
3191
|
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 |
|
17 ## usage: kolmogorov_smirnov_cdf (x [, tol]) |
|
18 ## |
|
19 ## Returns the CDF at x of the Kolmogorov-Smirnov distribution, |
|
20 ## i.e. Q(x) = sum_{k=-\infty}^\infty (-1)^k exp(-2 k^2 x^2), x > 0. |
|
21 ## |
|
22 ## The optional tol specifies the precision up to which the series |
|
23 ## should be evaluated; the default is tol = eps. |
3426
|
24 |
3191
|
25 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
26 ## Description: CDF of the Kolmogorov-Smirnov distribution |
|
27 |
|
28 function cdf = kolmogorov_smirnov_cdf (x, tol) |
3426
|
29 |
3191
|
30 if (nargin < 1 || nargin > 2) |
|
31 usage ("kolmogorov_smirnov_cdf (x [, tol])"); |
|
32 endif |
|
33 |
|
34 if (nargin == 1) |
|
35 tol = eps; |
3426
|
36 else |
3191
|
37 if (!is_scalar (tol) || !(tol > 0)) |
|
38 error (["kolmogorov_smirnov_cdf: ", ... |
3426
|
39 "tol has to be a positive scalar."]); |
3191
|
40 endif |
|
41 endif |
|
42 |
|
43 [nr, nc] = size(x); |
|
44 if (min (nr, nc) == 0) |
|
45 error ("kolmogorov_smirnov_cdf: x must not be empty."); |
|
46 endif |
|
47 |
|
48 n = nr * nc; |
|
49 x = reshape (x, 1, n); |
|
50 cdf = zeros (1, n); |
|
51 ind = find (x > 0); |
|
52 if (length (ind) > 0) |
|
53 y = x(ind); |
|
54 K = ceil( sqrt( - log (tol) / 2 ) / min (y) ); |
|
55 k = (1:K)'; |
|
56 A = exp( - 2 * k.^2 * y.^2 ); |
|
57 odd = find (rem (k, 2) == 1); |
|
58 A(odd, :) = -A(odd, :); |
|
59 cdf(ind) = 1 + 2 * sum (A); |
|
60 endif |
|
61 |
|
62 cdf = reshape (cdf, nr, nc); |
3426
|
63 |
3191
|
64 endfunction |