NAG FL Interface
g13mef (inhom_​iema)

Settings help

FL Name Style:


FL Specification Language:


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

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 (ti,zi), for i=1,2,,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 τ, 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 ν depends on the method of interpolation chosen. g13mef gives the option of three interpolation methods:
1. Previous point: ν=1;
2. Linear: ν= (1-μ) / α ;
3. Next point: ν=μ.
The m-iterated exponential moving average, EMA[τ,m;z](ti), 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 Arguments

1: 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: nb0.
2: iema(nb) Real (Kind=nag_wp) array Input/Output
On entry: zi, the current block of observations, for i=k+1,,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 iema(i) = EMA [τ,m;z] (ti) .
3: t(nb) Real (Kind=nag_wp) array Input
On entry: ti, the times for the current block of observations, for i=k+1,,k+b, where k is the number of observations processed so far, i.e., the value supplied in pn on entry.
If titi-1, ifail=31 will be returned, but g13mef will continue as if t was strictly increasing by using the absolute value.
4: tau Real (Kind=nag_wp) Input
On entry: τ, the parameter controlling the rate of decay, which must be sufficiently large that e-α, α=(ti-ti-1)/τ can be calculated without overflowing, for all i.
Constraint: tau>0.0.
5: m Integer Input
On entry: m, the number of times the EMA operator is to be iterated.
Constraint: m1.
6: sinit(m+2) Real (Kind=nag_wp) array Input
On entry: if pn=0, the values used to start the iterative process, with
  • sinit(1)=t0,
  • sinit(2)=z0,
  • sinit(j+2)= EMA [τ,j;z] (t0) , for j=1,2,,m.
If pn0, sinit is not referenced.
7: inter(2) Integer array Input
On entry: the type of interpolation used with inter(1) indicating the interpolation method to use when calculating EMA[τ,1;z] and inter(2) the interpolation method to use when calculating EMA[τ,j;z], j>1.
Three types of interpolation are possible:
inter(i)=1
Previous point, with ν=1.
inter(i)=2
Linear, with ν=(1-μ)/α.
inter(i)=3
Next point, ν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., inter(2)=2, irrespective of the interpolation method used at the first iteration, i.e., the value of inter(1).
Constraint: inter(i)=1, 2 or 3, for i=1,2.
8: 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: pn0.
9: rcomm(lrcomm) Real (Kind=nag_wp) array Communication Array
On entry: communication array, used to store information between calls to g13mef. If lrcomm=0, rcomm is not referenced, pn must be set to 0 and all the data must be supplied in one go.
10: lrcomm Integer Input
On entry: the dimension of the array rcomm as declared in the (sub)program from which g13mef is called.
Constraint: lrcomm=0 or lrcommm+20.
11: 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 -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry 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:
ifail=11
On entry, nb=value.
Constraint: nb0.
ifail=31
On entry, i=value, t(i-1)=value and t(i)=value.
Constraint: t should be strictly increasing.
ifail=32
On entry, i=value, t(i-1)=value and t(i)=value.
Constraint: t(i)t(i-1) if linear interpolation is being used.
ifail=41
On entry, tau=value.
Constraint: tau>0.0.
ifail=42
On entry, tau=value.
On entry at previous call, tau=value.
Constraint: if pn>0 then tau must be unchanged since previous call.
ifail=51
On entry, m=value.
Constraint: m1.
ifail=52
On entry, m=value.
On entry at previous call, m=value.
Constraint: if pn>0 then m must be unchanged since previous call.
ifail=71
On entry, inter(1)=value.
Constraint: inter(1)=1, 2 or 3.
ifail=72
On entry, inter(2)=value.
Constraint: inter(2)=1, 2 or 3.
ifail=73
On entry, inter(1)=value and inter(2)=value.
On entry at previous call, inter(1)=value, inter(2)=value.
Constraint: if pn0, inter must be unchanged since the previous call.
ifail=81
On entry, pn=value.
Constraint: pn0.
ifail=82
On entry, pn=value.
On exit from previous call, pn=value.
Constraint: if pn>0 then pn must be unchanged since previous call.
ifail=91
rcomm has been corrupted between calls.
ifail=101
On entry, pn=0, lrcomm=value and m=value.
Constraint: if pn=0, lrcomm=0 or lrcommm+20.
ifail=102
On entry, pn0, lrcomm=value and m=value.
Constraint: if pn0, lrcommm+20.
ifail=301
Truncation occurred to avoid overflow, check for extreme values in t, iema or for tau. Results are returned using the truncated values.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface 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 implementation-specific information.

9 Further Comments

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 α 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 ifail=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, (t,z) 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 τ and illustrates the effect on the EMA of altering this argument.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 10 20 30 40 50 60 70 80 90 100 Value Time Example Program Simulated inhomogeneous time series and the corresponding EMA(τ,2;y) for a variety of values of τ gnuplot_plot_1 τ = 8.0 gnuplot_plot_2 τ = 2.0 gnuplot_plot_3 τ = 0.5 gnuplot_plot_4