%% Set Prior Hyperparameters for Minnesota Regularization
% normalbvarm options enable you to specify coefficient 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_34d57d6f-313b-45bd-b28c-680a8b0d2855. The model contains 39  coefficients. For coefficient sparsity, create a normal 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 self-lag coefficients have variance 100. This large variance setting allows the data to influence the posterior more than the prior.
% Prior cross-lag coefficients have variance 1. This small variance setting tightens the cross-lag coefficients to zero during estimation.
% Prior coefficient covariances decay with increasing lag at a rate of 2 (that is, lower lags are more important than higher lags).
% The innovations covariance \Sigma = I.

numseries = 3;
numlags = 4;
seriesnames = ["INFL"; "UNRATE"; "FEDFUNDS"];
Sigma = eye(numseries);
PriorMdl = bayesvarm(numseries,numlags,'ModelType','normal','Sigma',Sigma,...
    'Center',0.75,'SelfLag',100,'CrossLag',1,'Decay',2,'SeriesNames',seriesnames)

% Display all prior coefficient means.

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

% Display a heatmap of the prior coefficient covariances for each response equation.

numexocoeffseqn = PriorMdl.IncludeConstant + ...
    PriorMdl.IncludeTrend + PriorMdl.NumPredictors;             % Number of exogenous coefficients per equation
numcoeffseqn = PriorMdl.NumSeries*PriorMdl.P + numexocoeffseqn; % Total number of coefficients per equation
arcoeffnames = strings(numseries,numlags,numseries);
for j = 1:numseries         % Equations
    for r = 1:numlags
        for k = 1:numseries % Response Variables
            arcoeffnames(k,r,j) = "\phi_{"+r+","+j+k+"}";
        end
    end
    arcoeffseqn = arcoeffnames(:,:,j);
    idx = ((j-1)*numcoeffseqn + 1):(numcoeffseqn*j) - numexocoeffseqn;
    Veqn = PriorMdl.V(idx,idx);
    figure
    heatmap(arcoeffseqn(:),arcoeffseqn(:),Veqn);
    title(sprintf('Equation of %s',seriesnames(j)))
end

% Copyright 2020 The MathWorks, Inc.
%
%