%% Adjust Posterior Sampling Options
% Consider the 3-D VAR(4) model of docid:econ_ug.mw_1f230922-b87b-4b6a-a9d5-1cc611d85f95. In this case, assume that the coefficients and innovations covariance matrix are independent (a semiconjugate prior model).
% 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 semiconjugate Bayesian VAR(4) prior model for the three response series. Specify the response variable names.

numseries = numel(seriesnames);
numlags = 4;

PriorMdl = bayesvarm(numseries,numlags,'Model','semiconjugate',...
    'SeriesNames',seriesnames);

% Because the joint posterior of a semiconjugate prior model is analytically intractable, estimate uses a Gibbs sampler to form the joint posterior by sampling from the tractable full conditionals.
% Estimate the posterior distribution. For the Gibbs sampler, specify an effective number of draws of 20,000, a burn-in period of 5000, and a thinning factor of 10.

rng(1) % For reproducibility
PosteriorMdl = estimate(PriorMdl,rmDataTable{:,seriesnames},...
    'Display','equation','NumDraws',20000,'Burnin',5000,'Thin',10);

% PosteriorMdl is an empiricalbvarm model represented by draws from the full conditionals.  After removing the first burn-in period draws and thinning the remaining draws by keeping every 10th draw, estimate stores the draws in the CoeffDraws and SigmaDraws properties.
% Copyright 2020 The MathWorks, Inc.
%
%