NAG Library Routine Document

g13bcf (multi_xcorr)


    1  Purpose
    7  Accuracy


g13bcf calculates cross-correlations between two time series.


Fortran Interface
Subroutine g13bcf ( x, y, nxy, nl, s, r0, r, stat, ifail)
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
C Header Interface
#include nagmk26.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)


Given two series x1,x2,,xn and y1,y2,,yn the routine calculates the cross-correlations between xt and lagged values of yt:
rxyl=t=1 n-lxt-x-yt+l-y- nsxsy ,  l=0,1,,L  
x-=t= 1nxtn  
sx2=t=1n xt-x- 2n  
and similarly for y.
The ratio of standard deviations sy/sx is also returned, and a portmanteau statistic is calculated:
stat=nl=1Lrxy l 2.  
Provided n is large, L much less than n, and both xt,yt 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 χ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


1:     xnxy – Real (Kind=nag_wp) arrayInput
On entry: the n values of the x series.
2:     ynxy – 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: nxy2.
4:     nl – IntegerInput
On entry: L, the maximum lag for calculating cross-correlations.
Constraint: 1nl<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, sy/sx.
6:     r0 – Real (Kind=nag_wp)Output
On exit: the cross-correlation between the x and y series at lag zero.
7:     rnl – Real (Kind=nag_wp) arrayOutput
On exit: rl contains the cross-correlations between the x and y series at lags L, rxyl, for l=1,2,,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​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry 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:
On entry,nxy1,
One or both of the x and y series have zero variance and hence cross-correlations cannot be calculated.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


All computations are believed to be stable.

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

Further Comments

If n<100, or L<10logn 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 nlogn. If FFTs are used then g13bcf internally allocates approximately 6n real elements.


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.

Program Text

Program Text (g13bcfe.f90)

Program Data

Program Data (g13bcfe.d)

Program Results

Program Results (g13bcfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017