]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: adding standardize, differences, rescale.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 21:18:45 +0000 (14:18 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 21:18:45 +0000 (14:18 -0700)
basis/math/statistics/statistics-docs.factor
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index 129125d7c3d063435734e17bb52b6f757c8bea1d..b153aeaf66691782ab7387adfee3486dc0ae3eef 100644 (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"
 "Counting elements in a sequence:"
 { $subsections
index 6b96b5e61e15d27064b669ad24265ffca8d4d3d1..b64a2bda8262bdacb1d65cd0ce3fd51ba3150c61 100644 (file)
@@ -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
index 13a796396ce2a9aea1f9b7f59ced754603f33d47..cfe62b5e029c9b4b796fd1a14984a0b1b8d9fac8 100644 (file)
@@ -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* ;