NAG Library Function Document

nag_tsa_inhom_ma (g13mgc)


    1  Purpose
    7  Accuracy


nag_tsa_inhom_ma (g13mgc) provides a moving average, moving norm, moving variance and moving standard deviation operator for an inhomogeneous time series.


#include <nag.h>
#include <nagg13.h>
void  nag_tsa_inhom_ma (Integer nb, double ma[], const double t[], double tau, Integer m1, Integer m2, const double sinit[], const Nag_TS_Interpolation inter[], Nag_TS_Transform ftype, double *p, Integer *pn, double wma[], double rcomm[], NagError *fail)


nag_tsa_inhom_ma (g13mgc) 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  
EMA τ~,1;y ti = EMA τ~;y ti  
EMA τ~ ; y ti = μ EMA τ~;y ti-1 + ν-μ yi-1 + 1-ν yi  
μ = 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. nag_tsa_inhom_ma (g13mgc) 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 nag_tsa_inhom_ma (g13mgc), a few of which are:
(i) Moving Average (MA), as defined in (1) (obtained by setting ftype=Nag_Identity and p=1).
(ii) Moving Norm (MNorm), defined as
MNorm τ,m,p;z = MA τ,1,m; z p 1 / p  
(obtained by setting ftype=Nag_AbsValScaled, 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=Nag_AbsDiff, 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=Nag_AbsDiffScaled, 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 nag_tsa_inhom_ma (g13mgc) called multiple times.


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


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 nag_tsa_inhom_ma (g13mgc).
Constraint: nb0.
2:     ma[nb] doubleInput/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=Nag_AbsValScaled or Nag_AbsDiffScaled
ma[i-1] = MA τ,m1,m2;y ti 1/p ,
ma[i-1] = MA τ,m1,m2;y ti .
3:     t[nb] const doubleInput
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, fail.code= NE_NOT_STRICTLY_INCREASING will be returned, but nag_tsa_inhom_ma (g13mgc) 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 doubleInput
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[dim] const doubleInput
Note: the dimension, dim, of the array sinit must be at least
  • 2×m2+3 when ftype=Nag_AbsDiff or Nag_AbsDiffScaled;
  • m2+2 when ftype=Nag_Identity, Nag_AbsVal or Nag_AbsValScaled;
  • sinit may be NULL when pn0.
On entry: if pn=0, the values used to start the iterative process, with
  • sinit[0]=t0,
  • sinit[1]=y0,
  • sinit[j+1]= EMA τ,j ; y t0 , for i=1,2,,m2.
In addition, if ftype=Nag_AbsDiff or Nag_AbsDiffScaled then
  • sinit[m2+2]=z0,
  • sinit[m2+j+1]= 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 and may be NULL.
Constraint: if ftypeNag_Identity, sinit[j-1]0, for j=2,3,,m2+2.
8:     inter[2] const Nag_TS_InterpolationInput
On entry: the type of interpolation used with inter[0] indicating the interpolation method to use when calculating EMAτ,1;z and inter[1] the interpolation method to use when calculating EMAτ,j;z, j>1.
Three types of interpolation are possible:
Previous point, with ν=1.
Linear, with ν=1-μ/α.
Next point, ν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., inter[1]=Nag_Linear, irrespective of the interpolation method used at the first iteration, i.e., the value of inter[0].
Constraint: inter[i-1]=Nag_PreviousPoint, Nag_Linear or Nag_NextPoint, for i=1,2.
9:     ftype Nag_TS_TransformInput
On entry: the function type used to define the relationship between y and z when calculating EMAτ,1;y. Three functions are provided:
The identity function, with yi = zi p .
ftype=Nag_AbsVal or Nag_AbsValScaled
The absolute value, with yi = zi p .
ftype=Nag_AbsDiff or Nag_AbsDiffScaled
The absolute difference, with yi = zi - MA τ , m ; y ti p .
If ftype=Nag_AbsValScaled or Nag_AbsDiffScaled then the resulting vector of averages is scaled by p-1 as described in ma.
Constraint: ftype=Nag_Identity, Nag_AbsVal, Nag_AbsDiff, Nag_AbsValScaled or Nag_AbsDiffScaled.
10:   p double *Input/Output
On entry: p, the power used in the transformation function.
On exit: if ftype=Nag_Identity, 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 nag_tsa_inhom_ma (g13mgc), 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 nag_tsa_inhom_ma (g13mgc).
On exit: k+b, the updated number of observations processed so far.
Constraint: pn0.
12:   wma[nb] doubleOutput
On exit: either the moving average or exponential moving average, depending on the value of ftype.
if ftype=Nag_AbsDiff or Nag_AbsDiffScaled
wma[i-1] = MA τ ; y ti
wma[i-1] = EMA τ~ ; y ti .
13:   rcomm[2×m2+20] doubleCommunication Array
On entry: communication array, used to store information between calls to nag_tsa_inhom_ma (g13mgc). If rcommisNULL then pn must be set to zero and all the data must be supplied in one go.
14:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
rcomm has been corrupted between calls.
On entry, m1=value.
Constraint: m11.
On entry, nb=value.
Constraint: nb0.
On entry, pn=value.
Constraint: pn0.
On entry, m1=value and m2=value.
Constraint: m2m1.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, i=value, t[i-2]=value and t[i-1]=value.
Constraint: t should be strictly increasing.
If pn>0 then ftype must be unchanged since previous call.
If pn>0 then inter must be unchanged since previous call.
On entry, m1=value.
On entry at previous call, m1=value.
Constraint: if pn>0 then m1 must be unchanged since previous call.
On entry, m2=value.
On entry at previous call, m2=value.
Constraint: if pn>0 then m2 must be unchanged since previous call.
On entry, p=value.
On exit from previous call, p=value.
Constraint: if pn>0 then p must be unchanged since previous call.
On entry, pn=value.
On exit from previous call, pn=value.
Constraint: if pn>0 then pn must be unchanged since previous call.
On entry, tau=value.
On entry at previous call, tau=value.
Constraint: if pn>0 then tau must be unchanged since previous call.
On entry, i=value, ma[i-1]=value and p=value.
Constraint: if ftype=Nag_Identity, Nag_AbsVal or Nag_AbsValScaled and ma[i-1]=0 for any i then p>0.0.
On entry, i=value, ma[i-1]=value, wma[i-1]=value and p=value.
Constraint: if p<0.0, ma[i-1]-wma[i-1]0.0, for any i.
On entry, p=value.
Constraint: absolute value of p must be representable as an integer.
On entry, p=value.
Constraint: if ftypeNag_Identity, p0.0. If ftype=Nag_Identity, the nearest integer to p must not be 0.
On entry, tau=value.
Constraint: tau>0.0.
On entry, ftypeNag_Identity, j=value and sinit[j-1]=value.
Constraint: if ftypeNag_Identity, sinit[j-1]0.0, for j=2,3,,m2+2.
On entry, i=value, t[i-2]=value and t[i-1]=value.
Constraint: t[i-1]t[i-2] if linear interpolation is being used.
Truncation occurred to avoid overflow, check for extreme values in t, ma or for tau. Results are returned using the truncated values.


Not applicable.

Parallelism and Performance

nag_tsa_inhom_ma (g13mgc) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_tsa_inhom_ma (g13mgc) 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Further Comments

Approximately 4m2 real elements are internally allocated by nag_tsa_inhom_ma (g13mgc). If ftype=Nag_AbsDiff or Nag_AbsDiffScaled then a further nb real elements are also allocated.
The more data you supply to nag_tsa_inhom_ma (g13mgc) in one call, i.e., the larger nb is, the more efficient the function will be.
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 fail.code= NW_OVERFLOW_WARN is returned. This should not occur in standard usage and will only occur if extreme values of ma, t or tau are supplied.


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.

Program Text

Program Text (g13mgce.c)

Program Data

Program Data (g13mgce.d)

Program Results

Program Results (g13mgce.r)

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