Code covered by the BSD License  

Highlights from
Autocorrelation Function (ACF)

4.83333

4.8 | 6 ratings Rate this file 165 Downloads (last 30 days) File Size: 2.01 KB File ID: #30540
image thumbnail

Autocorrelation Function (ACF)

by Calvin Price

 

25 Feb 2011

Computes ACF for a given series and plots correlogram.

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
03 Aug 2011 Ana  
11 Aug 2011 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.

16 Nov 2011 MiguelMauricio

Very useful, clear and easy to follow. Thank you

21 Nov 2011 Cristhiano Moreno

Thank you.

How do you calculate the Bartlett bands ?

06 Dec 2011 Martin Dimkovski  
08 May 2012 Dmitry

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

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
statistics Calvin Price 28 Feb 2011 13:36:56
statistics Jonas 11 Aug 2011 11:12:49
statistics Bram 23 Nov 2011 06:27:32
statistics Ximing 05 Mar 2012 19:37:34

Contact us at files@mathworks.com