G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13DMF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G13DMF ( MATRIX, K, N, M, W, KMAX, WMEAN, R0, R, IFAIL)
 INTEGER K, N, M, KMAX, IFAIL REAL (KIND=nag_wp) W(KMAX,N), WMEAN(K), R0(KMAX,K), R(KMAX,KMAX,M) CHARACTER(1) MATRIX

## 3  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 routine 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.
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

## 5  Parameters

1:     MATRIX – CHARACTER(1)Input
On entry: 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:     K – INTEGERInput
On entry: $k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{K}}\ge 1$.
3:     N – INTEGERInput
On entry: $n$, the number of observations in the series.
Constraint: ${\mathbf{N}}\ge 2$.
4:     M – INTEGERInput
On entry: $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}}$.
5:     W(KMAX,N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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$.
6:     KMAX – INTEGERInput
On entry: the first dimension of the arrays W, R0 and R and the second dimension of the array R as declared in the (sub)program from which G13DMF is called.
Constraint: ${\mathbf{KMAX}}\ge {\mathbf{K}}$.
7:     WMEAN(K) – REAL (KIND=nag_wp) arrayOutput
On exit: the means, ${\stackrel{-}{w}}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
8:     R0(KMAX,K) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
9:     R(KMAX,KMAX,M) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\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.
10:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\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 ${\mathbf{KMAX}}<{\mathbf{K}}$.
${\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.

## 7  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}$.

## 9  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.

### 9.1  Program Text

Program Text (g13dmfe.f90)

### 9.2  Program Data

Program Data (g13dmfe.d)

### 9.3  Program Results

Program Results (g13dmfe.r)