annotate scripts/statistics/tests/kolmogorov_smirnov_test.m @ 21562:6c2fd62db1f7

maint: Eliminate accidental double spaces in code. * make_int.cc, file-editor-tab.h, file-editor.cc, shortcut-manager.h, __contourc__.cc, cellfun.cc, debug.cc, errwarn.h, graphics.in.h, jit-ir.h, oct-stream.cc, quadcc.cc, qz.cc, sparse-xdiv.cc, symtab.cc, __init_fltk__.cc, ov-class.cc, ov-float.h, ov-scalar.h, op-int.h, CNDArray.cc, CSparse.cc, MSparse.cc, Sparse.cc, boolNDArray.cc, chNDArray.cc, dNDArray.cc, dSparse.cc, fCNDArray.cc, fNDArray.cc, int16NDArray.cc, int32NDArray.cc, int64NDArray.cc, int8NDArray.cc, intNDArray.cc, uint16NDArray.cc, uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc, randgamma.h, randmtzig.h, randpoisson.c, randpoisson.h, Sparse-op-defs.h, oct-time.cc, kpse.cc, lo-array-errwarn.h, lo-ieee.h, del2.m, num2str.m, __isequal__.m, lookfor.m, hsv2rgb.m, colorspace_conversion_revert.m, fminbnd.m, create_pkgadddel.m, shading.m, struct2hdl.m, gallery.m, discrete_cdf.m, kolmogorov_smirnov_test.m, assert.m, datestr.m, classes.tst, jit.tst: Eliminate accidental double spaces in code.
author Rik <rik@octave.org>
date Wed, 30 Mar 2016 15:19:12 -0700
parents 516bb87ea72e
children dcf8922b724b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1995-2015 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
20 ## @deftypefn {} {[@var{pval}, @var{ks}] =} kolmogorov_smirnov_test (@var{x}, @var{dist}, @var{params}, @var{alt})
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
21 ## Perform a Kolmogorov-Smirnov test of the null hypothesis that the
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
22 ## sample @var{x} comes from the (continuous) distribution @var{dist}.
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
23 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
24 ## if F and G are the CDFs corresponding to the sample and dist,
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
25 ## respectively, then the null is that F == G.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
26 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
27 ## The optional argument @var{params} contains a list of parameters of
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
28 ## @var{dist}. For example, to test whether a sample @var{x} comes from
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
29 ## a uniform distribution on [2,4], use
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
30 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
31 ## @example
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
32 ## kolmogorov_smirnov_test (x, "unif", 2, 4)
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
33 ## @end example
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
34 ##
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
35 ## @noindent
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## @var{dist} can be any string for which a function @var{distcdf}
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
37 ## that calculates the CDF of distribution @var{dist} exists.
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
38 ##
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ## With the optional argument string @var{alt}, the alternative of interest
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ## can be selected. If @var{alt} is @qcode{"!="} or @qcode{"<>"}, the null
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
41 ## is tested against the two-sided alternative F != G@. In this case, the
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
42 ## test statistic @var{ks} follows a two-sided Kolmogorov-Smirnov
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
43 ## distribution. If @var{alt} is @qcode{">"}, the one-sided alternative F >
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## G is considered. Similarly for @qcode{"<"}, the one-sided alternative F >
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## G is considered. In this case, the test statistic @var{ks} has a
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## one-sided Kolmogorov-Smirnov distribution. The default is the two-sided
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## case.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
48 ##
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
49 ## The p-value of the test is returned in @var{pval}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
50 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
51 ## If no output argument is given, the p-value is displayed.
3454
d8b731d3f7a3 [project @ 2000-01-18 10:13:31 by jwe]
jwe
parents: 3426
diff changeset
52 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
53
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
54 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
55 ## Description: One-sample Kolmogorov-Smirnov test
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
56
3979
e0b7a493e5a8 [project @ 2002-07-10 17:45:34 by jwe]
jwe
parents: 3922
diff changeset
57 function [pval, ks] = kolmogorov_smirnov_test (x, dist, varargin)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
58
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
59 if (nargin < 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
60 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
62
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3979
diff changeset
63 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
64 error ("kolmogorov_smirnov_test: X must be a vector");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
66
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
67 n = length (x);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
79
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
80 alt = "!=";
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
91 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
95 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
96 ks = sqrt (n) * max (max ([abs(z - (0:(n-1))/n); abs(z - (1:n)/n)]));
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
97 pval = 1 - kolmogorov_smirnov_cdf (ks);
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
98 elseif (strcmp (alt, ">"))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
99 ks = sqrt (n) * max (max ([z - (0:(n-1))/n; z - (1:n)/n]));
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
100 pval = exp (- 2 * ks^2);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
101 elseif (strcmp (alt, "<"))
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
102 ks = - sqrt (n) * min (min ([z - (0:(n-1))/n; z - (1:n)/n]));
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
103 pval = exp (- 2 * ks^2);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
104 else
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
105 error ("kolmogorov_smirnov_test: alternative %s not recognized", alt);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
106 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
107
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
108 if (nargout == 0)
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3454
diff changeset
109 printf ("pval: %g\n", pval);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
110 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
111
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
112 endfunction
11340
ef65ebb325e9 Add tests to kolmogorov_smirnov_test
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 11336
diff changeset
113
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
114
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
115 ## 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
116 %!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
117 ## 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
118 %!assert (kolmogorov_smirnov_test (0:100, "logistic"), 0)
21562
6c2fd62db1f7 maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents: 20852
diff changeset
119 ## test for F < G
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
120 %!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
121
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
122 %!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
123 %!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
124 %!error <no not_a_distcdf or not_a_dist_cdf function found>
17337
a31b54b5f84a Use only 1 space between '%!' and start of test/demo code.
Rik <rik@octave.org>
parents: 17281
diff changeset
125 %! 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
126 %!error <alternative foo not recognized>
17337
a31b54b5f84a Use only 1 space between '%!' and start of test/demo code.
Rik <rik@octave.org>
parents: 17281
diff changeset
127 %! kolmogorov_smirnov_test (1, "unif", 2, 4, "foo");
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
128