NAG FL Interface
g13mgf (inhom_​ma)

1 Purpose

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

2 Specification

Fortran Interface
Subroutine g13mgf ( nb, ma, t, tau, m1, m2, sinit, inter, ftype, p, pn, wma, rcomm, lrcomm, ifail)
Integer, Intent (In) :: nb, m1, m2, inter(2), ftype, lrcomm
Integer, Intent (Inout) :: pn, ifail
Real (Kind=nag_wp), Intent (In) :: t(nb), tau, sinit(*)
Real (Kind=nag_wp), Intent (Inout) :: ma(nb), p, rcomm(lrcomm)
Real (Kind=nag_wp), Intent (Out) :: wma(nb)
C Header Interface
#include <nag.h>
void  g13mgf_ (const Integer *nb, double ma[], const double t[], const double *tau, const Integer *m1, const Integer *m2, const double sinit[], const Integer inter[], const Integer *ftype, double *p, Integer *pn, double wma[], double rcomm[], const Integer *lrcomm, Integer *ifail)
The routine may be called by the names g13mgf or nagf_tsa_inhom_ma.

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. 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:
  1. (i)Moving Average (MA), as defined in (1) (obtained by setting ftype=1 and p=1).
  2. (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).
  3. (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).
  4. (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 Arguments

1: nb Integer Input
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: manb 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 moving average:
if ftype=4 or 5
mai = MA τ,m1,m2;y ti 1/p ,
otherwise
mai = MA τ,m1,m2;y ti .
3: tnb 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 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 Integer Input
On entry: m1, the iteration of the EMA operator at which the sum is started.
Constraint: m11.
6: m2 Integer Input
On entry: m2, the iteration of the EMA operator at which the sum is ended.
Constraint: m2m1.
7: sinit* Real (Kind=nag_wp) array Input
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: inter2 Integer array Input
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 Integer Input
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 Integer Input/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 must 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: wmanb Real (Kind=nag_wp) array Output
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: rcommlrcomm Real (Kind=nag_wp) array Communication 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 Integer Input
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 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, 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, ftype1, j=value and sinitj=value.
Constraint: if ftype1, 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 p must not be 0.
ifail=103
On entry, i=value, mai=value and p=value.
Constraint: if ftype=1, 2 or 4 and mai=0 for any i then p>0.0.
ifail=104
On entry, i=value, mai=value, wmai=value and p=value.
Constraint: if p<0.0, mai-wmai0.0, for any 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=-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

g13mgf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13mgf 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 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.

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

10.1 Program Text

Program Text (g13mgfe.f90)

10.2 Program Data

Program Data (g13mgfe.d)

10.3 Program Results

Program Results (g13mgfe.r)