%% Estimate Sample ACF and PACF in MATLAB®
% This example shows how to estimate and plot the ACF and PACF of a time series by using Econometrics Toolbox™ functions docid:econ_ug#btziv_u and docid:econ_ug#btzkx8j, and docid:econ_ug#mw_8c829983-255a-4869-af30-bb53dff4e3d3.
%
%% Generate Synthetic Time Series
% Simulate an MA(2) process y_t by filtering a series of 1000 standard Gaussian deviates \varepsilon_t \;through the difference equation
% y_t= \varepsilon_{t} - \varepsilon_{t-1} + \varepsilon_{t-2}.

rng('default') % For reproducibility
e = randn(1000,1);
y = filter([1 -1 1],1,e);

%
%% Plot and Estimate ACF
% Plot the ACF of y_t by passing the simulated time series to autocorr.

autocorr(y)

% The sample autocorrelation of lags greater than 2 are insignificant.
% Compute the sample ACF by calling autocorr again. Return the first output argument.

acf = autocorr(y)

% acf(j) is the sample autocorrelation of y_t at lag j - 1.
%
%% Plot and Estimate PACF
% Plot the PACF of y_t by passing the simulated time series to parcorr.

parcorr(y)

% The sample PACF gradually decreases with increasing lag.
% Compute the sample PACF by calling parcorr again. Return the first output argument.

pacf = parcorr(y)

% pacf(j) is the sample partial autocorrelation of y_t at lag j - 1.
% The sample ACF and PACF suggest that y_t is an MA(2) process.
%
%% Use Econometric Modeler
% Open Econometric Modeler by entering econometricModeler in the MATLAB® command prompt.
% econometricModeler
% Load the simulated time series y.
% On the Econometric Modeler tab, in the Import section, select Import > Import From Workspace.
% In the Import Data dialog box, in the Import? column, select the check box for the y variable.
% Click Import.
%
% The variable y1 appears in the Data Browser, and its time series plot appears in the Time Series Plot(y1) figure window.
% Plot the ACF by clicking ACF on the Plots tab.
% Plot the PACF by clicking PACF on the Plots tab. Position the PACF plot below the ACF plot by dragging the PACF(y1) tab to the lower half of the document.
%
% Copyright 2020 The MathWorks, Inc.