From 0435098bd8f68ef346ce04d3a3383fa4dece4e03 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 4 May 2012 14:18:45 -0700 Subject: [PATCH] math.statistics: adding standardize, differences, rescale. --- basis/math/statistics/statistics-docs.factor | 12 ++++++++++++ basis/math/statistics/statistics-tests.factor | 8 ++++++++ basis/math/statistics/statistics.factor | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index 129125d7c3..b153aeaf66 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -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" "Counting elements in a sequence:" { $subsections diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 6b96b5e61e..b64a2bda82 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.factor @@ -140,3 +140,11 @@ IN: math.statistics.tests { 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 diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index 13a796396c..cfe62b5e02 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -282,3 +282,13 @@ ERROR: empty-sequence ; : binary-entropy ( p -- h ) [ 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* ;