math.statistics: adding standardize, differences, rescale.

db4
John Benediktsson 2012-05-04 14:18:45 -07:00
parent 6f3fe950a7
commit 0435098bd8
3 changed files with 30 additions and 0 deletions

View File

@ -214,6 +214,18 @@ HELP: cum-max
} }
} ; } ;
HELP: standardize
{ $values { "u" sequence } { "v" sequence } }
{ $description "Shifts and rescales the elements of " { $snippet "u" } " to have zero mean and unit sample variance." } ;
HELP: differences
{ $values { "u" sequence } { "v" sequence } }
{ $description "Returns the successive differences of elements in " { $snippet "u" } "." } ;
HELP: rescale
{ $values { "u" sequence } { "v" sequence } }
{ $description "Returns " { $snippet "u" } " rescaled to run from 0 to 1 over the range min to max." } ;
ARTICLE: "histogram" "Computing histograms" ARTICLE: "histogram" "Computing histograms"
"Counting elements in a sequence:" "Counting elements in a sequence:"
{ $subsections { $subsections

View File

@ -140,3 +140,11 @@ IN: math.statistics.tests
{ 1.0 } [ 0.5 binary-entropy ] unit-test { 1.0 } [ 0.5 binary-entropy ] unit-test
{ { -4 13 -5 2 4 } } [ { 1 -3 10 5 7 11 } differences ] unit-test
{ t t } [
{ 6.5 3.8 6.6 5.7 6.0 6.4 5.3 } standardize
[ mean 0 1e-10 ~ ] [ var 1 1e-10 ~ ] bi
] unit-test
{ { 0 1/4 1/2 3/4 1 } } [ 5 iota rescale ] unit-test

View File

@ -282,3 +282,13 @@ ERROR: empty-sequence ;
: binary-entropy ( p -- h ) : binary-entropy ( p -- h )
[ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ; [ dup log * ] [ 1 swap - dup log * ] bi + neg 2 log / ;
: standardize ( u -- v )
[ dup mean v-n ] [ std ] bi v/n ;
: differences ( u -- v )
[ 1 tail-slice ] keep [ - ] 2map ;
: rescale ( u -- v )
[ ] [ infimum ] [ supremum over - ] tri
[ v-n ] [ v/n ] bi* ;