Mercurial > hg > octave-lyh
annotate scripts/statistics/tests/kolmogorov_smirnov_test.m @ 14554:69eec23c8b3d
allow kron to work for two diag matrix arguments (bug #35647)
* kron.cc (dispatch_kron): Fix recursive call for case of two diagonal
matrix objects as arguments. New tests.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 12 Apr 2012 14:01:39 -0400 |
parents | f3d52523cde1 |
children | 5d3a684236b0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1995-2012 Kurt Hornik |
3426 | 2 ## |
3922 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3454 | 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
22 ## sample @var{x} comes from the (continuous) distribution dist. I.e., |
3454 | 23 ## if F and G are the CDFs corresponding to the sample and dist, |
24 ## respectively, then the null is that F == G. | |
3200 | 25 ## |
3454 | 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 | |
3200 | 29 ## |
3454 | 30 ## @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
|
31 ## kolmogorov_smirnov_test(x, "unif", 2, 4) |
3454 | 32 ## @end example |
3200 | 33 ## |
6754 | 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 ## | |
3454 | 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 | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
41 ## != G@. In this case, the test statistic @var{ks} follows a two-sided |
3454 | 42 ## Kolmogorov-Smirnov distribution. If @var{alt} is @code{">"}, the |
3457 | 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. | |
3200 | 47 ## |
3454 | 48 ## The p-value of the test is returned in @var{pval}. |
3200 | 49 ## |
50 ## If no output argument is given, the p-value is displayed. | |
3454 | 51 ## @end deftypefn |
3200 | 52 |
5428 | 53 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 54 ## Description: One-sample Kolmogorov-Smirnov test |
3200 | 55 |
3979 | 56 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin) |
3426 | 57 |
3200 | 58 if (nargin < 2) |
6046 | 59 print_usage (); |
3200 | 60 endif |
61 | |
4030 | 62 if (! isvector (x)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11340
diff
changeset
|
63 error ("kolmogorov_smirnov_test: X must be a vector"); |
3200 | 64 endif |
65 | |
66 n = length (x); | |
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 end_try_catch |
3200 | 78 |
79 alt = "!="; | |
3426 | 80 |
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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 endif |
3426 | 90 endif |
3200 | 91 |
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
|
92 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
|
93 |
3200 | 94 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
3456 | 95 ks = sqrt (n) * max (max ([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)])); |
3200 | 96 pval = 1 - kolmogorov_smirnov_cdf (ks); |
97 elseif (strcmp (alt, ">")) | |
3456 | 98 ks = sqrt (n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n])); |
99 pval = exp (- 2 * ks^2); | |
3200 | 100 elseif (strcmp (alt, "<")) |
3456 | 101 ks = - sqrt (n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n])); |
102 pval = exp (- 2 * ks^2); | |
3200 | 103 else |
3456 | 104 error ("kolmogorov_smirnov_test: alternative %s not recognized", alt); |
3200 | 105 endif |
3426 | 106 |
3200 | 107 if (nargout == 0) |
3456 | 108 printf ("pval: %g\n", pval); |
3200 | 109 endif |
3426 | 110 |
3200 | 111 endfunction |
11340
ef65ebb325e9
Add tests to kolmogorov_smirnov_test
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
11336
diff
changeset
|
112 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
113 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
114 ## test for recognition of unifcdf function |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 %!assert (kolmogorov_smirnov_test (0:100, "unif", 0, 100), 1.0, eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
116 ## test for recognition of logistic_cdf function |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
117 %!assert (kolmogorov_smirnov_test (0:100, "logistic"), 0) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
118 ## test for F < G |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
119 %!assert (kolmogorov_smirnov_test (50:100, "unif", 0, 50, "<")) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
120 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 %!error kolmogorov_smirnov_test (1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
122 %!error <X must be a vector> kolmogorov_smirnov_test ({}, "unif", 2, 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!error <no not_a_distcdf or not_a_dist_cdf function found> |
11340
ef65ebb325e9
Add tests to kolmogorov_smirnov_test
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
11336
diff
changeset
|
124 %! kolmogorov_smirnov_test (1, "not_a_dist"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
125 %!error <alternative foo not recognized> |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 %! kolmogorov_smirnov_test (1, "unif", 2, 4, "foo"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
127 |