File Exchange

image thumbnail

Autocorrelation Function (ACF)

version 1.0 (2.01 KB) by

Computes ACF for a given series and plots correlogram.

4.76471
17 Ratings

130 Downloads

Updated

View License

Computes ACF for a given series. Returns a vector of autocorrelations through lag p. Also produces bar graph of autocorrelations, with rejection region bands for testing (under white noise assumption) each autocorrelation = 0.

Example: >> myacf = acf(y,12)

Does not require any toolboxes.

Comments and Ratings (20)

Hi Calvin, I want to apply this function on a time series gained by the number of entities over time (Simevents Simulation), but the version of matlab i am using tells this function does not exist!!! I use R2015b.
How is it possible while the release is marked for R2009a?!
Can you please give me a solution?

Alex

Alex (view profile)

Nice code! Thank you. Have you used this code for prediction of the next number in a series?

Thank u, very useful.

chen tai yu

good

Andres Kiani

Robin Szeto

Simple and nice!

Nakul Bansal

brilliant. Just one thing ... if you could make the bars thinner

harry

harry (view profile)

thanks for your share, very useful.

Alan

Alan (view profile)

Thanks, this saved me a lot of time.

Fatima

Fatima (view profile)

The code solved my problem after got confused with several other codes although I m so biginner and I had to be paitint to get it.
Thank you

Martin

Martin (view profile)

Dmitry

Dmitry (view profile)

Hey Calvin, ACFs produced by your code are biased towards zero...

The reason for that is that the first k elements in cross_sum (variable of the sub-function) are always zero. Also, dimensions of cross_sum after the loop in lines 104-106 are always Nx1. In large sample the bias is small but in small samples it might be sensible.

Given that matlab is very bad at handling loops it is better to avoid them altogether if possible. I adjusted your code by removing the sub-function completely, "global" attributes for N and ybar (lines 46 and 48) and substituting loop in lines 52-54 by

for i = 1:p
cross_sum=(y(i+1:N)-ybar)'*(y(1:N-i)-ybar);
yvar = (y-ybar)'*(y-ybar) ;
ta(i) = (cross_sum / yvar)*(N/(N-i)) ;
end

Hope that helps everyone

Thank you.

How do you calculate the Bartlett bands ?

Very useful, clear and easy to follow. Thank you

Steven White

It did what I wanted it to do!

It might be nice to include a more meaningful example, rather than just an ACF of some random data.

Ana

Ana (view profile)

MATLAB Release
MATLAB 7.8 (R2009a)
Tags Add Tags
Acknowledgements

Inspired: Estimate AutoCorrelation Function (ACF)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video