math.statistics: adding cumulative versions of sum, product, min, and max.
parent
c577a5468e
commit
e9ffd2da37
|
@ -174,6 +174,46 @@ HELP: sequence>hashtable
|
|||
}
|
||||
} ;
|
||||
|
||||
HELP: cum-sum
|
||||
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||
{ $description "Returns the cumulative sum of " { $snippet "seq" } "." }
|
||||
{ $examples
|
||||
{ $example "USING: math.statistics prettyprint ;"
|
||||
"{ 1 -1 2 -1 4 } cum-sum ."
|
||||
"{ 1 0 2 1 5 }"
|
||||
}
|
||||
} ;
|
||||
|
||||
HELP: cum-product
|
||||
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||
{ $description "Returns the cumulative product of " { $snippet "seq" } "." }
|
||||
{ $examples
|
||||
{ $example "USING: math.statistics prettyprint ;"
|
||||
"{ 1 2 3 4 } cum-product ."
|
||||
"{ 1 2 6 24 }"
|
||||
}
|
||||
} ;
|
||||
|
||||
HELP: cum-min
|
||||
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||
{ $description "Returns the cumulative min of " { $snippet "seq" } "." }
|
||||
{ $examples
|
||||
{ $example "USING: math.statistics prettyprint ;"
|
||||
"{ 5 3 4 1 } cum-min ."
|
||||
"{ 5 3 3 1 }"
|
||||
}
|
||||
} ;
|
||||
|
||||
HELP: cum-max
|
||||
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||
{ $description "Returns the cumulative max of " { $snippet "seq" } "." }
|
||||
{ $examples
|
||||
{ $example "USING: math.statistics prettyprint ;"
|
||||
"{ 1 -1 3 5 } cum-max ."
|
||||
"{ 1 1 3 5 }"
|
||||
}
|
||||
} ;
|
||||
|
||||
ARTICLE: "histogram" "Computing histograms"
|
||||
"Counting elements in a sequence:"
|
||||
{ $subsections
|
||||
|
|
|
@ -71,3 +71,8 @@ IN: math.statistics.tests
|
|||
|
||||
[ 1.0 ] [ { 1 2 3 } { 1 2 3 } corr ] unit-test
|
||||
[ -1.0 ] [ { 1 2 3 } { -4 -5 -6 } 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
|
||||
[ { 5 3 3 1 } ] [ { 5 3 4 1 } cum-min ] unit-test
|
||||
[ { 1 3 3 5 } ] [ { 1 3 1 5 } cum-max ] unit-test
|
||||
|
|
|
@ -156,3 +156,15 @@ ERROR: empty-sequence ;
|
|||
|
||||
: corr ( {x} {y} -- corr )
|
||||
[ cov ] [ [ var ] bi@ * sqrt ] 2bi / ;
|
||||
|
||||
: cum-sum ( seq -- seq' )
|
||||
0 swap [ + dup ] map nip ;
|
||||
|
||||
: cum-product ( seq -- seq' )
|
||||
1 swap [ * dup ] map nip ;
|
||||
|
||||
: cum-min ( seq -- seq' )
|
||||
[ ?first ] keep [ min dup ] map nip ;
|
||||
|
||||
: cum-max ( seq -- seq' )
|
||||
[ ?first ] keep [ max dup ] map nip ;
|
||||
|
|
Loading…
Reference in New Issue