G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13MEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G13MEF calculates the iterated exponential moving average for an inhomogeneous time series.

## 2  Specification

 SUBROUTINE G13MEF ( NB, IEMA, T, TAU, M, SINIT, INTER, PN, RCOMM, LRCOMM, IFAIL)
 INTEGER NB, M, INTER(2), PN, LRCOMM, IFAIL REAL (KIND=nag_wp) IEMA(NB), T(NB), TAU, SINIT(M+2), RCOMM(LRCOMM)

## 3  Description

G13MEF calculates the iterated exponential moving average for an inhomogeneous time series. The time series is represented by two vectors of length $n$; a vector of times, $t$; and a vector of values, $z$. Each element of the time series is therefore composed of the pair of scalar values $\left({t}_{\mathit{i}},{z}_{i}\right)$, for $\mathit{i}=1,2,\dots ,n$. The time $t$ can be measured in any arbitrary units, as long as all elements of $t$ use the same units.
The exponential moving average (EMA), with parameter $\tau$, is an average operator, with the exponentially decaying kernel given by
 $e -t/τ τ .$
The exponential form of this kernel gives rise to the following iterative formula for the EMA operator (see Zumbach and Müller (2001)):
 $EMA τ;z ti = μ ⁢ EMA τ;z ti-1 + ν-μ ⁢ zi-1 + 1-ν ⁢ zi$
where
 $μ = e-α and α = ti - ti-1 τ .$
The value of $\nu$ depends on the method of interpolation chosen. G13MEF gives the option of three interpolation methods:
 1 Previous point: $\nu =1$; 2 Linear: $\nu =\left(1-\mu \right)/\alpha$; 3 Next point: $\nu =\mu$.
The $m$-iterated exponential moving average, $\text{EMA}\left[\tau ,m;z\right]\left({t}_{i}\right)$, $m>1$, is defined using the recursive formula:
 $EMA τ,m;z = EMA τ ; EMA τ,m-1 ; z$
with
 $EMA τ,1;z = EMA τ ; z .$
For large datasets or where all the data is not available at the same time, $z$ and $t$ can be split into arbitrary sized blocks and G13MEF called multiple times.

## 4  References

Dacorogna M M, Gencay R, Müller U, Olsen R B and Pictet O V (2001) An Introduction to High-frequency Finance Academic Press
Zumbach G O and Müller U A (2001) Operators on inhomogeneous time series International Journal of Theoretical and Applied Finance 4(1) 147–178

## 5  Parameters

1:     NB – INTEGERInput
On entry: $b$, the number of observations in the current block of data. The size of the block of data supplied in IEMA and T can vary; therefore NB can change between calls to G13MEF.
Constraint: ${\mathbf{NB}}\ge 0$.
2:     IEMA(NB) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${z}_{\mathit{i}}$, the current block of observations, for $\mathit{i}=k+1,\dots ,k+b$, where $k$ is the number of observations processed so far, i.e., the value supplied in PN on entry.
On exit: the iterated EMA, with ${\mathbf{IEMA}}\left(i\right)=\text{EMA}\left[\tau ,m;z\right]\left({t}_{i}\right)$.
3:     T(NB) – REAL (KIND=nag_wp) arrayInput
On entry: ${t}_{i}$, the times for the current block of observations, for $\mathit{i}=k+1,\dots ,k+b$, where $k$ is the number of observations processed so far, i.e., the value supplied in PN on entry.
If ${t}_{i}\le {t}_{i-1}$, a warning will be issued, but G13MEF will continue as if $t$ was strictly increasing by using the absolute value.
4:     TAU – REAL (KIND=nag_wp)Input
On entry: $\tau$, the parameter controlling the rate of decay, which must be sufficiently large that ${e}^{-\alpha }$, $\alpha =\left({t}_{i}-{t}_{i-1}\right)/\tau$ can be calculated without overflowing, for all $i$.
Constraint: ${\mathbf{TAU}}>0.0$.
5:     M – INTEGERInput
On entry: $m$, the number of times the EMA operator is to be iterated.
Constraint: ${\mathbf{M}}\ge 1$.
6:     SINIT(${\mathbf{M}}+2$) – REAL (KIND=nag_wp) arrayInput
On entry: if ${\mathbf{PN}}=0$, the values used to start the iterative process, with
• ${\mathbf{SINIT}}\left(1\right)={t}_{0}$,
• ${\mathbf{SINIT}}\left(2\right)={z}_{0}$,
• ${\mathbf{SINIT}}\left(\mathit{j}+2\right)=\text{EMA}\left[\tau ,\mathit{j};z\right]\left({t}_{0}\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
If ${\mathbf{PN}}\ne 0$, SINIT is not referenced.
7:     INTER($2$) – INTEGER arrayInput
On entry: the type of interpolation used with ${\mathbf{INTER}}\left(1\right)$ indicating the interpolation method to use when calculating $\text{EMA}\left[\tau ,1;z\right]$ and ${\mathbf{INTER}}\left(2\right)$ the interpolation method to use when calculating $\text{EMA}\left[\tau ,j;z\right]$, $j>1$.
Three types of interpolation are possible:
${\mathbf{INTER}}\left(i\right)=1$
Previous point, with $\nu =1$.
${\mathbf{INTER}}\left(i\right)=2$
Linear, with $\nu =\left(1-\mu \right)/\alpha$.
${\mathbf{INTER}}\left(i\right)=3$
Next point, $\nu =\mu$.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., ${\mathbf{INTER}}\left(2\right)=2$, irrespective of the interpolation method used at the first iteration, i.e., the value of ${\mathbf{INTER}}\left(1\right)$.
Constraint: ${\mathbf{INTER}}\left(\mathit{i}\right)=1$, $2$ or $3$, for $\mathit{i}=1,2$.
8:     PN – INTEGERInput/Output
On entry: $k$, the number of observations processed so far. On the first call to G13MEF, or when starting to summarise a new dataset, PN should be set to $0$. On subsequent calls it must be the same value as returned by the last call to G13MEF.
On exit: $k+b$, the updated number of observations processed so far.
Constraint: ${\mathbf{PN}}\ge 0$.
9:     RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G13MEF. If ${\mathbf{LRCOMM}}=0$, RCOMM is not referenced, PN must be set to $0$ and all the data must be supplied in one go.
10:   LRCOMM – INTEGERInput
On entry: the dimension of the array RCOMM as declared in the (sub)program from which G13MEF is called.
Constraint: ${\mathbf{LRCOMM}}=0$ or ${\mathbf{LRCOMM}}\ge {\mathbf{M}}+20$.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=11$
On entry, ${\mathbf{NB}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NB}}\ge 0$.
${\mathbf{IFAIL}}=31$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{T}}\left(i-1\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{T}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
Constraint: T should be strictly increasing.
${\mathbf{IFAIL}}=32$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{T}}\left(i-1\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{T}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{T}}\left(i\right)\ne {\mathbf{T}}\left(i-1\right)$ if linear interpolation is being used.
${\mathbf{IFAIL}}=41$
On entry, ${\mathbf{TAU}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{TAU}}>0.0$.
${\mathbf{IFAIL}}=42$
On entry, ${\mathbf{TAU}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{TAU}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then TAU must be unchanged since previous call.
${\mathbf{IFAIL}}=51$
On entry, ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{M}}\ge 1$.
${\mathbf{IFAIL}}=52$
On entry, ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then M must be unchanged since previous call.
${\mathbf{IFAIL}}=71$
On entry, ${\mathbf{INTER}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{INTER}}\left(1\right)=1$, $2$ or $3$.
${\mathbf{IFAIL}}=72$
On entry, ${\mathbf{INTER}}\left(2\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{INTER}}\left(2\right)=1$, $2$ or $3$.
${\mathbf{IFAIL}}=73$
On entry, ${\mathbf{INTER}}\left(1\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{INTER}}\left(2\right)=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{INTER}}\left(1\right)=⟨\mathit{\text{value}}⟩$, ${\mathbf{INTER}}\left(2\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}\ne 0$, INTER must be unchanged since the previous call.
${\mathbf{IFAIL}}=81$
On entry, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{PN}}\ge 0$.
${\mathbf{IFAIL}}=82$
On entry, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then PN must be unchanged since previous call.
${\mathbf{IFAIL}}=91$
RCOMM has been corrupted between calls.
${\mathbf{IFAIL}}=101$
On entry, ${\mathbf{PN}}=0$, ${\mathbf{LRCOMM}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}=0$, ${\mathbf{LRCOMM}}=0$ or ${\mathbf{LRCOMM}}\ge {\mathbf{M}}+20$.
${\mathbf{IFAIL}}=102$
On entry, ${\mathbf{PN}}\ne 0$, ${\mathbf{LRCOMM}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}\ne 0$, ${\mathbf{LRCOMM}}\ge {\mathbf{M}}+20$.
${\mathbf{IFAIL}}=301$
Truncation occurred to avoid overflow, check for extreme values in T, IEMA or for TAU. Results are returned using the truncated values.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.

## 7  Accuracy

Not applicable.

Approximately $4m$ real elements are internally allocated by G13MEF.
The more data you supply to G13MEF in one call, i.e., the larger NB is, the more efficient the routine will be, particularly if the routine is being run using more than one thread.
Checks are made during the calculation of $\alpha$ to avoid overflow. If a potential overflow is detected the offending value is replaced with a large positive or negative value, as appropriate, and the calculations performed based on the replacement values. In such cases ${\mathbf{IFAIL}}={\mathbf{301}}$ is returned. This should not occur in standard usage and will only occur if extreme values of IEMA, T or TAU are supplied.

## 9  Example

The example reads in a simulated time series, $\left(t,z\right)$ and calculates the iterated exponential moving average.

### 9.1  Program Text

Program Text (g13mefe.f90)

### 9.2  Program Data

Program Data (g13mefe.d)

### 9.3  Program Results

Program Results (g13mefe.r)

This example plot shows the exponential moving average for the same data using three different values of $\tau$ and illustrates the effect on the EMA of altering this parameter.