This folder contains the data and codes for replicating the results in
"The Role of Time-Varying Price Elasticities in Accounting for Volatility Changes in the Crude Oil Market"
by Christiane Baumeister and Gert Peersman.
Forthcoming in the Journal of Applied Econometrics.
March 2012.


The main file is oilVAR3world_setup.m, which runs the MCMC algorithm and imposes
the sign restrictions for the identification of the structural shocks. This script 
calls all the other functions. The folder 'data' contains all the data used in
the estimation which are called by xlsread.m. The results are stored in the folder
'results'. The figures of the main paper are automatically created at the end. 
At the beginning of the script file (lines 17-19) some parameters have to be set 
that determine which model is estimated (benchmark model and three alternative 
models for robustness checks and additional results).

The 4 main functions in the script file are:
1) TVP_BURN.m: estimates the reduced-form TVP-VAR for a burn-in period.
2) TVP_MCMC.m: samples from the ergodic distribution.
3) convergence_check: ascertains convergence to the ergodic distribution
                      by computing inefficiency factors.
4) TVP_IRFS.m: computes impulse responses to structural shocks identified
               by sign restrictions.

The other sub-routines are called from the 4 folders contained in the folder 
'mymatprog' the content of which is summarized below.

*****************************************************************************

The folder 'reduced' contains routines called by TVP_BURN.m and TVP_MCMC.m.
Here is an overview of those functions:

BAYESREG.M runs a Bayesian regression.  This is used for initial estimates
for the covariance parameters A.

CHOFAC.M copies the covariance parameters A from a vector into a lower 
triangular matrix.

CHOVEC.M extracts the Choleski coefficients from a lower triangular 
Choleski matrix. They are vectorised column by column. 

GETALPHA.M performs the Kalman filter recursions to obtain a time-varying
estimate of the covariance parameters A.

GIBBS1.M simulates the VAR parameters using the forward filter, 
backward sampler approach of Carter and Kohn.  It uses outputs 
generated by the files KFR.M and KFP.M, which are Kalman-filter routines.

GIBBS2Q.M simulates an inverse-Wishart matrix.  Here it is used for
Q, the covariance matrix for theta innovations.  It takes as input
the scale matrix and degree of freedom parameter produced by IWPQ.M.

IG2.M generates a draw from an inverse-gamma density.  This is used
for part of the stochastic volatility model.

INNOVM.M calculates the innovations for the measurement equation.

LAGDEP.M creates the VAR lag structure.

STACKA.M extracts the coefficients below the diagonal from a lower 
triangular matrix. They are vectorised row by row.

SUR.M calculates a seemingly unrelated regression.  This is used to
calibrate the priors. 

The files SVMH.M, SVMH0.M, and SVMHT.M generate Metropolis-Hastings 
draws for the stochastic volatilities following the JPR algorithm.  
The '0' and 'T' of the latter two refer to the beginning and end of 
the sample, respectively. The first file simulates draws for interior 
dates.

VARROOTS.M checks the stability of the VAR system i.e. whether the roots
of the autoregressive lag order polynomial are outside the unit root circle.

XMAT.M takes data on lagged dependent variables and transforms them
into a convenient tensor.


*****************************************************************************


The folder 'structural' contains routines called by TVP_IRFS_BP1.m.
Here is an overview of those functions:

DATABASICS.M does basic data transformations.

GIRFs_N3.M computes the generalized impulse responses for 3 variables and 
identifies the structural shocks with sign restrictions.

GIRFs_N3_EXACT.M computes the generalized impulse responses for 3 variables
and identifies the strucutral shocks with sign restrictions and one zero
restriction.

GIRFs_N3_PERSIST.M computes the generalized impulse responses and the 
contribution of structural shocks (identified by sign restrictions) to the 
persistence and variance of the endogenous variables.

IMPACT.M computes a candidate contemporeanous impact matrix.

MYFLIPLR.M flips matrix in left-right direction i.e. changes the order of the 
columns.

MYSQRT_VARM.M computes the matrix square root of a variance-covariance matrix.

MYVEC.M vectorizes a matrix.

OILMARKET_MODEL.M computes the short-run price elasticities and the size of the 
structural shocks.

PERSIST.M computes the contribution of structural shocks to the persistence of 
the variable of interest.

ROWS.M returns the row dimension of a series.

STACKA.M extracts the coefficients below the diagonal from a lower 
triangular matrix. They are vectorised row by row.

VARCOMPANION.M creates the companion form of a matrix.

VEC.M vectorizes a matrix.


*****************************************************************************


The folder 'utilities' contains routines called mainly for the figures.
Here is an overview of those functions:

CFFILTER.M performs band-pass filtering of a series of interest.

DetectNAN.M detects NaN entries.

GETVOLATILITIES_VARIABLES.M computes unconditional standard deviations for the 
time-varying VAR model.

OUTCHECK1.M checks a series for outliers.

PERSISTENCEPO.M computes the normalized spectral density at frequency zero.

PLOTX1.M plots 1 shaded area.

PLOTX2.M plots 2 shaded areas.

SUMQ.M takes the sum over the quarter of monthly data.

VOL_ROLL.M computes the standard deviation over a rolling window.


******************************************************************************


The folder 'convergence' contains routines called by convergence_check.m.
Here is an overview of those functions:

BARTWIND.M creates a Bartlett spectral window for a pre-defined bandwidth.

BELTBLOOM.M selects the optimal bandwidth for a Bartlett spectral window 
estimator of the spectral density matrix.

BELTBLOOML.M computes an approximated cross-validated log-likelihood in the
frequency domain.

BOOTSTRAP.M bootstraps the residuals matrix U.

CHOFAC.M transforms a vector of coefficients into a lower triangular matrix.

COSPSMOOTH.M smooths the real and the imaginary parts of the cross-periodogram.

DEMEAN.M demeans a matrix.

GROWTHRATE.M computes the L-period growth rates.

MAXINDC.M returns the place of the largest entry in each column of a matrix.

MININDC.M returns the place of the smallest entry in each column of a matrix.

SPECDENSESTIMA.M estimates the spectrum for a time series.

SPECSMOOTH.M smooths a periodogram to get the estimated spectrum.


*******************************************************************************

Please address questions to: Christiane Baumeister
                             cbaumeister <at> bankofcanada.ca