%% Compute Sample ACF and PACF
% This example shows how to compute the sample autocorrelation function (ACF) and partial autocorrelation function (PACF) to qualitatively assess autocorrelation.
% The time series is 57 consecutive days of overshorts from a gasoline tank in Colorado.
%% Step 1. Load the data.
% Load the time series of overshorts.

load('Data_Overshort.mat')
Y = Data;
N = length(Y);

figure
plot(Y)
xlim([0,N])
title('Overshorts for 57 Consecutive Days')

% The series appears to be stationary.
%
%% Step 2. Plot the sample ACF and PACF.
% Plot the sample autocorrelation function (ACF) and partial autocorrelation function (PACF).

figure
subplot(2,1,1)
autocorr(Y)
subplot(2,1,2)
parcorr(Y)

% The sample ACF and PACF exhibit significant autocorrelation. The sample ACF has significant autocorrelation at lag 1. The sample PACF has significant autocorrelation at lags 1, 3, and 4.
% The distinct cutoff of the ACF combined with the more gradual decay of the PACF suggests an MA(1) model might be appropriate for this data.
%
%% Step 3. Store the sample ACF and PACF values.
% Store the sample ACF and PACF values up to lag 15.

acf = autocorr(Y,'NumLags',15);
pacf = parcorr(Y,'NumLags',15);
[length(acf) length(pacf)]

% The outputs acf and pacf are vectors storing the sample autocorrelation and partial autocorrelation at lags 0, 1,...,15 (a total of 16 lags).
% Copyright 2015 The MathWorks, Inc.