G13MFF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G13MFF

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.

+ Contents

    1  Purpose
    7  Accuracy

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 ti,zi, for i=1,2,,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 τ, 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 ν 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: ν=1;
2. Linear: ν= 1-μ / α ;
3. Next point: ν=μ.
and three transformation functions:
1. Identity: yi = zi p ;
2. Absolute value: yi = zi p ;
3. Absolute difference: yi = zi - xi p ;
where the notation p 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, ti,zi,xi.
The iterated exponential moving average, EMAτ,m;yti, 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: 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: NB0.
3:     Z(NB) – REAL (KIND=nag_wp) arrayInput
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.
4:     IEMA(LDIEMA,*) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array IEMA must be at least M2-M1+1 if SORDER=1, otherwise at least NB.
On exit: the iterated exponential moving average.
If SORDER=1, IEMAij = EMA τ,j+M1-1 ; y ti+k .
If SORDER=2, IEMAji = EMA τ,j+M1-1 ; y ti+k .
For i=1,2,,NB, j=1,2,,M2-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 SORDER=1, LDIEMANB;
  • otherwise LDIEMAM2-M1+1.
6:     T(NB) – REAL (KIND=nag_wp) arrayInput
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, 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: τ, the parameter controlling the rate of decay. τ must be sufficiently large that e-α, α=ti-ti-1/τ can be calculated without overflowing, for all i.
Constraint: TAU>0.0.
8:     M1 – INTEGERInput
On entry: the minimum number of times the EMA operator is to be iterated.
Constraint: M11.
9:     M2 – INTEGERInput
On entry: the maximum number of times the EMA operator is to be iterated. Therefore G13MFF returns EMA τ,m;y , for m=M1,M1+1,,M2.
Constraint: M2M1.
10:   SINIT(M2+2) – REAL (KIND=nag_wp) arrayInput
On entry: if PN=0, the values used to start the iterative process, with
  • SINIT1=t0,
  • SINIT2=y0,
  • SINITj+2= EMA τ,j ; y t0 , j=1,2,,M2.
If PN0 then SINIT is not referenced.
Constraint: if FTYPE1, SINITj0, for j=2,3,,M2+2.
11:   INTER(2) – INTEGER arrayInput
On entry: the type of interpolation used with INTER1 indicating the interpolation method to use when calculating EMAτ,1;z and INTER2 the interpolation method to use when calculating EMAτ,j;z, j>1.
Three types of interpolation are possible:
INTERi=1
Previous point, with ν=1.
INTERi=2
Linear, with ν=1-μ/α.
INTERi=3
Next point, ν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., INTER2=2, irrespective of the interpolation method used at the first iteration, i.e., the value of INTER1.
Constraint: INTERi=1, 2 or 3, for i=1,2.
12:   FTYPE – INTEGERInput
On entry: the function type used to define the relationship between y and z when calculating EMAτ,1;y. Three functions are provided:
FTYPE=1
The identity function, with yi = zi p .
FTYPE=2
The absolute value, with yi = zi p .
FTYPE=3
The absolute difference, with yi = zi - xi p , where the vector x is supplied in X.
Constraint: 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 FTYPE=1, then p, the actual power used in the transformation function is returned, otherwise P is unchanged.
Constraint: P0.
14:   X(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array X must be at least NB if FTYPE=3.
On entry: if FTYPE=3, xi, the vector used to shift 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 FTYPE3 then X is not referenced.
Constraint: if FTYPE=3 and P<0, XiZi, for i=1,2,,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: PN0.
16:   RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G13MFF. If 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: LRCOMM=0 or LRCOMMM2+20.
18:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -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, SORDER=value.
Constraint: SORDER=1 or 2.
IFAIL=21
On entry, NB=value.
Constraint: NB0.
IFAIL=51
On entry, SORDER=1, LDIEMA=value and NB=value.
Constraint: LDIEMANB.
On entry, SORDER=2, LDIEMA=value and M2-M1+1=value.
Constraint: LDIEMAM2-M1+1.
IFAIL=61
On entry, i=value, Ti-1=value and Ti=value.
Constraint: T should be strictly increasing.
IFAIL=62
On entry, i=value, Ti-1=value and Ti=value.
Constraint: TiTi-1 if linear interpolation is being used.
IFAIL=71
On entry, TAU=value.
Constraint: TAU>0.0.
IFAIL=72
On entry, TAU=value.
On entry at previous call, TAU=value.
Constraint: if PN>0 then TAU must be unchanged since previous call.
IFAIL=81
On entry, M1=value.
Constraint: M11.
IFAIL=82
On entry, M1=value.
On entry at previous call, M1=value.
Constraint: if PN>0 then M1 must be unchanged since previous call.
IFAIL=91
On entry, M1=value and M2=value.
Constraint: M2M1.
IFAIL=92
On entry, M2=value.
On entry at previous call, M2=value.
Constraint: if PN>0 then M2 must be unchanged since previous call.
IFAIL=101
On entry, FTYPE1, j=value and SINITj=value.
Constraint: if FTYPE1, SINITj0.0, for j=2,3,,M2+2.
IFAIL=111
On entry, INTER1=value.
Constraint: INTER1=1, 2 or 3.
IFAIL=112
On entry, INTER2=value.
Constraint: INTER2=1, 2 or 3.
IFAIL=113
On entry, INTER1=value and INTER2=value.
On entry at previous call, INTER1=value, INTER2=value.
Constraint: if PN0, INTER must be unchanged since the last call.
IFAIL=121
On entry, FTYPE=value.
Constraint: FTYPE=1, 2 or 3.
IFAIL=122
On entry, FTYPE=value, On entry at previous call, FTYPE=value.
Constraint: if PN0, FTYPE must be unchanged since the previous call.
IFAIL=131
On entry, P=value.
Constraint: absolute value of P must be representable as an integer.
IFAIL=132
On entry, P=value.
Constraint: if FTYPE1, P0.0. If FTYPE=1, the nearest integer to ​P0.
IFAIL=133
On entry, i=value, Zi=value and P=value.
Constraint: if FTYPE=1 or 2 and Zi=0 for all i then P0.0.
IFAIL=134
On entry, i=value, Zi=value, Xi=value and P=value.
Constraint: if FTYPE=3 and Zi=Xi for all i then P0.0.
IFAIL=135
On entry, P=value.
On exit from previous call, P=value.
Constraint: if PN>0 then P must be unchanged since previous call.
IFAIL=151
On entry, PN=value.
Constraint: PN0.
IFAIL=152
On entry, PN=value.
On exit from previous call, PN=value.
Constraint: if PN>0 then PN must be unchanged since previous call.
IFAIL=161
RCOMM has been corrupted between calls.
IFAIL=171
On entry, PN=0, LRCOMM=value and M2=value.
Constraint: if PN=0, LRCOMM=0 or LRCOMMM2+20.
IFAIL=172
On entry, PN0, LRCOMM=value and M2=value.
Constraint: if PN0 then LRCOMMM2+20.
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.
IFAIL=-999
Dynamic memory allocation failed.

7  Accuracy

Not applicable.

8  Further Comments

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 α and yi 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 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)


G13MFF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012