G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13BCF

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

G13BCF calculates cross-correlations between two time series.

## 2  Specification

 SUBROUTINE G13BCF ( X, Y, NXY, NL, S, R0, R, STAT, IFAIL)
 INTEGER NXY, NL, IFAIL REAL (KIND=nag_wp) X(NXY), Y(NXY), S, R0, R(NL), STAT

## 3  Description

Given two series ${x}_{1},{x}_{2},\dots ,{x}_{n}$ and ${y}_{1},{y}_{2},\dots ,{y}_{n}$ the routine 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.
Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day

## 5  Parameters

1:     X(NXY) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ values of the $x$ series.
2:     Y(NXY) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ values of the $y$ series.
3:     NXY – INTEGERInput
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{NXY}}\ge 2$.
4:     NL – INTEGERInput
On entry: $L$, the maximum lag for calculating cross-correlations.
Constraint: $1\le {\mathbf{NL}}<{\mathbf{NXY}}$.
5:     S – REAL (KIND=nag_wp)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:     R0 – REAL (KIND=nag_wp)Output
On exit: the cross-correlation between the $x$ and $y$ series at lag zero.
7:     R(NL) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{R}}\left(\mathit{l}\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:     STAT – REAL (KIND=nag_wp)Output
On exit: the statistic for testing for absence of cross-correlation.
9:     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{NXY}}\le 1$, or ${\mathbf{NL}}<1$, or ${\mathbf{NL}}\ge {\mathbf{NXY}}$.
${\mathbf{IFAIL}}=2$
One or both of the $x$ and $y$ series have zero variance and hence cross-correlations cannot be calculated.

## 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 G13BCF 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 G13BCF 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 (g13bcfe.f90)

### 9.2  Program Data

Program Data (g13bcfe.d)

### 9.3  Program Results

Program Results (g13bcfe.r)