Documentation

crosscorr

Sample cross-correlation

Syntax

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

Description

example

crosscorr(y1,y2)plots thesample cross correlation(XCF) between the two univariate, stochastic time seriesy1andy2with confidence bounds.

example

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

example

crosscorr(y1,y2,numLags,numSTD)plots the XCF, wherenumSTDspecifies 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 seriesy1andy2.

example

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

example

xcf= crosscorr(y1,y2,numLags,numSTD)returns the XCF, wherenumSTDspecifies 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

collapse all

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

rng(1);% For reproducibilityx = randn(100,1);

Create a 4-period delayed version ofx.

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
bounds =0.2000 -0.2000
crosscorr(x,y)

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

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

whereis Gaussian with mean 0 and variance 1.

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

Simulate data fromMdl.

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

collapse all

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

Data Types:double

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

Data Types:double

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

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

Number of standard deviations for the sample XCF estimation error assumingy1andy2are 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

collapse all

Sample XCF between the univariate time seriesy1andy2, returned as a vector of length2*numLags + 1.

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

The software returnsxcfin the same orientation asy1.

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

Approximate confidence bounds of the XCF assumingy1andy2are uncorrelated, returned as a two-element vector.

More About

collapse all

Sample Cross Correlation

The sample cross covariance function is an estimate of the covariance between two time series,y1tandy2t, at lagsk= 0, ±1, ±2,....

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

c y 1 y 2 ( k ) = { 1 T t = 1 T k ( y 1 t y ¯ 1 ) ( y 2 , t + k y ¯ 2 ) ; k = 0 , 1 , 2 , 1 T t = 1 T + k ( y 2 t y ¯ 2 ) ( y 1 , t k y ¯ 1 ) ; k = 0 , 1 , 2 , ,

where y ¯ 1 and y ¯ 2 are the sample means of the series.

The sample standard deviations of the series are:

  • s y 1 = c y 1 y 1 ( 0 ) , where c y 1 y 1 ( 0 ) = V a r ( y 1 ) .

  • s y 2 = c y 2 y 2 ( 0 ) , where c y 2 y 2 ( 0 ) = V a r ( y 2 ) .

An estimate of the cross-correlation is

r y 1 y 2 ( k ) = c y 1 y 2 ( k ) s y 1 s y 2 ; k = 0 , ± 1 , ± 2 , .

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.

Introduced before R2006a

Was this topic helpful?