]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/math/statistics/statistics.factor
Resolved merge.
[factor.git] / basis / math / statistics / statistics.factor
index 3812e79ec595fe56035513b5a93c5dd7fb307815..a1a214b2c015cebc694ac06e52bbbbb7b3e97e98 100644 (file)
@@ -2,7 +2,7 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays combinators kernel math math.analysis
 math.functions math.order sequences sorting locals
-sequences.private ;
+sequences.private assocs fry ;
 IN: math.statistics
 
 : mean ( seq -- x )
@@ -56,6 +56,13 @@ IN: math.statistics
 : median ( seq -- x )
     dup length odd? [ lower-median ] [ medians + 2 / ] if ;
 
+: frequency ( seq -- hashtable )
+    H{ } clone [ '[ _ inc-at ] each ] keep ;
+
+: mode ( seq -- x )
+    frequency >alist
+    [ ] [ [ [ second ] bi@ > ] 2keep ? ] map-reduce first ;
+
 : minmax ( seq -- min max )
     #! find the min and max of a seq in one pass
     [ 1/0. -1/0. ] dip [ [ min ] [ max ] bi-curry bi* ] each ;