# NAG FL Interfaceg13mff (inhom_​iema_​all)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g13mff ( nb, z, iema, t, tau, m1, m2, p, x, pn,
 Integer, Intent (In) :: sorder, nb, ldiema, m1, m2, inter(2), ftype, lrcomm Integer, Intent (Inout) :: pn, ifail Real (Kind=nag_wp), Intent (In) :: z(nb), t(nb), tau, sinit(m2+2), x(*) Real (Kind=nag_wp), Intent (Inout) :: iema(ldiema,*), p, rcomm(lrcomm)
#include <nag.h>
 void g13mff_ (const Integer *sorder, const Integer *nb, const double z[], double iema[], const Integer *ldiema, const double t[], const double *tau, const Integer *m1, const Integer *m2, const double sinit[], const Integer inter[], const Integer *ftype, double *p, const double x[], Integer *pn, double rcomm[], const Integer *lrcomm, Integer *ifail)
The routine may be called by the names g13mff or nagf_tsa_inhom_iema_all.

## 3Description

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$. 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 (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}={|{z}_{i}|}^{p}$; 3 Absolute difference: ${y}_{i}={|{z}_{i}-{x}_{i}|}^{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 $m$-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.

## 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{sorder}$Integer Input
On entry: determines the storage order of output returned in iema.
Constraint: ${\mathbf{sorder}}=1$ or $2$.
2: $\mathbf{nb}$Integer Input
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: $\mathbf{z}\left({\mathbf{nb}}\right)$Real (Kind=nag_wp) array Input
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.
Constraint: if ${\mathbf{ftype}}=1$ or $2$ and ${\mathbf{p}}<0.0$, ${\mathbf{z}}\left(\mathit{i}\right)\ne 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{nb}}$.
4: $\mathbf{iema}\left({\mathbf{ldiema}},*\right)$Real (Kind=nag_wp) array Output
Note: the second dimension of the array iema must be at least ${\mathbf{m2}}-{\mathbf{m1}}+1$ if ${\mathbf{sorder}}=1$, and at least ${\mathbf{nb}}$ otherwise.
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: $\mathbf{ldiema}$Integer Input
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: $\mathbf{t}\left({\mathbf{nb}}\right)$Real (Kind=nag_wp) array Input
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}$, ${\mathbf{ifail}}={\mathbf{61}}$ will be returned, but g13mff will continue as if $t$ was strictly increasing by using the absolute value.
7: $\mathbf{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: $\mathbf{m1}$Integer Input
On entry: the minimum number of times the EMA operator is to be iterated.
Constraint: ${\mathbf{m1}}\ge 1$.
9: $\mathbf{m2}$Integer Input
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: $\mathbf{sinit}\left({\mathbf{m2}}+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)={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: $\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$.
12: $\mathbf{ftype}$Integer Input
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}={|{z}_{i}|}^{p}$.
${\mathbf{ftype}}=3$
The absolute difference, with ${y}_{i}={|{z}_{i}-{x}_{i}|}^{p}$, where the vector $x$ is supplied in x.
Constraint: ${\mathbf{ftype}}=1$, $2$ or $3$.
13: $\mathbf{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: $\mathbf{x}\left(*\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{pn}$Integer Input/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 must 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: $\mathbf{rcomm}\left({\mathbf{lrcomm}}\right)$Real (Kind=nag_wp) array Communication 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: $\mathbf{lrcomm}$Integer Input
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: $\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{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$, the nearest integer to ${\mathbf{p}}$ must not be $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 any $i$ then ${\mathbf{p}}>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 any $i$ then ${\mathbf{p}}>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}}=-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

g13mff is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13mff 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 $4×{\mathbf{m2}}$ 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, x or tau are supplied.

## 10Example

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$.

### 10.1Program Text

Program Text (g13mffe.f90)

### 10.2Program Data

Program Data (g13mffe.d)

### 10.3Program Results

Program Results (g13mffe.r)