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

NAG Library Routine Document

G13MGF

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G13MGF provides a moving average, moving norm, moving variance and moving standard deviation operator for an inhomogeneous time series.

2  Specification

SUBROUTINE G13MGF ( NB, MA, T, TAU, M1, M2, SINIT, INTER, FTYPE, P, PN, WMA, RCOMM, LRCOMM, IFAIL)
INTEGER  NB, M1, M2, INTER(2), FTYPE, PN, LRCOMM, IFAIL
REAL (KIND=nag_wp)  MA(NB), T(NB), TAU, SINIT(*), P, WMA(NB), RCOMM(LRCOMM)

3  Description

G13MGF provides a number of operators 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 main operator available, the moving average (MA), with parameter τ is defined as
MA τ, m1, m2; y ti = 1 m2 - m1 +1 j=m1 m2 EMA τ~, j; y ti (1)
where τ~= 2τ m2+m1 , m1 and m2 are user-supplied integers controlling the amount of lag and smoothing respectively, with m2m1 and EMA(·) is the iterated exponential moving average operator.
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 .
and
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. G13MGF 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 - MA τ, m1, m2; z ti p .
where the notation p is used to denote the integer nearest to p. In addition, if either the absolute value or absolute difference transformation are used then the resulting moving average can be scaled by p-1.
The various parameter options allow a number of different operators to be applied by G13MGF, a few of which are:
(i) Moving Average (MA), as defined in (1) (obtained by setting FTYPE=1 and P=1).
(ii) Moving Norm (MNorm), defined as
MNorm τ,m,p;z = MA τ,1,m; z p 1 / p
(obtained by setting FTYPE=4, M1=1 and M2=m).
(iii) Moving Variance (MVar), defined as
MVar τ,m,p;z = MA τ,1,m; z - MA τ,1,m;z p
(obtained by setting FTYPE=3, M1=1 and M2=m).
(iv) Moving Standard Deviation (MSD), defined as
MSD τ,m,p;z = MA τ,1,m; z - MA τ,1,m;z p 1 / p
(obtained by setting FTYPE=5, M1=1 and M2=m).
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 G13MGF 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. At each call the size of the block of data supplied in MA and T can vary; therefore NB can change between calls to G13MGF.
Constraint: NB0.
2:     MA(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 moving average:
if FTYPE=4 or 5
MAi = MA τ,m1,m2;y ti 1/p ,
otherwise
MAi = MA τ,m1,m2;y 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 G13MGF will continue as if t was strictly increasing by using the absolute value. The lagged difference, ti-ti-1 must be sufficiently small that e-α, α=ti-ti-1/τ~ can be calculated without overflowing, for all i.
4:     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, where τ~ = 2τ m2+m1 .
Constraint: TAU>0.0.
5:     M1 – INTEGERInput
On entry: m1, the iteration of the EMA operator at which the sum is started.
Constraint: M11.
6:     M2 – INTEGERInput
On entry: m2, the iteration of the EMA operator at which the sum is ended.
Constraint: M2M1.
7:     SINIT(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array SINIT must be at least 2×M2+3 if FTYPE=3 or 5, and at least M2+2 otherwise.
On entry: if PN=0, the values used to start the iterative process, with
  • SINIT1=t0,
  • SINIT2=y0,
  • SINITj+2= EMA τ,j ; y t0 , for i=1,2,,M2.
In addition, if FTYPE=3 or 5 then
  • SINITM2+3=z0,
  • SINITM2+j+2= EMA τ,j ; z t0 , for j=1,2,,M2.
i.e., initial values based on the original data z as opposed to the transformed data y.
If PN0, SINIT is not referenced.
Constraint: if FTYPE1, SINITj0, for j=2,3,,M2+2.
8:     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.
9:     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 or 4
The absolute value, with yi = zi p .
FTYPE=3 or 5
The absolute difference, with yi = zi - MA τ , m ; y ti p .
If FTYPE=4 or 5 then the resulting vector of averages is scaled by p-1 as described in MA.
Constraint: FTYPE=1, 2, 3, 4 or 5.
10:   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.
11:   PN – INTEGERInput/Output
On entry: k, the number of observations processed so far. On the first call to G13MGF, 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 G13MGF.
On exit: k+b, the updated number of observations processed so far.
Constraint: PN0.
12:   WMA(NB) – REAL (KIND=nag_wp) arrayOutput
On exit: either the moving average or exponential moving average, depending on the value of FTYPE.
if FTYPE=3 or 5
WMAi = MA τ ; y ti
otherwise
WMAi = EMA τ~ ; y ti .
13:   RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G13MGF. If LRCOMM=0, RCOMM is not referenced, PN must be set to 0 and all the data must be supplied in one go.
14:   LRCOMM – INTEGERInput
On entry: the dimension of the array RCOMM as declared in the (sub)program from which G13MGF is called.
Constraint: LRCOMM=0 or LRCOMM2×M2+20.
15:   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, M1=value.
Constraint: M11.
IFAIL=52
On entry, M1=value.
On entry at previous call, M1=value.
Constraint: if PN>0 then M1 must be unchanged since previous call.
IFAIL=61
On entry, M1=value and M2=value.
Constraint: M2M1.
IFAIL=62
On entry, M2=value.
On entry at previous call, M2=value.
Constraint: if PN>0 then M2 must be unchanged since previous call.
IFAIL=71
On entry, j=value and SINITj=value.
Constraint: SINITj0.0, for j=2,3,,M2+2.
IFAIL=81
On entry, INTER1=value.
Constraint: INTER1=1, 2 or 3.
IFAIL=82
On entry, INTER2=value.
Constraint: INTER2=1, 2 or 3.
IFAIL=83
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=91
On entry, FTYPE=value.
Constraint: FTYPE=1, 2, 3, 4 or 5.
IFAIL=92
On entry, FTYPE=value, On entry at previous call, FTYPE=value.
Constraint: if PN0, FTYPE must be unchanged since the previous call.
IFAIL=101
On entry, P=value.
Constraint: absolute value of P must be representable as an integer.
IFAIL=102
On entry, P=value.
Constraint: if FTYPE1, P0.0. If FTYPE=1, the nearest integer to ​P0.
IFAIL=103
On entry, i=value, MAi=value and P=value.
Constraint: if FTYPE=1, 2 or 4 and MAi=0 for all i then P0.0.
IFAIL=104
On entry, i=value, MAi=value, WMAi=value and P=value.
Constraint: if P<0.0, MAi-WMAi0.0, for all i.
IFAIL=105
On entry, P=value.
On exit from previous call, P=value.
Constraint: if PN>0 then P must be unchanged since previous call.
IFAIL=111
On entry, PN=value.
Constraint: PN0.
IFAIL=112
On entry, PN=value.
On exit from previous call, PN=value.
Constraint: if PN>0 then PN must be unchanged since previous call.
IFAIL=131
RCOMM has been corrupted between calls.
IFAIL=141
On entry, PN=0, LRCOMM=value and M2=value.
Constraint: if PN=0, LRCOMM=0 or LRCOMM2M2+20.
IFAIL=142
On entry, PN0, LRCOMM=value and M2=value.
Constraint: if PN0, LRCOMM2M2+20.
IFAIL=301
Truncation occurred to avoid overflow, check for extreme values in T, MA or for TAU. Results are returned using the truncated values.
IFAIL=-999
Dynamic memory allocation failed.

7  Accuracy

Not applicable.

8  Further Comments

Approximately 4m2 real elements are internally allocated by G13MGF. If FTYPE=3 or 5 then a further NB real elements are also allocated.
The more data you supply to G13MGF 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 MA, T or TAU are supplied.

9  Example

The example reads in a simulated time series, t,z and calculates the moving average. The data is supplied in three blocks of differing sizes.

9.1  Program Text

Program Text (g13mgfe.f90)

9.2  Program Data

Program Data (g13mgfe.d)

9.3  Program Results

Program Results (g13mgfe.r)


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

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