G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13MFF

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

G13MFF calculates the iterated exponential moving average for an inhomogeneous time series, returning the intermediate results.

## 2  Specification

 SUBROUTINE G13MFF ( SORDER, NB, Z, IEMA, LDIEMA, T, TAU, M1, M2, SINIT, INTER, FTYPE, P, X, PN, RCOMM, LRCOMM, IFAIL)
 INTEGER SORDER, NB, LDIEMA, M1, M2, INTER(2), FTYPE, PN, LRCOMM, IFAIL REAL (KIND=nag_wp) Z(NB), IEMA(LDIEMA,*), T(NB), TAU, SINIT(M2+2), P, X(*), RCOMM(LRCOMM)

## 3  Description

G13MFF 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}_{\mathit{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 (Zumbach and Müller (2001)) for the EMA operator:
 $EMA τ ; y ti = μ ⁢ EMA τ;y ti-1 + ν-μ ⁢ yi-1 + 1-ν ⁢ yi$
where
 $μ = e-α and α = ti - ti-1 τ .$
The value of $\nu$ depends on the method of interpolation chosen and the relationship between $y$ and the input series $z$ depends on the transformation function chosen. G13MFF 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$.
and three transformation functions:
 1 Identity: ${y}_{i}={{z}_{i}}^{\left[p\right]}$; 2 Absolute value: ${y}_{i}={\left|{z}_{i}\right|}^{p}$; 3 Absolute difference: ${y}_{i}={\left|{z}_{i}-{x}_{i}\right|}^{p}$;
where the notation $\left[p\right]$ is used to denote the integer nearest to $p$. In the case of the absolute difference $x$ is a user-supplied vector of length $n$ and therefore each element of the time series is composed of the triplet of scalar values, $\left({t}_{i},{z}_{i},{x}_{i}\right)$.
The iterated exponential moving average, $\text{EMA}\left[\tau ,m;y\right]\left({t}_{i}\right)$, is defined using the recursive formula:
 $EMA τ,m ; y ti = EMA τ ; EMA τ,m-1 ; y ti ti$
with
 $EMA τ,1;y ti = EMA τ;y ti .$
For large datasets or where all the data is not available at the same time, $z,t$ and, where required, $x$ can be split into arbitrary sized blocks and G13MFF 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:     SORDER – INTEGERInput
On entry: determines the storage order of output returned in IEMA.
Constraint: ${\mathbf{SORDER}}=1$ or $2$.
2:     NB – INTEGERInput
On entry: $b$, the number of observations in the current block of data. At each call the size of the block of data supplied in Z, T and X can vary; therefore NB can change between calls to G13MFF.
Constraint: ${\mathbf{NB}}\ge 0$.
3:     Z(NB) – REAL (KIND=nag_wp) arrayInput
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.
4:     IEMA(LDIEMA,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array IEMA must be at least ${\mathbf{M2}}-{\mathbf{M1}}+1$ if ${\mathbf{SORDER}}=1$, otherwise at least NB.
On exit: the iterated exponential moving average.
If ${\mathbf{SORDER}}=1$, ${\mathbf{IEMA}}\left(i,j\right)=\text{EMA}\left[\tau ,j+{\mathbf{M1}}-1;y\right]\left({t}_{i+k}\right)$.
If ${\mathbf{SORDER}}=2$, ${\mathbf{IEMA}}\left(j,i\right)=\text{EMA}\left[\tau ,j+{\mathbf{M1}}-1;y\right]\left({t}_{i+k}\right)$.
For $i=1,2,\dots ,{\mathbf{NB}}$, $j=1,2,\dots ,{\mathbf{M2}}-{\mathbf{M1}}+1$ and $k$ is the number of observations processed so far, i.e., the value supplied in PN on entry.
5:     LDIEMA – INTEGERInput
On entry: the first dimension of the array IEMA as declared in the (sub)program from which G13MFF is called.
Constraints:
• if ${\mathbf{SORDER}}=1$, ${\mathbf{LDIEMA}}\ge {\mathbf{NB}}$;
• otherwise ${\mathbf{LDIEMA}}\ge {\mathbf{M2}}-{\mathbf{M1}}+1$.
6:     T(NB) – REAL (KIND=nag_wp) arrayInput
On entry: ${t}_{\mathit{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 G13MFF will continue as if $t$ was strictly increasing by using the absolute value.
7:     TAU – REAL (KIND=nag_wp)Input
On entry: $\tau$, the parameter controlling the rate of decay. $\tau$ 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$.
8:     M1 – INTEGERInput
On entry: the minimum number of times the EMA operator is to be iterated.
Constraint: ${\mathbf{M1}}\ge 1$.
9:     M2 – INTEGERInput
On entry: the maximum number of times the EMA operator is to be iterated. Therefore G13MFF returns $\text{EMA}\left[\tau ,m;y\right]$, for $m={\mathbf{M1}},{\mathbf{M1}}+1,\dots ,{\mathbf{M2}}$.
Constraint: ${\mathbf{M2}}\ge {\mathbf{M1}}$.
10:   SINIT(${\mathbf{M2}}+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)={y}_{0}$,
• ${\mathbf{SINIT}}\left(j+2\right)=\text{EMA}\left[\tau ,j;y\right]\left({t}_{0}\right)$, $j=1,2,\dots ,{\mathbf{M2}}$.
If ${\mathbf{PN}}\ne 0$ then SINIT is not referenced.
Constraint: if ${\mathbf{FTYPE}}\ne 1$, ${\mathbf{SINIT}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=2,3,\dots ,{\mathbf{M2}}+2$.
11:   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$.
12:   FTYPE – INTEGERInput
On entry: the function type used to define the relationship between $y$ and $z$ when calculating $\text{EMA}\left[\tau ,1;y\right]$. Three functions are provided:
${\mathbf{FTYPE}}=1$
The identity function, with ${y}_{i}={{z}_{i}}^{\left[p\right]}$.
${\mathbf{FTYPE}}=2$
The absolute value, with ${y}_{i}={\left|{z}_{i}\right|}^{p}$.
${\mathbf{FTYPE}}=3$
The absolute difference, with ${y}_{i}={\left|{z}_{i}-{x}_{i}\right|}^{p}$, where the vector $x$ is supplied in X.
Constraint: ${\mathbf{FTYPE}}=1$, $2$ or $3$.
13:   P – REAL (KIND=nag_wp)Input/Output
On entry: $p$, the power used in the transformation function.
On exit: if ${\mathbf{FTYPE}}=1$, then $\left[p\right]$, the actual power used in the transformation function is returned, otherwise P is unchanged.
Constraint: ${\mathbf{P}}\ne 0$.
14:   X($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array X must be at least ${\mathbf{NB}}$ if ${\mathbf{FTYPE}}=3$.
On entry: if ${\mathbf{FTYPE}}=3$, ${x}_{i}$, the vector used to shift 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 ${\mathbf{FTYPE}}\ne 3$ then X is not referenced.
Constraint: if ${\mathbf{FTYPE}}=3$ and ${\mathbf{P}}<0$, ${\mathbf{X}}\left(\mathit{i}\right)\ne {\mathbf{Z}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NB}}$.
15:   PN – INTEGERInput/Output
On entry: $k$, the number of observations processed so far. On the first call to G13MFF, 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 G13MFF.
On exit: $k+b$, the updated number of observations processed so far.
Constraint: ${\mathbf{PN}}\ge 0$.
16:   RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G13MFF. If ${\mathbf{LRCOMM}}=0$, RCOMM is not referenced, PN must be set to $0$ and all the data must be supplied in one go.
17:   LRCOMM – INTEGERInput
On entry: the dimension of the array RCOMM as declared in the (sub)program from which G13MFF is called.
Constraint: ${\mathbf{LRCOMM}}=0$ or ${\mathbf{LRCOMM}}\ge {\mathbf{M2}}+20$.
18:   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{SORDER}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{SORDER}}=1$ or $2$.
${\mathbf{IFAIL}}=21$
On entry, ${\mathbf{NB}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NB}}\ge 0$.
${\mathbf{IFAIL}}=51$
On entry, ${\mathbf{SORDER}}=1$, ${\mathbf{LDIEMA}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{NB}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LDIEMA}}\ge {\mathbf{NB}}$.
On entry, ${\mathbf{SORDER}}=2$, ${\mathbf{LDIEMA}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M2}}-{\mathbf{M1}}+1=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LDIEMA}}\ge {\mathbf{M2}}-{\mathbf{M1}}+1$.
${\mathbf{IFAIL}}=61$
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}}=62$
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}}=71$
On entry, ${\mathbf{TAU}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{TAU}}>0.0$.
${\mathbf{IFAIL}}=72$
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}}=81$
On entry, ${\mathbf{M1}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{M1}}\ge 1$.
${\mathbf{IFAIL}}=82$
On entry, ${\mathbf{M1}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{M1}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then M1 must be unchanged since previous call.
${\mathbf{IFAIL}}=91$
On entry, ${\mathbf{M1}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M2}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{M2}}\ge {\mathbf{M1}}$.
${\mathbf{IFAIL}}=92$
On entry, ${\mathbf{M2}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{M2}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then M2 must be unchanged since previous call.
${\mathbf{IFAIL}}=101$
On entry, ${\mathbf{FTYPE}}\ne 1$, $j=⟨\mathit{\text{value}}⟩$ and ${\mathbf{SINIT}}\left(j\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{FTYPE}}\ne 1$, ${\mathbf{SINIT}}\left(\mathit{j}\right)\ge 0.0$, for $\mathit{j}=2,3,\dots ,{\mathbf{M2}}+2$.
${\mathbf{IFAIL}}=111$
On entry, ${\mathbf{INTER}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{INTER}}\left(1\right)=1$, $2$ or $3$.
${\mathbf{IFAIL}}=112$
On entry, ${\mathbf{INTER}}\left(2\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{INTER}}\left(2\right)=1$, $2$ or $3$.
${\mathbf{IFAIL}}=113$
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 last call.
${\mathbf{IFAIL}}=121$
On entry, ${\mathbf{FTYPE}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{FTYPE}}=1$, $2$ or $3$.
${\mathbf{IFAIL}}=122$
On entry, ${\mathbf{FTYPE}}=⟨\mathit{\text{value}}⟩$, On entry at previous call, ${\mathbf{FTYPE}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}\ne 0$, FTYPE must be unchanged since the previous call.
${\mathbf{IFAIL}}=131$
On entry, ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
Constraint: absolute value of P must be representable as an integer.
${\mathbf{IFAIL}}=132$
On entry, ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{FTYPE}}\ne 1$, ${\mathbf{P}}\ne 0.0$. If ${\mathbf{FTYPE}}=1$, $\text{the nearest integer to ​}{\mathbf{P}}\ne 0$.
${\mathbf{IFAIL}}=133$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{Z}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{FTYPE}}=1$ or $2$ and ${\mathbf{Z}}\left(i\right)=0$ for all $i$ then ${\mathbf{P}}\ge 0.0$.
${\mathbf{IFAIL}}=134$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{Z}}\left(i\right)=⟨\mathit{\text{value}}⟩$, ${\mathbf{X}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{FTYPE}}=3$ and ${\mathbf{Z}}\left(i\right)={\mathbf{X}}\left(i\right)$ for all $i$ then ${\mathbf{P}}\ge 0.0$.
${\mathbf{IFAIL}}=135$
On entry, ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{P}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$ then P must be unchanged since previous call.
${\mathbf{IFAIL}}=151$
On entry, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{PN}}\ge 0$.
${\mathbf{IFAIL}}=152$
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}}=161$
RCOMM has been corrupted between calls.
${\mathbf{IFAIL}}=171$
On entry, ${\mathbf{PN}}=0$, ${\mathbf{LRCOMM}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M2}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}=0$, ${\mathbf{LRCOMM}}=0$ or ${\mathbf{LRCOMM}}\ge {\mathbf{M2}}+20$.
${\mathbf{IFAIL}}=172$
On entry, ${\mathbf{PN}}\ne 0$, ${\mathbf{LRCOMM}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M2}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}\ne 0$ then ${\mathbf{LRCOMM}}\ge {\mathbf{M2}}+20$.
${\mathbf{IFAIL}}=301$
Truncation occurred to avoid overflow, check for extreme values in T, Z X 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 G13MFF.
The more data you supply to G13MFF 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$ and ${y}_{i}$ 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 Z, T or TAU are supplied.

## 9  Example

This example reads in three blocks of simulated data from an inhomogeneous time series, then calculates and prints the iterated EMA for $m$ between $2$ and $6$.

### 9.1  Program Text

Program Text (g13mffe.f90)

### 9.2  Program Data

Program Data (g13mffe.d)

### 9.3  Program Results

Program Results (g13mffe.r)