7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
|
2 ## 2007 Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3191
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3191
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
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 |
6754
|
25 ## $$ Q(x) = \sum_{k=-\infty}^\infty (-1)^k \exp(-2 k^2 x^2) $$ |
3456
|
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 |
5428
|
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
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) |
6046
|
49 print_usage (); |
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 |