math.statistics: some fixes to entropy, adding maximum-entropy and normalized-entropy.

db4
John Benediktsson 2012-09-25 20:17:34 -07:00
parent 1cf27a2af9
commit a283782abe
2 changed files with 12 additions and 3 deletions

View File

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

View File

@ -330,8 +330,17 @@ ALIAS: corr sample-corr
: cum-max ( seq -- seq' )
[ ?first ] keep [ max dup ] map nip ;
: entropy ( seq -- n )
histogram values dup sum '[ _ / dup log * ] map-sum neg ;
: probabilities ( seq -- probabilities )
[ histogram values ] [ length ] bi v/n ;
: entropy ( probabilities -- n )
dup sum '[ _ / dup log * ] map-sum neg ;
: maximum-entropy ( probabilities -- n )
length log ;
: normalized-entropy ( probabilities -- n )
[ entropy ] [ maximum-entropy ] bi / ;
: binary-entropy ( p -- h )
[ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ;