]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: add rank-values
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 5 Sep 2012 01:52:22 +0000 (18:52 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 5 Sep 2012 01:52:38 +0000 (18:52 -0700)
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index 716800f000c194940da577e524789a75143641a7..b9cf7c4546d16a59813084bae34d208b86661ee6 100644 (file)
@@ -157,3 +157,15 @@ IN: math.statistics.tests
 ] 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
index 8073554b4f3bd093c1948b5098afe282c5820446..1a12ae0b00c21e2a4e6c0056885b04958a7a2007 100644 (file)
@@ -2,7 +2,8 @@
 ! 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 )
@@ -343,3 +344,11 @@ ALIAS: corr sample-corr
 
 : 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 ;