%% Plot Cumulative IRFs of Estimated Model to Specified Axes
% Simulate data from a known model, fit the data to a state-space model, and then the plot cumulative IRFs of the estimated model to specified axes.
% 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 state-space model template for estimation that represents the model
% \begin{array}{l}
% x_t=c+\phi x_{t-2}+\eta u_t\\
% y_t= x_{t}.
% \end{array}

A = [0 NaN NaN; 0 1 0; 1 0 0];
B = [NaN; 0; 0];
C = [1 0 0];
D = 0;
Mdl = ssm(A,B,C,D,'StateType',[0 1 0]);

% Fit the model template to the data. Specify a set of positive, random standard  Gaussian starting values for the three model parameters.

EstMdl = estimate(Mdl,y,abs(randn(3,1)));

% EstMdl is a fully specified dssm model object.
% Plot the cumulative IRFs of the first and third state variables, and the measurement variable in EstMdl. Return the plot in the same figure, on three separate subplots.

ax = gobjects(3,1);
for j = 1:numel(ax)
    ax(j) = subplot(3,1,j);
end
irfplot(ax,EstMdl,'Cumulative',true,'PlotX',[1 3]);

% Because y_t =x_t, the top two IRFs in the figure are equivalent.  Because x_{1,t-1} =x_{3,t}, the IRF in the subplot at the bottom of the figure is shifted to the left, relative to the other two plots.
% Copyright 2020 The MathWorks, Inc.