%% Estimate Conditional Posterior Distributions
% Consider the regression model and prior distribution in docid:econ_ug.mw_33fe5bc0-0b85-4cc1-9474-d2497f40714b.
% Create a Bayesian lasso regression prior model for 3 predictors and specify variable names.  Specify the shrinkage values 0.01, 10, 1e5, and 10 for the intercept, and the coefficients of IPI, E, and WR.

p = 3;
PriorMdl = bayeslm(p,'ModelType','lasso','VarNames',["IPI" "E" "WR"],...
    'Lambda',[0.01; 10; 1e5; 10]);

% Load the Nelson-Plosser data set. Create variables for the response and predictor series.

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

%
% Estimate the conditional posterior distribution of \beta given the data and that \sigma^2 = 10, and return the estimation summary table to access the estimates.

rng(1); % For reproducibility
[Mdl,SummaryBeta] = estimate(PriorMdl,X,y,'Sigma2',10);

% estimate displays a summary of the conditional posterior distribution of \beta. Because \sigma^2 is fixed at 10 during estimation, inferences on it are trivial.
% Display Mdl.

Mdl

% Because estimate computes the conditional posterior distribution, it returns the model input PriorMdl, not the conditional posterior, in the first position of the output argument list.
% Display the estimation summary table.

SummaryBeta

% SummaryBeta contains the conditional posterior estimates.
%
% Estimate the conditional posterior distributions of \sigma^2 given that \beta is the conditional posterior mean of \beta\vert\sigma^2,X,y (stored in SummaryBeta.Mean(1:(end – 1))). Return the estimation summary table.

condPostMeanBeta = SummaryBeta.Mean(1:(end - 1));
[~,SummarySigma2] = estimate(PriorMdl,X,y,'Beta',condPostMeanBeta);

% estimate displays an estimation summary of the conditional posterior distribution of \sigma^2 given the data and that \beta is condPostMeanBeta. In the display, inferences on \beta are trivial.
% Copyright 2018 The MathWorks, Inc.