comparison scripts/statistics/distributions/kolmogorov_smirnov_cdf.m @ 13171:19b9f17d22af

Overhaul of statistical distribution functions Support class "single" 75% reduction in memory usage More Matlab compatibility for corner cases * betacdf.m, betainv.m, betapdf.m, betarnd.m, binocdf.m, binoinv.m, binopdf.m, binornd.m, cauchy_cdf.m, cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m, chi2inv.m, chi2pdf.m, chi2rnd.m, discrete_cdf.m, discrete_inv.m, discrete_pdf.m, discrete_rnd.m, empirical_cdf.m, empirical_inv.m, empirical_pdf.m, empirical_rnd.m, expcdf.m, expinv.m, exppdf.m, exprnd.m, fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m, gaminv.m, gampdf.m, gamrnd.m, geocdf.m, geoinv.m, geopdf.m, geornd.m, hygecdf.m, hygeinv.m, hygepdf.m, hygernd.m, kolmogorov_smirnov_cdf.m, laplace_cdf.m, laplace_inv.m, laplace_pdf.m, laplace_rnd.m, logistic_cdf.m, logistic_inv.m, logistic_pdf.m, logistic_rnd.m, logncdf.m, logninv.m, lognpdf.m, lognrnd.m, nbincdf.m, nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m, norminv.m, normpdf.m, normrnd.m, poisscdf.m, poissinv.m, poisspdf.m, poissrnd.m, stdnormal_cdf.m, stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m, tcdf.m, tinv.m, tpdf.m, trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m, unifcdf.m, unifinv.m, unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m, wblrnd.m: Return "single" outputs for "single" inputs, Use logical indexing rather than find() for 75% memory savings, Add tests for all functions, Use consistent documentation across all functions, More Matlab compatibilitcy for corner cases.
author Rik <octave@nomad.inbox5.com>
date Tue, 20 Sep 2011 12:13:13 -0700
parents fd0a3ac60b0e
children 72c96de7a403
comparison
equal deleted inserted replaced
13169:078729410a0d 13171:19b9f17d22af
1 ## Copyright (C) 2011 Rik Wehbring
1 ## Copyright (C) 1995-2011 Kurt Hornik 2 ## Copyright (C) 1995-2011 Kurt Hornik
2 ## 3 ##
3 ## This file is part of Octave. 4 ## This file is part of Octave.
4 ## 5 ##
5 ## Octave is free software; you can redistribute it and/or modify it 6 ## Octave is free software; you can redistribute it and/or modify it
16 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
18 19
19 ## -*- texinfo -*- 20 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} kolmogorov_smirnov_cdf (@var{x}, @var{tol}) 21 ## @deftypefn {Function File} {} kolmogorov_smirnov_cdf (@var{x}, @var{tol})
21 ## Return the CDF at @var{x} of the Kolmogorov-Smirnov distribution, 22 ## Return the cumulative distribution function (CDF) at @var{x} of the
23 ## Kolmogorov-Smirnov distribution,
22 ## @tex 24 ## @tex
23 ## $$ Q(x) = \sum_{k=-\infty}^\infty (-1)^k \exp(-2 k^2 x^2) $$ 25 ## $$ Q(x) = \sum_{k=-\infty}^\infty (-1)^k \exp (-2 k^2 x^2) $$
24 ## @end tex 26 ## @end tex
25 ## @ifnottex 27 ## @ifnottex
26 ## 28 ##
27 ## @example 29 ## @example
28 ## @group 30 ## @group
29 ## Inf 31 ## Inf
30 ## Q(x) = SUM (-1)^k exp(-2 k^2 x^2) 32 ## Q(x) = SUM (-1)^k exp (-2 k^2 x^2)
31 ## k = -Inf 33 ## k = -Inf
32 ## @end group 34 ## @end group
33 ## @end example 35 ## @end example
34 ## 36 ##
35 ## @end ifnottex 37 ## @end ifnottex
59 if (! (isscalar (tol) && (tol > 0))) 61 if (! (isscalar (tol) && (tol > 0)))
60 error ("kolmogorov_smirnov_cdf: TOL must be a positive scalar"); 62 error ("kolmogorov_smirnov_cdf: TOL must be a positive scalar");
61 endif 63 endif
62 endif 64 endif
63 65
64 n = numel (x); 66 if (numel (x) == 0)
65 if (n == 0)
66 error ("kolmogorov_smirnov_cdf: X must not be empty"); 67 error ("kolmogorov_smirnov_cdf: X must not be empty");
67 endif 68 endif
68 69
69 cdf = zeros (size (x)); 70 cdf = zeros (size (x));
70 71
71 ind = find (x > 0); 72 ind = find (x > 0);
72 if (length (ind) > 0) 73 if (length (ind) > 0)
73 if (size(ind,2) < size(ind,1)) 74 if (columns (ind) < rows (ind))
74 y = x(ind.'); 75 y = x(ind.');
75 else 76 else
76 y = x(ind); 77 y = x(ind);
77 endif 78 endif
78 K = ceil (sqrt (- log (tol) / 2) / min (y)); 79 K = ceil (sqrt (- log (tol) / 2) / min (y));
79 k = (1:K)'; 80 k = (1:K)';
80 A = exp (- 2 * k.^2 * y.^2); 81 A = exp (- 2 * k.^2 * y.^2);
81 odd = find (rem (k, 2) == 1); 82 odd = find (rem (k, 2) == 1);
82 A(odd,:) = -A(odd,:); 83 A(odd,:) = -A(odd,:);
83 cdf(ind) = 1 + 2 * sum (A); 84 cdf(ind) = 1 + 2 * sum (A);
84 endif 85 endif
85 86
86 endfunction 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