# NAG C Library Function Document

## 1Purpose

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

## 2Specification

 #include #include
 void nag_tsa_multi_cross_corr (Nag_CovOrCorr matrix, Integer k, Integer n, Integer m, const double w[], double wmean[], double r0[], double r[], NagError *fail)

## 3Description

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

## 5Arguments

1:    $\mathbf{matrix}$Nag_CovOrCorrInput
On entry: indicates whether the cross-covariance or cross-correlation matrices are to be computed.
${\mathbf{matrix}}=\mathrm{Nag_AutoCov}$
The cross-covariance matrices are computed.
${\mathbf{matrix}}=\mathrm{Nag_AutoCorr}$
The cross-correlation matrices are computed.
Constraint: ${\mathbf{matrix}}=\mathrm{Nag_AutoCov}$ or $\mathrm{Nag_AutoCorr}$.
2:    $\mathbf{k}$IntegerInput
On entry: $k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
3:    $\mathbf{n}$IntegerInput
On entry: $n$, the number of observations in the series.
Constraint: ${\mathbf{n}}\ge 2$.
4:    $\mathbf{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:    $\mathbf{w}\left[{\mathbf{k}}×{\mathbf{n}}\right]$const doubleInput
On entry: ${\mathbf{w}}\left[\left(\mathit{t}-1\right)k+\mathit{i}-1\right]$ must contain the value for series $\mathit{i}$ at time $\mathit{t}$, for $\mathit{i}=1,2,\dots ,k$ and $\mathit{t}=1,2,\dots ,n$.
6:    $\mathbf{wmean}\left[{\mathbf{k}}\right]$doubleOutput
On exit: the means, ${\stackrel{-}{w}}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
7:    $\mathbf{r0}\left[{\mathbf{k}}×{\mathbf{k}}\right]$doubleOutput
On exit: if ${\mathbf{matrix}}=\mathrm{Nag_AutoCov}$, ${\mathbf{r0}}\left[\left(j-1\right)k+i-1\right]$ contains the $\left(i,j\right)$th element of the sample cross-covariance matrix.
If ${\mathbf{matrix}}=\mathrm{Nag_AutoCorr}$, ${\mathbf{r0}}\left[\left(j-1\right)k+i-1\right]$, $i\ne j$ contains the $\left(i,j\right)$th element of the sample cross-correlation matrix and ${\mathbf{r0}}\left[\left(i-1\right)k+i-1\right]$ contains the standard deviation of the $i$th series.
8:    $\mathbf{r}\left[{\mathbf{k}}×{\mathbf{k}}×{\mathbf{m}}\right]$doubleOutput
On exit: if ${\mathbf{matrix}}=\mathrm{Nag_AutoCov}$, ${\mathbf{r}}\left[\left(l-1\right){k}^{2}+\left(j-1\right)k+i-1\right]$ contains the $\left(i,j\right)$th element of the sample cross-covariance matrix at lag $l$.
If ${\mathbf{matrix}}=\mathrm{Nag_AutoCorr}$, then it contains the $\left(\mathit{i},\mathit{j}\right)$th element of the sample cross-correlation matrix lag $\mathit{l}$, for $\mathit{l}=1,2,\dots ,m$, $\mathit{i}=1,2,\dots ,k$ and $\mathit{j}=1,2,\dots ,k$.
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_INT_2
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$ and ${\mathbf{m}}<{\mathbf{n}}$.
NE_INTERNAL_ERROR
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.
NE_NO_LICENCE
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.
NE_ZERO_VARIANCE
On entry, at least one of the series is such that all its elements are practically identical giving zero (or near zero) variance. In this case if ${\mathbf{matrix}}=\mathrm{Nag_AutoCorr}$ all the correlations in r0 and r involving this variance are set to zero.

## 7Accuracy

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.

## 8Parallelism and Performance

nag_tsa_multi_cross_corr (g13dmc) 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.

## 9Further Comments

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

## 10Example

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.

### 10.1Program Text

Program Text (g13dmce.c)

### 10.2Program Data

Program Data (g13dmce.d)

### 10.3Program Results

Program Results (g13dmce.r)