Sign in Join
Wiki copytemporary

Ranking vectors of data: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Ranking vectors of data

OldNewLine
1+**Ranking** is an important method in statistics and data science. It is a preparative step that replaces each value by a rank in the order of magnitude of the original data. This operation is necessary for many non-parametric methods, e.g. the U test by Wilcoxon-Mann-Whitney or Spearman's rank correlation.
2+
3+## Simple algorithm not caring for ties
4+
5+In the simplest case, where every value is distinct without identical data, the following frugal algorithm may be sufficient:
6+
7+``` pascal
8+function rank(x: array of longint): array of real;
9+var
10+ i, j, n: integer;
11+ lessCount: integer; { number of observations less than a given observation }
12+begin
13+ n := length(x);
14+ SetLength(result, n);
15+ for i := 0 to n - 1 do
16+ begin
17+ lessCount := 0;
18+ for j := 0 to n - 1 do
19+ if x[j] < x[i] then
20+ inc(lessCount);
21+ result[i] := lessCount + 1;
22+ end;
23+end;
24+```
25+
26+This function delivers a list of ranks that replace the original data. It doesn't deliver satisfactory results if the data contain identical values, e.g. in the vector 3, 4, 4, 9, 0, 1. In statistical terminology, repeated observation-values are referred to as *ties*. The potential presence of ties requires a more complex algorithm:
27+
28+## Advanced algorithm addressing potential ties
29+
30+``` pascal
31+type
32+ TTiesMethod = (ascend, descend, average, min, max);
33+
34+function rank(x: array of longint;
35+ TiesMethod: TTiesMethod = average): array of real;
36+var
37+ i, j, n: integer;
38+ equalCount, lessCount: array of integer;
39+begin
40+ n := length(x);
41+ SetLength(lessCount, n);
42+ SetLength(equalCount, n);
43+ SetLength(Result, n);
44+ for i := 0 to n - 1 do
45+ begin
46+ equalCount[i] := 0;
47+ lessCount[i] := 0;
48+ for j := 0 to n - 1 do
49+ case TiesMethod of
50+ average, min, max:
51+ if x[j] = x[i] then
52+ Inc(equalCount[i])
53+ else if x[j] < x[i] then
54+ Inc(lessCount[i]);
55+ ascend:
56+ if (j <= i) and (x[j] = x[i]) then
57+ Inc(equalCount[i])
58+ else if x[j] < x[i] then
59+ Inc(lessCount[i]);
60+ descend:
61+ if (j >= i) and (x[j] = x[i]) then
62+ Inc(equalCount[i])
63+ else if x[j] < x[i] then
64+ Inc(lessCount[i]);
65+ end;
66+ end;
67+ for i := 0 to n - 1 do
68+ case TiesMethod of
69+ average:
70+ Result[i] := lessCount[i] + (equalCount[i] + 1) / 2;
71+ min:
72+ Result[i] := lessCount[i] + 1;
73+ max, ascend, descend:
74+ Result[i] := lessCount[i] + equalCount[i];
75+ end;
76+end;
77+```
78+
79+This function delivers a vector of ranks that is corrected for ties. Similar to the function *rank* of the statistical programming language S (e.g. in the environment R) it supports different methods to address potential ties. They include *average* (default method, the rank for each occurrence of a repeated number is given as *rank*\[*i*\] = *lessCount* + (*equalCount* + 1) / 2), *ascend* (permutation with increasing values at each index set of ties), *descend* (decreasing values), *min* (replaces values with ties with their minimum) and *max* (replacement by maximum).
80+
81+## See also
82+
83+- [[Samples and permutations]]
84+- [[Dev random]]
85+- [[Functions for descriptive statistics]]
86+- [[Generating Random Numbers]]
87+- [[Marsaglia&|Marsaglia's pseudo random number generators]]
88+- [[A simple implementation of the Mersenne twister]]
89+- [[Delphi compatible LCG Random]]
90+- [[EverettRandom]]
91+
92+## References
93+
94+1. Cooke D, Craven AH, Clarke GM. Statistical Computing in Pascal. Edward Arnold (Publishers), London 1985
95+2. Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
96+3. R Documentation: [Sample Ranks](https://stat.ethz.ch/R-manual/R-patched/library/base/html/rank.html).
97+
98+---
99+*From the Free Pascal wiki, page "Ranking vectors of data", as it stood on 2021-03-07 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
100+Category: Code
101+Category: Mathematics
102+Category: Statistical algorithms

Back to history