math.statistics: separate "sample-" and "full-" versions of var, std, ste, and corr.
parent
a2f8735c9a
commit
bacdd691fc
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ;
|
||||
|
|
Loading…
Reference in New Issue