<commit_before>! Copyright (C) 2008 <NAME>, <NAME>.
! 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

<PRIVATE

: weighted ( x y a -- z )
    [ * ] [ 1 - neg * ] bi-curry bi* + ;

: a ( n -- a )
    1 + 2 swap / ;

PRIVATE>

: ema ( seq n -- newseq )
    a swap unclip [ swap pick weighted ] accumulate 2nip ;

: sma ( seq n -- newseq )
    clump [ mean ] map ;

: dema ( seq n -- newseq )
    [ ema ] keep [ drop 2 v*n ] [ ema ] 2bi v- ;

: gdema ( seq n v -- newseq )
    [ [ ema ] keep dupd ema ] dip
    [ 1 + v*n ] [ v*n ] bi-curry bi* v- ;

: tema ( seq n -- newseq )
    [ ema ] keep dupd [ ema ] keep
    [ drop [ 3 v*n ] bi@ v- ] [ ema nip ] 3bi v+ ;

: macd ( seq n1 n2 -- newseq )
    rot dup ema [ swap ema ] dip v- ;

: momentum ( seq n -- newseq )<commit_msg>math.finance: some fixes to dema, gdema, and tema.<commit_after>! Copyright (C) 2008 <NAME>, <NAME>.
! 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

: sma ( seq n -- newseq )
    clump [ mean ] map ;

<PRIVATE

: weighted ( prev elt a -- newelt )
    [ 1 swap - * ] [ * ] bi-curry bi* + ; inline

: a ( n -- a )
    1 + 2 swap / ; inline

PRIVATE>

: ema ( seq n -- newseq )
    [ cut [ mean dup ] dip ] [ a ] bi
    [ weighted dup ] curry map nip swap prefix ;

: dema ( seq n -- newseq )
    [ ema ] keep [ drop 2 v*n ] [ ema ] 2bi
    [ length tail* ] keep v- ;

: gdema ( seq n v -- newseq )
    [ [ ema ] keep dupd ema ] dip
    [ 1 + v*n ] [ v*n ] bi-curry bi*
    [ length tail* ] keep v- ;

: tema ( seq n -- newseq )
    [ ema ] keep dupd [ ema ] keep
    [ drop [ 3 v*n ] bi@ [ length tail* ] keep v- ] [ ema nip ] 3bi
    [ length tail* ] keep v+ ;

: macd ( seq n1 n2 -- newseq )
    rot dup ema [ swap ema ] dip v- ;

: momentum ( seq n -- newseq )
{'fraction_non_alphanumeric': 0.14864864864864866, 'fraction_numerical': 0.014514514514514515, 'mean_word_length': 2.0706605222734256, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4102283', 'n_tokens_mistral': 807, 'n_tokens_neox': 767, 'n_words': 270}