1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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.
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.
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/>.
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.
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
31 ## kolmogorov_smirnov_test(x, "unif", 2, 4)
35 ## @var{dist} can be any string for which a function @var{dist_cdf}
36 ## that calculates the CDF of distribution @var{dist} exists.
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.
48 ## The p-value of the test is returned in @var{pval}.
50 ## If no output argument is given, the p-value is displayed.
53 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
54 ## Description: One-sample Kolmogorov-Smirnov test
56 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin)
63 error ("kolmogorov_smirnov_test: X must be a vector");
69 f = str2func (sprintf ("%scdf", dist));
72 f = str2func (sprintf ("%s_cdf", dist));
74 error ("kolmogorov_smirnov_test: no %scdf or %s_cdf function found",
82 nvargs = numel (varargin);
84 if (ischar (varargin{end}))
86 args(2:nvargs) = varargin(1:end-1);
88 args(2:nvargs+1) = varargin;
92 z = reshape (feval (f, args{:}), 1, n);
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);
104 error ("kolmogorov_smirnov_test: alternative %s not recognized", alt);
108 printf ("pval: %g\n", pval);
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);
126 %! assert (kolmogorov_smirnov_test (50:100, "unif", 0, 50, "<"));