]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: separate "sample-" and "full-" versions of var, std, ste, and corr.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 21:31:26 +0000 (14:31 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 21:31:26 +0000 (14:31 -0700)
basis/math/statistics/statistics-docs.factor
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index b153aeaf66691782ab7387adfee3486dc0ae3eef..f7001e43b192999d9689289acb5038b1bde4a866 100644 (file)
@@ -50,13 +50,13 @@ HELP: std
 { $values { "seq" sequence } { "x" "a non-negative real number"} }
 { $description "Computes the standard deviation of " { $snippet "seq" } ", which is the square root of the variance. It measures how widely spread the values in a sequence are about the mean." }
 { $examples
-  { $example "USING: math.statistics prettyprint ;" "{ 4 6 8 10 12 14 16 } std ." "4.0" } } ;
+  { $example "USING: math.statistics prettyprint ;" "{ 7 8 9 } std ." "1.0" } } ;
 
 HELP: ste
   { $values { "seq" sequence } { "x" "a non-negative real number"} }
   { $description "Computes the standard error of the mean for " { $snippet "seq" } ". It's defined as the standard deviation divided by the square root of the length of the sequence, and measures uncertainty associated with the estimate of the mean." }
   { $examples
-    { $example "USING: math.statistics prettyprint ;" "{ 4 6 6 8 10 12 14 14 16 } ste ." "1.333333333333333" }
+    { $example "USING: math.statistics prettyprint ;" "{ -2 2 } ste ." "2.0" }
   } ;
 
 HELP: var
@@ -65,8 +65,8 @@ HELP: var
 { $notes "If the number of elements in " { $snippet "seq" } " is 1 or less, it outputs 0." }
 { $examples
   { $example "USING: math.statistics prettyprint ;" "{ 1 } var ." "0" }
-  { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } var ." "2/3" }
-  { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 5 } var ." "2" } } ;
+  { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } var ." "1" }
+  { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 4 } var ." "1+2/3" } } ;
 
 HELP: cov
 { $values { "{x}" sequence } { "{y}" sequence } { "cov" "a real number" } }
@@ -258,3 +258,8 @@ ARTICLE: "math.statistics" "Statistics"
 { $subsection "histogram" } ;
 
 ABOUT: "math.statistics"
+
+{ var full-var sample-var } related-words
+{ std full-std sample-std } related-words
+{ ste full-ste sample-ste } related-words
+{ corr full-corr sample-corr } related-words
index b64a2bda8262bdacb1d65cd0ce3fd51ba3150c61..4aa42dede55d968b3bfd17ddd9c99e30254c14c9 100644 (file)
@@ -45,11 +45,11 @@ IN: math.statistics.tests
 [ 2 ] [ { 1 2 } upper-median ] unit-test
 [ 3/2 ] [ { 1 2 } median ] unit-test
 
-[ 1 ] [ { 1 2 3 } sample-var ] unit-test
-[ 16 ] [ { 4 6 8 10 10 12 14 16 } sample-var ] unit-test
+[ 1 ] [ { 1 2 3 } var ] unit-test
+[ 16 ] [ { 4 6 8 10 10 12 14 16 } var ] unit-test
 
-[ 16 ] [ { 4 6 8 10 12 14 16 } var ] unit-test
-[ 4.0 ] [ { 4 6 8 10 12 14 16 } std ] unit-test
+[ 16 ] [ { 4 6 8 10 12 14 16 } full-var ] unit-test
+[ 1.0 ] [ { 7 8 9 } std ] unit-test
 [ t ] [ { 1 2 3 4 } ste 0.6454972243679028 - .0001 < ] unit-test
 
 [ t ] [ { 23.2 33.4 22.5 66.3 44.5 } std 18.1906 - .0001 < ] unit-test
@@ -80,8 +80,8 @@ IN: math.statistics.tests
 [ 0 ] [ { 1 } { 1 } cov ] unit-test
 [ 2/3 ] [ { 1 2 3 } { 4 5 6 } cov ] unit-test
 
-[ 1.0 ] [ { 1 2 3 } { 1 2 3 } corr ] unit-test
-[ -1.0 ] [ { 1 2 3 } { -4 -5 -6 } corr ] unit-test
+[ 0.75 ] [ { 1 2 3 4 } dup corr ] unit-test
+[ -0.75 ] [ { 1 2 3 4 } { -4 -5 -6 -7 } corr ] unit-test
 
 [ { 1 2 4 7 } ] [ { 1 1 2 3 } cum-sum ] unit-test
 [ { 1 1 2 6 } ] [ { 1 1 2 3 } cum-product ] unit-test
index cfe62b5e029c9b4b796fd1a14984a0b1b8d9fac8..4655aa7d6505271a11d7f9c97315cbb4943bcbaf 100644 (file)
@@ -223,7 +223,7 @@ ERROR: empty-sequence ;
         [ length 1 - ] bi /
     ] if ;
 
-: var ( seq -- x )
+: full-var ( seq -- x )
     dup length 1 <= [
         drop 0
     ] [
@@ -231,9 +231,19 @@ ERROR: empty-sequence ;
         [ length ] bi /
     ] if ;
 
-: std ( seq -- x ) var sqrt ;
+ALIAS: var sample-var
 
-: ste ( seq -- x ) [ std ] [ length ] bi sqrt / ;
+: sample-std ( seq -- x ) sample-var sqrt ;
+
+: full-std ( seq -- x ) full-var sqrt ;
+
+ALIAS: std sample-std
+
+: sample-ste ( seq -- x ) [ sample-std ] [ length ] bi sqrt / ;
+
+: full-ste ( seq -- x ) [ full-std ] [ length ] bi sqrt / ;
+
+ALIAS: ste sample-ste
 
 : ((r)) ( mean(x) mean(y) {x} {y} -- (r) )
     ! finds sigma((xi-mean(x))(yi-mean(y))
@@ -261,8 +271,13 @@ ERROR: empty-sequence ;
 : cov ( {x} {y} -- cov )
     [ dup mean v-n ] bi@ v* mean ;
 
-: corr ( {x} {y} -- corr )
-     [ cov ] [ [ var ] bi@ * sqrt ] 2bi / ;
+: sample-corr ( {x} {y} -- corr )
+     [ cov ] [ [ sample-var ] bi@ * sqrt ] 2bi / ;
+
+: full-corr ( {x} {y} -- corr )
+     [ cov ] [ [ full-var ] bi@ * sqrt ] 2bi / ;
+
+ALIAS: corr sample-corr
 
 : cum-sum ( seq -- seq' )
     0 swap [ + dup ] map nip ;