]> gitweb.factorcode.org Git - factor.git/commitdiff
Adding distribute word to math-finance.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 24 Sep 2008 19:24:01 +0000 (12:24 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 24 Sep 2008 19:24:01 +0000 (12:24 -0700)
extra/math/finance/finance-docs.factor
extra/math/finance/finance-tests.factor
extra/math/finance/finance.factor

index 9094a5bff6e3607486dd7ebd376b50b66e84754e..10deec8a69891cb36b8a45dededac638585ad19d 100644 (file)
@@ -9,6 +9,34 @@ HELP: enumerate
 { $values { "seq" "a sequence" } { "newseq" "a sequence" } }
 { $description "Returns a new sequence where each element is an array of { value, index }" } ;
 
+HELP: distribute
+{ $values { "amount" "a number of amount" } { "n" "a number of buckets" } { "seq" "a sequence" } } 
+{ $description
+    "Distribute 'amount' in 'n' buckets, as equally as possible.  Returns a list of 'n' elements that sum to 'amount'.\n"
+} 
+{ $examples
+    { $example
+        "USING: math.finance"
+        "3 1 distribute"
+        "{ 3 }" }
+    { $example
+        "USING: math.finance"
+        "3 3 distribute"
+        "{ 1 1 1 }" }
+    { $example
+        "USING: math.finance"
+        "5 3 distribute"
+        "{ 2 1 2 }" }
+    { $example
+        "USING: math.finance"
+        "3 5 distribute"
+        "{ 1 0 1 0 1 }" }
+    { $example
+        "USING: math.finance"
+        "1000 7 distribute"
+        "{ 143 143 143 142 143 143 143 }" }
+} ;
+
 HELP: sma
 { $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } }
 { $description "Returns the Simple Moving Average with the specified periodicity." } ;
index dce701bb2f984fd3a139887ad38aaed0caa9b1a0..8c98583c84808976383db6857f5b6314b82a2268 100644 (file)
@@ -6,3 +6,9 @@ IN: math.finance.tests
 
 [ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test
 
+[ { 3 } ] [ 3 1 distribute ] unit-test
+[ { 1 1 1 } ] [ 3 3 distribute ] unit-test
+[ { 2 1 2 } ] [ 5 3 distribute ] unit-test
+[ { 1 0 1 0 1 } ] [ 3 5 distribute ] unit-test
+[ { 143 143 143 142 143 143 143 } ] [ 1000 7 distribute ] unit-test
+
index ffb02208d385bd9a40dee3b37a73d726b51a2fe4..ce598364420492b3a7f3c39a8bf3147a1cc588eb 100644 (file)
@@ -1,13 +1,20 @@
 ! Copyright (C) 2008 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: arrays assocs fry kernel grouping math math.statistics math.vectors sequences ;
+USING: arrays assocs kernel grouping sequences shuffle
+math math.functions math.statistics math.vectors ;
 
 IN: math.finance
 
 : enumerate ( seq -- newseq )
     <enum> >alist ;
 
+: distribute ( amount n -- seq ) 
+    [ / ] keep 0 <array> [ 0 0 ] dip 
+    [ + [ [ dup ] dip + ] dip  
+      [ dup round ] dip 2dup -
+      [ drop ] dip ] map 3nip ;
+
 <PRIVATE
 
 : weighted ( x y a -- z ) 
@@ -22,7 +29,7 @@ IN: math.finance
 PRIVATE>
 
 : ema ( seq n -- newseq )
-    a swap first-rest swap '[ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
+    a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
 
 : sma ( seq n -- newseq )
     clump [ mean ] map ;
@@ -34,4 +41,3 @@ PRIVATE>
     2dup tail-slice -rot swap [ length ] keep
     [ - neg ] dip swap head-slice v- ;
 
-