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.
The plot does not indicate significant cross-correlation between the two series.
Plot the XCF for 60 lags on either side of lag 0.
The plot shows significant correlation at lag 36, as expected.
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.
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.
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:
An estimate of the cross-correlation is
 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.