%% Plot IRF Confidence Bounds
% Plot the measurement variuable IRF and the 95% confidence intervals on the true IRFs.
% Assume that the data generating process (DGP) is the AR(1) model
% x_t = 1 + 0.75x_{t-2} + u_t,
% where u_t is a series of independent and identically distributed Gaussian variables with mean 0 and variance 1.
% Simulate 500 observations from the model.

rng(1); % For reproducibility
DGP = arima('Constant',1,'AR',{0 0.75},'Variance',1);
y = simulate(DGP,500);

% Explicitly create a diffuse state-space model template for estimation that represents the model.  Fit the model to the data, and return parameter estimates and their corresponding estimated covariance matrix.

A = [0 NaN NaN; 0 1 0; 1 0 0];
B = [NaN; 0; 0];
C = [1 0 0];
D = 0;
Mdl = dssm(A,B,C,D,'StateType',[0 1 0]);
[~,estParams,EstParamCov] = estimate(Mdl,y,abs(randn(3,1)));

% Mdl is a fully specified, estimated dssm model object.
% Plot the IRF, with its 95% confidence intervals, of the measurement variable.

irfplot(Mdl,'Params',estParams,'EstParamCov',EstParamCov,...
    'PlotX',[]);

% The blue line represents the estimated IRF of y_t. The dashed red lines represent the upper and lower, pointwise 95% confidence bounds on the true IRF. The model has only one lag term (lag 2), as the shock filters through the system, it impacts the first state variable during odd periods only.
% Copyright 2020 The MathWorks, Inc.