NAG CL Interface
g13mec (inhom_​iema)

Settings help

CL Name Style:


1 Purpose

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

2 Specification

#include <nag.h>
void  g13mec (Integer nb, double iema[], const double t[], double tau, Integer m, const double sinit[], const Nag_TS_Interpolation inter[], Integer *pn, double rcomm[], NagError *fail)
The function may be called by the names: g13mec or nag_tsa_inhom_iema.

3 Description

g13mec 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. g13mec 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 g13mec 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 g13mec.
Constraint: nb0.
2: iema[nb] double 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-1] = EMA [τ,m;z] (ti) .
3: t[nb] const double 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, fail.code= NE_NOT_STRICTLY_INCREASING will be returned, but g13mec will continue as if t was strictly increasing by using the absolute value.
4: tau double 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] const double Input
On entry: if pn=0, the values used to start the iterative process, with
  • sinit[0]=t0,
  • sinit[1]=z0,
  • sinit[j+1]= EMA [τ,j;z] (t0) , for j=1,2,,m.
If pn0, sinit is not referenced and may be NULL.
7: inter[2] const Nag_TS_Interpolation Input
On entry: the type of interpolation used with inter[0] indicating the interpolation method to use when calculating EMA[τ,1;z] and inter[1] the interpolation method to use when calculating EMA[τ,j;z], j>1.
Three types of interpolation are possible:
inter[i]=Nag_PreviousPoint
Previous point, with ν=1.
inter[i]=Nag_Linear
Linear, with ν=(1-μ)/α.
inter[i]=Nag_NextPoint
Next point, ν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., inter[1]=Nag_Linear, irrespective of the interpolation method used at the first iteration, i.e., the value of inter[0].
Constraint: inter[i-1]=Nag_PreviousPoint, Nag_Linear or Nag_NextPoint, for i=1,2.
8: pn Integer * Input/Output
On entry: k, the number of observations processed so far. On the first call to g13mec, 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 g13mec.
On exit: k+b, the updated number of observations processed so far.
Constraint: pn0.
9: rcomm[dim] double Communication Array
Note: the dimension, dim, of the array rcomm must be at least
  • m+20, when rcommis notNULL;
  • 0, otherwise.
On entry: communication array, used to store information between calls to g13mec. If rcommisNULL then pn must be set to zero and all the data must be supplied in one go.
10: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_ILLEGAL_COMM
rcomm has been corrupted between calls.
NE_INT
On entry, m=value.
Constraint: m1.
On entry, nb=value.
Constraint: nb0.
On entry, pn=value.
Constraint: pn0.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NOT_STRICTLY_INCREASING
On entry, i=value, t[i-2]=value and t[i-1]=value.
Constraint: t should be strictly increasing.
NE_PREV_CALL
If pn>0 then inter must be unchanged since previous call.
On entry, m=value.
On entry at previous call, m=value.
Constraint: if pn>0 then m must be unchanged since previous call.
On entry, pn=value.
On exit from previous call, pn=value.
Constraint: if pn>0 then pn must be unchanged since previous call.
On entry, tau=value.
On entry at previous call, tau=value.
Constraint: if pn>0 then tau must be unchanged since previous call.
NE_REAL
On entry, tau=value.
Constraint: tau>0.0.
NE_REAL_ARRAY
On entry, i=value, t[i-2]=value and t[i-1]=value.
Constraint: t[i-1]t[i-2] if linear interpolation is being used.
NW_OVERFLOW_WARN
Truncation occurred to avoid overflow, check for extreme values in t, iema or for tau. Results are returned using the truncated values.

7 Accuracy

Not applicable.

8 Parallelism and Performance

g13mec is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13mec 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 function. 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 g13mec.
The more data you supply to g13mec in one call, i.e., the larger nb is, the more efficient the function will be.
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 fail.code= NW_OVERFLOW_WARN 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 (g13mece.c)

10.2 Program Data

Program Data (g13mece.d)

10.3 Program Results

Program Results (g13mece.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