%% Specify Custom Prior Regime Probability Distribution for SSVS
% Consider the linear regression model in <docid:econ_ug.mw_092bf8cb-44d0-4615-a7bd-8ab9b02f26d0>.
%% 
% Load the Nelson-Plosser data set. Create variables for the response and 
% predictor series. Add example-specific files to the MATLAB(R) path.
load Data_NelsonPlosser
VarNames = ["IPI" "E" "WR"];
X = DataTable{:,VarNames};
y = DataTable{:,"GNPR"};

path = fullfile(matlabroot,'examples','econ','main');
addpath(path);
%% 
% Assume the following:
%
% * The intercept is in the model with probability 0.9.
% * |IPI| and |E| are in the model with probability 0.75.
% * If |E| is included in the model, then the probability that |WR| is
% included in the model is 0.9.
% * If |E| is excluded from the model, then the probability that |WR| is
% included is 0.25.
%
%%
% Declare a function named |priorssvsexample.m| that:
%
% * Accepts a logical vector indicating whether the intercept and variables
% are in the model (|true| for model inclusion). Element 1 corresponds to
% the intercept, and the rest of the elements correspond to the 
% variables in the data.
% * Returns a numeric scalar representing the log of the described prior regime
% probability distribution.
%
% <include>priorssvsexample.m</include>
%
% |priorssvsexample.m| is an example-specific file included with
% Econometrics Toolbox(TM). To access it, enter |edit priorssvsexample.m|
% at the command line.
%%
% Create a prior model for performing SSVS. Assume that $\beta$ and 
% $\sigma^2$ are (a conjugate mixture model). Specify the
% number of predictors |p| the names of the regression coefficients, and
% custom, prior probability distribution of the variable-inclusion
% regimes.
p = 3;
PriorMdl = mixconjugateblm(p,'VarNames',["IPI" "E" "WR"],...
    'Probability',@priorssvsexample);
%% 
% Implement SSVS by estimating the marginal posterior distributions of
% $\beta$ and $\sigma^2$. Because SSVS uses MCMC for estimation, set a
% random number seed to reproduce the results.
rng(1);
PosteriorMdl = estimate(PriorMdl,X,y);
%% 
% Assuming, that variables with |Regime| < 0.1 should be
% removed from the model, the results suggest that you can include all
% variables in the model.
%%
% Because this example requires |path| to access example-specific files,
% clean up by removing |path| from the MATLAB(R) path.
rmpath(path);
%%
% Copyright 2018 The MathWorks, Inc.