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