]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/kolmogorov_smirnov_test.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / kolmogorov_smirnov_test.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{pval}, @var{ks}] =} kolmogorov_smirnov_test (@var{x}, @var{dist}, @var{params}, @var{alt})
21 ## Perform a Kolmogorov-Smirnov test of the null hypothesis that the
22 ## sample @var{x} comes from the (continuous) distribution dist.  I.e.,
23 ## if F and G are the CDFs corresponding to the sample and dist,
24 ## respectively, then the null is that F == G.
25 ##
26 ## The optional argument @var{params} contains a list of parameters of
27 ## @var{dist}.  For example, to test whether a sample @var{x} comes from
28 ## a uniform distribution on [2,4], use
29 ##
30 ## @example
31 ## kolmogorov_smirnov_test(x, "unif", 2, 4)
32 ## @end example
33 ##
34 ## @noindent
35 ## @var{dist} can be any string for which a function @var{dist_cdf}
36 ## that calculates the CDF of distribution @var{dist} exists.
37 ##
38 ## With the optional argument string @var{alt}, the alternative of
39 ## interest can be selected.  If @var{alt} is @code{"!="} or
40 ## @code{"<>"}, the null is tested against the two-sided alternative F
41 ## != G@.  In this case, the test statistic @var{ks} follows a two-sided
42 ## Kolmogorov-Smirnov distribution.  If @var{alt} is @code{">"}, the
43 ## one-sided alternative F > G is considered.  Similarly for @code{"<"},
44 ## the one-sided alternative F > G is considered.  In this case, the
45 ## test statistic @var{ks} has a one-sided Kolmogorov-Smirnov
46 ## distribution.  The default is the two-sided case.
47 ##
48 ## The p-value of the test is returned in @var{pval}.
49 ##
50 ## If no output argument is given, the p-value is displayed.
51 ## @end deftypefn
52
53 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
54 ## Description: One-sample Kolmogorov-Smirnov test
55
56 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin)
57
58   if (nargin < 2)
59     print_usage ();
60   endif
61
62   if (! isvector (x))
63     error ("kolmogorov_smirnov_test: X must be a vector");
64   endif
65
66   n = length (x);
67   s = sort (x);
68   try
69     f = str2func (sprintf ("%scdf", dist));
70   catch
71     try
72       f = str2func (sprintf ("%s_cdf", dist));
73     catch
74       error ("kolmogorov_smirnov_test: no %scdf or %s_cdf function found",
75              dist, dist);
76     end_try_catch
77   end_try_catch
78
79   alt  = "!=";
80
81   args{1} = s;
82   nvargs = numel (varargin);
83   if (nvargs > 0)
84     if (ischar (varargin{end}))
85       alt = varargin{end};
86       args(2:nvargs) = varargin(1:end-1);
87     else
88       args(2:nvargs+1) = varargin;
89     endif
90   endif
91
92   z = reshape (feval (f, args{:}), 1, n);
93
94   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
95     ks   = sqrt (n) * max (max ([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)]));
96     pval = 1 - kolmogorov_smirnov_cdf (ks);
97   elseif (strcmp (alt, ">"))
98     ks   = sqrt (n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n]));
99     pval = exp (- 2 * ks^2);
100   elseif (strcmp (alt, "<"))
101     ks   = - sqrt (n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n]));
102     pval = exp (- 2 * ks^2);
103   else
104     error ("kolmogorov_smirnov_test: alternative %s not recognized", alt);
105   endif
106
107   if (nargout == 0)
108     printf ("pval: %g\n", pval);
109   endif
110
111 endfunction
112
113 %!error <Invalid call to kolmogorov_smirnov_test>
114 %!  kolmogorov_smirnov_test (1);
115 %!error <kolmogorov_smirnov_test: X must be a vector>
116 %!  kolmogorov_smirnov_test ({}, "unif", 2, 4);
117 %!error <kolmogorov_smirnov_test: no not_a_distcdf or not_a_dist_cdf function found>
118 %!  kolmogorov_smirnov_test (1, "not_a_dist");
119 %!error <kolmogorov_smirnov_test: alternative bla not recognized>
120 %!  kolmogorov_smirnov_test (1, "unif", 2, 4, "bla");
121 %!test # for recognition of unifcdf function
122 %!  assert (kolmogorov_smirnov_test (0:100, "unif", 0, 100), 1.0, eps);
123 %!test # for recognition of logistic_cdf function
124 %!  assert (kolmogorov_smirnov_test (0:100, "logistic"), 0);
125 %!test # F < G
126 %!  assert (kolmogorov_smirnov_test (50:100, "unif", 0, 50, "<"));