Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_tsa_multi_xcorr (g13bc)

## Purpose

nag_tsa_multi_xcorr (g13bc) calculates cross-correlations between two time series.

## Syntax

[s, r0, r, stat, ifail] = g13bc(x, y, nl, 'nxy', nxy)
[s, r0, r, stat, ifail] = nag_tsa_multi_xcorr(x, y, nl, 'nxy', nxy)

## Description

Given two series x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$ and y1,y2,,yn${y}_{1},{y}_{2},\dots ,{y}_{n}$ the function calculates the cross-correlations between xt${x}_{t}$ and lagged values of yt${y}_{t}$:
 rxy(l) = ( ∑ t = 1n − l(xt − x)(yt + l − y))/(nsxsy),  l = 0,1, … ,L $rxy(l)=∑t=1 n-l(xt-x-)(yt+l-y-) nsxsy , l=0,1,…,L$
where
 x = ( ∑ t = 1nxt)/n $x-=∑t= 1nxtn$
 sx2 = ( ∑ t = 1n(xt − x)2)/n $sx2=∑t=1n (xt-x-) 2n$
and similarly for y$y$.
The ratio of standard deviations sy / sx${s}_{y}/{s}_{x}$ is also returned, and a portmanteau statistic is calculated:
 L stat = n ∑ rxy(l)2. l = 1
$stat=n∑l=1Lrxy (l) 2.$
Provided n$n$ is large, L$L$ much less than n$n$, and both xt,yt${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 χ2${\chi }^{2}$-distribution with L$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

## Parameters

### Compulsory Input Parameters

1:     x(nxy) – double array
nxy, the dimension of the array, must satisfy the constraint nxy2${\mathbf{nxy}}\ge 2$.
The n$n$ values of the x$x$ series.
2:     y(nxy) – double array
nxy, the dimension of the array, must satisfy the constraint nxy2${\mathbf{nxy}}\ge 2$.
The n$n$ values of the y$y$ series.
3:     nl – int64int32nag_int scalar
L$L$, the maximum lag for calculating cross-correlations.
Constraint: 1nl < nxy$1\le {\mathbf{nl}}<{\mathbf{nxy}}$.

### Optional Input Parameters

1:     nxy – int64int32nag_int scalar
Default: The dimension of the arrays x, y. (An error is raised if these dimensions are not equal.)
n$n$, the length of the time series.
Constraint: nxy2${\mathbf{nxy}}\ge 2$.

None.

### Output Parameters

1:     s – double scalar
The ratio of the standard deviation of the y$y$ series to the standard deviation of the x$x$ series, sy / sx${s}_{y}/{s}_{x}$.
2:     r0 – double scalar
The cross-correlation between the x$x$ and y$y$ series at lag zero.
3:     r(nl) – double array
r(l)${\mathbf{r}}\left(\mathit{l}\right)$ contains the cross-correlations between the x$x$ and y$y$ series at lags L$L$, rxy(l)${r}_{xy}\left(\mathit{l}\right)$, for l = 1,2,,L$\mathit{l}=1,2,\dots ,L$.
4:     stat – double scalar
The statistic for testing for absence of cross-correlation.
5:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, nxy ≤ 1${\mathbf{nxy}}\le 1$, or nl < 1${\mathbf{nl}}<1$, or ${\mathbf{nl}}\ge {\mathbf{nxy}}$.
ifail = 2${\mathbf{ifail}}=2$
One or both of the x$x$ and y$y$ series have zero variance and hence cross-correlations cannot be calculated.

## Accuracy

All computations are believed to be stable.

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

## Example

```function nag_tsa_multi_xcorr_example
x = [0.02;
0.05;
0.08;
0.03;
-0.05;
0.11;
-0.01;
-0.08;
-0.08;
-0.11;
-0.18;
-0.19;
-0.09;
0.03;
0.1;
0.15;
-0.14;
0.07;
0.09;
0.16];
y = [3.18;
3.21;
3.26;
3.25;
3.08;
3.01;
3.06;
3.17;
3.12;
3.04;
3.26;
3.45;
3.33;
3.7;
3.31;
3.81;
3.33;
2.96;
3.28;
3.1];
nl = int64(15);
[s, r0, r, stat, ifail] = nag_tsa_multi_xcorr(x, y, nl)
```
```

s =

2.0053

r0 =

0.0568

r =

0.0438
-0.3762
-0.4864
-0.6294
-0.3871
-0.1690
-0.0678
0.0962
0.0788
0.2910
0.0950
0.0547
0.1855
0.0243
0.0034

stat =

22.1269

ifail =

0

```
```function g13bc_example
x = [0.02;
0.05;
0.08;
0.03;
-0.05;
0.11;
-0.01;
-0.08;
-0.08;
-0.11;
-0.18;
-0.19;
-0.09;
0.03;
0.1;
0.15;
-0.14;
0.07;
0.09;
0.16];
y = [3.18;
3.21;
3.26;
3.25;
3.08;
3.01;
3.06;
3.17;
3.12;
3.04;
3.26;
3.45;
3.33;
3.7;
3.31;
3.81;
3.33;
2.96;
3.28;
3.1];
nl = int64(15);
[s, r0, r, stat, ifail] = g13bc(x, y, nl)
```
```

s =

2.0053

r0 =

0.0568

r =

0.0438
-0.3762
-0.4864
-0.6294
-0.3871
-0.1690
-0.0678
0.0962
0.0788
0.2910
0.0950
0.0547
0.1855
0.0243
0.0034

stat =

22.1269

ifail =

0

```