annotate scripts/statistics/ranks.m @ 26077:31b443b5a6c1

ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372). * NEWS: Announce overhaul of ranks function and tie-breaking. * contributors.in: Add Dave Goel to list of contributors. * ranks.m: Rewrite docstring to include new options. Redo input validation to support three arguments. Use switch statement on RTYPE to call appropriate subfunction for ranking with possible tie values. Add new BIST tests for tie-breaking functionality. Redo input validation BIST tests. * ranks.m (_competition, _modified, _dense): Three new subfunctions to break ties according to different algorithms.
author Dave Goel <deego3@gmail.com>
date Thu, 15 Nov 2018 11:35:54 -0800
parents 6652d3823428
children 00f796120a6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25054
6652d3823428 maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 24547
diff changeset
1 ## Copyright (C) 1995-2018 Kurt Hornik
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
2 ## Copyright (C) 2012 Dave Goel
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
5 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
6 ## Octave is free software: you can redistribute it and/or modify it
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
7 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
8 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
9 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
14 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
15 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## 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: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
18 ## <https://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
20 ## -*- texinfo -*-
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
21 ## @deftypefn {} {} ranks (@var{x})
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
22 ## @deftypefnx {} {} ranks (@var{x}, @var{dim})
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
23 ## @deftypefnx {} {} ranks (@var{x}, @var{dim}, @var{rtype})
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
24 ## Return the ranks (in the sense of order statistics) of @var{x} along the
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
25 ## first non-singleton dimension adjusted for ties.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
26 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
27 ## If the optional @var{dim} argument is given, operate along this dimension.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
28 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
29 ## The optional parameter @var{rtype} determines how ties are handled. All
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
30 ## examples below assume an input of @code{[ 1, 2, 2, 4 ]}.
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
32 ## @table @asis
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
33 ## @item 0 or @qcode{"fractional"} (default) for fractional ranking (1, 2.5,
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
34 ## 2.5, 4);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
35 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
36 ## @item 1 or @qcode{"competition"} for competition ranking (1, 2, 2, 4);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
37 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
38 ## @item 2 or @qcode{"modified"} for modified competition ranking (1, 3, 3, 4);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
39 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
40 ## @item 3 or @qcode{"ordinal"} for ordinal ranking (1, 2, 3, 4);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
41 ##
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
42 ## @item 4 or @qcode{"dense"} for dense ranking (1, 2, 2, 3).
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
43 ## @end table
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
44 ##
12575
d0b799dafede Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
45 ## @seealso{spearman, kendall}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
46 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
47
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
48 function y = ranks (x, dim, rtype = 0)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
49
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
50 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
51 print_usage ();
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
52 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
53
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
54 if (! (isnumeric (x) || islogical (x)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
55 error ("ranks: X must be a numeric vector or matrix");
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10650
diff changeset
56 endif
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10650
diff changeset
57
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
58 nd = ndims (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
59 sz = size (x);
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
60
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
61 if (nargin < 2 || isempty (dim))
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
62 ## Find the first non-singleton dimension.
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
63 (dim = find (sz > 1, 1)) || (dim = 1);
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
64 else
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
65 if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10650
diff changeset
66 error ("ranks: DIM must be an integer and a valid dimension");
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
67 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
68 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
69
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
70 if (sz(dim) == 1)
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
71 y = ones (sz); # dimension DIM is singleton, so all are ranked first.
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
72 else
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
73 ## The algorithm works only on dim = 1, so permute if necessary.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
74 ## FIXME: Most all functions now accept a dim argument.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
75 ## Would it be faster not to permute and use the dim argument
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
76 ## to sort, find, cumsum, etc.?
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
77 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
78 perm = [1 : nd];
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
79 perm(1) = dim;
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
80 perm(dim) = 1;
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
81 x = permute (x, perm);
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
82 sz = size (x);
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
83 endif
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
84
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
85 [sx, ids] = sort (x); # sx is sorted x.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
86 lin = repmat ((1:rows (x))', [1, sz(2:end)]); # linearly increasing array.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
87
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
88 switch (rtype)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
89 case {0, "fractional"};
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
90 lin = (_competition (lin, sx, sz) + _modified (lin, sx, sz)) / 2;
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
91 case {1, "competition"};
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
92 lin = _competition (lin, sx, sz);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
93 case {2, "modified"};
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
94 lin = _modified (lin, sx, sz);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
95 case {3, "ordinal"};
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
96 ## no processing needed here.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
97 case {4, "dense"};
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
98 lin = _dense (lin, sx, sz);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
99 otherwise
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
100 if (! ischar (rtype))
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
101 rtype = num2str (rtype);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
102 end
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
103 error ("ranks: unknown RTYPE '%s'", rtype);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
104 endswitch
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
105
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
106 y = NaN (size (lin));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
107
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
108 ## Offsets to map indices into each column to indices into the linear array.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
109 ## FIXME: Would sub2ind be faster here?
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
110 idf = zeros (sz);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
111 idf(1, :) = 0 : sz(1) : (numel (ids)-1);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
112 idf(:, :) = repmat (idf(1, :), [sz(1), ones(1,length(sz)-1)]);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
113 y(ids + idf) = lin;
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
114
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
115 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
116 y = permute (y, perm);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
117 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
118 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
119
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
120 endfunction
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
121
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
122 function linnew = _dense (lin, sx, sz)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
123 infvec = -Inf ([1, sz(2:end)]);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
124 fnewp = logical (diff ([infvec; sx]));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
125 linnew = cumsum (fnewp, 1);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
126 endfunction
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
127
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
128 function linnew = _competition (lin, sx, sz)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
129 ## Stop increasing lin when sx does not increase. Otherwise, same as before.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
130 infvec = -Inf ([1, sz(2:end)]);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
131 fnewp = find (diff ([infvec; sx]));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
132 linnew = zeros (size (lin));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
133 linnew(fnewp) = lin(fnewp);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
134 linnew = cummax (linnew, 1);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
135 endfunction
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
136
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
137 function linnew = _modified (lin, sx, sz)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
138 ## Traverse lin backwards. Stop decreasing it when sx doesn't decrease.
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
139 infvec = Inf ([1, sz(2:end)]);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
140 fnewp = find (diff ([sx; infvec]));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
141 linnew = Inf (size (lin));
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
142 linnew(fnewp) = lin(fnewp);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
143 linnew = flip (cummin (flip (linnew, 1)), 1);
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
144 endfunction
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
145
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
146
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
147 %!assert (ranks (1:2:10), 1:5)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
148 %!assert (ranks (10:-2:1), 5:-1:1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
149 %!assert (ranks ([2, 1, 2, 4]), [2.5, 1, 2.5, 4])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
150 %!assert (ranks (ones (1, 5)), 3*ones (1, 5))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
151 %!assert (ranks (1e6*ones (1, 5)), 3*ones (1, 5))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
152 %!assert (ranks (rand (1, 5), 1), ones (1, 5))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
153
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
154 %!assert (ranks ([1, 2, 2, 4], [], "fractional"), [1, 2.5, 2.5, 4])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
155 %!assert (ranks ([1, 2, 2, 4], [], "competition"), [1, 2, 2, 4])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
156 %!assert (ranks ([1, 2, 2, 4], [], "modified"), [1, 3, 3, 4])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
157 %!assert (ranks ([1, 2, 2, 4], [], "ordinal"), [1, 2, 3, 4])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
158 %!assert (ranks ([1, 2, 2, 4], [], "dense"), [1, 2, 2, 3])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
159
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
160 ## Test input validation
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
161 %!error ranks ()
26077
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
162 %!error ranks (1, 2, 3, 4)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
163 %!error <X must be a numeric vector or matrix> ranks ({1, 2})
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
164 %!error <X must be a numeric vector or matrix> ranks (['A'; 'B'])
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
165 %!error <DIM must be an integer> ranks (1, 1.5)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
166 %!error <DIM must be .* a valid dimension> ranks (1, 0)
31b443b5a6c1 ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents: 25054
diff changeset
167 %!error <unknown RTYPE 'foobar'> ranks (ones (2), 1, "foobar")