diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index 7397b9f4f1..bab07f7190 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -204,6 +204,16 @@ HELP: cum-product } } ; +HELP: cum-mean +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative mean of " { $snippet "seq" } "." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 1.0 2.0 3.0 } cum-mean ." + "{ 1.0 1.5 2.0 }" + } +} ; + HELP: cum-min { $values { "seq" sequence } { "seq'" sequence } } { $description "Returns the cumulative min of " { $snippet "seq" } "." } diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 7e22222225..65d347f5ed 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.factor @@ -117,6 +117,7 @@ IN: math.statistics.tests [ { 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 +[ { 1.0 1.5 2.0 } ] [ { 1.0 2.0 3.0 } cum-mean ] unit-test { t } [ diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index 30fbce55ed..65606c9e46 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -1,7 +1,7 @@ -! Copyright (C) 2008 Doug Coleman, Michael Judge. +! Copyright (C) 2008 Doug Coleman, Michael Judge, Loryn Jenkins. ! See http://factorcode.org/license.txt for BSD license. USING: assocs combinators generalizations kernel locals math -math.functions math.order math.vectors sequences +math.functions math.order math.vectors math.ranges sequences sequences.private sorting fry arrays grouping sets splitting.monotonic ; IN: math.statistics @@ -350,6 +350,9 @@ ALIAS: std sample-std : cum-product ( seq -- seq' ) 1 [ * ] cum-map ; + +: cum-mean ( seq -- seq' ) + [ cum-sum ] [ length [1,b] ] bi [ / ] 2map ; : cum-count ( seq quot -- seq' ) [ 0 ] dip '[ _ call [ 1 + ] when ] cum-map ; inline