I am working on a new implementation of the operators at http://www.thalesians.com/archive/public/academic/finance/papers/Zumbach_2000.pdf
EDIT: a clearer explanation here: https://www.olseninvest.com/customer/pdf /paper/001207-emaOfEma.pdf
In short, this is a whole bunch of cool time series operators, based on a recurrence relation of the exponential moving average, where each application of the ema () operator takes a new value and the previous ema result. I cannot make latex in this stack exchange, but in any case, my problem now is the software problem.
I implemented this in Scala by hiding the var deep inside the thunks that create the EMA functions. It all works, but it's super complicated, because calling ema (5) and then ema (5) again naturally leads to a different result. I would like to try redoing all this using State Monads, but I quickly get lost in the weeds.
For example, I have the following simplified EMA State monad in Haskell:
import Control.Monad.State
type EMAState = Double
type Tau = Double
ema :: Tau -> Double -> State EMAState Double
ema tau x = state $ \y ->
let alpha = 1 / tau
mu = exp(-alpha)
mu' = 1 - mu
y' = (mu * y) + (mu' * x)
in (y', y')
which I can easily check in GHCI:
*Main Control.Monad.State> runState (ema 5 10) 0
(1.8126924692201818,1.8126924692201818)
applying input 10 to a 5-period EMA initialized to 0. This is all well and good using forM. I can apply multiple input values, etc. Now the next step is to implement a “repeated EMA,” which EMA is applied to itself N times.
iEMA[n](x) = EMA(iEMA[n-1](x))
EMA , , EMA. , , - ( ):
iema :: Int -> Tau -> Double -> State [EMAState] [Double]
EMA:
iEMA[3](x) = EMA(EMA(EMA(x,s1),s2),s3) = (x, [s1,s2,s3]) -> ([y1,y2,y3], [s1',s2',s3'])
, , EMA...
... -> (y3, [s1', s2', s3'])
, , EMA .., , , .
, , , , , . - ?
EDIT:
ema , . ema . :
tau 5
mu 0.818730753
muprime 0.181269247
ema1 ema2 ema3
x 0 0 0 <- States_0
1 0.1812 0.03285 0.00595 <- States_1
5 1.0547 0.21809 0.04441 <- States_2
x , ema1 , /. ema2 ( x!), . ema (ema (x)). ema3 = ema (ema (ema (x))). , , , , , ema, ema3 , , [ema] ema, .