g13 Chapter Contents
g13 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_tsa_cross_corr (g13bcc)

## 1  Purpose

nag_tsa_cross_corr (g13bcc) calculates cross-correlations between two time series.

## 2  Specification

 #include #include
 void nag_tsa_cross_corr (const double x[], const double y[], Integer nxy, Integer nl, double *s, double *r0, double r[], double *stat, NagError *fail)

## 3  Description

Given two series ${x}_{1},{x}_{2},\dots ,{x}_{n}$ and ${y}_{1},{y}_{2},\dots ,{y}_{n}$ the function calculates the cross-correlations between ${x}_{t}$ and lagged values of ${y}_{t}$:
 $rxyl=∑t=1 n-lxt-x-yt+l-y- nsxsy , l=0,1,…,L$
where
 $x-=∑t= 1nxtn$
 $sx2=∑t=1n xt-x- 2n$
and similarly for $y$.
The ratio of standard deviations ${s}_{y}/{s}_{x}$ is also returned, and a portmanteau statistic is calculated:
 $stat=n∑l=1Lrxy l 2.$
Provided $n$ is large, $L$ much less than $n$, and both ${x}_{t},{y}_{t}$ are samples of series whose true autocorrelation functions are zero, then, under the null hypothesis that the true cross-correlations between the series are zero, stat has a ${\chi }^{2}$-distribution with $L$ degrees of freedom. Values of stat in the upper tail of this distribution provide evidence against the null hypothesis.

## 4  References

Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day

## 5  Arguments

1:     x[nxy]const doubleInput
On entry: the $n$ values of the $x$ series.
2:     y[nxy]const doubleInput
On entry: the $n$ values of the $y$ series.
3:     nxyIntegerInput
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nxy}}\ge 2$.
4:     nlIntegerInput
On entry: $L$, the maximum lag for calculating cross-correlations.
Constraint: $1\le {\mathbf{nl}}<{\mathbf{nxy}}$.
5:     sdouble *Output
On exit: the ratio of the standard deviation of the $y$ series to the standard deviation of the $x$ series, ${s}_{y}/{s}_{x}$.
6:     r0double *Output
On exit: the cross-correlation between the $x$ and $y$ series at lag zero.
7:     r[nl]doubleOutput
On exit: ${\mathbf{r}}\left[\mathit{l}-1\right]$ contains the cross-correlations between the $x$ and $y$ series at lags $L$, ${r}_{xy}\left(\mathit{l}\right)$, for $\mathit{l}=1,2,\dots ,L$.
8:     statdouble *Output
On exit: the statistic for testing for absence of cross-correlation.
9:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{nl}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nl}}\ge 1$.
On entry, ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nxy}}>1$.
NE_INT_2
On entry, ${\mathbf{nl}}\ge {\mathbf{nxy}}$: ${\mathbf{nl}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nxy}}=〈\mathit{\text{value}}〉$.
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.
NE_ZERO_VARIANCE
One or both of the $x$ and $y$ series have zero variance.

## 7  Accuracy

All computations are believed to be stable.

If $n<100$, or $L<10\mathrm{log}\left(n\right)$ then the autocorrelations are calculated directly and the time taken by nag_tsa_cross_corr (g13bcc) is approximately proportional to $nL$, otherwise the autocorrelations are calculated by utilizing fast Fourier transforms (FFTs) and the time taken is approximately proportional to $n\mathrm{log}\left(n\right)$. If FFTs are used then nag_tsa_auto_corr (g13abc) internally allocates approximately $6n$ real elements.

## 9  Example

This example reads two time series of length $20$. It calculates and prints the cross-correlations up to lag $15$ for the first series leading the second series and then for the second series leading the first series.

### 9.1  Program Text

Program Text (g13bcce.c)

### 9.2  Program Data

Program Data (g13bcce.d)

### 9.3  Program Results

Program Results (g13bcce.r)