Quantcast

Documentation Center

  • Trial Software
  • Product Updates

crosscorr

Sample cross-correlation

Syntax

  • xcf = crosscorr(y1,y2) example
  • xcf = crosscorr(y1,y2,numLags) example
  • xcf = crosscorr(y1,y2,numLags,numSTD) example
  • [xcf,lags,bounds] = crosscorr(___) example

Description

example

crosscorr(y1,y2) plots the sample cross correlation (XCF) between the two univariate, stochastic time series y1 and y2 with confidence bounds.

example

crosscorr(y1,y2,numLags) plots the XCF, where numLags indicates the number of lags in the sample XCF.

example

crosscorr(y1,y2,numLags,numSTD) plots the XCF, where numSTD specifies the number of standard deviations of the sample XCF estimation error.

example

xcf = crosscorr(y1,y2) returns the sample cross-correlation function (XCF) between the two univariate, stochastic time series y1 and y2.

example

xcf = crosscorr(y1,y2,numLags) returns the XCF, where numLags specifies the number of lags in the sample XCF.

example

xcf = crosscorr(y1,y2,numLags,numSTD) returns the XCF, where numSTD specifies the number of standard deviations of the sample ACF estimation error.

example

[xcf,lags,bounds] = crosscorr(___) additionally returns the lags (lags) corresponding to the ACF and the approximate upper and lower confidence bounds (bounds), using any of the input arguments in the previous syntaxes.

Examples

expand all

Plot the Cross Covariance of Two Time Series

Generate 100 random deviates from a Gaussian distribution with mean 0 and variance 1.

rng(1); % For reproducibility
x = randn(100,1);

Create a 4-period delayed version of x.

y = lagmatrix(x,4);

Compute and plot the XCF.

y(isnan(y)) = 0; % crosscorr does not accept NaNs
[XCF,lags,bounds] = crosscorr(x,y);
bounds

crosscorr(x,y)
bounds =

    0.2000
   -0.2000

bounds displays the upper and lower confidence bounds, which are the horizontal lines in the XCF plot. As you should expect, XCF peaks at lag 4.

Specify More Lags for the Cross-Correlation Plot

Specify the AR(1) model for the first series:

$$y_{1t} = 2 + 0.3y_{1t-1}+\varepsilon_t,$$

where $\varepsilon_t$ is Gaussian with mean 0 and variance 1.

MdlY1 = arima('AR',0.3,'Constant',2,'Variance',1);

Simulate data from Mdl.

rng(1);
T = 1000;
y1 = simulate(MdlY1,T);

Simulate data for the second series by inducing correlation at lag 36.

y2 = [randn(36,1);y1(1:end-36)+randn(T-36,1)*0.1];

Plot the XCF using the default settings.

figure
crosscorr(y1,y2,[],3)

The plot does not indicate significant cross-correlation between the two series.

Plot the XCF for 60 lags on either side of lag 0.

figure
crosscorr(y1,y2,60,3)

The plot shows significant correlation at lag 36, as expected.

Input Arguments

expand all

y1 — First observed univariate time seriesvector

First observed univariate time series for which the software computes or plots the XCF, specified as a vector. The last element of y1 contains the most recent observation.

Data Types: double

y2 — Second observed univariate time seriesvector

Second observed univariate time series for which the software computes or plots the XCF, specified as a vector. The last element of y2 contains the most recent observation.

Data Types: double

numLags — Number of lagsmin(20,min(length(y1),length(y2))-1) (default) | positive integer

Number of lags of the XCF that the software returns or plots, specified as a positive integer. crosscorr returns the XCF at lags 0, ±1, ±2,... ±numLags.

For example, crosscorr(y1,y2,10) plots the XCF for lags 0, ±1, ±2,...,±10.

numSTD — Number of standard deviations2 (default) | positive scalar

Number of standard deviations for the sample XCF estimation error assuming y1 and y2 are uncorrelated. For example, crosscorr(y1,y2,[],1.5) plots the XCF with estimation error bounds 1.5 standard deviations away from 0.

The default (numSTD = 2) corresponds to approximate 95% confidence bounds.

Output Arguments

expand all

xcf — Sample XCFvector

Sample XCF between the univariate time series y1 and y2, returned as a vector of length 2*numLags + 1.

The elements of xcf correspond to lags 0, ±1, ±2,... ±numLags, with the center element containing the XCF for lag 0.

The software returns xcf in the same orientation as y1.

lags — Sample XCF lagsvector

Sample XCF lags, returned as a vector. Specifically, lags = -numLags:numLags.

bounds — Approximate confidence boundsvector

Approximate confidence bounds of the XCF assuming y1 and y2 are uncorrelated, returned as a two-element vector.

More About

expand all

Sample Cross Correlation

The sample cross covariance function is an estimate of the covariance between two time series, y1t and y2t, at lags k = 0, ±1, ±2,....

For data pairs (y11,y21), (y12,y22),...,(y1T,y2T), an estimate of the lag k cross-covariance is

where and are the sample means of the series.

The sample standard deviations of the series are:

  • where

  • where

An estimate of the cross-correlation is

References

[1] Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. Time Series Analysis: Forecasting and Control. 3rd ed. Englewood Cliffs, NJ: Prentice Hall, 1994.

See Also

| |

Was this topic helpful?