Moving average filters (SMA, EMA, LRMA)

Moving average filter (also known as rolling average, running average) is a time series filter which calculates running weighted sum of time series.

ALGLIB package provides you with dual licensed (open source and commercial) implementation of SMA/LRMA/EMA filters in several programming languages, including our flagship products:

Contents

    1 Moving average filters
           SMA (simple moving average)
           EMA (exponential moving average)
           LRMA (linear regression moving average)
    2 Downloads section

Moving average filters

SMA (simple moving average)

Simple moving average filter, denoted as SMA(k), is a finite impulse response filter. For any moment t it returns average of previous k values (or t values, for t<k). This filter has nice property that for any filter width k and time series length N its output can be efficiently calculated in O(N) time (no dependence on k).

SMA(k) filter is implemented in ALGLIB by the filtersma function. An example of usage can be found in ALGLIB Reference manual: filters_d_sma.

Note #1
One major distinction is that between centered and uncentered versions of the filter. Former uses both past and future values of time series, i.e. FILTER(time=t) = AVG(..., xt-1 , x, xt+1 , ...). Latter uses only past and present values to calculate filter output: FILTER(time=t) = AVG(..., xt-2 , xt-1 , x). As for now, ALGLIB implements only uncentered version of SMA(k) filter (only past/present values are used). If you need centered version, you may obtain it by simply shifting filter output by (k-1)/2 forward in future (for odd filter width k).

EMA (exponential moving average)

Exponential moving average filter, denoted as EMA(k), is an infinite impulse response filter, a transformation of time series according to the formula below:

EMA(t) = α·X(t) + (1-α)·EMA(t-1)

EMA(k) filter is implemented in ALGLIB by the filterlema function. An example of usage can be found in ALGLIB Reference manual: filters_d_ema.

LRMA (linear regression moving average)

Linear regression moving average filter, denoted as LRMA(k), is a finite impulse response filter, which fits straight line to a sliding window with width k. Although LRMA filtering costs more than simple or exponential moving average filters (its running time is O(N·k)), linear regression moving average estimate tends to better follow the trend line.

LRMA(k) filter is implemented in ALGLIB by the filterlrma function. An example of usage can be found in ALGLIB Reference manual: filters_d_lrma.

This article is licensed for personal use only.

Download ALGLIB for C++ / C# / ...

ALGLIB Project offers you two editions of ALGLIB:

ALGLIB Free Edition:
delivered for free
offers full set of numerical functionality
extensive algorithmic optimizations
no low level optimizations
non-commercial license

ALGLIB Commercial Edition:
flexible pricing
offers full set of numerical functionality
extensive algorithmic optimizations
high performance (SMP, SIMD)
commercial license with support plan

Links to download sections for Free and Commercial editions can be found below:

ALGLIB 3.14.0 for C++

C++ library.
Delivered with sources.
Monolithic design.
Extreme portability.
Editions:   FREE   COMMERCIAL

ALGLIB 3.14.0 for C#

C# library with native kernels.
Delivered with sources.
VB.NET and IronPython wrappers.
Extreme portability.
Editions:   FREE   COMMERCIAL

ALGLIB 3.14.0 for Delphi

Delphi wrapper around C core.
Delivered as precompiled binary.
Compatible with FreePascal.
Editions:   FREE   COMMERCIAL

ALGLIB 3.14.0 for CPython

CPython wrapper around C core.
Delivered as precompiled binary.
Editions:   FREE   COMMERCIAL