PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_tsa_multi_xcorr (g13bc)
Purpose
nag_tsa_multi_xcorr (g13bc) calculates cross-correlations between two time series.
Syntax
Description
Given two series
and
the function calculates the cross-correlations between
and lagged values of
:
where
and similarly for
.
The ratio of standard deviations
is also returned, and a portmanteau statistic is calculated:
Provided
is large,
much less than
, and both
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
-distribution with
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:
– double array
-
The values of the series.
- 2:
– double array
-
The values of the series.
- 3:
– int64int32nag_int scalar
-
, the maximum lag for calculating cross-correlations.
Constraint:
.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
x,
y. (An error is raised if these dimensions are not equal.)
, the length of the time series.
Constraint:
.
Output Parameters
- 1:
– double scalar
-
The ratio of the standard deviation of the series to the standard deviation of the series, .
- 2:
– double scalar
-
The cross-correlation between the and series at lag zero.
- 3:
– double array
-
contains the cross-correlations between the and series at lags , , for .
- 4:
– double scalar
-
The statistic for testing for absence of cross-correlation.
- 5:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | , |
or | . |
-
-
One or both of the and series have zero variance and hence cross-correlations cannot be calculated.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
All computations are believed to be stable.
Further Comments
If , or then the autocorrelations are calculated directly and the time taken by nag_tsa_multi_xcorr (g13bc) is approximately proportional to , otherwise the autocorrelations are calculated by utilizing fast Fourier transforms (FFTs) and the time taken is approximately proportional to . If FFTs are used then nag_tsa_multi_xcorr (g13bc) internally allocates approximately real elements.
Example
This example reads two time series of length . It calculates and prints the cross-correlations up to lag for the first series leading the second series and then for the second series leading the first series.
Open in the MATLAB editor:
g13bc_example
function g13bc_example
fprintf('g13bc example results\n\n');
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.10; 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.70; 3.31; 3.81; 3.33; 2.96; 3.28; 3.10];
nl = int64(15);
[sxy, r0xy, rxy, statxy, ifail] = g13bc( ...
x, y, nl);
[syx, r0yx, ryx, statyx, ifail] = g13bc( ...
y, x, nl);
fprintf('%34s%15s\n', 'Between', 'Between');
fprintf('%34s%15s\n', 'x and y', 'y and x');
fmt1 = '\n%24s%10.4f%15.4f\n';
fprintf(fmt1, 'Standard deviation ratio', sxy, syx);
fprintf('\nCross correlation at lag');
fprintf(fmt1, '0', r0xy, r0yx);
ivar = double([1:nl]');
fprintf('%24d%10.4f%15.4f\n', [ivar rxy ryx]')
fprintf(fmt1,'Test statistic ', statxy, statyx);
g13bc example results
Between Between
x and y y and x
Standard deviation ratio 2.0053 0.4987
Cross correlation at lag
0 0.0568 0.0568
1 0.0438 -0.0151
2 -0.3762 0.3955
3 -0.4864 0.3417
4 -0.6294 0.5486
5 -0.3871 0.2291
6 -0.1690 0.3190
7 -0.0678 0.1980
8 0.0962 0.0438
9 0.0788 -0.1428
10 0.2910 -0.1376
11 0.0950 -0.0387
12 0.0547 -0.0380
13 0.1855 -0.1551
14 0.0243 -0.1536
15 0.0034 -0.0696
Test statistic 22.1269 17.2917
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015