# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# crosscorr

Sample cross-correlation

## Syntax

• crosscorr(y1,y2)
example
• crosscorr(y1,y2,numLags)
example
• crosscorr(y1,y2,numLags,numSTD)
example
• 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

collapse all

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 the AR(1) model for the first series:

 

where 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

collapse all

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

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

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.

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

collapse all

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.

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

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

collapse 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

${c}_{{y}_{1}{y}_{2}}\left(k\right)=\left\{\begin{array}{c}\frac{1}{T}\sum _{t=1}^{T-k}\left({y}_{1t}-{\overline{y}}_{1}\right)\left({y}_{2,t+k}-{\overline{y}}_{2}\right);\text{\hspace{0.17em}}k=0,1,2,\dots \\ \frac{1}{T}\sum _{t=1}^{T+k}\left({y}_{2t}-{\overline{y}}_{2}\right)\left({y}_{1,t-k}-{\overline{y}}_{1}\right);\text{\hspace{0.17em}}k=0,-1,-2,\dots \end{array},$

where ${\overline{y}}_{1}$ and ${\overline{y}}_{2}$ are the sample means of the series.

The sample standard deviations of the series are:

• ${s}_{{y}_{1}}=\sqrt{{c}_{{y}_{1}{y}_{1}}\left(0\right)},$ where ${c}_{{y}_{1}{y}_{1}}\left(0\right)=Var\left({y}_{1}\right).$

• ${s}_{{y}_{2}}=\sqrt{{c}_{{y}_{2}{y}_{2}}\left(0\right)},$ where ${c}_{{y}_{2}{y}_{2}}\left(0\right)=Var\left({y}_{2}\right).$

An estimate of the cross-correlation is

${r}_{{y}_{1}{y}_{2}}\left(k\right)=\frac{{c}_{{y}_{1}{y}_{2}}\left(k\right)}{{s}_{{y}_{1}}{s}_{{y}_{2}}};\text{\hspace{0.17em}}k=0,±1,±2,\dots \text{.}$

## 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.