hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_tsa_inhom_ma (g13mg)

Purpose

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

Syntax

[ma, p, pn, wma, rcomm, ifail] = g13mg(ma, t, tau, m1, m2, sinit, inter, ftype, p, 'nb', nb, 'pn', pn, 'rcomm', rcomm)
[ma, p, pn, wma, rcomm, ifail] = nag_tsa_inhom_ma(ma, t, tau, m1, m2, sinit, inter, ftype, p, 'nb', nb, 'pn', pn, 'rcomm', rcomm)

Description

nag_tsa_inhom_ma (g13mg) provides a number of operators for an inhomogeneous time series. The time series is represented by two vectors of length nn; a vector of times, tt; and a vector of values, zz. Each element of the time series is therefore composed of the pair of scalar values (ti,zi)(ti,zi), for i = 1,2,,ni=1,2,,n. Time tt can be measured in any arbitrary units, as long as all elements of tt use the same units.
The main operator available, the moving average (MA), with parameter ττ is defined as
m2
MA[τ,m1,m2 ; y](ti) = 1/( m2 m1 + 1 )EMA[τ̃,j ; y](ti)
j = m1
MA [ τ, m1, m2; y ] (ti) = 1 m2 - m1 +1 j=m1 m2 EMA [ τ~, j; y ] (ti)
(1)
where τ̃ = (2τ)/(m2 + m1) τ~= 2τ m2+m1 , m1m1 and m2m2 are user-supplied integers controlling the amount of lag and smoothing respectively, with m2m1m2m1 and EMA( · )EMA(·) is the iterated exponential moving average operator.
The iterated exponential moving average, EMA[τ̃,m ; y](ti)EMA[τ~,m;y](ti), is defined using the recursive formula:
EMA [τ̃,m ; y] (ti) = EMA [τ̃ ; EMA[τ̃,m1 ; y](ti)] (ti)
EMA [ τ~,m ; y ] (ti) = EMA [ τ~ ; EMA [ τ~,m-1 ; y ] (ti) ] (ti)
with
EMA [τ̃,1 ; y] (ti) = EMA [τ̃ ; y] (ti)
EMA [ τ~,1;y ] (ti) = EMA [τ~;y] (ti)
and
EMA [τ̃ ; y] (ti) = μ EMA [τ̃ ; y] (ti1) + (νμ) yi1 + (1ν) yi
EMA [ τ~ ; y ] (ti) = μ EMA [τ~;y] ( ti-1 ) + (ν-μ) yi-1 + (1-ν) yi
where
μ = eα   and   α = ( ti ti1 )/(τ̃) .
μ = e-α   and   α = ti - ti-1 τ~ .
The value of νν depends on the method of interpolation chosen and the relationship between yy and the input series zz depends on the transformation function chosen. nag_tsa_inhom_ma (g13mg) gives the option of three interpolation methods:
1. Previous point: ν = 1ν=1.
2. Linear: ν = (1μ) / α ν= (1-μ) / α .
3. Next point: ν = μν=μ.
and three transformation functions:
1. Identity: yi = zi[p] yi = zi [p] .
2. Absolute value: yi = |zi|p yi = |zi| p .
3. Absolute difference: yi = |ziMA[τ,m1,m2 ; z](ti)|p yi = | zi - MA [ τ, m1, m2; z ] ( ti ) | p .
where the notation [p][p] is used to denote the integer nearest to pp. In addition, if either the absolute value or absolute difference transformation are used then the resulting moving average can be scaled by p1p-1.
The various parameter options allow a number of different operators to be applied by nag_tsa_inhom_ma (g13mg), a few of which are:
(i) Moving Average (MA), as defined in (1) (obtained by setting ftype = 1ftype=1 and p = 1p=1).
(ii) Moving Norm (MNorm), defined as
MNorm (τ,m,p ; z) = MA [τ,1,m ; |z|p] 1 / p
MNorm ( τ,m,p;z ) = MA [ τ,1,m; |z| p ] 1 / p
(obtained by setting ftype = 4ftype=4, m1 = 1m1=1 and m2 = mm2=m).
(iii) Moving Variance (MVar), defined as
MVar (τ,m,p ; z) = MA [τ,1,m ; |zMA[τ,1,m ; z]|p]
MVar ( τ,m,p;z ) = MA [ τ,1,m; | z - MA [ τ,1,m;z ] | p ]
(obtained by setting ftype = 3ftype=3, m1 = 1m1=1 and m2 = mm2=m).
(iv) Moving Standard Deviation (MSD), defined as
MSD (τ,m,p ; z) = MA [τ,1,m ; |zMA[τ,1,m ; z]|p] 1 / p
MSD ( τ,m,p;z ) = MA [ τ,1,m; | z - MA [ τ,1,m;z ] | p ] 1 / p
(obtained by setting ftype = 5ftype=5, m1 = 1m1=1 and m2 = mm2=m).
For large datasets or where all the data is not available at the same time, zz and tt can be split into arbitrary sized blocks and nag_tsa_inhom_ma (g13mg) called multiple times.

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

Parameters

Compulsory Input Parameters

1:     ma(nb) – double array
nb, the dimension of the array, must satisfy the constraint nb0nb0.
zizi, the current block of observations, for i = k + 1,,k + bi=k+1,,k+b, where kk is the number of observations processed so far, i.e., the value supplied in pn on entry.
2:     t(nb) – double array
nb, the dimension of the array, must satisfy the constraint nb0nb0.
titi, the times for the current block of observations, for i = k + 1,,k + bi=k+1,,k+b, where kk is the number of observations processed so far, i.e., the value supplied in pn on entry.
If titi1titi-1, ifail = 31ifail=31 will be returned, but nag_tsa_inhom_ma (g13mg) will continue as if tt was strictly increasing by using the absolute value. The lagged difference, titi1ti-ti-1 must be sufficiently small that eαe-α, α = (titi1) / τ̃α=(ti-ti-1)/τ~ can be calculated without overflowing, for all ii.
3:     tau – double scalar
ττ, the parameter controlling the rate of decay. ττ must be sufficiently large that eαe-α, α = (titi1) / τ̃α=(ti-ti-1)/τ~ can be calculated without overflowing, for all ii, where τ̃ = (2τ)/(m2 + m1) τ~ = 2τ m2+m1 .
Constraint: tau > 0.0tau>0.0.
4:     m1 – int64int32nag_int scalar
m1m1, the iteration of the EMA operator at which the sum is started.
Constraint: m11m11.
5:     m2 – int64int32nag_int scalar
m2m2, the iteration of the EMA operator at which the sum is ended.
Constraint: m2m1m2m1.
6:     sinit( : :) – double array
Note: the dimension of the array sinit must be at least 2 × m2 + 32×m2+3 if ftype = 3ftype=3 or 55, and at least m2 + 2m2+2 otherwise.
If pn = 0pn=0, the values used to start the iterative process, with
  • sinit(1) = t0sinit1=t0,
  • sinit(2) = y0sinit2=y0,
  • sinit(j + 2) = EMA [τ,j ; y] (t0) sinitj+2= EMA [ τ,j ; y ] ( t0 ) , for i = 1,2,,m2i=1,2,,m2.
In addition, if ftype = 3ftype=3 or 55 then
  • sinit(m2 + 3) = z0sinitm2+3=z0,
  • sinit(m2 + j + 2) = EMA [τ,j ; z] (t0) sinitm2+j+2= EMA [ τ,j ; z ] ( t0 ) , for j = 1,2,,m2j=1,2,,m2.
i.e., initial values based on the original data zz as opposed to the transformed data yy
If pn0pn0, sinit is not referenced.
Constraint: if ftype1ftype1, sinit(j)0sinitj0, for j = 2,3,,m2 + 2j=2,3,,m2+2.
7:     inter(22) – int64int32nag_int array
The type of interpolation used with inter(1)inter1 indicating the interpolation method to use when calculating EMA[τ,1 ; z]EMA[τ,1;z] and inter(2)inter2 the interpolation method to use when calculating EMA[τ,j ; z]EMA[τ,j;z], j > 1j>1.
Three types of interpolation are possible:
inter(i) = 1interi=1
Previous point, with ν = 1ν=1.
inter(i) = 2interi=2
Linear, with ν = (1μ) / αν=(1-μ)/α.
inter(i) = 3interi=3
Next point, ν = μν=μ.
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e., inter(2) = 2inter2=2, irrespective of the interpolation method used at the first iteration, i.e., the value of inter(1)inter1.
Constraint: inter(i) = 1interi=1, 22 or 33, for i = 1,2i=1,2.
8:     ftype – int64int32nag_int scalar
The function type used to define the relationship between yy and zz when calculating EMA[τ,1 ; y]EMA[τ,1;y]. Three functions are provided:
ftype = 1ftype=1
The identity function, with yi = zi[p] yi = zi [ p ] .
ftype = 2ftype=2 or 44
The absolute value, with yi = |zi|p yi = | zi | p .
ftype = 3ftype=3 or 55
The absolute difference, with yi = |ziMA[τ,m ; y](ti)|p yi = | zi - MA [ τ , m ; y ] ( ti ) | p .
If ftype = 4ftype=4 or 55 then the resulting vector of averages is scaled by p1p-1 as described in ma.
Constraint: ftype = 1ftype=1, 22, 33, 44 or 55.
9:     p – double scalar
pp, the power used in the transformation function.
Constraint: p0p0.

Optional Input Parameters

1:     nb – int64int32nag_int scalar
Default: The dimension of the arrays ma, t. (An error is raised if these dimensions are not equal.)
bb, 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 (g13mg).
Constraint: nb0nb0.
2:     pn – int64int32nag_int scalar
kk, the number of observations processed so far. On the first call to nag_tsa_inhom_ma (g13mg), or when starting to summarise a new dataset, pn must be set to 00. On subsequent calls it must be the same value as returned by the last call to nag_tsa_inhom_ma (g13mg).
Default: 00
Constraint: pn0pn0.
3:     rcomm(2 × m2 + 202×m2+20) – double array
Communication array, used to store information between calls to nag_tsa_inhom_ma (g13mg). On the first call to nag_tsa_inhom_ma (g13mg), or if all the data is provided in one go, rcomm need not be provided.

Input Parameters Omitted from the MATLAB Interface

lrcomm

Output Parameters

1:     ma(nb) – double array
The moving average:
if ftype = 4ftype=4 or 55
ma(i) = {MA[τ,m1,m2 ; y](ti)}1 / p mai = { MA [ τ,m1,m2;y ] (ti) } 1/p ,
otherwise
ma(i) = MA [τ,m1,m2 ; y] (ti) mai = MA [ τ,m1,m2;y ] (ti) .
2:     p – double scalar
If ftype = 1ftype=1, then [p][p], the actual power used in the transformation function is returned, otherwise p is unchanged.
3:     pn – int64int32nag_int scalar
Default: 00
k + bk+b, the updated number of observations processed so far.
4:     wma(nb) – double array
Either the moving average or exponential moving average, depending on the value of ftype.
if ftype = 3ftype=3 or 55
wma(i) = MA [τ ; y] (ti) wmai = MA [ τ ; y ] ( ti )
otherwise
wma(i) = EMA [τ̃ ; y] (ti) wmai = EMA [ τ~ ; y ] ( ti ) .
5:     rcomm(2 × m2 + 202×m2+20) – double array
Communication array, used to store information between calls to nag_tsa_inhom_ma (g13mg).
6:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

  ifail = 11ifail=11
Constraint: nb0nb0.
W ifail = 31ifail=31
Constraint: t should be strictly increasing.
  ifail = 32ifail=32
Constraint: t(i)t(i1)titi-1 if linear interpolation is being used.
  ifail = 41ifail=41
Constraint: tau > 0.0tau>0.0.
  ifail = 42ifail=42
Constraint: if pn > 0pn>0 then tau must be unchanged since previous call.
  ifail = 51ifail=51
Constraint: m11m11.
  ifail = 52ifail=52
Constraint: if pn > 0pn>0 then m1 must be unchanged since previous call.
  ifail = 61ifail=61
Constraint: m2m1m2m1.
  ifail = 62ifail=62
Constraint: if pn > 0pn>0 then m2 must be unchanged since previous call.
  ifail = 71ifail=71
Constraint: if ftype1ftype1, sinit(j)0.0sinitj0.0, for j = 2,3,,m2 + 2j=2,3,,m2+2.
  ifail = 81ifail=81
On entry, inter(1) = _inter1=_.
Constraint: inter(1) = 1inter1=1, 22 or 33.
  ifail = 82ifail=82
On entry, inter(2) = _inter2=_.
Constraint: inter(2) = 1inter2=1, 22 or 33.
  ifail = 83ifail=83
Constraint: if pn0pn0, inter must be unchanged since the last call.
  ifail = 91ifail=91
On entry, ftype = _ftype=_.
Constraint: ftype = 1ftype=1, 22, 33, 44 or 55.
  ifail = 92ifail=92
Constraint: if pn0pn0, ftype must be unchanged since the previous call.
  ifail = 101ifail=101
Constraint: absolute value of p must be representable as an integer.
  ifail = 102ifail=102
Constraint: if ftype1ftype1, p0.0p0.0. If ftype = 1ftype=1, the nearest integer to pp must not be 00.
  ifail = 103ifail=103
Constraint: if ftype = 1ftype=1, 22 or 44 and ma(i) = 0mai=0 for any ii then p > 0.0p>0.0.
  ifail = 104ifail=104
Constraint: if p < 0.0p<0.0, ma(i)wma(i)0.0mai-wmai0.0, for any ii.
  ifail = 105ifail=105
Constraint: if pn > 0pn>0 then p must be unchanged since previous call.
  ifail = 111ifail=111
Constraint: pn0pn0.
  ifail = 112ifail=112
Constraint: if pn > 0pn>0 then pn must be unchanged since previous call.
  ifail = 131ifail=131
rcomm has been corrupted between calls.
  ifail = 141ifail=141
Constraint: if pn = 0pn=0, lrcomm = 0lrcomm=0 or lrcomm2m2 + 20lrcomm2m2+20.
  ifail = 142ifail=142
Constraint: if pn0pn0, lrcomm2m2 + 20lrcomm2m2+20.
W ifail = 301ifail=301
Truncation occurred to avoid overflow, check for extreme values in t, ma or for tau. Results are returned using the truncated values.
  ifail = 999ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

Approximately 4m24m2 real elements are internally allocated by nag_tsa_inhom_ma (g13mg). If ftype = 3ftype=3 or 55 then a further nb real elements are also allocated.
The more data you supply to nag_tsa_inhom_ma (g13mg) in one call, i.e., the larger nb is, the more efficient the function will be, particularly if the function is being run using more than one thread.
Checks are made during the calculation of αα and yiyi 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 = 301ifail=301 is returned. This should not occur in standard usage and will only occur if extreme values of ma, t or tau are supplied.

Example

function nag_tsa_inhom_ma_example
m1 = int64(1);
m2 = int64(2);
ftype = int64(1);
p = 1;
inter = [int64(3); 2];
tau = 2;
sinit = zeros(8, 1);
nb = [5, 10, 15];
rcomm = zeros(20+2*m2, 1);
t = cell(3, 1);
z = cell(3, 1);
t{1} = [7.5; 8.2; 18.1; 22.8; 25.8];
ma{1} = [0.6; 0.6; 0.8; 0.1; 0.2];
t{2} = [26.8; 31.1; 38.4; 45.9; 48.2; 48.9; 57.9; 58.5; 63.9; 65.2];
ma{2} = [0.2;  0.5;  0.7;  0.1;  0.4;  0.7;  0.8;  0.3;  0.2;  0.5];
t{3} = [66.6; 67.4; 69.3; 69.9; 73.0; 75.6; 77.0; 84.7; 86.8; 88.0; ...
        88.5; 91.0; 93.0; 93.7; 94.0];
ma{3} = [0.2;  0.3;  0.8;  0.6;  0.1;  0.7;  0.9;  0.6;  0.3;  0.1;  ...
         0.1;  0.4;  1.0;  1.0;  0.1];
fprintf('\n             Time           MA\n');

% Loop over each block of data.
for i = 1:numel(nb)
  if i == 1
    % Initialise the moving average operator for this block of data
    [ma{i}, p, pn, wma, rcomm, ifail] = ...
      nag_tsa_inhom_ma(ma{i}, t{i}, tau, m1, m2, sinit, inter, ftype, p);
  else
    % Update the moving average operator for this block of data
    [ma{i}, p, pn, wma, rcomm, ifail] = ...
        nag_tsa_inhom_ma(ma{i}, t{i}, tau, m1, m2, sinit, ...
              inter, ftype, p, 'pn', pn, 'rcomm', rcomm);
  end

  % Display the results for this block of data
  for j=1:nb(i)
    fprintf('%3d    %10.1f    %10.3f\n', pn-nb(i)+j, t{i}(j), ma{i}(j));
  end
  fprintf('\n');
end
 

             Time           MA
  1           7.5         0.545
  2           8.2         0.567
  3          18.1         0.786
  4          22.8         0.214
  5          25.8         0.187

  6          26.8         0.192
  7          31.1         0.444
  8          38.4         0.680
  9          45.9         0.155
 10          48.2         0.298
 11          48.9         0.406
 12          57.9         0.777
 13          58.5         0.677
 14          63.9         0.258
 15          65.2         0.351

 16          66.6         0.291
 17          67.4         0.289
 18          69.3         0.572
 19          69.9         0.593
 20          73.0         0.244
 21          75.6         0.532
 22          77.0         0.715
 23          84.7         0.618
 24          86.8         0.426
 25          88.0         0.284
 26          88.5         0.240
 27          91.0         0.332
 28          93.0         0.723
 29          93.7         0.814
 30          94.0         0.744


function g13mg_example
m1 = int64(1);
m2 = int64(2);
ftype = int64(1);
p = 1;
inter = [int64(3); 2];
tau = 2;
sinit = zeros(8, 1);
nb = [5, 10, 15];
rcomm = zeros(20+2*m2, 1);
t = cell(3, 1);
z = cell(3, 1);
t{1} = [7.5; 8.2; 18.1; 22.8; 25.8];
ma{1} = [0.6; 0.6; 0.8; 0.1; 0.2];
t{2} = [26.8; 31.1; 38.4; 45.9; 48.2; 48.9; 57.9; 58.5; 63.9; 65.2];
ma{2} = [0.2;  0.5;  0.7;  0.1;  0.4;  0.7;  0.8;  0.3;  0.2;  0.5];
t{3} = [66.6; 67.4; 69.3; 69.9; 73.0; 75.6; 77.0; 84.7; 86.8; 88.0; ...
        88.5; 91.0; 93.0; 93.7; 94.0];
ma{3} = [0.2;  0.3;  0.8;  0.6;  0.1;  0.7;  0.9;  0.6;  0.3;  0.1;  ...
         0.1;  0.4;  1.0;  1.0;  0.1];
fprintf('\n             Time           MA\n');

% Loop over each block of data.
for i = 1:numel(nb)
  if i == 1
    % Initialise the moving average operator for this block of data
    [ma{i}, p, pn, wma, rcomm, ifail] = ...
      g13mg(ma{i}, t{i}, tau, m1, m2, sinit, inter, ftype, p);
  else
    % Update the moving average operator for this block of data
    [ma{i}, p, pn, wma, rcomm, ifail] = ...
        g13mg(ma{i}, t{i}, tau, m1, m2, sinit, ...
              inter, ftype, p, 'pn', pn, 'rcomm', rcomm);
  end

  % Display the results for this block of data
  for j=1:nb(i)
    fprintf('%3d    %10.1f    %10.3f\n', pn-nb(i)+j, t{i}(j), ma{i}(j));
  end
  fprintf('\n');
end
 

             Time           MA
  1           7.5         0.545
  2           8.2         0.567
  3          18.1         0.786
  4          22.8         0.214
  5          25.8         0.187

  6          26.8         0.192
  7          31.1         0.444
  8          38.4         0.680
  9          45.9         0.155
 10          48.2         0.298
 11          48.9         0.406
 12          57.9         0.777
 13          58.5         0.677
 14          63.9         0.258
 15          65.2         0.351

 16          66.6         0.291
 17          67.4         0.289
 18          69.3         0.572
 19          69.9         0.593
 20          73.0         0.244
 21          75.6         0.532
 22          77.0         0.715
 23          84.7         0.618
 24          86.8         0.426
 25          88.0         0.284
 26          88.5         0.240
 27          91.0         0.332
 28          93.0         0.723
 29          93.7         0.814
 30          94.0         0.744



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013