NAG Library Routine Document
g13mef (inhom_iema)
1
Purpose
g13mef calculates the iterated exponential moving average for an inhomogeneous time series.
2
Specification
Fortran Interface
Subroutine g13mef ( 
nb, iema, t, tau, m, sinit, inter, pn, rcomm, lrcomm, ifail) 
Integer, Intent (In)  ::  nb, m, inter(2), lrcomm  Integer, Intent (Inout)  ::  pn, ifail  Real (Kind=nag_wp), Intent (In)  ::  t(nb), tau, sinit(m+2)  Real (Kind=nag_wp), Intent (Inout)  ::  iema(nb), rcomm(lrcomm) 

C Header Interface
#include nagmk26.h
void 
g13mef_ (const Integer *nb, double iema[], const double t[], const double *tau, const Integer *m, const double sinit[], const Integer inter[], Integer *pn, double rcomm[], const Integer *lrcomm, Integer *ifail) 

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$. Time 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
The exponential form of this kernel gives rise to the following iterative formula for the EMA operator (see
Zumbach and Müller (2001)):
where
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:
with
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 Highfrequency 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
Arguments
 1: $\mathbf{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: $\mathbf{iema}\left({\mathbf{nb}}\right)$ – 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: $\mathbf{t}\left({\mathbf{nb}}\right)$ – 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}_{i1}$, ${\mathbf{ifail}}={\mathbf{31}}$ will be returned, but g13mef will continue as if $t$ was strictly increasing by using the absolute value.
 4: $\mathbf{tau}$ – Real (Kind=nag_wp)Input

On entry: $\tau $, the argument controlling the rate of decay, which must be sufficiently large that ${e}^{\alpha}$, $\alpha =\left({t}_{i}{t}_{i1}\right)/\tau $ can be calculated without overflowing, for all $i$.
Constraint:
${\mathbf{tau}}>0.0$.
 5: $\mathbf{m}$ – IntegerInput

On entry: $m$, the number of times the EMA operator is to be iterated.
Constraint:
${\mathbf{m}}\ge 1$.
 6: $\mathbf{sinit}\left({\mathbf{m}}+2\right)$ – 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: $\mathbf{inter}\left(2\right)$ – 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: $\mathbf{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 must 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: $\mathbf{rcomm}\left({\mathbf{lrcomm}}\right)$ – 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: $\mathbf{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: $\mathbf{ifail}$ – IntegerInput/Output

On entry:
ifail must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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}}=0$ or
$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}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{nb}}\ge 0$.
 ${\mathbf{ifail}}=31$

On entry,
$i=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{t}}\left(i1\right)=\u2329\mathit{\text{value}}\u232a$ and
${\mathbf{t}}\left(i\right)=\u2329\mathit{\text{value}}\u232a$.
Constraint:
t should be strictly increasing.
 ${\mathbf{ifail}}=32$

On entry, $i=\u2329\mathit{\text{value}}\u232a$, ${\mathbf{t}}\left(i1\right)=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{t}}\left(i\right)=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{t}}\left(i\right)\ne {\mathbf{t}}\left(i1\right)$ if linear interpolation is being used.
 ${\mathbf{ifail}}=41$

On entry, ${\mathbf{tau}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{tau}}>0.0$.
 ${\mathbf{ifail}}=42$

On entry,
${\mathbf{tau}}=\u2329\mathit{\text{value}}\u232a$.
On entry at previous call,
${\mathbf{tau}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{pn}}>0$ then
tau must be unchanged since previous call.
 ${\mathbf{ifail}}=51$

On entry, ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{m}}\ge 1$.
 ${\mathbf{ifail}}=52$

On entry,
${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
On entry at previous call,
${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{pn}}>0$ then
m must be unchanged since previous call.
 ${\mathbf{ifail}}=71$

On entry, ${\mathbf{inter}}\left(1\right)=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{inter}}\left(1\right)=1$, $2$ or $3$.
 ${\mathbf{ifail}}=72$

On entry, ${\mathbf{inter}}\left(2\right)=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{inter}}\left(2\right)=1$, $2$ or $3$.
 ${\mathbf{ifail}}=73$

On entry,
${\mathbf{inter}}\left(1\right)=\u2329\mathit{\text{value}}\u232a$ and
${\mathbf{inter}}\left(2\right)=\u2329\mathit{\text{value}}\u232a$.
On entry at previous call,
${\mathbf{inter}}\left(1\right)=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{inter}}\left(2\right)=\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{pn}}\ne 0$,
inter must be unchanged since the previous call.
 ${\mathbf{ifail}}=81$

On entry, ${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{pn}}\ge 0$.
 ${\mathbf{ifail}}=82$

On entry,
${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
On exit from previous call,
${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
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}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
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}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
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}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
Not applicable.
8
Parallelism and Performance
g13mef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13mef makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementationspecific information.
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.
10
Example
The example reads in a simulated time series, $\left(t,z\right)$ and calculates the iterated exponential moving average.
10.1
Program Text
Program Text (g13mefe.f90)
10.2
Program Data
Program Data (g13mefe.d)
10.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 argument.