]> gitweb.factorcode.org Git - factor.git/commitdiff
math.extras: adding moving-average and exponential-moving-average words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 16:04:27 +0000 (09:04 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 May 2012 16:04:58 +0000 (09:04 -0700)
extra/math/extras/extras-tests.factor
extra/math/extras/extras.factor

index aa9307194f3c4cb58ea694e58089b1124e651ac1..2f71ee3a407ffe4229e52151d8e641f2a45beb63 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2012 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: math.extras tools.test ;
+USING: math.extras math.ranges tools.test ;
 
 IN: math.extras.test
 
@@ -9,3 +9,12 @@ IN: math.extras.test
 { 0 } [ 3 3 jacobi ] unit-test
 { -1 } [ 127 703 jacobi ] unit-test
 { 1 } [ -4 197 jacobi ] unit-test
+
+{ { 2 3 4 5 6 7 8 9 } } [ 10 [1,b] 3 moving-average ] unit-test
+{ { 1+1/2 2+1/2 3+1/2 4+1/2 5+1/2 6+1/2 7+1/2 8+1/2 9+1/2 } }
+[ 10 [1,b] 2 moving-average ] unit-test
+
+{ { 1 1+1/2 2+1/4 3+1/8 4+1/16 5+1/32 } }
+[ 6 [1,b] 1/2 exponential-moving-average ] unit-test
+{ { 1 3 3 5 5 7 7 9 9 11 } }
+[ 10 [1,b] 2 exponential-moving-average ] unit-test
index 2b9c556acbfc37d944f38763dcd75c940b0561af..88d40ac9cb70ad8f73b435f1c0888c568f9dbc08 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2012 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: combinators.short-circuit kernel math math.combinatorics
-math.functions math.order math.primes math.ranges
-math.statistics math.vectors memoize sequences ;
+USING: combinators.short-circuit grouping kernel math
+math.combinatorics math.functions math.order math.primes
+math.ranges math.statistics math.vectors memoize sequences ;
 
 IN: math.extras
 
@@ -85,3 +85,9 @@ PRIVATE>
 
 : legendere ( a m -- n )
     check-legendere jacobi ;
+
+: moving-average ( seq n -- newseq )
+    clump [ mean ] map ;
+
+: exponential-moving-average ( seq a -- newseq )
+    [ 1 ] 2dip [ [ dupd swap - ] dip * + dup ] curry map nip ;