%% Access Expected Smoothed State Probabilities and Loglikelihood
% Consider the model and data in docid:econ_ug.mw_4a734b08-4bfa-4749-9816-7dc6832c6c20.
% Create the partially specified model for estimation.

P = NaN(2);
mc = dtmc(P,'StateNames',["Expansion" "Recession"]);
mdl = arima(0,0,0);
Mdl = msVAR(mc,[mdl; mdl]);

% Create the fully specified model containing initial parameter values for the estimation procedure.

P0 = 0.5*ones(2);
mc0 = dtmc(P0,'StateNames',Mdl.StateNames);
mdl01 = arima('Constant',1,'Variance',1);
mdl02 = arima('Constant',-1,'Variance',1);
Mdl0 = msVAR(mc0,[mdl01; mdl02]);

% Load and preprocess the data.

load Data_GDP
qrate = diff(Data(2:230))./Data(2:229); 
arate = 100*((1 + qrate).^4 - 1);       

% Fit the model to the data. Return the expected smoothed state probabilities and loglikelihood when the algorithm terminates.

[EstMdl,SS,logL] = estimate(Mdl,Mdl0,arate);

% SS is a 228-by-2 matrix of expected smoothed state probabilities; rows correspond to periods in the estimation sample, and columns correspond to the regimes. logL is the final loglikelihood.
% Display the expected smoothed state probabilities for the last period in the estimation sample, and display the final loglikelihood.

SS(end,:)
logL

% Copyright 2019 The MathWorks, Inc.
%