# NAG FL Interfaceg13bcf (multi_​xcorr)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g13bcf calculates cross-correlations between two time series.

## 2Specification

Fortran Interface
 Subroutine g13bcf ( x, y, nxy, nl, s, r0, r, stat,
 Integer, Intent (In) :: nxy, nl Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(nxy), y(nxy) Real (Kind=nag_wp), Intent (Out) :: s, r0, r(nl), stat
#include <nag.h>
 void g13bcf_ (const double x[], const double y[], const Integer *nxy, const Integer *nl, double *s, double *r0, double r[], double *stat, Integer *ifail)
The routine may be called by the names g13bcf or nagf_tsa_multi_xcorr.

## 3Description

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}$:
 $rxy(l)=∑t=1 n-l(xt-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.

## 4References

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

## 5Arguments

1: $\mathbf{x}\left({\mathbf{nxy}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n$ values of the $x$ series.
2: $\mathbf{y}\left({\mathbf{nxy}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n$ values of the $y$ series.
3: $\mathbf{nxy}$Integer Input
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nxy}}\ge 2$.
4: $\mathbf{nl}$Integer Input
On entry: $L$, the maximum lag for calculating cross-correlations.
Constraint: $1\le {\mathbf{nl}}<{\mathbf{nxy}}$.
5: $\mathbf{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: $\mathbf{r0}$Real (Kind=nag_wp) Output
On exit: the cross-correlation between the $x$ and $y$ series at lag zero.
7: $\mathbf{r}\left({\mathbf{nl}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{stat}$Real (Kind=nag_wp) Output
On exit: the statistic for testing for absence of cross-correlation.
9: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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{nl}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nl}}\ge 1$.
On entry, ${\mathbf{nl}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nxy}}=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{nl}}<{\mathbf{nxy}}$.
On entry, ${\mathbf{nxy}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nxy}}>1$.
${\mathbf{ifail}}=2$
One or both of the $x$ and $y$ series have zero variance.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

All computations are believed to be stable.

## 8Parallelism and Performance

g13bcf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13bcf 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 routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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.

## 10Example

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.

### 10.1Program Text

Program Text (g13bcfe.f90)

### 10.2Program Data

Program Data (g13bcfe.d)

### 10.3Program Results

Program Results (g13bcfe.r)