%% Return Innovations and States
% Consider the model for the annual, CPI-based, Canadian inflation rate series in docid:econ_ug#mw_eb4c0d1c-dff3-434a-974b-d428961ee389.
% Create the LSTAR model for the series.

t = [2 8];
tt = threshold([2 8],Type="logistic",Rates=[3.5 1.5]);
  
mdl1 = arima(Constant=-5,Variance=0.1);
mdl2 = arima(Constant=0,Variance=0.2);
mdl3 = arima(Constant=5,Variance=0.3);
  
Mdl = tsVAR(tt,[mdl1; mdl2; mdl3]);

%
% Load the Canadian inflation and interest rate data set and extract the inflation rate series.

load Data_Canada
INF_C = DataTable.INF_C; 
numObs = length(INF_C);

% Simulate a length numObs path from the model.  Specify the threshold variable type and its data. Return the innovations and states.

[y,e,s] = simulate(Mdl,numObs,NumPaths=10,Type="exogenous",Z=INF_C);

tiledlayout(3,1)
nexttile
plot(y);
ylabel("Simulated Response")
grid on

nexttile
plot(e)
ylabel('Innovation')
grid on

nexttile
stem(s)
ylabel('State')
yticks([1 2 3])
yticklabels(Mdl.StateNames)

% Copyright 2020 The MathWorks, Inc.