bamMCMC#
Purpose#
Main procedure for conducting MCMC-Gibbs style sampling of models.
Format#
- bamOut0 = bamMCMC(myData, MCCtl0)#
- Parameters:
myData (struct) –
An instance of the
dgpControl
structure. For an instance of thedgpcontrol
structure named myData, the members are:model
String, specifies model type. Must be one of the following GAUSS supported model specifications:
”Linear” - Univariate or multivariate linear model.
”Auto” - Univariate linear model with autoregressive error terms.
”HB” - Hierarchical bayes estimation.
”Probit” - Binary choice probit model.
”Logit” - Binary choice logit model.
numObs
Scalar, number of observations to be generated per subject.
numSubjects, NTot
Scalar, total number of subjects and total number of observations, respectively.
numMix
Scalar, number of mixing components.
trueBeta
Matrix, numX x numY, true beta coefficients for linear dependency.
trueSigma
Matrix, numX x numY, true standard deviation of error terms.
trueLambda
Matrix, numX x numX, upper triangular matrix for HB model standard deviation of coefficients in stage one equation.
trueTheta
Matrix, numZ x numX, HB linear dependency coefficients in stage one equation.
Member
Description
trueInterceptX
Scalar, intercept value for all x equations.
trueTrendX
Scalar, coefficient on linear trend in x equation.
trueInterceptZ
Scalar, intercept value for all z equations.
trueTrendZ
Scalar, coefficient on linear trend in z equation.
MCCtl0 (struct) –
An instance of the
bayesControl
structure. For an instance of thebayesControl
structure named MCCtl0 the members are:MCCtl0.model
String, specifies model type. Must be one of the following GAUSS supported model specifications:
”Linear” - Univariate or multivariate linear model.
”Auto” - Univariate linear model with autoregressive error terms.
”HB” - Hierarchical bayes estimation.
”Probit” - Binary choice probit model.
”Logit” - Binary choice logit model.
MCCtl0.savedIter
Scalar, number of sampler iterations to be saved.
MCCtl0.saveSkip
Scalar, number of sampler iterations to skip between saving iterations.
MCCtl0.burnIter
Scalar, number of sampler burn-in iterations.
MCCtl0.MLE
Scalar, indicator parameter set to one to use MLE to find initial values.
MCCtl0.printOut
Scalar, indicator parameter set to one to print output details to screen.
MCCtl0.graphOut
Scalar, indicator parameter set to one to produce post sampler graphs of posterior.
MCCtl0.Intercept
Scalar, parameter indicating deterministic trends to include in the model:
0 for deterministic trends
1 for intercept only
2 for trend and intercept
3 for trend only
MCCtl0.numX
Scalar, number of dependent variables, when relevant.
MCCtl0.numZ
Scalar, number of first stage Z dependent variables, when relevant.
MCCtl0.numLags
Scalar, number of autoregressive lags, when relevant.
MCCtl0.numMix
Scalar, number of mixtures components, when relevant.
MCCtl0.numSubjects
Scalar, number of subjects, when relevant.
- Returns:
bamOut0 (struct) –
An instance of the
bayesOut
structure. For an instance of thebayesOut
structure named bamOut0, the members are:bamOut0.estBeta
Matrix, stored draws of the posterior distribution of beta.
bamOut0.estSigma
Matrix, stored draws of the posterior distribution of sigma.
bamOut0.estRho
Matrix, stored draws of the posterior distribution of rho.
bamOut0.estLambda
Matrix, stored draws of the posterior distribution of lambda.
bamOut0.estTheta
Matrix, stored draws of the posterior distribution of theta.
bamOut0.estPhi
Matrix, stored draws of the posterior distribution of phi.
bamOut0.estZProb
Matrix, stored draws of the posterior distribution of individual level class membership probabilities.
bamOut0.betaMean
Matrix, means of the posterior distributions of beta coefficients.
bamOut0.betaStd
Matrix, standard deviation of the posterior distributions of beta coefficients.
bamOut0.sigmaMean
Matrix, means of the posterior distributions of sigma.
bamOut0.sigmaStd
Matrix, standard deviation of the posterior distributions of sigma.
bamOut0.rhoMean
Matrix, means of the posterior distributions of rho coefficients.
bamOut0.rhoStd
Matrix, standard deviation of the posterior distributions of rho coefficients.
bamOut0.phiMean
Matrix, means of the posterior distributions of phi coefficients.
bamOut0.phiStd
Matrix, standard deviation of the posterior distributions of phi coefficients.
bamOut0.lambdaMean
Matrix, means of the posterior distributions of lambda coefficients.
bamOut0.lambdaStd
Matrix, standard deviation of the posterior distributions of lambda coefficients.
bamOut0.thetaMean
Matrix, means of the posterior distributions of theta coefficients.
bamOut0.thetaStd
Matrix, standard deviation of the posterior distributions of theta coefficients.
bamOut0.yhat
Matrix, predicted y-values using the posterior coefficients distributions.
bamOut0.resid
Matrix, residuals using predicted y-values.
bamOut0.cy
Matrix, correlation between y and y-hat.
bamOut0.cy2
Matrix, r-squared.
bamOut0.estLL
Matrix, estimation log-likelihood.
Examples#
Linear model with AR error terms#
Step One: Data Generation#
The first step when using bamMCMC()
is to identify the data for the model. For this model, we will use the dataGen()
procedure to generate our data. This data must then be placed in the dgpOut
structure.
new;
cls;
library bet;
// Data generation control structure
struct dgpControl GC0;
// Specify autoregressive model
GC0.Model = "AUTO";
// Turn on plotting of generated data
GC0.PlotData = 1;
// Specify number of observations
GC0.NumObs = 100;
// Specify model parameters
GC0.TrueBeta = -3.1|2|-1.0|3;
GC0.TrueRho = 0.3|-0.5;
// Error variance
GC0.TrueSigmaE = 2;
// X Error Variance
GC0.TrueSigmaX = 2;
GC0.TrueTrendX = 0;
GC0.TrueInterceptX = 0;
// Call data generation function
struct dgpOut myData;
myData = DataGen(GC0);
Step Two: Initialize Parameters for MCMC#
Next, we set up the specifications for the MCMC simulation.
// Declare instance of the bayesControl structure
struct bayesControl MCCtl0;
// Specify AR(2) model
MCCtl0.model = "AUTO";
MCCtl0.numLags = 2;
// Number of saved iterations
MCCtl0.SavedIter = 4000;
// Save skipped iterations
MCCtl0.SaveSkip = 1;
// Number of burn-in iterations
MCCtl0.BurnIter = 1000;
// No intercept
MCCtl0.InterceptX = 0;
// Turn of MLE for start values
MCCtl0.MLE = 0;
// Control printing and graphs
MCCtl0.printGraph = 1;
MCCtl0.printOut = 1;
Step Three: Perform MCMC#
The final step is to call bamMCMC()
to perform MCMC simulation.
// Step Three: MCMC
struct bayesOut BAMSt0;
bamOut0 = bamMCMC(myData, MCCtl0);
Linear model with loaded data#
In this example, the loadd()
procedure is used to load the model data.
Step One: Load data#
new;
library bet;
// Load data from gbs_auto.gdat file
data = loadd(__FILE_DIR $+ "gbs_ato.gdat");
// Call data generation function
struct dgpOut myData;
mydata.ydata = data[., "Y"];
myData.xdata = ones(rows(data), 1)~data[., "X"];
Step Two: Initialize Parameters for MCMC#
// Declare instance of the bayesControl structure
struct bayesControl MCCtl0;
// Specify AR(2) model
MCCtl0.model = "AUTO";
MCCtl0.numLags = 1;
// Number of saved iterations
MCCtl0.SavedIter = 4000;
// Save skipped iterations
MCCtl0.SaveSkip = 1;
// Number of burn-in iterations
MCCtl0.BurnIter = 1000;
// No intercept
MCCtl0.InterceptX = 0;
// Turn off MLE for start values
MCCtl0.MLE = 0;
// Control printing and graphs
MCCtl0.printGraph = 1;
MCCtl0.printOut = 1;
Step Three: Perform MCMC#
// Define storage structure
struct bayesOut BAMSt0;
BAMSt0 = bamMCMC(myData, MCCtl0);
Remarks#
The bamMCMC()
procedure is the main procedure for model estimation in BET. It will can be used to estimate a number of different models. The type of model to be estimated is specified in the bayesControl
structure.
Estimation with the BET library using bamMCMC()
requires three steps:
- Data loading or generation
The BET library allows you to input data using standard GAUSS data loading tools, such as
loadd()
. However, it also provides a complete suite of data generation tools that allow users to specify true data parameters and build hypothetical data sets for analysis. Whether user defined or GAUSS generated, thedgpOut
structure is used to input data into thebamMCMC()
procedure.
- Initialize the MCMC
The next step is to setup the parameters of the MCMC simulation using the
bayesControl
structure. This includes the:Model
Number of saved iterations
Number of iterations to skip
Number of burn-in iterations
Total number of iterations
Inclusion of intercept
Plotting behavior
- Perform bayesian analysis
The final step is to call the
bamMCMC()
procedure usingdgpOut
data structure along with thebayesControl
structure. In this step, GAUSS performs Markov Chain Monte Carlo numerical simulation, combined with assumed statistical structures and priors, to numerically compute parameter posterior distributions.In addition to producing graphs of all MCMC iterations for all parameters and posterior distributions for all parameters, this procedure has one return structure the
bayesOut
structure. ThebayesOut
structure includes:Draws for all parameters at each iteration
Posterior Mean for all parameters
Posterior standard deviation for all parameters
Predicted values
Residuals
Correlation matrix between \(Y`\) and \(\hat{Y}\)
PDF values and corresponding PDF grid for all posterior distributions
Log-likelihood value (when applicable)