%% Estimate VARX Model
% Consider the 2-D VARX(1) model for the US real GDP (RGDP) and investment (GCE) rates that treats the personal consumption (PCEC) rate  as exogenous:
% \left[ {\begin{array}{*{20}{l}}
% {\texttt{RGDP}_t}\\
% {\texttt{GCE}_t}
% \end{array}} \right] = c + \Phi\left[ {\begin{array}{*{20}{l}}
% {\texttt{RGDP}_{t-1}\\
% {\texttt{GCE}_{t-1}
% \end{array}} \right] + \texttt{PCEC}_t\beta+\varepsilon_t.
% For all t, \varepsilon_t is a series of independent 2-D normal innovations with a mean of 0 and covariance \Sigma. Assume the following prior distributions:
% {\left\lbrack \begin{array}{ccc}
% \Phi  & c & \beta
% \end{array}\right\rbrack }^{\prime } \left|\Sigma \sim N_{4\times 2} \left(M,V,\Sigma \right)\right., where M is a 4-by-2 matrix of means and V is the 4-by-4 among-coefficient scale matrix. Equivalently, \textrm{vec}\left({\left\lbrack \begin{array}{ccc}
% \Phi  & c & \beta
% \end{array}\right\rbrack }^{\prime } \right)\left|\Sigma \sim N_8 \left(\textrm{vec}\left(M\right),\Sigma \otimes \;V\right)\right..
% \Sigma \sim \textrm{Inverse}\;\textrm{Wishart}\left(\Omega ,\nu \right), where Ω is the 2-by-2 scale matrix and \nu is the degrees of freedom.
% Load the US macroeconomic data set.  Compute the real GDP, investment, and personal consumption rate series.  Remove all missing values from the resulting series.

load Data_USEconModel
DataTable.RGDP = DataTable.GDP./DataTable.GDPDEF;
seriesnames = ["PCEC"; "RGDP"; "GCE"];
rates = varfun(@price2ret,DataTable,'InputVariables',seriesnames);
rates = rmmissing(rates);
rates.Properties.VariableNames = seriesnames;

% Create a conjugate prior model for the 2-D VARX(1) model parameters.

numseries = 2;
numlags = 1;
numpredictors = 1;
PriorMdl = conjugatebvarm(numseries,numlags,'NumPredictors',numpredictors,...
    'SeriesNames',seriesnames(2:end));

% Estimate the posterior distribution.  Specify the exogenous predictor data.

PosteriorMdl = estimate(PriorMdl,rates{:,2:end},...
    'X',rates{:,1},'Display','equation');

% By default, estimate uses the first p = 1 observations in the specified response data as a presample, and it removes the corresponding observations in the predictor data from the sample.
% The posterior means (and standard deviations) of the regression coefficients appear below the X1 column of the estimation summary table.
% Copyright 2020 The MathWorks, Inc.
%