3191
|
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 |
3191
|
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 ## |
3191
|
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/>. |
3191
|
18 |
3456
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} kolmogorov_smirnov_cdf (@var{x}, @var{tol}) |
|
21 ## Return the CDF at @var{x} of the Kolmogorov-Smirnov distribution, |
|
22 ## @iftex |
|
23 ## @tex |
6754
|
24 ## $$ Q(x) = \sum_{k=-\infty}^\infty (-1)^k \exp(-2 k^2 x^2) $$ |
3456
|
25 ## @end tex |
|
26 ## @end iftex |
|
27 ## @ifinfo |
|
28 ## @example |
|
29 ## Inf |
|
30 ## Q(x) = SUM (-1)^k exp(-2 k^2 x^2) |
|
31 ## k = -Inf |
|
32 ## @end example |
|
33 ## @end ifinfo |
3191
|
34 ## |
3456
|
35 ## @noindent |
|
36 ## for @var{x} > 0. |
|
37 ## |
|
38 ## The optional parameter @var{tol} specifies the precision up to which |
|
39 ## the series should be evaluated; the default is @var{tol} = @code{eps}. |
|
40 ## @end deftypefn |
3426
|
41 |
5428
|
42 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
43 ## Description: CDF of the Kolmogorov-Smirnov distribution |
3191
|
44 |
|
45 function cdf = kolmogorov_smirnov_cdf (x, tol) |
3426
|
46 |
3191
|
47 if (nargin < 1 || nargin > 2) |
6046
|
48 print_usage (); |
3191
|
49 endif |
|
50 |
|
51 if (nargin == 1) |
|
52 tol = eps; |
3426
|
53 else |
4030
|
54 if (! isscalar (tol) || ! (tol > 0)) |
3456
|
55 error ("kolmogorov_smirnov_cdf: tol has to be a positive scalar"); |
3191
|
56 endif |
|
57 endif |
|
58 |
4859
|
59 n = numel (x); |
|
60 if (n == 0) |
3456
|
61 error ("kolmogorov_smirnov_cdf: x must not be empty"); |
3191
|
62 endif |
|
63 |
4859
|
64 cdf = zeros (size (x)); |
|
65 |
3191
|
66 ind = find (x > 0); |
|
67 if (length (ind) > 0) |
4859
|
68 if (size(ind,2) < size(ind,1)) |
|
69 y = x(ind.'); |
|
70 else |
|
71 y = x(ind); |
|
72 endif |
3457
|
73 K = ceil (sqrt (- log (tol) / 2) / min (y)); |
3191
|
74 k = (1:K)'; |
3457
|
75 A = exp (- 2 * k.^2 * y.^2); |
3191
|
76 odd = find (rem (k, 2) == 1); |
3457
|
77 A(odd,:) = -A(odd,:); |
3191
|
78 cdf(ind) = 1 + 2 * sum (A); |
|
79 endif |
|
80 |
|
81 endfunction |