Mercurial > octave
annotate scripts/statistics/tests/kolmogorov_smirnov_test.m @ 11336:5a4a7febe37c
fix omission from previous change to kolmogorov_smirnov_test
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 09 Dec 2010 19:02:07 -0500 |
parents | db091f68798c |
children | ef65ebb325e9 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, |
9245 | 2 ## 2006, 2007, 2009 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 |
3200 | 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 ## | |
3200 | 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/>. | |
3200 | 19 |
3454 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {[@var{pval}, @var{ks}] =} kolmogorov_smirnov_test (@var{x}, @var{dist}, @var{params}, @var{alt}) | |
22 ## Perform a Kolmogorov-Smirnov test of the null hypothesis that the | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
23 ## sample @var{x} comes from the (continuous) distribution dist. I.e., |
3454 | 24 ## if F and G are the CDFs corresponding to the sample and dist, |
25 ## respectively, then the null is that F == G. | |
3200 | 26 ## |
3454 | 27 ## The optional argument @var{params} contains a list of parameters of |
28 ## @var{dist}. For example, to test whether a sample @var{x} comes from | |
29 ## a uniform distribution on [2,4], use | |
3200 | 30 ## |
3454 | 31 ## @example |
11335
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
32 ## kolmogorov_smirnov_test(x, "unif", 2, 4) |
3454 | 33 ## @end example |
3200 | 34 ## |
6754 | 35 ## @noindent |
36 ## @var{dist} can be any string for which a function @var{dist_cdf} | |
37 ## that calculates the CDF of distribution @var{dist} exists. | |
38 ## | |
3454 | 39 ## With the optional argument string @var{alt}, the alternative of |
40 ## interest can be selected. If @var{alt} is @code{"!="} or | |
41 ## @code{"<>"}, the null is tested against the two-sided alternative F | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
42 ## != G@. In this case, the test statistic @var{ks} follows a two-sided |
3454 | 43 ## Kolmogorov-Smirnov distribution. If @var{alt} is @code{">"}, the |
3457 | 44 ## one-sided alternative F > G is considered. Similarly for @code{"<"}, |
45 ## the one-sided alternative F > G is considered. In this case, the | |
46 ## test statistic @var{ks} has a one-sided Kolmogorov-Smirnov | |
47 ## distribution. The default is the two-sided case. | |
3200 | 48 ## |
3454 | 49 ## The p-value of the test is returned in @var{pval}. |
3200 | 50 ## |
51 ## If no output argument is given, the p-value is displayed. | |
3454 | 52 ## @end deftypefn |
3200 | 53 |
5428 | 54 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 55 ## Description: One-sample Kolmogorov-Smirnov test |
3200 | 56 |
3979 | 57 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin) |
3426 | 58 |
3200 | 59 if (nargin < 2) |
6046 | 60 print_usage (); |
3200 | 61 endif |
62 | |
4030 | 63 if (! isvector (x)) |
3458 | 64 error ("kolmogorov_smirnov_test: x must be a vector"); |
3200 | 65 endif |
66 | |
67 n = length (x); | |
68 s = sort (x); | |
11335
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
69 try |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
70 f = str2func (sprintf ("%scdf", dist)); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
71 catch |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
72 try |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
73 f = str2func (sprintf ("%s_cdf", dist)); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
74 catch |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
75 error ("kolmogorov_smirnov_test: no %scdf or %s_cdf function found", |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
76 dist, dist); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
77 end_try_catch |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
78 end_try_catch |
3200 | 79 |
80 alt = "!="; | |
3426 | 81 |
11335
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
82 args{1} = s; |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
83 nvargs = numel (varargin); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
84 if (nvargs > 0) |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
85 if (ischar (varargin{end})) |
11336
5a4a7febe37c
fix omission from previous change to kolmogorov_smirnov_test
John W. Eaton <jwe@octave.org>
parents:
11335
diff
changeset
|
86 alt = varargin{end}; |
11335
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
87 args(2:nvargs) = varargin(1:end-1); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
88 else |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
89 args(2:nvargs+1) = varargin; |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
90 endif |
3426 | 91 endif |
3200 | 92 |
11335
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
93 z = reshape (feval (f, args{:}), 1, n); |
db091f68798c
allow kolmogorov_smirnov_test to work with either DIST_cdf or DISTcdf functions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
94 |
3200 | 95 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
3456 | 96 ks = sqrt (n) * max (max ([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)])); |
3200 | 97 pval = 1 - kolmogorov_smirnov_cdf (ks); |
98 elseif (strcmp (alt, ">")) | |
3456 | 99 ks = sqrt (n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n])); |
100 pval = exp (- 2 * ks^2); | |
3200 | 101 elseif (strcmp (alt, "<")) |
3456 | 102 ks = - sqrt (n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n])); |
103 pval = exp (- 2 * ks^2); | |
3200 | 104 else |
3456 | 105 error ("kolmogorov_smirnov_test: alternative %s not recognized", alt); |
3200 | 106 endif |
3426 | 107 |
3200 | 108 if (nargout == 0) |
3456 | 109 printf ("pval: %g\n", pval); |
3200 | 110 endif |
3426 | 111 |
3200 | 112 endfunction |