diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index 8ed42560a7..df4a8c4d37 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -188,6 +188,16 @@ HELP: cum-sum } } ; +HELP: cum-sum0 +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative sum of " { $snippet "seq" } " starting with 0 and not including the whole sum." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 1 -1 2 -1 4 } cum-sum0 ." + "{ 0 1 0 2 1 }" + } +} ; + HELP: cum-count { $values { "seq" sequence } { "quot" quotation } { "seq'" sequence } } { $description "Returns the cumulative count of how many times " { $snippet "quot" } " returns true." } @@ -209,6 +219,16 @@ HELP: cum-product } } ; +HELP: cum-product1 +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative product of " { $snippet "seq" } " starting with 1 and not including the whole product." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 2 3 4 } cum-product1 ." + "{ 1 2 6 }" + } +} ; + HELP: cum-mean { $values { "seq" sequence } { "seq'" sequence } } { $description "Returns the cumulative mean of " { $snippet "seq" } "." } @@ -309,6 +329,7 @@ $nl cum-sum cum-sum0 cum-product + cum-product1 } "Cumulative comparisons:" { $subsections diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 516e626928..df58c7be7f 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.factor @@ -212,6 +212,9 @@ IN: math.statistics.tests { { 0 1 3 6 } } [ { 1 2 3 4 } cum-sum0 ] unit-test +{ { 1 2 6 } } +[ { 2 3 4 } cum-product1 ] unit-test + { H{ { 0 V{ 600 603 606 609 } } diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index 080c7acc7e..5b8d8127fc 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -349,6 +349,9 @@ ALIAS: std sample-std : cum-product ( seq -- seq' ) 1 [ * ] accumulate* ; +: cum-product1 ( seq -- seq' ) + 1 [ * ] accumulate nip ; + : cum-mean ( seq -- seq' ) 0 swap [ [ + dup ] dip 1 + / ] map-index nip ;