math.statistics: adding entropy function.

db4
John Benediktsson 2012-04-26 18:40:59 -07:00
parent 7bba1972a1
commit 5878b6982b
2 changed files with 12 additions and 1 deletions

View File

@ -136,3 +136,7 @@ IN: math.statistics.tests
{ 18.9375 40.0 42.8125 } .00001 v~
] unit-test
{ 1.0986122886681096 } [ { 1 2 3 } entropy ] unit-test
{ 1.0 } [ 0.5 binary-entropy ] unit-test

View File

@ -2,7 +2,7 @@
! 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 ;
sequences.private sorting fry arrays grouping sets ;
IN: math.statistics
: mean ( seq -- x )
@ -275,3 +275,10 @@ ERROR: empty-sequence ;
: cum-max ( seq -- seq' )
[ ?first ] keep [ max dup ] map nip ;
: entropy ( seq -- n )
dup members [ [ = ] curry count ] with map
dup sum v/n dup [ log ] map v* sum neg ;
: binary-entropy ( p -- h )
[ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ;