﻿ g13bc Method
g13bc calculates cross-correlations between two time series.

# Syntax

C#
```public static void g13bc(
double[] x,
double[] y,
int nxy,
int nl,
out double s,
out double r0,
double[] r,
out double stat,
out int ifail
)```
Visual Basic
```Public Shared Sub g13bc ( _
x As Double(), _
y As Double(), _
nxy As Integer, _
nl As Integer, _
<OutAttribute> ByRef s As Double, _
<OutAttribute> ByRef r0 As Double, _
r As Double(), _
<OutAttribute> ByRef stat As Double, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g13bc(
array<double>^ x,
array<double>^ y,
int nxy,
int nl,
[OutAttribute] double% s,
[OutAttribute] double% r0,
array<double>^ r,
[OutAttribute] double% stat,
[OutAttribute] int% ifail
)```
F#
```static member g13bc :
x : float[] *
y : float[] *
nxy : int *
nl : int *
s : float byref *
r0 : float byref *
r : float[] *
stat : float byref *
ifail : int byref -> unit
```

#### Parameters

x
Type: array<System..::..Double>[]()[][]
An array of size [nxy]
On entry: the $n$ values of the $x$ series.
y
Type: array<System..::..Double>[]()[][]
An array of size [nxy]
On entry: the $n$ values of the $y$ series.
nxy
Type: System..::..Int32
On entry: $n$, the length of the time series.
Constraint: ${\mathbf{nxy}}\ge 2$.
nl
Type: System..::..Int32
On entry: $L$, the maximum lag for calculating cross-correlations.
Constraint: $1\le {\mathbf{nl}}<{\mathbf{nxy}}$.
s
Type: System..::..Double%
On exit: the ratio of the standard deviation of the $y$ series to the standard deviation of the $x$ series, ${s}_{y}/{s}_{x}$.
r0
Type: System..::..Double%
On exit: the cross-correlation between the $x$ and $y$ series at lag zero.
r
Type: array<System..::..Double>[]()[][]
An array of size [nl]
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$.
stat
Type: System..::..Double%
On exit: the statistic for testing for absence of cross-correlation.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

Given two series ${x}_{1},{x}_{2},\dots ,{x}_{n}$ and ${y}_{1},{y}_{2},\dots ,{y}_{n}$ the method calculates the cross-correlations between ${x}_{t}$ and lagged values of ${y}_{t}$:
 $rxyl=∑t=1n-lxt-x-yt+l-y-nsxsy, l=0,1,…,L$
where
 $x-=∑t=1nxtn$
 $sx2=∑t=1nxt-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=1Lrxyl2.$
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.

# References

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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\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.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

All computations are believed to be stable.

# Parallelism and Performance

None.

If $n<100$, or $L<10\mathrm{log}\left(n\right)$ then the autocorrelations are calculated directly and the time taken by g13bc 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 g13bc internally allocates approximately $6n$ real elements.

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

Example program (C#): g13bce.cs

Example program data: g13bce.d

Example program results: g13bce.r