%% Summarize Prior and Posterior Distributions
% Consider the multiple linear regression model that predicts the US real gross national product (GNPR) using a linear combination of industrial production index (IPI), total employment (E), and real wages (WR).
% \texttt{GNPR}_t=\beta_0+\beta_1\texttt{IPI}_t+\beta_2\texttt{E}_t+\beta_3\texttt{WR}_t+\varepsilon_t.
% For all t, \varepsilon_t is a series of independent Gaussian disturbances with a mean of 0 and variance \sigma^2.
% Assume these prior distributions for k = 0,...,3:
% \beta_k\vert\sigma^2,\gamma_k = \gamma_k\sigma\sqrt{V_{k1}}Z_1 + (1 - \gamma_k)\sigma\sqrt{V_{k2}}Z_2, where Z_1 and Z_2 \;are independent, standard normal random variables.  Therefore, the coefficients have a Gaussian mixture distribution. Assume all coefficients are conditionally independent, a priori, but they are dependent on the disturbance variance.
% \sigma^2 \sim IG(A,B). A and B are the shape and scale, respectively, of an inverse gamma distribution.
% \gamma_k \in \left\lbrace 0,1\right\rbraceand it represents the random variable-inclusion regime variable with a  discrete uniform distribution.
% Create a prior model for SSVS. Specify the number of predictors p.

p = 3;
VarNames = ["IPI" "E" "WR"];
PriorMdl = bayeslm(p,'ModelType','mixconjugateblm','VarNames',VarNames);

% PriorMdl is a mixconjugateblm Bayesian linear regression model object for SSVS predictor selection representing the prior distribution of the regression coefficients and disturbance variance.
%
% Summarize the prior distribution.

summarize(PriorMdl)

% The function displays a table of summary statistics and other information about the prior distribution at the command line.
%
% Load the Nelson-Plosser data set, and create variables for the predictor and response data.

load Data_NelsonPlosser
X = DataTable{:,PriorMdl.VarNames(2:end)};
y = DataTable.GNPR;

%
% Estimate the posterior distributions. Suppress the estimation display.

PosteriorMdl = estimate(PriorMdl,X,y,'Display',false);

% PosteriorMdl is an empiricalblm model object that contains the posterior distributions of \beta and \sigma^2.
%
% Obtain summary statistics from the posterior distribution.

summary = summarize(PosteriorMdl);

% summary is a structure array containing two fields: MarginalDistributions and JointDistribution.
%
% Display the marginal distribution summary by using dot notation.

summary.MarginalDistributions

% The MarginalDistributions field is a table of summary statistics and other information about the posterior distribution.
% Copyright 2012 The MathWorks, Inc.
%