%% Set Prior Hyperparameters for Minnesota Regularization
% conjugatebvarm options enable you to specify prior hyperparameter values directly, but bayesvarm options are well suited for tuning hyperparameters following the Minnesota regularization method.
% Consider the 3-D VAR(4) model of docid:econ_ug#mw_22b40c74-722c-4ed0-bd9d-6de2110b22c7. The model contains 39 coefficients. For coefficient sparsity, create a conjugate Bayesian VAR model by using bayesvarm. Specify the following, a priori:
% Each response is an AR(1) model, on average, with lag 1 coefficient 0.75.
% Prior scaled coefficient covariances decay with increasing lag at a rate of 2 (that is, lower lags are more important than higher lags).

numseries = 3;
numlags = 4;
PriorMdl = bayesvarm(numseries,numlags,'ModelType','conjugate',...
    'Center',0.75,'Decay',2)

% Display the prior coefficient means in the equation of the first response.

Phi1 = PriorMdl.AR{1}
Phi2 = PriorMdl.AR{2}
Phi3 = PriorMdl.AR{3}
Phi4 = PriorMdl.AR{4}

% Display a heatmap of the prior scaled covariances of the coefficients in the first response 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:end-1,1:end-1));

% For conjugate Bayesian VAR models, scaled covariances are proportional among equations.
% Copyright 2019 The MathWorks, Inc.