NAG FL Interface
g13mff (inhom_​iema_​all)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

g13mff calculates the iterated exponential moving average for an inhomogeneous time series, returning the intermediate results.

2 Specification

Fortran Interface
Subroutine g13mff ( sorder, nb, z, iema, ldiema, t, tau, m1, m2, sinit, inter, ftype, p, x, pn, rcomm, lrcomm, ifail)
Integer, Intent (In) :: sorder, nb, ldiema, m1, m2, inter(2), ftype, lrcomm
Integer, Intent (Inout) :: pn, ifail
Real (Kind=nag_wp), Intent (In) :: z(nb), t(nb), tau, sinit(m2+2), x(*)
Real (Kind=nag_wp), Intent (Inout) :: iema(ldiema,*), p, rcomm(lrcomm)
C Header Interface
#include <nag.h>
void  g13mff_ (const Integer *sorder, const Integer *nb, const double z[], double iema[], const Integer *ldiema, const double t[], const double *tau, const Integer *m1, const Integer *m2, const double sinit[], const Integer inter[], const Integer *ftype, double *p, const double x[], Integer *pn, double rcomm[], const Integer *lrcomm, Integer *ifail)
The routine may be called by the names g13mff or nagf_tsa_inhom_iema_all.

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. 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 (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 m-iterated exponential moving average, EMA[τ,m;y](ti), 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 Arguments

1: sorder Integer Input
On entry: determines the storage order of output returned in iema.
Constraint: sorder=1 or 2.
2: 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 z, t and x can vary;, therefore, nb can change between calls to g13mff.
Constraint: nb0.
3: z(nb) Real (Kind=nag_wp) array Input
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.
Constraint: if ftype=1 or 2 and p<0.0, z(i)0, for i=1,2,,nb.
4: iema(ldiema,*) Real (Kind=nag_wp) array Output
Note: the second dimension of the array iema must be at least m2-m1+1 if sorder=1, and at least nb otherwise.
On exit: the iterated exponential moving average.
If sorder=1, iema(i,j) = EMA [τ,j+m1-1;y] (ti+k) .
If sorder=2, iema(j,i) = 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 Integer Input
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) 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=61 will be returned, 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 Integer Input
On entry: the minimum number of times the EMA operator is to be iterated.
Constraint: m11.
9: m2 Integer Input
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) array Input
On entry: if pn=0, the values used to start the iterative process, with
  • sinit(1)=t0,
  • sinit(2)=y0,
  • sinit(j+2)= EMA [τ,j;y] (t0) , j=1,2,,m2.
If pn0 then sinit is not referenced.
Constraint: if ftype1, sinit(j)0, for j=2,3,,m2+2.
11: inter(2) Integer array Input
On entry: the type of interpolation used with inter(1) indicating the interpolation method to use when calculating EMA[τ,1;z] and inter(2) the interpolation method to use when calculating EMA[τ,j;z], j>1.
Three types of interpolation are possible:
inter(i)=1
Previous point, with ν=1.
inter(i)=2
Linear, with ν=(1-μ)/α.
inter(i)=3
Next point, ν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., inter(2)=2, irrespective of the interpolation method used at the first iteration, i.e., the value of inter(1).
Constraint: inter(i)=1, 2 or 3, for i=1,2.
12: 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
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) array Input
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, x(i)z(i), for i=1,2,,nb.
15: pn Integer Input/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 must 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) array Communication 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 Integer Input
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 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, 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, t(i-1)=value and t(i)=value.
Constraint: t should be strictly increasing.
ifail=62
On entry, i=value, t(i-1)=value and t(i)=value.
Constraint: t(i)t(i-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 sinit(j)=value.
Constraint: if ftype1, sinit(j)0.0, for j=2,3,,m2+2.
ifail=111
On entry, inter(1)=value.
Constraint: inter(1)=1, 2 or 3.
ifail=112
On entry, inter(2)=value.
Constraint: inter(2)=1, 2 or 3.
ifail=113
On entry, inter(1)=value and inter(2)=value.
On entry at previous call, inter(1)=value, inter(2)=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 p must not be 0.
ifail=133
On entry, i=value, z(i)=value and p=value.
Constraint: if ftype=1 or 2 and z(i)=0 for any i then p>0.0.
ifail=134
On entry, i=value, z(i)=value, x(i)=value and p=value.
Constraint: if ftype=3 and z(i)=x(i) for any i then p>0.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=-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

g13mff is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13mff 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 4×m2 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, x or tau are supplied.

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

10.1 Program Text

Program Text (g13mffe.f90)

10.2 Program Data

Program Data (g13mffe.d)

10.3 Program Results

Program Results (g13mffe.r)