PDF version (NAG web site
, 64-bit version, 64-bit version)
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 ; a vector of times, ; and a vector of values, . Each element of the time series is therefore composed of the pair of scalar values , for . Time can be measured in any arbitrary units, as long as all elements of use the same units.
The main operator available, the moving average (MA), with parameter
is defined as
where
,
and
are user-supplied integers controlling the amount of lag and smoothing respectively, with
and
is the iterated exponential moving average operator.
The iterated exponential moving average,
, is defined using the recursive formula:
with
and
where
The value of
depends on the method of interpolation chosen and the relationship between
and the input series
depends on the transformation function chosen.
nag_tsa_inhom_ma (g13mg) gives the option of three interpolation methods:
1. |
Previous point: |
. |
2. |
Linear: |
. |
3. |
Next point: |
. |
and three transformation functions:
1. |
Identity: |
. |
2. |
Absolute value: |
. |
3. |
Absolute difference: |
. |
where the notation
is used to denote the integer nearest to
. In addition, if either the absolute value or absolute difference transformation are used then the resulting moving average can be scaled by
.
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 and ). |
(ii) |
Moving Norm (MNorm), defined as
(obtained by setting , and ). |
(iii) |
Moving Variance (MVar), defined as
(obtained by setting , and ). |
(iv) |
Moving Standard Deviation (MSD), defined as
(obtained by setting , and ). |
For large datasets or where all the data is not available at the same time, and 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:
– double array
-
, the current block of observations, for
, where
is the number of observations processed so far, i.e., the value supplied in
pn on entry.
- 2:
– double array
-
, the times for the current block of observations, for
, where
is the number of observations processed so far, i.e., the value supplied in
pn on entry.
If , will be returned, but nag_tsa_inhom_ma (g13mg) will continue as if was strictly increasing by using the absolute value. The lagged difference, must be sufficiently small that , can be calculated without overflowing, for all .
- 3:
– double scalar
-
, the parameter controlling the rate of decay. must be sufficiently large that , can be calculated without overflowing, for all , where .
Constraint:
.
- 4:
– int64int32nag_int scalar
-
, the iteration of the EMA operator at which the sum is started.
Constraint:
.
- 5:
– int64int32nag_int scalar
-
, the iteration of the EMA operator at which the sum is ended.
Constraint:
.
- 6:
– double array
-
The dimension of the array
sinit
must be at least
if
or
, and at least
otherwise
If
, the values used to start the iterative process, with
- ,
- ,
- , for .
In addition, if
or
then
- ,
-
, for .
i.e., initial values based on the original data
as opposed to the transformed data
If
,
sinit is not referenced.
Constraint:
if , , for .
- 7:
– int64int32nag_int array
-
The type of interpolation used with
indicating the interpolation method to use when calculating
and
the interpolation method to use when calculating
,
.
Three types of interpolation are possible:
- Previous point, with .
- Linear, with .
- Next point, .
Zumbach and Müller (2001) recommend that linear interpolation is used in second and subsequent iterations, i.e.,
, irrespective of the interpolation method used at the first iteration, i.e., the value of
.
Constraint:
, or , for .
- 8:
– int64int32nag_int scalar
-
The function type used to define the relationship between
and
when calculating
. Three functions are provided:
- The identity function, with .
- or
- The absolute value, with .
- or
- The absolute difference, with .
If
or
then the resulting vector of averages is scaled by
as described in
ma.
Constraint:
, , , or .
- 9:
– double scalar
-
, the power used in the transformation function.
Constraint:
.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
ma,
t. (An error is raised if these dimensions are not equal.)
, 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:
.
- 2:
– int64int32nag_int scalar
Default:
, 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
. On subsequent calls it must be the same value as returned by the last call to
nag_tsa_inhom_ma (g13mg).
Constraint:
.
- 3:
– 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.
Output Parameters
- 1:
– double array
-
The moving average:
- if or
- ,
- otherwise
- .
- 2:
– double scalar
-
If
, then
, the actual power used in the transformation function is returned, otherwise
p is unchanged.
- 3:
– int64int32nag_int scalar
Default:
, the updated number of observations processed so far.
- 4:
– double array
-
Either the moving average or exponential moving average, depending on the value of
ftype.
- if or
-
- otherwise
- .
- 5:
– double array
Communication array, used to store information between calls to nag_tsa_inhom_ma (g13mg).
- 6:
– int64int32nag_int scalar
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.
-
-
Constraint: .
- W
-
Constraint:
t should be strictly increasing.
-
-
Constraint: if linear interpolation is being used.
-
-
Constraint: .
-
-
Constraint: if
then
tau must be unchanged since previous call.
-
-
Constraint: .
-
-
Constraint: if
then
m1 must be unchanged since previous call.
-
-
Constraint: .
-
-
Constraint: if
then
m2 must be unchanged since previous call.
-
-
Constraint: if , , for .
-
-
Constraint: , or .
-
-
Constraint: , or .
-
-
Constraint: if
,
inter must be unchanged since the last call.
-
-
Constraint: , , , or .
-
-
Constraint: if
,
ftype must be unchanged since the previous call.
-
-
Constraint: absolute value of
p must be representable as an integer.
-
-
Constraint: if , . If , the nearest integer to must not be .
-
-
Constraint: if , or and for any then .
-
-
Constraint: if , , for any .
-
-
Constraint: if
then
p must be unchanged since previous call.
-
-
Constraint: .
-
-
Constraint: if
then
pn must be unchanged since previous call.
-
-
rcomm has been corrupted between calls.
-
-
Constraint: if , or .
-
-
Constraint: if , .
- W
-
Truncation occurred to avoid overflow, check for extreme values in
t,
ma or for
tau. Results are returned using the truncated values.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
Not applicable.
Further Comments
Approximately
real elements are internally allocated by
nag_tsa_inhom_ma (g13mg). If
or
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
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
is returned. This should not occur in standard usage and will only occur if extreme values of
ma,
t or
tau are supplied.
Example
The example reads in a simulated time series, and calculates the moving average. The data is supplied in three blocks of differing sizes.
Open in the MATLAB editor:
g13mg_example
function g13mg_example
fprintf('g13mg example results\n\n');
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(' Time MA\n');
for i = 1:numel(nb)
if i == 1
[ma{i}, p, pn, wma, rcomm, ifail] = ...
g13mg( ...
ma{i}, t{i}, tau, m1, m2, sinit, inter, ftype, p, 'rcomm', rcomm);
else
[ma{i}, p, pn, wma, rcomm, ifail] = ...
g13mg( ...
ma{i}, t{i}, tau, m1, m2, sinit, inter, ftype, p, ...
'pn', pn, 'rcomm', rcomm);
end
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
g13mg example results
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015