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

# autocorr

Sample autocorrelation

## Syntax

• autocorr(y)
example
• autocorr(y,numLags)
example
• autocorr(y,numLags,numMA,numSTD)
example
• acf = autocorr(y)
example
• acf = autocorr(y,numLags)
example
• acf = autocorr(y,numLags,numMA,numSTD)
example
• [acf,lags,bounds] = autocorr(___)
example

## Description

example

autocorr(y) plots the sample autocorrelation function (ACF) of the univariate, stochastic time series y with confidence bounds.

example

autocorr(y,numLags) plots the ACF, where numLags indicates the number of lags in the sample ACF.

example

autocorr(y,numLags,numMA,numSTD) plots the ACF, where numMA specifies the number of lags beyond which the theoretical ACF is effectively 0, and numSTD specifies the number of standard deviations of the sample ACF estimation error.

example

acf = autocorr(y) returns the sample ACF of the univariate, stochastic time series y.

example

acf = autocorr(y,numLags) returns the ACF, where numLags specifies the number of lags in the sample ACF.

example

acf = autocorr(y,numLags,numMA,numSTD) returns the ACF, where numMA specifies the number of lags beyond which the theoretical ACF is effectively 0, and numSTD specifies the number of standard deviations of the sample ACF estimation error. 

example

[acf,lags,bounds] = autocorr(___) 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

Specify the MA(2) model:

 

where is Gaussian with mean 0 and variance 1.

rng(1); % For reproducibility Mdl = arima('MA',{-0.5 0.4},'Constant',0,'Variance',1) 
Mdl = ARIMA(0,0,2) Model: -------------------- Distribution: Name = 'Gaussian' P: 0 D: 0 Q: 2 Constant: 0 AR: {} SAR: {} MA: {-0.5 0.4} at Lags [1 2] SMA: {} Variance: 1 

Simulate 1000 observations from Mdl.

y = simulate(Mdl,1000); 

Compute the ACF.

[ACF,lags,bounds] = autocorr(y,[],2); bounds 
bounds = 0.0843 -0.0843 

[] tells the software to return the default number of lags (20). numMA = 2 indicates that the ACF is effectively 0 after the second lag. bounds displays (-0.0843, 0.0843), which are the upper and lower confidence bounds.

Plot the ACF.

autocorr(y) 

The ACF cuts off after the second lag. This behavior indicataes an MA(2) process.

Specify the mutliplicative seasonal ARMA model:

 

where is Gaussian with mean 0 and variance 1.

Mdl = arima('AR',{0.75,0.15},'SAR',{0.9,-0.5,0.5},... 'SARLags',[12,24,36],'MA',-0.5,'Constant',2,... 'Variance',1); 

Simulate data from Mdl.

rng(1); % For reproducibility y = simulate(Mdl,1000); 

Plot the default autocorrelation function (ACF).

figure autocorr(y) 

The default correlogram does not display the dependence structure for higher lags.

Plot the ACF for 40 lags.

figure autocorr(y,40,[],3) 

The correlogram shows the larger correlations at lags 12, 24, and 36.

Although various estimates of the sample autocorrelation function exist, autocorr uses the form in Box, Jenkins, and Reinsel, 1994. In their estimate, they scale the correlation at each lag by the sample variance (var(y,1)) so that the autocorrelation at lag 0 is unity. However, certain applications require rescaling the normalized ACF by another factor.

Simulate 1000 observations from the standard Gaussian distribution.

rng(1); % For reproducibility y = randn(1000, 1); 

Compute the normalized and unnormalized sample ACF.

[normalizedACF, lags] = autocorr(y, 10); unnormalizedACF = normalizedACF*var(y,1); 

Compare the first 10 lags of the sample ACF with and without normalization.

[lags normalizedACF unnormalizedACF] 
ans = 0 1.0000 0.9960 1.0000 -0.0180 -0.0180 2.0000 0.0536 0.0534 3.0000 -0.0206 -0.0205 4.0000 -0.0300 -0.0299 5.0000 -0.0086 -0.0086 6.0000 -0.0108 -0.0107 7.0000 -0.0116 -0.0116 8.0000 0.0309 0.0307 9.0000 0.0341 0.0340 10.0000 0.0076 0.0075 

## Input Arguments

collapse all

Observed univariate time series for which the software computes or plots the ACF, specified as a vector. The last element of y contains the most recent observation.

Data Types: double

Number of lags of the ACF that the software returns or plots, specified as a positive integer.

For example, autocorr(y,10) plots the ACF for lags 0 through 10.

Data Types: double

MA order that specifies the number of lags beyond which the theoretical ACF is effectively 0, specified as a nonnegative integer.

• numMA must be less than numLags.

• Specify numMA to assess whether the ACF is effectively zero beyond lag numMA [1]. The software uses Bartlett's approximation to estimate the large-lag standard error for lags that are greater than numMA.

• If numMA = 0, then the software assumes that y is a length T Gaussian white noise process. In this case, the standard error is approximately $\frac{1}{\sqrt{T}}.$

Example: [~,~,bounds] = autocorr(y,[],5)

Data Types: double

Number of standard deviations for the sample ACF estimation error assuming the theoretical ACF is 0 beyond lag numMA, specified as a positive scalar. For example, autocorr(y,[],[],1.5) plots the ACF with estimation error bounds 1.5 standard deviations away from 0.

If numMA = 0 and y a lengthT Gaussian process, then the confidence bounds are:

$±\frac{numSTD}{\sqrt{T}}.$

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

Data Types: double

## Output Arguments

collapse all

Sample ACF of the univariate time series y, returned as a vector of length numLags + 1.

The elements of acf correspond to lags 0,1,2,...,numLags. The first element, which corresponds to lag 0, is unity (i.e., acf(1) = 1).

Sample ACF lags, returned as a vector. Specifically, lags = 0:numLags.

Approximate confidence bounds of the ACF assuming y is an MA(numMA) process, returned as a two-element vector. bounds is approximate for lags > numMA.

collapse all

### Autocorrelation Function

Measures the correlation between yt and yt + k, where k = 0,...,K and yt is a stochastic process.

According to [1], the formula for the autocorrelation for lag k is

${r}_{k}=\frac{{c}_{k}}{{c}_{0}},$

where

• ${c}_{k}=\frac{1}{T-1}\sum _{t=1}^{T-k}\left({y}_{t}-\overline{y}\right)\left({y}_{t+k}-\overline{y}\right).$

• c0 is the sample variance of the time series.

The estimated standard error for the autocorrelation at lag k is

$SE\left({r}_{k}\right)=\sqrt{\frac{1}{T}\left(1+2\sum _{j=1}^{q}{r}_{j}^{2}\right)},$

where q is the lag beyond which the theoretical ACF is effectively 0. If the series is completely random, then the standard error reduces to $1/\sqrt{T}$ [1].

### Tips

To plot the ACF without confidence bounds, set numSTD to 0.

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

[2] Hamilton, J. D. Time Series Analysis. Princeton, NJ: Princeton University Press, 1994.