Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_tsa_multi_corrmat_cross (g13dm)

Purpose

nag_tsa_multi_corrmat_cross (g13dm) calculates the sample cross-correlation (or cross-covariance) matrices of a multivariate time series.

Syntax

[wmean, r0, r, ifail] = g13dm(matrix, k, m, w, 'n', n)
[wmean, r0, r, ifail] = nag_tsa_multi_corrmat_cross(matrix, k, m, w, 'n', n)

Description

Let ${W}_{t}={\left({w}_{1t},{w}_{2t},\dots ,{w}_{kt}\right)}^{\mathrm{T}}$, for $t=1,2,\dots ,n$, denote $n$ observations of a vector of $k$ time series. The sample cross-covariance matrix at lag $l$ is defined to be the $k$ by $k$ matrix $\stackrel{^}{C}\left(l\right)$, whose ($i,j$)th element is given by
 $C^ijl=1n∑t=l+1nwit-l-w-iwjt-w-j, l=0,1,2,…,m, ​i=1,2,…,k​ and ​j=1,2,…,k,$
where ${\stackrel{-}{w}}_{i}$ and ${\stackrel{-}{w}}_{j}$ denote the sample means for the $i$th and $j$th series respectively. The sample cross-correlation matrix at lag $l$ is defined to be the $k$ by $k$ matrix $\stackrel{^}{R}\left(l\right)$, whose $\left(i,j\right)$th element is given by
 $R^ ij l = C^ ij l C^ ii 0 C^ jj 0 , l=0,1,2,…,m , ​ i=1,2,…,k ​ and ​ j=1,2,…,k .$
The number of lags, $m$, is usually taken to be at most $n/4$.
If ${W}_{t}$ follows a vector moving average model of order $q$, then it can be shown that the theoretical cross-correlation matrices $\left(R\left(l\right)\right)$ are zero beyond lag $q$. In order to help spot a possible cut-off point, the elements of $\stackrel{^}{R}\left(l\right)$ are usually compared to their approximate standard error of 1/$\sqrt{n}$. For further details see, for example, Wei (1990).
The function uses a single pass through the data to compute the means and the cross-covariance matrix at lag zero. The cross-covariance matrices at further lags are then computed on a second pass through the data.

References

Wei W W S (1990) Time Series Analysis: Univariate and Multivariate Methods Addison–Wesley
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

Parameters

Compulsory Input Parameters

1:     $\mathrm{matrix}$ – string (length ≥ 1)
Indicates whether the cross-covariance or cross-correlation matrices are to be computed.
${\mathbf{matrix}}=\text{'V'}$
The cross-covariance matrices are computed.
${\mathbf{matrix}}=\text{'R'}$
The cross-correlation matrices are computed.
Constraint: ${\mathbf{matrix}}=\text{'V'}$ or $\text{'R'}$.
2:     $\mathrm{k}$int64int32nag_int scalar
$k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
3:     $\mathrm{m}$int64int32nag_int scalar
$m$, the number of cross-correlation (or cross-covariance) matrices to be computed. If in doubt set ${\mathbf{m}}=10$. However it should be noted that m is usually taken to be at most ${\mathbf{n}}/4$.
Constraint: $1\le {\mathbf{m}}<{\mathbf{n}}$.
4:     $\mathrm{w}\left(\mathit{kmax},{\mathbf{n}}\right)$ – double array
kmax, the first dimension of the array, must satisfy the constraint $\mathit{kmax}\ge {\mathbf{k}}$.
${\mathbf{w}}\left(\mathit{i},\mathit{t}\right)$ must contain the observation ${w}_{\mathit{i}\mathit{t}}$, for $\mathit{i}=1,2,\dots ,k$ and $\mathit{t}=1,2,\dots ,n$.

Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the second dimension of the array w.
$n$, the number of observations in the series.
Constraint: ${\mathbf{n}}\ge 2$.

Output Parameters

1:     $\mathrm{wmean}\left({\mathbf{k}}\right)$ – double array
The means, ${\stackrel{-}{w}}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
2:     $\mathrm{r0}\left(\mathit{kmax},{\mathbf{k}}\right)$ – double array
$\mathit{kmax}={\mathbf{k}}$.
If $i\ne j$, then ${\mathbf{r0}}\left(i,j\right)$ contains an estimate of the $\left(i,j\right)$th element of the cross-correlation (or cross-covariance) matrix at lag zero, ${\stackrel{^}{R}}_{ij}\left(0\right)$; if $i=j$, then if ${\mathbf{matrix}}=\text{'V'}$, ${\mathbf{r0}}\left(i,i\right)$ contains the variance of the $i$th series, ${\stackrel{^}{C}}_{ii}\left(0\right)$, and if ${\mathbf{matrix}}=\text{'R'}$, ${\mathbf{r0}}\left(i,i\right)$ contains the standard deviation of the $i$th series, $\sqrt{{\stackrel{^}{C}}_{ii}\left(0\right)}$.
If ${\mathbf{ifail}}={\mathbf{2}}$ and ${\mathbf{matrix}}=\text{'R'}$, then on exit all the elements in r0 whose computation involves the zero variance are set to zero.
3:     $\mathrm{r}\left(\mathit{kmax},\mathit{kmax},{\mathbf{m}}\right)$ – double array
$\mathit{kmax}={\mathbf{k}}$.
${\mathbf{r}}\left(\mathit{i},\mathit{j},\mathit{l}\right)$ contains an estimate of the ($\mathit{i},\mathit{j}$)th element of the cross-correlation (or cross-covariance) at lag $\mathit{l}$, ${\stackrel{^}{R}}_{\mathit{i}\mathit{j}}\left(\mathit{l}\right)$, for $\mathit{l}=1,2,\dots ,m$, $\mathit{i}=1,2,\dots ,k$ and $\mathit{j}=1,2,\dots ,k$.
If ${\mathbf{ifail}}={\mathbf{2}}$ and ${\mathbf{matrix}}=\text{'R'}$, then on exit all the elements in r whose computation involves the zero variance are set to zero.
4:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{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.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{matrix}}\ne \text{'V'}$ or $\text{'R'}$, or ${\mathbf{k}}<1$, or ${\mathbf{n}}<2$, or ${\mathbf{m}}<1$, or ${\mathbf{m}}\ge {\mathbf{n}}$, or $\mathit{kmax}<{\mathbf{k}}$.
W  ${\mathbf{ifail}}=2$
On entry, at least one of the $k$ series is such that all its elements are practically equal giving zero (or near zero) variance. In this case if ${\mathbf{matrix}}=\text{'R'}$ all the correlations in r0 and r involving this variance are set to zero.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Accuracy

For a discussion of the accuracy of the one-pass algorithm used to compute the sample cross-covariances at lag zero see West (1979). For the other lags a two-pass algorithm is used to compute the cross-covariances; the accuracy of this algorithm is also discussed in West (1979). The accuracy of the cross-correlations will depend on the accuracy of the computed cross-covariances.

The time taken is roughly proportional to $mn{k}^{2}$.

Example

This program computes the sample cross-correlation matrices of two time series of length $48$, up to lag $10$. It also prints the cross-correlation matrices together with plots of symbols indicating which elements of the correlation matrices are significant. Three * represent significance at the $0.5$% level, two * represent significance at the 1% level and a single * represents significance at the 5% level. The * are plotted above or below the line depending on whether the elements are significant in the positive or negative direction.
```function g13dm_example

fprintf('g13dm example results\n\n');

w = [-1.49, -1.62, 5.20, 6.23, 6.21, 5.86, 4.09, 3.18, 2.62, 1.49, 1.17, ...
0.85, -0.35, 0.24, 2.44, 2.58, 2.04, 0.40, 2.26, 3.34, 5.09, 5.00, ...
4.78,  4.11, 3.45, 1.65, 1.29, 4.09, 6.32, 7.50, 3.89, 1.58, 5.21, ...
5.25,  4.93, 7.38, 5.87, 5.81, 9.68, 9.07, 7.29, 7.84, 7.55, 7.32, ...
7.97,  7.76, 7.00, 8.35;
7.34,  6.35, 6.96, 8.54, 6.62, 4.97, 4.55, 4.81, 4.75, 4.76,10.88, ...
10.01, 11.62,10.36, 6.40, 6.24, 7.93, 4.04, 3.73, 5.60, 5.35, 6.81, ...
8.27,  7.68, 6.65, 6.08,10.25, 9.14,17.75,13.30, 9.63, 6.80, 4.08, ...
5.06,  4.94, 6.65, 7.94,10.76,11.89, 5.85, 9.01, 7.50,10.02,10.38, ...
8.15, 8.37, 10.73, 12.14];
[k,n] = size(w);
k     = int64(k);
matrix = 'R';
m = int64(10);
[wmean, r0, r, ifail] = g13dm( ...
matrix, k, m, w);

disp('The means');
disp(wmean');
disp('Cross-Correlation Matrices');
disp('Lag = 0');
disp(r0);
for l = 1:m
fprintf('Lag = %d\n',l);
disp(r(:,:,l));
end
sn1 = 1/sqrt(n);
fprintf('Standard error = 1/sqrt(n) = %7.4f\n\n',sn1);

disp('Tables Of Indicator Symbols');
fprintf('\nFor Lags 1 to %d\n',m);
lhs = {'              0.005  :'; '        +     0.01   :';
'              0.05   :';
'   Sig. Level        :- - - - - - - - - -  Lags';
'              0.05   :';
'        -     0.01   :'; '              0.005  :'};
c = sn1*[3.29, 2.58, 1.96, 0, -1.96, -2.58, -3.29];
for i = 1:k
for j=1:k
if i==j
fprintf('\nAuto-correlation function for series %d\n', i);
else
fprintf('\nCross-correlation function for series %d and series %d\n', ...
i, j);
end
rhs = lhs;
for t = 1:m
for u = 1:3
if r(i,j,t)>c(u)
rhs{u} = strcat(rhs{u},'*');
end
end
for u = 5:7
if r(i,j,t)<c(u)
rhs{u} = strcat(rhs{u},'*');
end
end
end
fprintf('\n');
fprintf('%s\n',rhs{1:end});
end
end

```
```g13dm example results

The means
4.3702    7.8675

Cross-Correlation Matrices
Lag = 0
2.8176    0.2493
0.2493    2.8149

Lag = 1
0.7359    0.1743
0.2114    0.5546

Lag = 2
0.4557    0.0764
0.0693    0.2605

Lag = 3
0.3792    0.0138
0.0260   -0.0381

Lag = 4
0.3224    0.1100
0.0933   -0.2359

Lag = 5
0.3411    0.2694
0.0872   -0.2501

Lag = 6
0.3631    0.3436
0.1323   -0.2265

Lag = 7
0.2800    0.4254
0.2069   -0.1285

Lag = 8
0.2480    0.5217
0.1970   -0.0846

Lag = 9
0.2398    0.2664
0.2537    0.0745

Lag = 10
0.1619   -0.0197
0.2667    0.0047

Standard error = 1/sqrt(n) =  0.1443

Tables Of Indicator Symbols

For Lags 1 to 10

Auto-correlation function for series 1

0.005  :*
+     0.01   :***
0.05   :******
Sig. Level        :- - - - - - - - - -  Lags
0.05   :
-     0.01   :
0.005  :

Cross-correlation function for series 1 and series 2

0.005  :*
+     0.01   :**
0.05   :***
Sig. Level        :- - - - - - - - - -  Lags
0.05   :
-     0.01   :
0.005  :

Cross-correlation function for series 2 and series 1

0.005  :
+     0.01   :
0.05   :
Sig. Level        :- - - - - - - - - -  Lags
0.05   :
-     0.01   :
0.005  :

Auto-correlation function for series 2

0.005  :*
+     0.01   :*
0.05   :*
Sig. Level        :- - - - - - - - - -  Lags
0.05   :
-     0.01   :
0.005  :
```