%% Matrix-Normal-Inverse-Wishart Conjugate Prior Model
% Consider the 3-D VAR(4) model in docid:econ_ug.mw_e27fdd84-2be8-45ad-ac37-9822b3f3d9ab. Assume the following:
% [\Phi_1,...,\Phi_4,c]^\prime\vert\Sigma \sim N_{13\times3}\left({\rm M},V,\Sigma\right).  M is a 13-by-3 matrix of prior coefficient means (M\left(1:3,1:3\right) is the prior mean matrix of \Phi_1^{\prime }, M\left(4:6,1:3\right) is the prior mean matrix of \Phi_2^{\prime },..., and M\left(13,1:3\right) is the prior mean vector of c). V is a 13-by-13 matrix representing the among-coefficient prior covariance matrix within an equation. \Sigma is the 3-by-3 random innovations covariance matrix.
% \Sigma \sim {\rm Inverse\;Wishart}(\Omega,\nu). \Omega is the 3-by-3 scale matrix, and \nu is the degrees of freedom of the inverse Wishart distribution.
% The coefficients and the innovations covariance matrix are dependent.
% Prior coefficient variances among the equations are proportional.
% These assumptions and the data likelihood imply a matrix-normal-inverse-Wishart conjugate model.
% Create a matrix-normal-inverse-Wishart conjugate prior model for the VAR model parameters.

numseries = 3;
numlags = 4;
PriorMdl = bayesvarm(numseries,numlags,'ModelType','conjugate')

%
% PriorMdl is a conjugatebvarm Bayesian VAR model object representing the prior distribution of the coefficients and innovations covariance matrix. bayesvarm displays a summary of the prior distributions at the command line; it returns the prior mean matrix in vectorized form.
% The model contains many estimable parameters. To achieve a parsimonious model, bayesvarm applies the Minnesota prior regularization method to the AR coefficients, by default. Inspect the default prior means (centers of shrinkage) of the AR coefficient matrices.

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

%
% Each series is an AR(1) model with AR coefficient 0.5, a priori.
% The tightness on shrinkage of the coefficients is proportional among the equations. Inspect the default tightness values by displaying a heatmap chart of the property V of PriorMdl, which contains a matrix of the scaled tightness on shrinkage of the coefficients for one equation (the unscaled shrinkage is \Sigma \otimes V = kron(PriorMdl.Covariance,PriorMdl.V)). Omit the final row and column, which correspond to the model constant.

% Create labels for the chart.
numARCoeffMats = PriorMdl.NumSeries*PriorMdl.P;
arcoeffnames = strings(numARCoeffMats,1);
for r = numlags:-1:1
    arcoeffnames(((r-1)*numseries+1):(numseries*r)) = ["\phi_{11,"+r+"}" "\phi_{12,"+r+"}" "\phi_{13,"+r+"}"];
end

heatmap(arcoeffnames,arcoeffnames,PriorMdl.V(1:end-1,1:end-1));

%
% The tightness values decrease with lag, which suggests (a priori) that the means of the corresponding greater-lagged coefficients are more tightly locked around their center of 0.
% Display the tightness of the model constant vector.

PriorMdl.V(end,end)

%
% The center of the model constant vector is 0 but has a large variance, which allows the estimation procedure to defer more to the data than the prior for the posterior mean of the constant vector.
% You can specify alternative values after you create a model by using dot notation. For example, increase the tightness of all coefficients by a factor of 100.

PriorMdl.V = 100*PriorMdl.V;

%
% Copyright 2020 The MathWorks, Inc.