crosscorrelation¶
Computes the sample cross-correlation function of two stationary time series.
Synopsis¶
crosscorrelation (x, y, lagmax)
Required Arguments¶
- float
x[]
(Input) - Array of length
nObservations
containing the first time series. - float
y[]
(Input) - Array of length
nObservations
containing the second time series. - int
lagmax
(Input) - Maximum lag of cross-covariances and cross-correlations to be computed.
lagmax
must be greater than or equal to 1 and less thannObservations
.
Return Value¶
An array of length 2 × lagmax
+ 1 containing the cross-correlations
between the time series x
and y
. The k-th element of this array
contains the cross-correlation between x
and y
at
lag(k-lagmax
) where k = 0, 1, …, 2*lagmax
. To
release this space, use free
. If no solution can be computed, None
is returned.
Optional Arguments¶
printLevel
, int (Input)Printing option.
printLevel
Action 0 No printing is performed. 1 Prints the means and variances. 2 Prints the means, variances, and cross-covariances. 3 Prints the means, variances, cross-covariances, cross-correlations, and standard errors of cross-correlations. Default = 0.
inputMeans
,xMeanIn,
yMeanIn
(Input)- If specified,
xMeanIn
is the user input of the estimate of the mean of the time seriesx
andyMeanIn
is the user input of the estimate of the mean of the time seriesy
. outputMeans
,xMeanOut,
yMeanOut
(Output)- If specified,
xMeanOut
is the means of the time seriesx
andyMeanOut
is the mean of the time seriesy
. variances
,xVariance
,yVariance
(Output)- If specified,
xVariance
is variance of the time seriesx
andyVariance
is variance of the time seriesy
. seCcf
,standardErrors
,seOption
(Output)- An array of length 2 ×
lagmax
+ 1containing the standard errors of the cross-correlations between the time seriesx
andy
. Method of computation for standard errors of the cross-correlations is chosen byseOption
.
seOption |
Action |
---|---|
1 | Compute standard errors of cross-correlations using Bartlett’s formula. |
2 | Compute standard errors of cross-correlations using Bartlett’s formula with the assumption of no cross-correlation. |
crossCovariances
(Output)- An array of length 2 ×
lagmax
+ 1 containing the cross-covariances between the time seriesx
andy
. The k-th element of this array contains the cross-covariances betweenx
andy
at lag (k-lagmax
), where k = 0, 1, …, 2 ×lagmax
.
Description¶
Function crosscorrelation
estimates the cross-correlation function of two
jointly stationary time series given a sample of n = nObservations
observations \(\{X_t\}\) and \(\{Y_t\}\) for \(t=1,2,\ldots,n\).
Let
be the estimate of the mean \(\mu_X\) of the time series \(\{X_t\}\) where
The autocovariance function of \(\{X_t\}\), \(\sigma_X(k)\), is estimated by
where K = lagmax
. Note that
is equivalent to the sample variance xVariance
. The autocorrelation
function \(\rho_X(k)\) is estimated by
Note that
by definition. Let
be similarly defined.
The cross-covariance function \(\sigma_{XY}(k)\) is estimated by
The cross-correlation function \(\rho_{XY}(k)\) is estimated by
The standard errors of the sample cross-correlations may be optionally
computed according to argument seOption
for the optional argument
seCcf
. One method is based on a general asymptotic expression for the
variance of the sample cross-correlation coefficient of two jointly
stationary time series with independent, identically distributed normal
errors given by Bartlett (1978, page 352). The theoretical formula is
For computational purposes, the autocorrelations \(\rho_X(k)\) and \(\rho_Y(k)\) and the cross-correlations \(\rho_{XY}(k)\) are replaced by their corresponding estimates for \(|k|\leq K\), and the limits of summation are equal to zero for all k such that \(|k|>K\).
A second method evaluates Bartlett’s formula under the additional assumption that the two series have no cross-correlation. The theoretical formula is
For additional special cases of Bartlett’s formula, see Box and Jenkins (1976, page 377).
An important property of the cross-covariance coefficient is \(\sigma_{XY}(k) =\sigma_{YX}(-k)\) for \(k\geq 0\). This result is used in the computation of the standard error of the sample cross-correlation for lag \(k<0\). In general, the cross-covariance function is not symmetric about zero so both positive and negative lags are of interest.
Example¶
Consider the Gas Furnace Data (Box and Jenkins 1976, pages 532–533) where
X is the input gas rate in cubic feet/minute and Y is the percent
\(CO_2\) in the outlet gas. Function crosscorrelation
is used to
compute the cross-covariances and cross-correlations between time series X
and Y with lags from -10 through lag 10. In addition, the estimated
standard errors of the estimated cross-correlations are computed. The
standard errors are based on the additional assumption that all
cross-correlations for X and Y are zero.
from __future__ import print_function
from numpy import *
from pyimsl.stat.crosscorrelation import crosscorrelation
from pyimsl.stat.dataSets import dataSets
nobs = 296
lagmax = 10
x = empty(nobs)
y = empty(nobs)
xymean = {}
xyvar = {}
secc = {'seOption': 2}
ccv = empty(0)
data = dataSets(7)
for i in range(0, nobs):
x[i] = data[i][0]
y[i] = data[i][1]
cc = crosscorrelation(x, y, lagmax,
outputMeans=xymean,
variances=xyvar,
seCcf=secc,
crossCovariances=ccv)
print("Mean of series X = %g" % xymean['xMeanOut'])
print("Variance of series X = %g\n" % xyvar['xVariance'])
print("Mean of series Y = %g" % xymean['yMeanOut'])
print("Variance of series Y = %g\n" % xyvar['yVariance'])
print("Lag CCV CC SECC\n")
for i in range(0, 2 * lagmax + 1):
print("%-5d%13g%13g%13g" %
(i - lagmax, ccv[i], cc[i], secc['standardErrors'][i]))
Output¶
Mean of series X = -0.0568345
Variance of series X = 1.14694
Mean of series Y = 53.5091
Variance of series Y = 10.2189
Lag CCV CC SECC
-10 -0.404502 -0.118154 0.162754
-9 -0.508491 -0.148529 0.16247
-8 -0.614369 -0.179456 0.162188
-7 -0.705476 -0.206068 0.161907
-6 -0.776167 -0.226716 0.161627
-5 -0.831474 -0.242871 0.161349
-4 -0.891315 -0.260351 0.161073
-3 -0.980605 -0.286432 0.160798
-2 -1.12477 -0.328542 0.160524
-1 -1.34704 -0.393467 0.160252
0 -1.65853 -0.484451 0.159981
1 -2.04865 -0.598405 0.160252
2 -2.48217 -0.725033 0.160524
3 -2.88541 -0.84282 0.160798
4 -3.16536 -0.924592 0.161073
5 -3.25344 -0.95032 0.161349
6 -3.13113 -0.914593 0.161627
7 -2.83919 -0.82932 0.161907
8 -2.45302 -0.71652 0.162188
9 -2.05269 -0.599584 0.16247
10 -1.69465 -0.495004 0.162754