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

NAG Library Routine Document

G13MEF

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

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

2  Specification

SUBROUTINE G13MEF ( NB, IEMA, T, TAU, M, SINIT, INTER, PN, RCOMM, LRCOMM, IFAIL)
INTEGER  NB, M, INTER(2), PN, LRCOMM, IFAIL
REAL (KIND=nag_wp)  IEMA(NB), T(NB), TAU, SINIT(M+2), RCOMM(LRCOMM)

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. 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 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;zti, 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  Parameters

1:     NB – INTEGERInput
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) arrayInput/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 IEMAi = EMA τ,m;z ti .
3:     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 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 – INTEGERInput
On entry: m, the number of times the EMA operator is to be iterated.
Constraint: M1.
6:     SINIT(M+2) – REAL (KIND=nag_wp) arrayInput
On entry: if PN=0, the values used to start the iterative process, with
  • SINIT1=t0,
  • SINIT2=z0,
  • SINITj+2= EMA τ,j ; z t0 , for j=1,2,,M.
If PN0, SINIT is not referenced.
7:     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.
8:     PN – INTEGERInput/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 should 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) arrayCommunication 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 – INTEGERInput
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 – 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, NB=value.
Constraint: NB0.
IFAIL=31
On entry, i=value, Ti-1=value and Ti=value.
Constraint: T should be strictly increasing.
IFAIL=32
On entry, i=value, Ti-1=value and Ti=value.
Constraint: TiTi-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, INTER1=value.
Constraint: INTER1=1, 2 or 3.
IFAIL=72
On entry, INTER2=value.
Constraint: INTER2=1, 2 or 3.
IFAIL=73
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 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=-999
Dynamic memory allocation failed.

7  Accuracy

Not applicable.

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

9  Example

The example reads in a simulated time series, t,z and calculates the iterated exponential moving average.

9.1  Program Text

Program Text (g13mefe.f90)

9.2  Program Data

Program Data (g13mefe.d)

9.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 parameter.
Produced by GNUPLOT 4.4 patchlevel 0 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(t,2;y) for a variety of values of t t = 8.0 t = 2.0 t = 0.5

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

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