]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: adding a normalized-histogram word.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 26 Sep 2012 15:39:11 +0000 (08:39 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 26 Sep 2012 15:39:11 +0000 (08:39 -0700)
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index e18129526237dfadba067e68aa7bb746ed441c29..ae614ab534f2e4f65314b01e8a263b5d9375f17e 100644 (file)
@@ -77,6 +77,9 @@ IN: math.statistics.tests
     "aabbcc" histogram
 ] unit-test
 
+{ H{ { 1 1/2 } { 2 1/6 } { 3 1/3 } } }
+[ { 1 1 1 1 1 1 2 2 3 3 3 3 } normalized-histogram ] unit-test
+
 {
     V{ 0 3 6 9 }
     V{ 1 4 7 }
index 12dac7c6721ace21444713eef517eeb4b0dfc48b..c610e2a64514ac54d7279477c27c81633946e961 100644 (file)
@@ -227,6 +227,9 @@ PRIVATE>
 : sorted-histogram ( seq -- alist )
     histogram sort-values ;
 
+: normalized-histogram ( seq -- alist )
+    [ histogram ] [ length ] bi '[ _ / ] assoc-map ;
+
 : collect-pairs ( seq quot: ( x -- v k ) -- hashtable )
     [ push-at ] sequence>hashtable ; inline