%% Set Minnesota Prior Parameters of Normal-Inverse-Wishart Semiconjugate Prior Model
% Consider the 3-D VAR(4) model in docid:econ_ug.mw_e27fdd84-2be8-45ad-ac37-9822b3f3d9ab. Assume the following:
% \left. {{\rm{vec}}\left( \left[{{\Phi _1},...,{\Phi _4},c}\right]^\prime \right)} \right|\Sigma \sim{N_{39}}\left( {\mu ,V} \right). \mu is a 39-by-1 vector of prior coefficient means (the model has 39 individual coefficients), and V is a 39-by-39 prior coefficient covariance matrix.
% \Sigma\sim {\rm Inverse Wishart}\left(\Omega,\nu\right). \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 independent.
% These assumptions and the data likelihood imply a normal-inverse-Wishart semiconjugate model.
% The model contains many estimable parameters. To achieve a parsimonious model, bayesvarm enables you to regularize the coefficients by using the Minnesota prior regularization method, rather than specifying each prior mean and variance.
% Create a normal-inverse-Wishart semiconjugate prior model for the VAR model parameters. Specify the following:
% All series are AR(1) models, a priori, with AR coefficient 0.9. Set the 'Center' name-value pair argument to a 3-by-1 vector composed of 0.9.
% The tightness around self lags in \Phi_1 is 1. Set the 'SelfLag' name-value pair argument to 1.
% The tightness around cross lags in \Phi_1 is 0.5. Set the 'CrossLag' name-value pair argument to 0.5.
% All tightness values decay by a factor of the lag degree squared. Set the 'Decay' name-value pair argument to 2.

numseries = 3;
numlags = 4;
center = 0.9*ones(numseries,1);
PriorMdl = bayesvarm(numseries,numlags,'ModelType','semiconjugate',...
    'Center',center,'SelfLag',1,'CrossLag',0.5,'Decay',2)

%
% PriorMdl is a semiconjugatebvarm 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.
% Display the prior means 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, a priori.
% The property V of PriorMdl contains a matrix of the tightness on shrinkage of the coefficients. The rows and columns of V correspond to the elements of the Mu property of PriorMdl.
% Elements 1 through 3 correspond to the lag 1 AR coefficients in the first equation ordered by response variable, that is, \phi_{1,11}, \phi_{1,12}, and \phi_{1,13}.
% Elements 4 through 6 correspond to the lag 2 AR coefficients in the first equation.
% Elements 7 through 9 correspond to the lag 3 AR coefficients in the first equation.
% Elements 10 through 12 correspond to the lag 4 AR coefficients in the first equation.
% Element 13 is the model constant in the first equation.
% MATLAB® repeats the pattern for each equation.
% In this example, the tightness of shrinkage is the same for all equations. Display a heatmap chart of the property V of PriorMdl for the tightness values of the AR coefficients in the first equation.

% 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_{"+r+",11}" "\phi_{"+r+",12}" "\phi_{"+r+"13}"];
end

heatmap(arcoeffnames,arcoeffnames,PriorMdl.V(1:numARCoeffMats,1:numARCoeffMats));

%
% 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. By default, AR coefficients are uncorrelated.
% Display the tightness of the model constant vector.

PriorMdl.V(numARCoeffMats + 1,numARCoeffMats + 1)

%
% 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.