I compared the calculation time of the moving average written in Python

Previous article Implementing LWMA of MetaTrader with FIR filter function of scipy So, I tried to rewrite LWMA (Linear Weighted Moving Average) with the lfilter () function, but I'm not thankful for it just being neat, so I compared the calculation time.

I thought that there would be not much difference as expected, so I tried to add a little more time series data to take the moving average. I read the 1-minute data for one year of EUR / USD by referring to the following (my blog article).

Read FX historical data with Python

import numpy as np
import pandas as pd

dataM1 = pd.read_csv('DAT_ASCII_EURUSD_M1_2015.csv', sep=';',
                     names=('Time','Open','High','Low','Close', ''),
                     index_col='Time', parse_dates=True)

There are about 370,000 each of the opening price, high price, low price, and closing price.

Comparison of LWMA

I tried using the % time command in Jupyter Notebook to measure the time. First, the older version of LWMA. Calculate the output one by one.

def LWMA(s, ma_period):
    y = pd.Series(0.0, index=s.index)
    for i in range(len(y)):
        if i<ma_period-1: y[i] = 'NaN'
        else:
            y[i] = 0
            for j in range(ma_period):
                y[i] += s[i-j]*(ma_period-j)
            y[i] /= ma_period*(ma_period+1)/2
    return y

%time MA = LWMA(dataM1['Close'], 10)

As a result

Wall time: 3min 35s

What a three and a half minutes. Even if there is a lot of data, it takes 370,000 pieces so much, is it not possible to use Python? .. ..

Take a second look and test a new version of LWMA. Using scipy's filter function.

from scipy.signal import lfilter
def LWMAnew(s, ma_period):
    h = np.arange(ma_period, 0, -1)*2/ma_period/(ma_period+1)
    y = lfilter(h, 1, s)
    y[:ma_period-1] = 'NaN'
    return pd.Series(y, index=s.index)

%time MA = LWMAnew(dataM1['Close'], 10)

As a result

Wall time: 6 ms

What! 6 milliseconds. Is it 35,000 times faster? Is the original too late? You can use scipy.

By the way, if you run it with % timeit instead of% time,

100 loops, best of 3: 3.4 ms per loop

Completed within 1 second even if executed 100 times. It seems that it can be executed in about 3 milliseconds when it is fast.

SMA comparison

Since scipy is fast, I compared it with the pandas function. SMA can be written as a method function of pandas as follows.

def SMA(s, ma_period):
    return s.rolling(ma_period).mean()

%timeit MA = SMA(dataM1['Close'], 10)

As a result

100 loops, best of 3: 16 ms per loop

Run 100 times and it's fast 16ms. It's pretty fast. Now, let's write this using scipy's filter function.

def SMAnew(s, ma_period):
    h = np.ones(ma_period)/ma_period
    y = lfilter(h, 1, s)
    y[:ma_period-1] = 'NaN'
    return pd.Series(y, index=s.index)

%timeit MA = SMAnew(dataM1['Close'], 10)

As a result

100 loops, best of 3: 3.44 ms per loop

fast. About 5 times as much as pandas. After all scipy is fast, so it may be better to rewrite other moving averages using lfilter ().

Recommended Posts

I compared the calculation time of the moving average written in Python
Comparison of exponential moving average (EMA) code written in Python
I replaced the numerical calculation of Python with Rust and compared the speed
Experience the good calculation efficiency of vectorization in Python
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
I compared the moving average of IIR filter type with pandas and scipy
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
I investigated the calculation time of "X in list" (linear search / binary search) and "X in set"
I tried the accuracy of three Stirling's approximations in python
I wrote the queue in Python
I wrote the stack in Python
I compared the speed of Hash with Topaz, Ruby and Python
[Introduction to Python] I compared the naming conventions of C # and Python.
I wrote the code to write the code of Brainf * ck in python
A function that measures the processing time of a method in python
I didn't know the basics of Python
The result of installing python in Anaconda
Calculation of forward average (Piyolog sleep time)
MongoDB for the first time in Python
The basics of running NoxPlayer in Python
The Python project template I think of.
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
In search of the fastest FizzBuzz in Python
Time comparison: Correlation coefficient calculation in Python
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
About Python code for simple moving average assuming the use of Numba
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
Python: I want to measure the processing time of a function neatly
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Calculation result after the decimal point in Python
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
Make a copy of the list in Python
I tried python programming for the first time.
Read the output of subprocess.Popen in real time
Find the divisor of the value entered in python
Try the free version of Progate [Python I]
I touched some of the new features of Python 3.8 ①
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
I implemented the inverse gamma function in python
At the time of python update on ubuntu
I want to display the progress in Python!
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
I compared the speed of go language web framework echo and python web framework flask
I want to use Python in the environment of pyenv + pipenv on Windows 10
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
What I did when I got stuck in the time limit with lambda python
I want to store the result of% time, %% time, etc. in an object (variable)
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python