# NAG FL Interfaceg13mef (inhom_​iema)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g13mef ( nb, iema, t, tau, m, pn,
 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)
#include <nag.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)
The routine may be called by the names g13mef or nagf_tsa_inhom_iema.

## 3Description

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
 $e -ti/τ τ .$
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.

## 4References

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

## 5Arguments

1: $\mathbf{nb}$Integer Input
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) array Input/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) array Input
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}$, ${\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 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: $\mathbf{m}$Integer Input
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) array Input
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 array Input
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}$Integer Input/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) array Communication 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}$Integer Input
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}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error 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}}=⟨\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}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism 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 implementation-specific 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.

## 10Example

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

### 10.1Program Text

Program Text (g13mefe.f90)

### 10.2Program Data

Program Data (g13mefe.d)

### 10.3Program 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.