Python implementation comparison of multi-index moving averages (DEMA, TEMA)

Differences between exponential moving average (EMA), double exponential moving average (DEMA) and triple exponential moving average (TEMA)

In the article, I introduced DEMA and TEMA, but this time,

Comparison of exponential moving average (EMA) code written in Python

Similarly, let's compare several implementations, such as using scipy's lfilter, coding directly but speeding up with Numba.

Input data

Input data is a random walk of 100,000 samples.

import numpy as np
dn = np.random.randint(2, size=100000)*2-1
gwalk = np.cumprod(np.exp(dn*0.01))*100

DEMA implementation comparison

Implementation using EMA

Comparison of exponential moving average (EMA) code written in Python This is the fastest EMA code.

from numba import jit
@jit(nopython=True)
def EMA(x, alpha):
    y = np.empty_like(x)
    y[0] = x[0]
    for i in range(1,len(x)):
        y[i] = alpha*x[i] + (1-alpha)*y[i-1]
    return y

%timeit y = EMA(gwalk, 0.15)
1000 loops, best of 3: 227 µs per loop

Use this to implement the DEMA formula as it is.

def DEMA1(x, period):
    alpha = 2/(period+1)
    ema = EMA(x, alpha)
    ema2 = EMA(ema, alpha)
    y = 2*ema-ema2
    return y

%timeit y1 = DEMA1(gwalk, 14)
1000 loops, best of 3: 1.19 ms per loop

I've only used EMA twice, but it's about five times slower than EMA alone.

Implementation using lfilter

It is an implementation using scipy's lfilter function. The filter coefficient is

Differences between exponential moving average (EMA), double exponential moving average (DEMA) and triple exponential moving average (TEMA)

As requested in.

import scipy.signal as sp
def DEMA2(x, period):
    alpha = 2/(period+1)
    a = [1, 2*(alpha-1), (1-alpha)**2]
    b = [alpha*(2-alpha), 2*alpha*(alpha-1)]
    zi = sp.lfilter_zi(b, a)
    y,zf = sp.lfilter(b, a, x, zi=zi*x[0])
    return y

%timeit y2 = DEMA2(gwalk, 14)
1000 loops, best of 3: 717 µs per loop

It's a little faster.

Direct implementation using Numba

@jit(nopython=True)
def DEMA3(x, period):
    alpha = 2/(period+1)
    a1 = 2*(alpha-1)
    a2 = (1-alpha)**2
    b0 = alpha*(2-alpha)
    b1 = 2*alpha*(alpha-1)
    y = np.empty_like(x)
    y[0] = x[0]
    y[1] = b0*x[1] + b1*x[0] - a1*y[0] - a2*y[0]
    for i in range(2,len(x)):
        y[i] = b0*x[i] + b1*x[i-1] - a1*y[i-1] - a2*y[i-2]
    return y

%timeit y3 = DEMA3(gwalk, 14)
1000 loops, best of 3: 488 µs per loop

It's even faster, with direct implementation being the fastest. However, the difference is smaller than in EMA. What about the next TEMA?

TEMA implementation comparison

Implementation using EMA

First is the implementation using EMA.

def TEMA1(x, period):
    alpha = 2/(period+1)
    ema = EMA(x, alpha)
    ema2 = EMA(ema, alpha)
    ema3 = EMA(ema2, alpha)
    y = 3*ema-3*ema2+ema3
    return y

%timeit y1 = TEMA1(gwalk, 14)
100 loops, best of 3: 1.89 ms per loop

First of all, this is about.

Implementation using lfilter

def TEMA2(x, period):
    alpha = 2/(period+1)
    a = [1, 3*(alpha-1), 3*(1-alpha)**2, (alpha-1)**3]
    b = [3*alpha*(1-alpha)+alpha**3, 3*alpha*(alpha-2)*(1-alpha), 3*alpha*(1-alpha)**2]
    zi = sp.lfilter_zi(b, a)
    y,zf = sp.lfilter(b, a, x, zi=zi*x[0])
    return y

%timeit y2 = TEMA2(gwalk, 14)
1000 loops, best of 3: 718 µs per loop

The speed is almost the same as DEMA.

Direct implementation using Numba

@jit(nopython=True)
def TEMA3(x, period):
    alpha = 2/(period+1)
    a1 = 3*(alpha-1)
    a2 = 3*(1-alpha)**2
    a3 = (alpha-1)**3
    b0 = 3*alpha*(1-alpha)+alpha**3
    b1 = 3*alpha*(alpha-2)*(1-alpha)
    b2 = 3*alpha*(1-alpha)**2
    y = np.empty_like(x)
    y[0] = x[0]
    y[1] = b0*x[1] + b1*x[0] + b2*x[0] - a1*y[0] - a2*y[0] - a3*y[0]
    y[2] = b0*x[2] + b1*x[1] + b2*x[0] - a1*y[1] - a2*y[0] - a3*y[0]
    for i in range(3,len(x)):
        y[i] = b0*x[i] + b1*x[i-1] + b2*x[i-2] - a1*y[i-1] - a2*y[i-2] - a3*y[i-3]
    return y

%timeit y3 = TEMA3(gwalk, 14)
1000 loops, best of 3: 604 µs per loop

In the case of TEMA as well, direct implementation kept the fastest. However, since the calculation time of lfilter does not change much even if the order increases, I think that it will be reversed when it becomes 4th or 5th order. Whether or not you need a higher order filter to analyze the market. .. ..

For the time being, DEMA and TEMA will implement directly using Numba. → GitHub

Recommended Posts

Python implementation comparison of multi-index moving averages (DEMA, TEMA)
Comparison of 4 Python web frameworks
Python implementation of particle filters
Implementation of quicksort in Python
Comparison of exponential moving average (EMA) code written in Python
Python implementation of self-organizing particle filters
Implementation of life game in Python
Comparison of calculation speed by implementation of python mpmath (arbitrary precision calculation) (Note)
Implementation of desktop notifications using Python
Python implementation of non-recursive Segment Tree
Implementation of Light CNN (Python Keras)
Implementation of original sorting in Python
Implementation of Dijkstra's algorithm with python
Speed comparison of Python XML parsing
Comparison of Japanese conversion module in Python3
python string comparison / use'list'and'in' instead of'==' and'or'
Comparison of Python serverless frameworks-Zappa vs Chalice
Comparison of matrix transpose speeds with Python
Non-recursive implementation of extended Euclidean algorithm (Python)
Python implementation of continuous hidden Markov model
Performance comparison of face detector with Python + OpenCV
Why the Python implementation of ISUCON 5 used Bottle
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
Comparison of k-means implementation examples of scikit-learn and pyclustering
Thorough comparison of three Python morphological analysis libraries
Implementation of TRIE tree with Python and LOUDS
Simple comparison of Python libraries that operate Excel
[Codeing interview] Implementation of enigma cryptographic machine (Python)
Comparison of R and Python writing (Euclidean algorithm)
Explanation of edit distance and implementation in Python
Comparison of Python and Ruby (Environment / Grammar / Literal)