%% Create Empirical Model
% Consider the 3-D VAR(4) model for the US inflation (INFL), unemployment (UNRATE), and federal funds (FEDFUNDS) rates.
% \left[ {\begin{array}{*{20}{l}}
% {\texttt{INFL}_t}\\
% {\texttt{UNRATE}_t}\\
% {\texttt{FEDFUNDS}_t}
% \end{array}} \right] = c + \sum\limits_{j = 1}^4 {{\Phi _j}\left[ {\begin{array}{*{20}{l}}
% {\texttt{INFL}_{t-j}\\
% {\texttt{UNRATE}_{t-j}\\
% {\texttt{FEDFUNDS}_{t-j}
% \end{array}} \right]} + \left[ {\begin{array}{*{20}{c}}
% {{\varepsilon _{1,t}}}\\
% {{\varepsilon _{2,t}}}\\
% {{\varepsilon _{3,t}}}
% \end{array}} \right].
% For all t, \varepsilon_t is a series of independent 3-D normal innovations with a mean of 0 and covariance \Sigma.
% You can create an empirical Bayesian VAR model for the coefficients {\left\lbrack \Phi_1 ,\ldotp \ldotp \ldotp ,\Phi_4 ,c\right\rbrack }^{\prime } and innovations covariance matrix \Sigma in two ways:
% Indirectly create an empiricalbvarm model by estimating the posterior distribution of a semiconjugate prior model.
% Directly create an empiricalbvarm model by supplying draws from the prior or posterior distribution of the parameters.
%
%% Indirect Creation
% Assume the following prior distributions:
% \textrm{vec}\left({\left\lbrack \Phi_1 ,\ldotp \ldotp \ldotp ,\Phi_4 ,c\right\rbrack }^{\prime } \right)\left|\Sigma \sim N_{39} \left(\mu ,V\right)\right., where \mu is a 39-by-1 vector of means and V is the 39-by-39 covariance matrix.
% \Sigma\sim{\rm Inverse\, Wishart}\left(\Omega,\nu\right), where \Omega is the 3-by-3 scale matrix and \nu is the degrees of freedom.
% Create a semiconjugate prior model for the 3-D VAR(4) model parameters.

numseries = 3;
numlags = 4;
PriorMdl = semiconjugatebvarm(numseries,numlags)

% PriorMdl is a semiconjugatebvarm Bayesian VAR model object representing the prior distribution of the coefficients and innovations covariance of the 3-D VAR(4) model.
%
% Load the US macroeconomic data set.  Compute the inflation rate.  Plot all response series.

load Data_USEconModel
seriesnames = ["INFL" "UNRATE" "FEDFUNDS"];
DataTable.INFL = 100*[NaN; price2ret(DataTable.CPIAUCSL)];
figure
plot(DataTable.Time,DataTable{:,seriesnames})
legend(seriesnames)

% Stabilize the unemployment and federal funds rates by applying the first difference to each series.

DataTable.DUNRATE = [NaN; diff(DataTable.UNRATE)];
DataTable.DFEDFUNDS = [NaN; diff(DataTable.FEDFUNDS)];
seriesnames(2:3) = "D" + seriesnames(2:3);

% Remove all missing values from the data.

rmDataTable = rmmissing(DataTable);

%
% Estimate the posterior distribution by passing the prior model and entire data series to estimate.

rng(1); % For reproducibility
PosteriorMdl = estimate(PriorMdl,rmDataTable{:,seriesnames},'Display','off')

% PosteriorMdl is an empiricalbvarm model representing the empirical posterior distribution of the coefficients and innovations covariance matrix. empiricalbvarm stores the draws from the posteriors of \lambda \; and \Sigma \; in the CoeffDraws and SigmaDraws properties, respectively.
%
%% Direct Creation
% Draw a random sample of size 1000 from the prior distribution PriorMdl.

numdraws = 1000;
[CoeffDraws,SigmaDraws] = simulate(PriorMdl,'NumDraws',numdraws);
size(CoeffDraws)
size(SigmaDraws)

% Create a Bayesian VAR model characterizing the empirical prior distributions of the parameters.

PriorMdlEmp = empiricalbvarm(numseries,numlags,'CoeffDraws',CoeffDraws,...
    'SigmaDraws',SigmaDraws)

% Display the prior covariance mean matrices of the four AR coefficients by setting each matrix in the cell to a variable.

AR1 = PriorMdlEmp.AR{1}
AR2 = PriorMdlEmp.AR{2}
AR3 = PriorMdlEmp.AR{3}
AR4 = PriorMdlEmp.AR{4}

% Copyright 2019 The MathWorks, Inc.
%
%