math.statistics: adding entropy function.
parent
7bba1972a1
commit
5878b6982b
|
@ -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
|
||||
|
||||
|
|
|
@ -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 / ;
|
||||
|
|
Loading…
Reference in New Issue