] unit-test
{ { 0 1/4 1/2 3/4 1 } } [ 5 iota rescale ] unit-test
+
+
+{
+ { 2 2 2 1 0 5 6 7 7 7 7 }
+} [
+ { 30 30 30 20 10 40 50 60 60 60 60 } rank-values
+] unit-test
+
+{
+ { 1 0 2 3 4 }
+}
+[ { 3 1 4 15 92 } rank-values ] unit-test
! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators generalizations kernel locals math
math.functions math.order math.vectors sequences
-sequences.private sorting fry arrays grouping sets ;
+sequences.private sorting fry arrays grouping sets
+splitting.monotonic ;
IN: math.statistics
: power-mean ( seq p -- x )
: rescale ( u -- v )
dup minmax over - [ v-n ] [ v/n ] bi* ;
+
+: rank-values ( seq -- seq' )
+ [
+ [ ] [ length iota ] bi zip sort-keys
+ [ [ first ] bi@ = ] monotonic-split
+ [ values ] map [ 0 [ length + ] accumulate nip ] [ ] bi zip
+ ] [ length f <array> ] bi
+ [ '[ first2 [ _ set-nth ] with each ] each ] keep ;