Merge branch 'master' of git://github.com/mrjbq7/factor

db4
Doug Coleman 2008-09-25 15:44:55 -05:00
commit fdde7e410b
14 changed files with 271 additions and 74 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,37 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: help.markup help.syntax ;
IN: math.compare
HELP: absmin
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the smaller absolute number with the original sign."
} ;
HELP: absmax
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the larger absolute number with the original sign."
} ;
HELP: posmax
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the most-positive value, or zero if both are negative."
} ;
HELP: negmin
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the most-negative value, or zero if both are positive."
} ;
HELP: clamp
{ $values { "a" "a number" } { "value" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the value when between 'a' and 'b', 'a' if <= 'a', or 'b' if >= 'b'."
} ;

View File

@ -0,0 +1,28 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: kernel math math.functions math.compare tools.test ;
IN: math.compare.tests
[ -1 ] [ -1 5 absmin ] unit-test
[ -1 ] [ -1 -5 absmin ] unit-test
[ -5 ] [ 1 -5 absmax ] unit-test
[ 5 ] [ 1 5 absmax ] unit-test
[ 0 ] [ -1 -3 posmax ] unit-test
[ 1 ] [ 1 -3 posmax ] unit-test
[ 3 ] [ -1 3 posmax ] unit-test
[ 0 ] [ 1 3 negmin ] unit-test
[ -3 ] [ 1 -3 negmin ] unit-test
[ -1 ] [ -1 3 negmin ] unit-test
[ 0 ] [ 0 -1 2 clamp ] unit-test
[ 1 ] [ 0 1 2 clamp ] unit-test
[ 2 ] [ 0 3 2 clamp ] unit-test

View File

@ -0,0 +1,22 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: math math.order kernel ;
IN: math.compare
: absmin ( a b -- x )
[ [ abs ] bi@ < ] 2keep ? ;
: absmax ( a b -- x )
[ [ abs ] bi@ > ] 2keep ? ;
: posmax ( a b -- x )
0 max max ;
: negmin ( a b -- x )
0 min min ;
: clamp ( a value b -- x )
min max ;

View File

@ -0,0 +1 @@
Comparison functions.

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,38 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: help.markup help.syntax ;
IN: math.finance
HELP: enumerate
{ $values { "seq" "a sequence" } { "newseq" "a sequence" } }
{ $description "Returns a new sequence where each element is an array of { value, index }" } ;
HELP: sma
{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } }
{ $description "Returns the Simple Moving Average with the specified periodicity." } ;
HELP: ema
{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } }
{ $description
"Returns the Exponential Moving Average with the specified periodicity, calculated by:\n"
{ $list
"A = 2.0 / (N + 1)"
"EMA[t] = (A * SEQ[t]) + ((1-A) * EMA[t-1])" }
} ;
HELP: macd
{ $values { "seq" "a sequence" } { "n1" "short number of periods" } { "n2" "long number of periods" } { "newseq" "a sequence" } }
{ $description
"Returns the Moving Average Converge of the sequence, calculated by:\n"
{ $list "MACD[t] = EMA2[t] - EMA1[t]" }
} ;
HELP: momentum
{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } }
{ $description
"Returns the Momentum of the sequence, calculated by:\n"
{ $list "MOM[t] = SEQ[t] - SEQ[t-n]" }
} ;

View File

@ -0,0 +1,8 @@
USING: kernel math math.functions math.finance tools.test ;
IN: math.finance.tests
[ { 2 4 } ] [ { 1 3 5 } 2 sma ] unit-test
[ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test

View File

@ -0,0 +1,37 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: arrays assocs kernel grouping sequences shuffle
math math.functions math.statistics math.vectors ;
IN: math.finance
: enumerate ( seq -- newseq )
<enum> >alist ;
<PRIVATE
: weighted ( x y a -- z )
tuck [ * ] [ 1 swap - * ] 2bi* + ;
: a ( n -- a )
1 + 2 swap / ;
: first-rest ( seq -- first rest )
[ first ] keep 1 tail-slice ;
PRIVATE>
: ema ( seq n -- newseq )
a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
: sma ( seq n -- newseq )
clump [ mean ] map ;
: macd ( seq n1 n2 -- newseq )
rot dup ema [ swap ema ] dip v- ;
: momentum ( seq n -- newseq )
2dup tail-slice -rot swap [ length ] keep
[ - neg ] dip swap head-slice v- ;

View File

@ -0,0 +1 @@
Moving averages and other calculations useful for finance.

8
extra/printf/printf-docs.factor Executable file → Normal file
View File

@ -39,7 +39,9 @@ HELP: sprintf
{ $see-also printf } ; { $see-also printf } ;
ARTICLE: "printf" "Formatted printing" ARTICLE: "printf" "Formatted printing"
"The " { $link printf } " and " { $link sprintf } " words are used for formatted printing.\n" "The " { $vocab-link "printf" } " vocabulary is used for formatted printing.\n"
{ $subsection printf }
{ $subsection sprintf }
"\n" "\n"
"Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n" "Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n"
{ $table { $table
@ -72,3 +74,7 @@ ARTICLE: "printf" "Formatted printing"
"\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point." "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point."
"\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent." "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent."
} ; } ;
ABOUT: "printf"

View File

@ -7,6 +7,10 @@ USING: kernel printf tools.test ;
[ "%s" sprintf ] must-infer [ "%s" sprintf ] must-infer
[ t ] [ "" "" sprintf = ] unit-test
[ t ] [ "asdf" "asdf" sprintf = ] unit-test
[ t ] [ "10" 10 "%d" sprintf = ] unit-test [ t ] [ "10" 10 "%d" sprintf = ] unit-test
[ t ] [ "+10" 10 "%+d" sprintf = ] unit-test [ t ] [ "+10" 10 "%+d" sprintf = ] unit-test

View File

@ -18,3 +18,8 @@ HELP: each-withn
"passed to the quotation given to each-withn for each element in the sequence." "passed to the quotation given to each-withn for each element in the sequence."
} }
{ $see-also map-withn } ; { $see-also map-withn } ;
HELP: randomize
{ $values { "seq" sequence } { "seq'" sequence } }
{ $description "Shuffle the elements in the sequence randomly, returning the new sequence." } ;

View File

@ -160,3 +160,11 @@ PRIVATE>
: ?nth* ( n seq -- elt/f ? ) : ?nth* ( n seq -- elt/f ? )
2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
USE: math.ranges
USE: random
: randomize ( seq -- seq' )
dup length 1 swap [a,b) <reversed> [ dup random pick exchange ] each ;