]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/kolmogorov_smirnov_cdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / kolmogorov_smirnov_cdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
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
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} kolmogorov_smirnov_cdf (@var{x}, @var{tol})
22 ## Return the cumulative distribution function (CDF) at @var{x} of the 
23 ## Kolmogorov-Smirnov distribution,
24 ## @tex
25 ## $$ Q(x) = \sum_{k=-\infty}^\infty (-1)^k \exp (-2 k^2 x^2) $$
26 ## @end tex
27 ## @ifnottex
28 ##
29 ## @example
30 ## @group
31 ##          Inf
32 ## Q(x) =   SUM    (-1)^k exp (-2 k^2 x^2)
33 ##        k = -Inf
34 ## @end group
35 ## @end example
36 ##
37 ## @end ifnottex
38 ## @noindent
39 ## for @var{x} > 0.
40 ##
41 ## The optional parameter @var{tol} specifies the precision up to which
42 ## the series should be evaluated; the default is @var{tol} = @code{eps}.
43 ## @end deftypefn
44
45 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
46 ## Description: CDF of the Kolmogorov-Smirnov distribution
47
48 function cdf = kolmogorov_smirnov_cdf (x, tol)
49
50   if (nargin < 1 || nargin > 2)
51     print_usage ();
52   endif
53
54   if (nargin == 1)
55     if (isa (x, "single"))
56       tol = eps ("single");
57     else
58       tol = eps;
59     endif
60   else
61     if (! (isscalar (tol) && (tol > 0)))
62       error ("kolmogorov_smirnov_cdf: TOL must be a positive scalar");
63     endif
64   endif
65
66   if (numel (x) == 0)
67     error ("kolmogorov_smirnov_cdf: X must not be empty");
68   endif
69
70   cdf = zeros (size (x));
71
72   ind = find (x > 0);
73   if (length (ind) > 0)
74     if (columns (ind) < rows (ind))
75       y = x(ind.');
76     else
77       y = x(ind);
78     endif
79     K   = ceil (sqrt (- log (tol) / 2) / min (y));
80     k   = (1:K)';
81     A   = exp (- 2 * k.^2 * y.^2);
82     odd = find (rem (k, 2) == 1);
83     A(odd,:) = -A(odd,:);
84     cdf(ind) = 1 + 2 * sum (A);
85   endif
86
87 endfunction
88
89
90 %% Test input validation
91 %!error kolmogorov_smirnov_cdf ()
92 %!error kolmogorov_smirnov_cdf (1,2,3)
93 %!error kolmogorov_smirnov_cdf (1, ones(2))
94 %!error kolmogorov_smirnov_cdf ([], 1)
95