Mercurial > hg > octave-lyh
annotate scripts/statistics/base/ranks.m @ 11436:e151e23f73bc
Overhaul base statistics functions and documentation of same.
Add or improve input validation.
Add input validation tests.
Add functional tests.
Improve or re-write documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 03 Jan 2011 21:23:08 -0800 |
parents | fe3c3dfc07eb |
children | fd0a3ac60b0e |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
8920 | 2 ## 2007, 2008, 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 |
3453 | 20 ## -*- texinfo -*- |
4885 | 21 ## @deftypefn {Function File} {} ranks (@var{x}, @var{dim}) |
8035 | 22 ## Return the ranks of @var{x} along the first non-singleton dimension |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
23 ## adjusted for ties. If the optional argument @var{dim} is |
4885 | 24 ## given, operate along this dimension. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
25 ## @seealso{spearman,kendall} |
3453 | 26 ## @end deftypefn |
3426 | 27 |
5428 | 28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 29 ## Description: Compute ranks |
3200 | 30 |
4885 | 31 ## This code was rather ugly, since it didn't use sort due to the |
32 ## fact of how to deal with ties. Now it does use sort and its | |
33 ## even uglier!!! At least it handles NDArrays.. | |
34 | |
35 function y = ranks (x, dim) | |
36 | |
37 if (nargin != 1 && nargin != 2) | |
6046 | 38 print_usage (); |
4885 | 39 endif |
3426 | 40 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
41 if (!isnumeric(x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
42 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
|
43 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
44 |
4885 | 45 nd = ndims (x); |
46 sz = size (x); | |
47 if (nargin != 2) | |
4886 | 48 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
49 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
50 if (isempty (dim)) |
4885 | 51 dim = 1; |
52 endif | |
53 else | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
54 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10687
diff
changeset
|
55 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
56 error ("ranks: DIM must be an integer and a valid dimension"); |
4885 | 57 endif |
3200 | 58 endif |
3426 | 59 |
4886 | 60 if (sz(dim) == 1) |
4885 | 61 y = ones(sz); |
62 else | |
8507 | 63 ## The algorithm works only on dim = 1, so permute if necesary. |
4885 | 64 if (dim != 1) |
65 perm = [1 : nd]; | |
4886 | 66 perm(1) = dim; |
67 perm(dim) = 1; | |
4885 | 68 x = permute (x, perm); |
69 endif | |
4886 | 70 sz = size (x); |
10540
952d4df5b686
Eliminate NaN*ones and Inf*ones constructs and just use Nan() and Inf()
Rik <code@nomad.inbox5.com>
parents:
9051
diff
changeset
|
71 infvec = -Inf ([1, sz(2 : end)]); |
4942 | 72 [xs, xi] = sort (x); |
4885 | 73 eq_el = find (diff ([xs; infvec]) == 0); |
74 if (isempty (eq_el)) | |
4942 | 75 [eq_el, y] = sort (xi); |
4885 | 76 else |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
77 runs = setdiff (eq_el, eq_el+1); |
4885 | 78 len = diff (find (diff ([Inf; eq_el; -Inf]) != 1)) + 1; |
4942 | 79 [eq_el, y] = sort (xi); |
4885 | 80 for i = 1 : length(runs) |
10549 | 81 y (xi (runs (i) + [0:(len(i)-1)]) + floor (runs (i) ./ sz(1)) |
82 * sz(1)) = eq_el(runs(i)) + (len(i) - 1) / 2; | |
4885 | 83 endfor |
84 endif | |
85 if (dim != 1) | |
86 y = permute (y, perm); | |
87 endif | |
3200 | 88 endif |
3426 | 89 |
3200 | 90 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
91 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
92 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
93 %!assert(ranks (1:2:10), 1:5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
94 %!assert(ranks (10:-2:1), 5:-1:1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
95 %!assert(ranks ([2, 1, 2, 4]), [2.5, 1, 2.5, 4]) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
96 %!assert(ranks (ones(1, 5)), 3*ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 %!assert(ranks (1e6*ones(1, 5)), 3*ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
98 %!assert(ranks (rand (1, 5), 1), ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
99 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
100 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
101 %!error ranks () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
102 %!error ranks (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
103 %!error ranks ({1, 2}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
104 %!error ranks (true(2,1)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
105 %!error ranks (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
106 %!error ranks (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
107 %!error ranks (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
108 |