%% Perform Bayesian Lasso Regression
% Consider the linear regression model in docid:econ_ug.bvllq77-1.
% Assume these prior distributions:
% For k = 0,...,3, \beta_k\vert\sigma^2 has a Laplace distribution with a mean of 0 and a scale of \sigma^2 /\lambda, where \lambda is the shrinkage parameter. The coefficients are conditionally independent.
% \sigma^2 \sim IG(A,B). A and B are the shape and scale, respectively, of an inverse gamma distribution.
% Create a prior model for Bayesian linear regression by using bayeslm. Specify the number of predictors p and the variable names.

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

% PriorMdl is a lassoblm Bayesian linear regression model object representing the prior distribution of the regression coefficients and disturbance variance. By default, bayeslm attributes a shrinkage of 0.01 to the intercept and 1 to the other coefficients in the model.
% Using dot notation, change the default shrinkages for all coefficients, except the intercept, by specifying a 3-by-1 vector containing the new values for the Lambda property of PriorMdl.
% Attribute a shrinkage of 10 to IPI and WR.
% Because E has a scale that is several orders of magnitude larger than the other variables, attribute a shrinkage of 1e5 to it.
% Lambda(2:end) contains the shrinkages of the coefficients corresponding to the specified variables in the VarNames property of PriorMdl.

PriorMdl.Lambda = [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"};

%
% Perform Bayesian lasso regression by passing the prior model and data to estimate, that is, by estimating the posterior distribution of \beta and \sigma^2. Bayesian lasso regression uses Markov chain Monte Carlo (MCMC) to sample from the posterior. For reproducibility, set a random seed.

rng(1);
PosteriorMdl = estimate(PriorMdl,X,y);

% Plot the posterior distributions.

plot(PosteriorMdl)

% Given a shrinkage of 10, the distribution of E is fairly dense around 0. Therefore, E might not be an important predictor.
% Copyright 2018 The MathWorks, Inc.