%% Estimate Posterior with Sparse Coefficients
% Consider the 3-D VAR(4) model of docid:econ_ug.mw_1f230922-b87b-4b6a-a9d5-1cc611d85f95. In this example, create a normal conjugate prior model with a fixed coefficient matrix instead of a diffuse model. The model contains 39 coefficients. For coefficient sparsity in the posterior, apply the Minnesota regularization method during estimation.
% Load the US macroeconomic data set.  Compute the inflation rate, stabilize the unemployment and federal funds rates, and remove missing values.

load Data_USEconModel
seriesnames = ["INFL" "UNRATE" "FEDFUNDS"];
DataTable.INFL = 100*[NaN; price2ret(DataTable.CPIAUCSL)];

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

% Create a normal conjugate Bayesian VAR(4) prior model for the three response series. Specify the response variable names, and set the innovations covariance matrix
% \Sigma = \left[ {\begin{array}{*{20}{c}}
% {{{10}^{ - 5}}}&0&{{{10}^{ - 4}}}\\
% 0&{0.1}&{ - 0.2}\\
% {{{10}^{ - 4}}}&{ - 0.2}&{1.6}
% \end{array}} \right].
% According to the Minnesota regularization method, specify the following:
% Each response is an AR(1) model, on average, with lag 1 coefficient 0.75.
% The prior self-lag coefficients have variance 100. This large variance setting allows the data to influence the posterior more than the prior.
% The prior cross-lag coefficients have variance 0.01. This small variance setting tightens the cross-lag coefficients to zero during estimation.
% Prior coefficient covariances decay with increasing lag at a rate of 10 (that is, lower lags are more important than higher lags).

numseries = numel(seriesnames);
numlags = 4;
Sigma = [10e-5 0 10e-4; 0 0.1 -0.2; 10e-4 -0.2 1.6]; 

PriorMdl = bayesvarm(numseries,numlags,'Model','normal','SeriesNames',seriesnames,...
    'Center',0.75,'SelfLag',100,'CrossLag',0.01,'Decay',10,...
    'Sigma',Sigma);

% Estimate the posterior distribution, and display the posterior response equations.

PosteriorMdl = estimate(PriorMdl,rmDataTable{:,seriesnames},'Display','equation');

% Compare the results to a posterior in which you specify no prior regularization.

PriorMdlNoReg = bayesvarm(numseries,numlags,'Model','normal','SeriesNames',seriesnames,...
    'Sigma',Sigma);
PosteriorMdlNoReg = estimate(PriorMdlNoReg,rmDataTable{:,seriesnames},'Display','equation');

% The posterior estimates of the Minnesota prior have lower magnitude, in general, compared to the estimates of the default normal conjugate prior model.
% Copyright 2019 The MathWorks, Inc.
%
%