Applied Bayesian Econometrics For Central Bankers Updated 2017 PDF
Applied Bayesian Econometrics For Central Bankers Updated 2017 PDF
Andrew Blake
Haroon Mumtaz
Center For Central Banking Studies, Bank of England
E-mail address: [email protected]
1. Introduction
This chapter provides an introduction to the technique of estimating linear regression models using Gibbs sam-
pling. While the linear regression model is a particularly simple case, the application of Gibbs sampling in this
scenario follows the same principles as the implementation in a more complicated models (considered in later chap-
ters) and thus serves as a useful starting point. We draw heavily on the seminal treatment of this topic in Kim and
Nelson (1999). A more formal (but equally accessible) reference is Koop (2003).
The reader should aim to become familiar with the following with the help of this chapter
• The prior distribution, the posterior distribution and Bayes Theorem.
• Bayesian treatment of the linear regression model.
• Why Gibbs sampling provides a convenient estimation method.
• Coding the Gibbs sampling algorithm for a linear regression in Matlab
Step 2. The researcher collects data on and and write down the likelihood function of the model
µ ¶
¡ 2
¢ ¡ ¢
2 − 2 ( − )0 ( − )
| = 2 exp −
2 2
This step is identical to the approach of the classical econometrican and represents the information about
the model parameters contained in the data.
Step 3. The researcher updates her prior belief on the model parameters (formed in step 1) based on the information
contained in the data¡ (using ¢ the likelihood function in step 2).
¡ In other ¢ words, the researcher combines the
2 2
prior
¡ distribution
¢ and the likelihood function | to obtain the posterior distribution
2 | .
¡ ¢
More formally, the Bayesian econometrician is interested in the posterior distribution 2 | which is
defined by the Bayes Law ¡ ¢ ¡ ¢
¡ 2
¢ | 2 × 2
| = (2.4)
( )
¡ 2
¢
Equation
¡ ¢ 2.4 simply states that the posterior distribution is a product of the likelihood | and the prior
2
divided by the density of the data ( ) (also referred to as the marginal likelihood or the marginal data
density). Note that ( ) is a scalar and will not have any operational significance as far as estimation is concerned
(although it is crucial for model comparison, a topic we return to). Therefore the Bayes Law can be written as
¡ ¢ ¡ ¢ ¡ ¢
2 | ∝ | 2 × 2 (2.5)
Equation 2.5 states that the posterior distribution is proportional to the likelihood times the prior. In practice, we
will consider equation 2.5 when considering the estimation of the linear regression model.
As an aside note that the Bayes¡ law in¢equation 2.4 can be easily derived by considering the joint density of the
data and parameters 2 2 and observing that ir can be factored in two ways
¡ ¢
2 = ( ) × ( 2 | ) = ( | 2 ) × ( 2 )
¡ ¢
That is the joint density 2 is the product of the marginal density of and the conditional density
of the parameters ( 2 | ) Or equivalently the joint density is the product of the conditional density of the data
and the marginal density of the parameters. Rearranging the terms after the first equality leads to equation 2.4.
These steps in Bayesian analysis have a number of noteworthy features. First, the Bayesian econometrician is
interested in the posterior distribution and not the mode of the likelihood function. Second, this approach combines
prior information with the information in the data. In contrast, the classical econometrician focusses on information
contained in the data about the parameters as summarised by the likelihood function.
To motivate the use of Gibbs sampling for estimating ( 2 | ) we will consider the derivation of the posterior
distribution in three circumstances. First we consider estimating the posterior distribution of under the assumption
that 2 is known. Next we consider estimating the posterior distribution of 2 under the assumption that is known
and finally we consider the general case when both sets of parameters are unknown.
2.1. Case 1: The posterior distribution of assuming 2 is known. Consider the scenario where the
econometrician wants to estimate in equation 2.1 but knows the value of 2 already. As discussed above, the
posterior distribution is derived using three steps.
Setting the prior. In the first step the researcher sets the prior distribution for A normally distributed prior
() ∼ (0 Σ0 ) for the coefficients is a conjugate prior. That is, when this prior is combined with the likelihood
function this results in a posterior with the same distribution as the prior. Since the form of the posterior is known
when using conjugate priors these are especially convenient from a practical point of view. The prior distribution is
given by the following equation
1 £ ¤
(2)−2 |Σ0 |− 2 exp −05 ( − 0 )0 Σ−1
0 ( − 0 ) (2.6)
£ 0 −1 ¤
∝ exp −05 ( − 0 ) Σ0 ( − 0 )
The equation in 2.6 simply defines a normal distribution with mean 0 and variance Σ0 Note that for practical
purposes
³ we only ´need to consider terms in the exponent (second line of equation 2.6) as the first two terms in 2.6
1
−2
(2) |Σ0 |− 2 are constants.
Setting up the likelihood function. In the second step, the researcher collects the data and forms the
likelihood function:
µ 0 ¶
¡ 2
¢ ¡ ¢
2 − 2 ( − ) ( − )
| = 2 exp − (2.7)
2 2
µ ¶
( − )0 ( − )
∝ exp −
22
¡ ¢− 2
As 2 is assumed to be known in this example, we can drop the first term in equation 2.7 2 2
2. A BAYESIAN APPROACH TO ESTIM ATING A LINEAR REGRESSION M ODEL 5
0.9
0.95
0.8
0.9
B0
0.7
0.85
0.6
0.8
Probability
Probability
0.5
0.75
Prior~N(1,10)
Prior~N(1,2)
0.4
0.7
0.3
0.65
0.2
0.6
Σ 0.1
0
0.55
−10 −5 0 5 10 −10 −5 0 5 10
B B
Calculating the posterior distribution. Recall from equation 2.5 that the posterior distribution is propor-
tional to the likelihood times the prior. Therefore to find the posterior distribution for (conditional on knowing 2 )
the researcher multiplies equation 2.6 and 2.7 to obtain
µ ¶
¡ 2
¢ £ 0 −1 ¤ ( − )0 ( − )
| ∝ exp −05 ( − 0 ) Σ0 ( − 0 ) × exp − (2.8)
2 2
Equation 2.8 is simply a product of two normal distributions and the result is also a normal distribution. Hence the
posterior distribution of conditional on 2 is given by:
¡ ¢
| 2 ˜ ( ∗ ∗ ) (2.9)
As shown in Hamilton (1994) pp 354 and Koop (2003) pp 61 the mean and the variance of this normal distribution
are given by the following expressions
µ ¶−1 µ ¶
∗ −1 1 0 −1 1 0
= Σ0 + 2 Σ0 0 + 2 (2.10)
µ ¶−1
1 0
∗ = Σ−1 +
0
2
¡ ¢−1 ¡ −1 ¢
Consider the expression for the mean of the conditional posterior distribution ∗ = Σ−1 1 0
0 + 2 Σ0 0 + 12 0 .
−1
Note that the final term 0 can be re-written as 0 where = (0 ) 0 . That is
µ ¶−1 µ ¶
∗ −1 1 0 −1 1 0
= Σ0 + 2 Σ0 0 + 2 (2.11)
The second term of the expression in equation 2.11 shows that the mean of the conditional posterior distribution is
weighted average of the prior mean 0 and the maximum likelihood estimator with the weights given by the
reciprocal of the variances of the two ( in particular Σ−1 1 0
0 and 2 ). A large number for Σ0 would imply a very
∗
small weight on the prior and hence would be dominated by the OLS estimate. A very small number for Σ0 , on
the other hand, would imply that the conditional posterior mean is dominated by the prior. Note also that if the
prior is removed from the expressions in equation 2.10 (i.e. if one removes 0 and Σ−10 from the expressions) , one
is left with the maximum likelihood estimates.
Example 2. Figure 1 shows a simple example about a prior distribution for a regression model with 1 coefficient
The X-axis of these figures show a range of values of . The Y-axis plots the value of the normal prior distribution
associated with these values of . The left panel shows a a prior distribution with a mean of 1 and a variance of 10.
As expected, the prior distribution is centered at 1 and the width of the distribution reflects the variance. The right
panel compares this prior distribution with a tighter prior centered around the same mean. In particular, the new
prior distribution (shown as the red line) has a variance of 2 and is much more tightly concentrated around the mean.
6 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
−30 Likelihood function for Y =5+e ,e ~N(0,1) Posterior Distribution using Prior~N(1,2)
x 10 t t t
9 1
8 0.9
0.8
7
0.7
6
0.6
Probability
Probability
5
0.5
4
0.4
3
0.3
2
0.2
1 0.1
0 0
−10 −5 0 5 10 −10 −5 0 5 10
B B
0.9
0.8
0.7
0.6
Probability
Prior~N(1,2)
0.5 Prior~N(1,0.01)
Prior~N(1,1000)
0.4
0.3
0.2
0.1
0
−10 −5 0 5 10
B
Figure 2. The posterior distribution for the model = 5 + ˜ (0 1) using different priors
The tope left panel of figure 2 plots the likelihood function for the simple regression model = + ˜ (0 1) =
5. As expected the likelihood function has its peak at = 5 The top right panel shows the posterior distribution which
combines the prior distribution in figure 1 ( (1 2) shown as the red line) with the likelihood function. Note that as
the posterior combines the prior information (with a mean of 1) and the likelihood function, the posterior distribution
is not exactly centered around 5, but around a value slightly less than 5, reflecting the influence of the prior. Note
that if the prior is tightened significantly and its variance reduced to 001, this has the affect of shifting the posterior
distribution with the mass concentrated around 1 (red line in the bottom left panel). In contrast, a loose prior with a
prior variance of 1000 is concentrated around 5.
2.2. Case 2: The posterior distribution of 2 assuming is known. In the second example we consider
the estimation of 2 in equation 2.1 assuming that the value of is known. The derivation of the (conditional)
posterior distribution of 2 proceeds in exactly the same three steps
Setting the prior. The normal distribution allows for negative numbers and is therefore not appropriate as a
prior distribution for 2 A conjugate prior for 2 is the inverse Gamma distribution or equivalently a conjugate prior
for 1 2 is the Gamma distribution.
Definition 1. (Gamma Distribution): Suppose we have iid numbers from the normal distribution
µ ¶
1
˜ 0
P
If we calculate the sum of squares of = =1 2 , then is distributed as a Gamma distribution with degrees
of freedom and a scale parameter
µ ¶
˜Γ (2.12)
2 2
The probability density function for the Gamma distribution has a simple form and is given by
µ ¶
−1 −
( ) ∝ 2 exp (2.13)
2
where the mean of the distribution is defined as ( ) =
¡ ¢ ¡ ¢
Setting the prior (continued). We set a Gamma prior for 1 2 . That is 1 2 ∼ Γ 20 20 where 0
denotes the prior degrees of freedom and 0 denotes the prior scale parameter. As discussed below, the choice of
0 and 0 affects the mean and the variance of the prior. The prior density, therefore, has the following form (see
equation 2.13.)
0 µ ¶
1 2 −1 −0
exp (2.14)
2 22
2. A BAYESIAN APPROACH TO ESTIM ATING A LINEAR REGRESSION M ODEL 7
Inverse Gamma distribution for different scale parameters Inverse Gamma distribution for different degrees of freedom
0.7 4
IG(T =1,θ =1) IG(T =1,θ =1)
1 1 1 1
IG(T1=1,θ1=2) IG(T1=2,θ1=1)
IG(T =1,θ =4) IG(T =4,θ =1)
1 1 1 1
3.5
0.6
0.5
2.5
0.4
0.3
1.5
0.2
0.1
0.5
0 0
0 0.5 1 1.5 2 2.5 3 0 0.5 1 1.5 2 2.5 3
σ2 σ2
Figure 3. The inverse Gamma distribution for different degrees of freedom and scale parameters.
Setting up the likelihood function. In the second step, the researcher collects the data and forms the
likelihood function:
µ 0 ¶
¡ 2
¢ ¡ ¢
2 − 2 ( − ) ( − )
| = 2 exp − (2.15)
2 2
µ ¶
¡ ¢− 2 ( − )0 ( − )
∝ 2 exp −
2 2
As 2 is assumed to be unknown in this example, we cannot drop the entire first term in equation 2.15.
Calculating the posterior distribution. To calculate the posterior distribution of 1 2 (conditional on )
we multiply the prior distribution in equation 2.14 and the likelihood function 2.15 to obtain
µ ¶ 0 µ ¶ µ ¶
1 1 2 −1 −0 2− 2 1 0
| ∝ 2 exp × exp − 2 ( − ) ( − )
2 2 2 2
→
0 µ ¶
1 2 −1− 2 1 £ 0 ¤
exp − 2 0 + ( − ) ( − )
2 2
→
1 µ ¶
1 2 −1 1
exp − 2 (2.16)
2 2
The resulting conditional posterior distribution for 1 2 in equation 2.16 can immediately be recognised as a Gamma
0
distribution with degrees of freedom 1 = 02+ and 1 = 0 +( −2 ) ( − ) . Note that the conditional posterior
distribution for 2 is inverse Gamma with degrees of freedom 1 and scale parameter 1
Consider the mean of the conditional posterior distribution (given by 11 )
0 +
(2.17)
0 + ( − )0 ( − )
It is interesting to note that without the prior parameters 0 and 0 , equation 2.17 simply defines the reciprocal of
the maximum likelihood estimator of 2
Example 4. The left panel of figure 3 plots the inverse Gamma distribution with the degrees of freedom held fixed
at 1 = 1, but for scale parameter 1 = 1 2 4 Note that as the scale parameter increases, the distribution becomes
skewed to the right and the mean increases. This suggests that an inverse Gamma prior with a larger scale parameter
incorporates a prior belief of a larger value for 2 The right panel of the figure plots the inverse Gamma distribution
for 1 = 1, but for degrees of freedom 1 = 1 2 4. As the degrees of freedom increase, the inverse Gamma distribution
is more tightly centered around the mean. This suggests that a higher value for the degrees of freedom implies a tighter
set of prior beliefs.
8 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
2.3. Case 3: The posterior distribution of 2 and . We now turn to the empirically relevant case when
both the coefficient vector and the variance 1 2 (in equation 2.1) is unknown. We proceed in exactly the same
three steps
Setting the prior. We set the joint prior density for
µ ¶ µ ¶ µ ¶
1 1 1
2 = × | (2.18)
2 2
¡ ¢ ¡ ¢ ¡ ¢ ¡ ¢ 0
−1 ¡ 0¢
where | 12 ˜ (0 2 Σ0 ) and 12 ˜Γ 20 20 That is: 12 = 12 2 exp − as in section 2.2 and
h i 2 2
¡ 1¢ −2 ¯¯ 2 ¯− 12 ¡ 2 ¢−1
| 2 = (2) Σ0 ¯ exp −05 ( − 0 ) Σ0
0
( − 0 ) . Note that the prior for is set conditional
on 2 This prior is referred to as the natural conjugate prior for the linear regression model. A natural conjugate
prior is a conjugate prior which has the same functional form as the likelihood.
Setting up the likelihood function. As above, the likelihood function is given by
µ ¶
¡ 2
¢ ¡ ¢
2 − 2 ( − )0 ( − )
| = 2 exp − (2.19)
2 2
Calculating the posterior distribution. The joint posterior distribution of and the variance 1 2 is
obtained by combining 2.18 and 2.19
µ ¶ µ ¶
1 1 ¡ ¢
2
| ∝ 2
× | 2 (2.20)
Note that equation 2.20 is a joint posterior distribution involving 12 and . Its form is more complicated than
the conditional distributions for and 12 shown in sections 2.1 and 2.2. To proceed further in terms of inference,
the researcher has to ‘isolate’ the component of the posterior relevant to or 12 For example, to conduct inference
about , the researcher has to derive the marginal posterior distribution for Similarly, inference on 12 is based
on the marginal posterior distribution for 12 The marginal posterior for is defined as
Z∞ µ ¶
1 1
(| ) = | 2 (2.21)
2
0
1
while the marginal posterior for 2 is given by
µ ¶ Z∞ µ ¶
1 1
| = | (2.22)
2 2
0
In the case of this simple linear regression model under the natural conjugate prior, analytical results for these
integrals are available. As shown in Hamilton (1994) pp 357, the marginal posterior distribution for is a multi-
variate T distribution, while the marginal posterior for 12 is a Gamma distribution. An intuitive description of these
analytical results can also be found in Koop (2003) Chapter 2.
However, for the linear regression model with other prior distributions (for example where the prior for the
coefficients is set independently from the prior for the variance) analytical derivation of the joint posterior and then
the marginal posterior distribution is not possible. Similarly, in more complex models with a larger set of unknown
parameters (i.e. models that may be more useful for inference and forecasting) these analytical results may be difficult
to obtain. This may happen if the form of the joint posterior is unknown or is too complex for analytical integration.
Readers should pause at this point and reflect on two key messages from the three cases considered above:
Example 3.• As shown by Case 1 and Case 2, conditional posterior distributions are relatively easy to
derive and work with.
• In contrast, as shown by Case 3, derivation of the marginal posterior distribution (from a joint posterior
distribution) requires analytical integration which may prove difficult in complex models.
This need for analytical integration to calculate the marginal posterior distribution was the main stumbling block
of Bayesian analysis making it difficult for applied researchers.
We describe this algorithm in detail below, first in a general setting and then applied specifically to the linear
regression model. Most importantly, we then describe how to code the algorithm for linear regression models. Note
that all the files referred to below are saved in the sub-folder called chapter 1 in the main folder called code.
3.1. Gibbs Sampling a general description. Suppose we have a joint distribution of variables
(1 2 ) (3.1)
This may, for example, be a joint posterior distribution.
and we are interested in obtaining the marginal distributions
( ) = 1 (3.2)
The standard way to do this is to integrate the joint distribution in 3.1. However, as discussed above, this integration
may be difficult or infeasible in some cases. It may be that the exact form of 3.1 is unknown or is to complicated for
direct analytical integration.
Assume that the form of the conditional distributions ( | ) 6= is known. A Gibbs sampling algorithm with
the following steps can be used to approximate the marginal distributions.
(1) Set starting values for 1
01 0
where the superscript 0 denotes the starting values.
(2) Sample 11 from the distribution of 1 conditional on current values of 2
¡ ¢
11 |02 0
(3) Sample 12 from the distribution of 2 conditional on current values of 1 3
¡ ¢
12 |11 03 0
·
·
·
k. Sample 1 from the distribution of conditional on current values of 1 2 −1
¡ ¢
1 |11 12 1−1
to complete 1 iteration of the Gibbs sampling algorithm.
As the number of Gibbs iterations increases to infinity, the samples or draws from the conditional distributions
converge to the joint and marginal distributions of at an exponential rate (for a proof of convergence see Casella and
George (1992)). Therefore after a large enough number of iterations, the marginal distributions can be approximated
by the empirical distribution of
In other words, one repeats the Gibbs iterations times (ie a number of iterations large enough for convergence)
and saves the last draws of (for eg = 1000). This implies that the researcher is left with values for 1 .
The histogram for 1 (or any other estimate of the empirical density) is an approximation for the marginal
density of 1
Thus an estimate of the mean of the marginal posterior distribution for is simply the sample mean of the
retained draws
1 X
=1
where the superscript indexes the (retained) Gibbs iterations. Similarly, the estimate of the variance of the marginal
posterior distribution is given by
How many Gibbs iterations are required for convergence? We will deal with this question in detail in section
section 3.7 below.
One crucial thing to note is that the implementation of the Gibbs sampling algorithm requires the researcher to
know the form of the conditional distributions ( | ). In addition, it must be possible to take random draws from
these conditional distributions.
3.2. Gibbs Sampling for a linear regression. We now proceed to our first practical example involving a
linear regression model. We first describe the application of the Gibbs sampling algorithm to the regression. This is
followed immediately by a line by line description of Matlab code needed to implement the algorithm.
Consider the estimation of the following AR(2) model via Gibbs sampling
= + 1 −1 + 2 −2 + ˜ (0 2 ) (3.3)
where is annual CPI inflation for the US over the period 1948Q1 to 2010Q3. Let = {1 −1 1 −2 } denote
the RHS variables in equation 3.3 and = { 1 2 } the coefficient vector. Our aim is to approximate the marginal
posterior distribution of 1 2 and 2 As discussed above it is difficult to derive these marginal distributions
analytically. Note, however, that we readily derived the posterior distribution of = { 1 2 } conditional on 2
10 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
(see section 2.1) and the posterior distribution of 2 conditional on = { 1 2 } (see section 2.2) Estimation of
this model proceeds in the following steps
Step 1 Set priors and starting values. We set a normal prior for the coefficients
⎛ ⎞
⎛ 0 ⎞ ⎛ ⎞
⎜ 0 Σ 0 0 ⎟
()˜ ⎜ ⎝
⎝ 1 ⎠ ⎝ 0 Σ1 0 ⎠⎟ ⎠ (3.4)
20 0 0 Σ2
0 Σ0
In other words, we specify the prior means for each coefficient in (denoted as 0 in 3.4) and the prior
variance Σ0 For this example (with three coefficients) 0 is a 3 × 1 vector, while Σ0 is 3 × 3 matrix with
each diagonal element specifying the prior variance of the corresponding element of 0
We set an inverse Gamma prior for 2 and set the prior degrees of freedom 0 and the prior scale matrix 0 (see
equation 3.5). We will therefore work with the inverse Gamma distribution in the Gibbs sampler below. Note that
this is equivalent to working with Gamma distribution and 1 2
µ ¶
¡ 2 ¢ −1 0 0
˜Γ (3.5)
2 2
To initialise the Gibbs sampler we need a starting value for either 2 or . In this example we will assume that the
starting value for 2 = 2 where 2 is the OLS estimate of 2 In linear models (such as linear regressions and
Vector Autoregressions) the choice of starting values has, in our experience, little impact on the final results given
that the number of Gibbs iterations is large enough.
Step 2 Given a value for 2 we sample from the conditional posterior distribution of As discussed in section 2.1,
this is a normal distribution with a known mean and variance given
¡ ¢
| 2 ˜ ( ∗ ∗ ) (3.6)
where
µ ¶−1 µ ¶
∗ −1 1 0 −1 1 0
= Σ0 + 2 Σ0 0 + 2 (3.7)
(3×1)
µ ¶−1
1 0
∗ = Σ−1 +
(3×3)
0
2
Note that we have all the ingredients to calculate ∗ and ∗ which in this example are 3 × 1 and 3 × 3
matrices respectively. We now need a sample from the normal distribution with mean ∗ and variance ∗ .
For this we can use the following algorithm.
Algorithm 1. To sample a × 1vector denoted by from the ( ) distribution, first generate × 1 numbers
from the standard normal distribution (call these 0 . Note that all computer packages will provide a routine to do
this). These standard normal numbers can then be transformed such that the mean is equal to and variance equals
using the following transformation
= + 0 × 12
0
Thus one adds the mean and multiplies by the square root of the variance.
Step 2 (continued) The procedure in algorithm 1 suggests that once we have calculated ∗ and ∗ , the draw for is obtained
as " #0
1 = ∗ + ̄ × ( ∗ )12 (3.8)
(3×1) (3×1) (1×3) (3×3)
where ̄ is a 1 × 3 vector from the standard normal distribution. Note that the superscript 1 in 1 denotes
the first Gibbs iteration.
Step 3 Given the draw 1 , we draw 2 form its conditional posterior distribution. As shown in section 2.2 the
conditional posterior distribution for 2 is inverse Gamma
µ ¶
¡ 2 ¢ −1 1 1
| ˜Γ (3.9)
2 2
where
1 = 0 + (3.10)
¡ ¢0 ¡ ¢
1 = 0 + − 1 − 1
A crucial
¡ thing1 to ¢note ¢ posterior scale parameter of this distribution 1 is the fact that the second
about the
0¡ 1
term ( − − ) is calculated using the previous draw of the coefficient vector (in this case
1 ). To draw from the inverse Gamma distribution in equation 3.9 we first calculate the parameters in
¡ ¢1
equation 3.10 and then use the following algorithm to draw 2 from the inverse Gamma distribution
¡ ¢
(note that 2 denotes the Gibbs draw) .
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 11
Algorithm 2. To sample a scalar from the Inverse Gamma distribution with degrees of freedom 2 and scale
parameter
2 i.e. Γ
−1
( 2 2 ): Generate numbers form the standard normal distribution 0 ˜ (0 1) Then
=
00 0
is a draw from the Γ−1 ( 2
2 ) distribution.
¡ ¢1 ¡ ¢
Step 4 Repeat steps 2 and 3 times to obtain 1 and 2 2 . The last values of and 2 from
these iterations is used to form the empirical distribution of these parameters. Note that this empirical
distribution is an approximation to the marginal posterior distribution. Note also that the first −
iterations which are discarded are referred to as burn-in iterations. These are the number of iterations
required for the Gibbs sampler to converge.
Its worth noting that it makes no difference which order steps 2 and 3 are repeated. For example one could start
the Gibbs sampler by drawing 2 conditional on starting values for (rather than the other way around as we have
done here)
3.2.1. Inference using output from the Gibbs sampler. The Gibbs sampler applied to the linear regression model
produces a sequence of draws from the approximate marginal posterior distribution of and 2 The mean of these
draws is an approximation to the posterior mean and provides a point estimate of of and 2 The percentiles
calculated from these draws can be used to produce posterior density intervals. For example, the 5 and the 95
percentiles approximate the 10% highest posterior density intervals (HPDI) or 10% credible sets which can be used
for simple hypothesis testing. For example, if the highest posterior density interval for does not contain zero, this
is evidence that the hypothesis that = 0 can be rejected.
More formal methods for model comparison involve the marginal likelihood ( ) mentioned in section 2. The
marginal likelihood is defined as Z
¡ ¢ ¡ ¢
( ) = | 2 2 Ξ
where Ξ = 2 In other words, the marginal likelihood represents the posterior distribution with the parameters
integrated out. Consider two models 1 and 2 . Model 1 is preferred if 1 ( ) 2 ( ) or the Bayes factor
1 ( )
2 ( ) is larger than 1. In comparison to HPDIs, inference based on marginal likelihoods or Bayes factors is more
complicated from a computational and statistical point of view. First, while an analytical expression for ( ) is
available for the linear regression model under the natural conjugate prior, numerical methods are generally required
to calculate the integral in the expression for ( ) above. In the appendix to this chapter, we provide an example
of how Gibbs sampling can be used to compute the marginal likelihood for the linear regression model. Second,
model comparison using marginal likelihoods requires the researchers to use proper priors (i.e. prior distributions
that integrate to 1). In addition, using non-informative priors may lead to problems when interpreting Bayes Factors.
An excellent description of these issues can be found in Koop (2003) pp 38.
3.3. Gibbs Sampling for a linear regression in Matlab (example1.m). We now go through the Matlab
code needed for implementing the algorithm described in the section above. Note that the aim is to estimate the
following AR(2) model via Gibbs sampling.
= + 1 −1 + 2 −2 + ˜ (0 2 ) (3.11)
where is annual CPI inflation for the US over the period 1948Q1 to 2010Q3 and = { 1 2 }. The code
presented below is marked with comments for convenience. The same code without comments accompanies this
monograph and is arranged by chapter. The code for the example we consider in this section is called example1.m
and is saved in the folder
Consider the code for this example presented in 4 and 5. Line 2 of the code adds functions that are needed as
utilities—eg for taking lags or differences. We will not discuss these further. On line 5, we load data for US inflation
from an excel file. Line 7 creates the regressors, a constant and two lags of inflation (using the function lag0 in the
folder functions). Line 11 specifies the total number of time series observations after removing the missing values
generated after taking lags. Line 14 sets prior mean for the regression coefficients.
⎛ 0 ⎞ ⎛ ⎞
0
⎝ 10 ⎠ = ⎝ 0 ⎠
20 0
The prior mean for each coefficient is set to zero in this example. The prior variance is set to an identity matrix on
line 15 in this example. ⎛ ⎞ ⎛ ⎞
Σ 0 0 1 0 0
⎝ 0 Σ1 0 ⎠=⎝ 0 1 0 ⎠
0 0 Σ2 0 0 1
Line 17 sets the prior degrees of freedom for the inverse Gamma distribution while line sets 0 the prior scale
parameter. Line 20 sets the starting value of , while line 21 sets the starting value for 2 Line 22 specifies the total
12 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
number of Gibbs iterations, while line 23 specifies the number to discard (in this example we save 1000 iterations for
inference). out1 and out2 on line 24 and 25 are empty matrices that will save the draws of and 2 respectively. Line
26 starts the main loop that carries out the Gibbs iterations. On line 28, we begin the first step of the Gibbs algorithm
¡ ¢−1 ¡ −1 ¢
and calculate the mean of the conditional posterior distribution of ( ∗ = Σ−1 1 0
0 + 2 Σ0 0 + 12 0 )
and on line 29 we calculate the variance of this conditional posterior distribution. Line 32 draws from the normal
distribution with this mean and variance. Note that it is standard to restrict the draw of the AR coefficients to be
stable. This is why line 31 has a while loop which keeps on drawing from the coefficients from the normal distribution
if the draws are unstable. Stability is checked on line 33 by computing the eigenvalues of the coefficient matrix written
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 13
60
50
50
40
40
30
30
20
20
10 10
0 0
0 0.1 0.2 0.3 0.4 0.5 1.25 1.3 1.35 1.4 1.45 1.5 1.55
AR(2) coefficient σ2
50
60
45
40 50
35
40
30
25 30
20
15 20
10
10
5
0 0
−0.6 −0.55 −0.5 −0.45 −0.4 −0.35 0.5 0.55 0.6 0.65 0.7 0.75 0.8
in first order companion form. That is the AR(2) model is re-written as (this is the companion form)
µ ¶ µ ¶ µ ¶µ ¶ µ ¶
1 2 −1
= + +
−1 0 1 0 −2 0
µ ¶
1 2
Then the AR model is stable if the eigenvalues of are less than or equal to 1 in absolute value. Note
1 0
that this check for stability is not required for the Gibbs sampling algorithm but usually added by researchers for
practical convenience. Line 41 computes the residuals using the last draw of the coefficients. Line 43 computes the
posterior degrees of freedom
¡ for the
¢0 ¡inverse 1Gamma
¢ distribution 1 = 0 + Line 44 computes the posterior scale
1
parameter 1 = 0 + − − Line 46 to 48 draw from the inverse Gamma distribution using
algorithm 2. Lines 49 to 51 save the draws of and 2 once the number of iterations exceed the burn-in period.
Running this file produces the histograms shown in figure 6 (see lines 54 to 70 in example1.m—these histograms are
drawn using the retained draws in out1 and out2). These histograms are the Gibbs sampling estimate of the marginal
posterior distribution of the coefficients and the variance. Note that the mean of the posterior distribution is easily
calculated as the sample mean of these saved draws. Similarly, the sample standard deviation and percentiles provide
measures of uncertainty. Researchers usually report the posterior mean, the standard deviation and the 5th and 95th
percentiles of the posterior distribution. Example1.m produces the following moments for the coefficients (see table
1).
Note that the percentiles of the distribution are a useful measure of uncertainty. These represent HPDIs, or the
posterior belief that the parameter lies within a range (see Canova (2007) page 337 and Koop (2003) pp 43). Suppose
that the lower bound for was less than 0. Then this would indicate that one cannot exclude the possibility that
the posterior mean for is equal to zero.
3.4. Gibbs Sampling for a linear regression in Matlab and forecasting (example2.m). The file ex-
ample2.m considers the same model as in the previous subsection. However, we know use the AR model to forecast
inflation and build the distribution of the forecast. This example shows that one can easily obtain the distribution of
functions of the regression coefficients. Note that the forecast from an AR(2) model is easily obtained via simulation.
In other words, given a value for the current and lagged data and the regression coefficients, the 1 period ahead
forecast is
̂+1 = + 1 + 2 −1 + (∗ ) (3.12)
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 15
where ∗ is a scalar drawn from the standard normal distribution. Similarly, the 2 period ahead forecast is
̂+2 = + 1 ̂+1 + 2 + (∗ ) (3.13)
and so forth. Note that we incorporate future shock uncertainty by adding the term ∗ i.e. a draw from the normal
distribution with mean 0 and variance 2
The code shown in figures 7 and 8 is identical to example 1 until line 54. Once past the burn in stage, we not
only save the draws from the conditional distributions of the coefficients and the variance, but we use these draws to
compute a two year ahead forecast for inflation. Line 55 intialises an empty matrix yhat which will save the forecast.
16 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
Line 56 fills the first two values of yhat as actual values of inflation in the last two periods of the sample. Line 58
to 60 carries out the recursion shown in equations 3.12 and 3.13 for 12 periods. Line 62 saves actual inflation and
the forecast in a matrix out3. The crucial thing to note is that this done for each Gibbs iteration after the burn-in
period. Therefore in the end we have a set of 1000 forecasts. This represents an estimate of the posterior density.
On line 92 we calculate the percentiles of the 1000 forecasts. The result gives us a fan chart for the inflation forecast
shown in figure 9.
3.5. Gibbs Sampling for a linear regression with serial correlation. We now proceed to our second
main example involving the linear regression model. We illustrate the power of the Gibbs sampler by considering
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 17
−1
−2
2000 2002 2004 2006 2008 2010 2012
the model in 3.3 but allowing for first order serial correlation in the residuals. We first describe the application of
the Gibbs sampling algorithm to the regression. This is followed immediately by a line by line description of Matlab
code needed to implement the algorithm. This algorithm was first developed in Chib (1993).
Consider the estimation of the following AR(2) model via Gibbs sampling
where is annual CPI inflation for the US over the period 1948Q1 to 2010Q3. Let = {1 −1 1 −2 } denote
the RHS variables in equation 3.3 and = { 1 2 } the coefficient vector. Our aim is to approximate the marginal
posterior distribution of 1 2 and 2 and
The key to seeting up the Gibbs sampler for this model is to make the following two observations
• Suppose we knew the value of . Then the model in equation 3.14 can be transformed to remove the serial
correlation. In particular we can re-write the model as
That is we subtract the lag of each variable times the serial correlation coefficient . Note that the trans-
formed error term − −1 is serially uncorrelated. Therefore after this transformation we are back to the
linear regression framework we saw in the first example (see section 3.2). In other words, after removing
the serial correlation, the conditional distribution of the coefficients and of the error variance is exactly as
described for the standard linear regression model in section 3.2.
• Suppose we know 1 and 2 Then we can compute = − ( + 1 −1 + 2 −2 ) and treat the
equation = −1 + ∼ (0 2 ) as a linear regression model in . Again, this is just a standard
linear regression model with an iid error term and the standard formulas for the conditional distribution of
the regression coefficient and the error variance 2 applies.
These two observations clearly suggest that to estimate this model, the Gibbs sampler needs three steps (instead
of two in the previous example). We draw 1 and 2 conditional on knowing 2 and after transforming the
model to remove serial correlation (as in equation 3.15). Conditional on 1 and 2 and 2 we draw Finally,
conditional on 1 ,2 and we draw 2 The steps are as follows
Step 1 Set priors and starting values. We set a normal prior for the coefficients
⎛ ⎞
⎛ 0 ⎞ ⎛ ⎞
⎜ Σ 0 0 ⎟
()˜ ⎜ ⎝ 0 ⎠ ⎝ 0 Σ1
⎝ 10 0 ⎠⎟ ⎠ (3.16)
2 0 0 Σ2
0 Σ0
18 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
In other words, we specify the prior means for each coefficient in (denoted as 0 in 3.4) and the prior
variance Σ0 For this example (with three coefficients) 0 is a 3 × 1 vector, while Σ0 is 3 × 3 matrix with
each diagonal element specifying the prior variance of the corresponding element of 0
We set a normal prior for the serial correlation coefficient
¡ ¢
()˜ 0 Σ (3.17)
We set an inverse Gamma prior for 2 and set the prior degrees of freedom 0 and the prior scale matrix 0 (see
equation 3.18). µ ¶
¡ 2 ¢ −1 0 0
˜Γ (3.18)
2 2
To initialise the Gibbs sampler we need a starting value for 2 and . In this example we will assume that the starting
value for 2 = 2 where 2 is the OLS estimate of 2 We assume that the starting value for = 0
Step 2 Given a value for 2 and we sample from the conditional posterior distribution of As discussed above,
this is done by first transforming the dependent and independent variables in the model to remove serial
correlation. Once this is done we are back to the standard linear regression framework. We create the
following transformed variables
∗ = − −1
∗ = − −1
where ∗ represent the right hand side variables in our AR model. The conditional distribution of the
regression coefficients is then given as
¡ ¢
| 2 ˜ ( ∗ ∗ ) (3.19)
where
µ ¶−1 µ ¶
1 ∗0 ∗ 1 ∗0 ∗
∗ = Σ−1 + Σ−1
0 + (3.20)
(3×1)
0
2 0
2
µ ¶−1
1 ∗0
∗ = Σ−1
0 + 2 ∗
(3×3)
Note that the mean and variance in equation 3.20 is identical to the expressions in equation 3.7. We have
simply replaced the dependent and independent variables with our transformed data.
Step 3 Conditional on 2 and we sample from the conditional distribution of Given the previous draw of
we can calculate the model residuals = − ( + 1 −1 + 2 −2 ) and treat the equation =
−1 + ∼ (0 2 ) as an AR(1) model in Therefore, the conditional distribution for is simply a
normal distribution with the mean and variance derived in section 2.1. That is, the conditional distribution
is ¡ ¢
| 2 ˜ (∗ ∗ ) (3.21)
where
µ ¶−1 µ ¶
∗ −1 1 0 −1 0 1 0
= Σ + 2 Σ + 2 (3.22)
(1×1)
µ ¶−1
∗ −1 1 0
= Σ + 2
(1×1)
where = and = −1 With a value for ∗ and ∗ in hand, we simply draw from the normal
distribution with this mean and variance
" #
1 = ∗ + ̄ × ( ∗ )12
(1×1) (1×1) (1×1) (1×1)
¡ ¢1 ¡ ¢
Step 5 Repeat steps 2 and 4 times to obtain 1 , 1 and 2 2 . The last values of
and 2 from these iterations is used to form the empirical distribution of these parameters. This example
shows that we reduce a relatively complicated model into three steps, each of which are simple and based
on the linear regression framework. As seen in later chapters, Gibbs sampling will operate in exactly the
same way in more complicated models—i.e. by breaking the problem down into smaller simpler steps.
3.6. Gibbs Sampling for a linear regression with serial correlation in Matlab (example3.m). The
matlab code for this example is a simple extension of example1.m and shown in figures 10, 11 and 12. Note that the
20 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
underlying data is exactly as before. This is loaded and lags etc created using the commands from lines 5 to 11. Lines
14 and 15 set the prior mean and variance for for lines 17 and lines 18 sets the prior scale parameter and degrees
2
of freedom for the
¡ 0 inverse
¢ Gamma prior for Lines 20 and 21 set the mean and variance for the normal prior for
, i.e. ()˜ Σ Lines 23 to 25 set starting values for the parameters. The first step of the Gibbs sampling
algorithm starts on line 34 and 35 where we create ∗ = − −1 ∗ = − −1 , the data transformed to
remove serial correlation. Lines 38 and 39 calculate the mean and the variance of the conditional distribution of
using this tranformed data. As in the previous example, lines 40 to 48 draw from its conditional distribution, but
ensure that the draw is stable. Line 50 calculates the (serially correlated) residuals = − ( + 1 −1 + 2 −2 )
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 21
using the previous draw of 1 and 2 and lines 50 and 51 create = and = −1 . Line 54 calculates the
¡ 1 0
¢−1 ¡ −1 0 ¢
mean of the conditional distribution of ∗ = Σ−1 + 2 Σ + 12 0 while line 55 calculates the
(1×1)
¡ 1 0
¢−1
variance of the conditional distribution ∗ = Σ−1 + 2 Line 59 draws from the normal distribution
(1×1)
" #
12
using 1 = ∗ + ̄ × ( ∗ ) and the while loop ensures that is less than or equal to 1 in absolute value.
(1×1) (1×1) (1×1) (1×1)
Line 67 calculates the serially uncorrelated residuals ∗ − 1 ∗ . These are used on lines 69 to 74 to draw 2 from
22 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
−1
−2
2000 2002 2004 2006 2008 2010 2012
α B
1
2
1.6
1.5 1.4
1.2
1
1
0.5 0.8
0.6
0
0.4
50 100 150 200 250 300 350 400 450 50 100 150 200 250 300 350 400 450
Gibbs iterations Gibbs iterations
B ρ
2
0.2
0.8
0
0.6
−0.2 0.4
0.2
−0.4
0
−0.6
−0.2
50 100 150 200 250 300 350 400 450 50 100 150 200 250 300 350 400 450
Gibbs iterations Gibbs iterations
σ
0.8
0.75
0.7
0.65
0.6
0.55
0.5
Figure 14. Sequence of retained Gibbs draws for the AR(2) model with serial correlation using 500 iterations
the inverse Gamma distribution. After the burn-in stage, the code computes the forecast from this AR(2) model with
serial correlation. Line 82 projects forward the equation for the error term i.e. + = +−1 + ∗ where ∗ is a
standard normal shock. Line 83 calculates the projected value of inflation given + This is done for each retained
draw of the Gibbs sampler with the results (along with actual data) stored in the matrix out1 (line 87). The resulting
distribution of the forecast is seen in 13.
3.7. Convergence of the Gibbs sampler. A question we have ignored so far is: How many draws of the
Gibbs sampling algorithm do we need before we can be confident that the draws from the conditional posterior
distributions have converged to the marginal posterior distribution? Generally researchers proceed in two steps
• Choose a minimum number of draws and run the Gibbs sampler
• Check if the algorithm has converged (using the procedures introduced below). If there is insufficient
evidence for convergence, increase and try again.
3. GIBBS SAM PLING FOR THE LINEAR REGRESSION M ODEL 23
α B
1
1.2 1.5
0.8
1
0.6
0.4
0.5
5 10 15 20 5 10 15 20
Gibbs iterations Gibbs iterations
B ρ
2
0.1 0.8
0
0.6
−0.1
−0.2 0.4
−0.3
0.2
−0.4
0
−0.5
−0.6
5 10 15 20 5 10 15 20
Gibbs iterations Gibbs iterations
σ
0.63
0.62
0.61
0.6
0.59
0.58
5 10 15 20
Gibbs iterations
Figure 15. Recursive means of the retained Gibbs draws for the AR(2) model with serial correlation
using 500 iterations
α B1
1 1
Sample Autocorrelation
Sample Autocorrelation
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 5 10 15 20 0 5 10 15 20
Lag Lag
B ρ
2
1 1
Sample Autocorrelation
Sample Autocorrelation
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 5 10 15 20 0 5 10 15 20
Lag Lag
σ
1
Sample Autocorrelation
0.8
0.6
0.4
0.2
0 5 10 15 20
Gibbs iterations
Figure 16. Autocorrelation of the retained Gibbs draws for the AR(2) model with serial correlation
using 500 iterations
The simplest way to check convergence is to examine the sequence of retained draws. If the Gibbs sampler has
converged to the target distibution, then the retained draws should fluctuate randomly around a stationary mean
and not display any trend. This visual inspection is usually easier if one plots the recursive mean of the retained
draws. If the Gibbs sampler has converged, then the recursive mean should show little fluctuation. A related method
to examine convergence is plot the autocorrelation of the retained draws. If convergence has occurred, the sequence
of draws should display little autocorrelation (i.e. they should be fluctuating randomly around a stationary mean).
In order to illustrate these ideas, we plot the sequence of retained draws, the recursive means of those draws
and the autocorrelation functions of the retained draws for the parameters of the model examined in section 3.6. In
particular, we estimate the AR(2) model with serial correlation using 500 Gibbs iterations (using the file example3.m)
and retain all of these draws. Figures 14, 15 and 16 examine the convergence of the model. Figures 14 and 15 clearly
show that the Gibbs draws are not stationary with the recursive mean for 1 2 and showing a large change
24 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
α B1
2 1.1
1
1.5
0.9
1 0.8
0.7
0.5
0.6
0 0.5
0.4
−0.5
100 200 300 400 500 600 700 800 900 1000 100 200 300 400 500 600 700 800 900 1000
Gibbs iterations Gibbs iterations
B ρ
2
0.3
0.9
0.2
0.8
0.1 0.7
0.6
0
0.5
−0.1
0.4
−0.2 0.3
100 200 300 400 500 600 700 800 900 1000 100 200 300 400 500 600 700 800 900 1000
Gibbs iterations Gibbs iterations
0.75
0.7
0.65
0.6
0.55
0.5
0.45
100 200 300 400 500 600 700 800 900 1000
Gibbs iterations
Figure 17. Sequence of retained Gibbs draws for the AR(2) model with serial correlation using
25000 iterations
α B1
1.2 0.8
0.75
1
0.7
0.8 0.65
0.6
0.6
0.55
0.5
5 10 15 20 25 30 35 40 45 5 10 15 20 25 30 35 40 45
Gibbs iterations Gibbs iterations
B ρ
2
0.85
0.12
0.8
0.1
0.75
0.08
0.7
0.06
0.65
0.04
0.6
0.02
0.55
5 10 15 20 25 30 35 40 45 5 10 15 20 25 30 35 40 45
Gibbs iterations Gibbs iterations
0.61
0.6
0.59
0.58
0.57
5 10 15 20 25 30 35 40 45
Gibbs iterations
Figure 18. Recursive means of retained Gibbs draws for the AR(2) model with serial correlation
using 25000 iterations
after 300 iterations (but 2 appears to have converged with the draws fluctuating around a stationary mean). This
also shows up in the autocorrelation functions, with the autocorrelation high for 1 2 and These figures can
be produced using the file example4.m. These results would indicate that a higher number of Gibbs iterations are
required. Figures 17, 18 and 19 plot the same objects when 25000 Gibbs iterations are used (with 24000 as the
number of burn-in iterations). The sequence of retained draws and the recursive means appear substantially more
stable. The autocorrelations for 1 2 and decay much faster in figure 19.
These graphical methods to assess convergence are widely used in applied work. A more formal test of convergence
has been proposed by Geweke (1991). The intuition behind this test is related to the idea behind the recursive mean
plot: If the Gibbs sampler has converged then the mean over different sub-samples of the retained draws should be
similar. Geweke (1991) suggests the following procedure:
4. FURTHER READING 25
α B
1
1 1
Sample Autocorrelation
Sample Autocorrelation
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 5 10 15 20 0 5 10 15 20
Lag Lag
B ρ
2
1 1
Sample Autocorrelation
Sample Autocorrelation
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 5 10 15 20 0 5 10 15 20
Lag Lag
σ
1
Sample Autocorrelation
0.8
0.6
0.4
0.2
0
0 5 10 15 20
Gibbs iterations
Figure 19. Autocorrelation of retained Gibbs draws for the AR(2) model with serial correlation
using 25000 iterations
(1) Divide the retained Gibbs draws of the model parameters into two subsamples 1 2 where Geweke
(1991) recommends 1 = 01 2 = 05 where N denotes the total number of retained draws.
P1 P
(2) Compute averages 1 = =1 1 and 2 = =2 +1 2
(3) Compute the asymptotic variance 1 (0)
1
and
2 (0)
2
where () is the spectral density at frequency
Note that this estimate of the variance takes into account the possibility that the Gibbs sequence may be
autocorrelated. For a description of spectral analysis see Hamilton (1994) and Canova (2007).
(4) Then the test statistic
1 − 2
=q (3.25)
1 (0) 2 (0)
1 + 2
is asymptotically distributed as (0 1). Large values of this test statistic indicate a significant difference
in the mean across the retained draws and suggests that one should increase the number of initial Gibbs
iterations (i.e. increase the number of burn-in draws).
Geweke (1991) suggests a related statistic to judge the efficiency of the Gibbs sampler and to gauge the total
number of Gibbs iterations to be used. The intuition behind this measure of relative numerical efficiency (RNE) is
as follows. Suppose one could takeP iid draws of ∈ {1 2 } directly from the posterior. Then the variance of
the posterior mean ( ) = 1 is given by
1 1 1
( ( )) = 2
(1 ) + 2 (2 ) + 2 ( )
= ( )
However, in practice one uses the Gibbs sampler to approximate draws from the posterior. These Gibbs draws are
likely to be autocorrelated and a measure of their variance which takes this into account is (0) . Thus a measure
of the RNE is
\
( )
= (3.26)
(0)
where \ ( ) is the sample variance of the Gibbs draws 1 2 . If the Gibbs sampler has converged then
should be close to 1 as the variance of the iid draws \ ( ) should be similar to the measure of the variance
that takes any possible autocorrelation into account.
The file example5.m illustrates the calculation of the statistics in equation 3.25 and 3.26.
4. Further Reading
• An intuitive description of the Gibbs sampling algorithm for the linear regression model can be found in
Kim and Nelson (1999) Chapter 7. Gauss codes for the examples in Kim and Nelson (1999) are available
at http://econ.korea.ac.kr/~cjkim/SSMARKOV.htm.
26 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
• A more formal treatment of the linear regression model from a Bayesian perspective can be found in Koop
(2003), Chapters 2, 3 and 4.
• The appendix in Zellner (1971) provides a detailed description of the Inverse Gamma and Gamma distrib-
utions. See Bauwens et al. (1999) for a detailed description of algorithms to draw from these distributions.
5. Appendix: Calculating the marginal likelihood for the linear regression model using the Gibbs
sampler.
Consider the following linear regression model
= + ˜ (0 2 )
The prior distributions are assumed to be
()˜ (0 Σ0 )
¡ ¢
2 ˜(0 0 )
The posterior distribution of the model parameters Φ = 2 is defined via the Bayes rule
( |Φ) × (Φ)
(Φ| ) = (5.1)
( )
¡ ¢− ¡ ¢
where ( |Φ) = 2 2 2 exp − 21 2 ( − )0 ( − ) is the likelihood function, (Φ) is the joint prior
distribution while ( ) is the marginal likelihood that we want to compute. Chib (1995) suggests computing the
marginal likelihood by re-arranging equation 5.1. Note that in logs we can re-write equation 5.1 as
ln ( ) = ln ( |Φ) + ln (Φ) − ln (Φ| ) (5.2)
Note that equation 5.2 can be evaluated at any value of the parameters Φ to calculate ln ( ). In practice a
high density point Φ∗ such as the posterior mean or posterior mode is used.
The first two terms on the right hand side of equation 9.3 are easy to evaluate at Φ∗ The first term is the log
likelihood function. The second term is the joint prior which is the product of a normal density for the coefficients
and an inverse Gamma density for the variance (see example below). Evaluating the third term ln (Φ∗ | ) is more
complicated as the posterior distribution is generally not known in closed form. Chib (1995) shows how this term
can be evaluated using the output from
¡ the ¢Gibbs sampling algorithm used to approximate the posterior distribution
for Φ Recall that (Φ∗ | ) = ∗ 2∗ where have dropped the conditioning on y on the right hand side for
simplicity. The marginal, conditional decomposition of this distribution is
¡ ¢ ¡ ¢ ¡ ¢
∗ 2∗ = ∗ | 2∗ × 2∗ (5.3)
¡ ∗ 2∗ ¢
The first term | is the conditional posterior distribution for the regression coefficients. Recall that this a
normal distribution with mean and variance given by
µ ¶−1 µ ¶
∗ −1 1 0 −1 1 0
= Σ0 + 2∗ Σ0 0 + 2∗
µ ¶−1
1
∗ = Σ−1 0
0 + 2∗
and therefore can be easily evaluated at¡ ∗ ¢and 2∗
The second term in equation 5.3 2∗ can be evaluated using the weak law of large numbers (see Koop (2003)
Appendix B). That is
¡ ¢ 1 X ¡ 2∗ ¢
2∗ ≈ |
=1
where denotes = 1 2 draws of the Gibbs sampler. Note that the conditional distribution is simply the
Inverse Gamma distribution derived for section 2.2 above.
The marginal likelihood is then given by
¡ ¢ ¡ ¢
ln ( ) = ln ( |Φ) + ln (Φ) − ln ∗ | 2∗ − ln 2∗ (5.4)
As an example we consider the following linear regression model based on 100 artificial observations
= 1 + 05 + ( ) = 02
µµ ¶ µ ¶¶
0 1 0
where ˜ (0 1) We assume a natural conjugate prior of the form (| 2 )˜ 4 2 and
¡ 2¢ 0 0 1
˜(25 3)
The matlab code for this example is shown in figures 20 and 21. The code on Lines 5 to 9 generates the artificial
data. We set the priors on lines 11 to 14. On line 16 we calculate the marginal likelihood for this model analytically
using the formula on page 41 in Koop (2003). We can now compare this estimate with the estimate produced
using Chib’s method. The Gibbs sampler used to estimate the model is coded on lines 19 to 43. Line 46 calculates
5. APPENDIX: CALCULATING THE M ARGINAL LIKELIHOOD FOR THE LINEAR REGRESSION M ODEL USING THE GIBBS SAM PLER.
27
the posterior mean of the coefficients, line 47 calculates the posterior mean of the variance while line 48 calculates
the posterior mean of 1 2 . For computational convenience, when considering the prior ln (Φ) and the posterior
distribution ln (Φ| ) in the expression for the marginal likelihood (see equation 5.2) we consider the precision 1 2
and use the Gamma distribution. This allows us to use built in matlab functions to evaluate the Gamma PDF.
On line 51, we evaluate the log of the prior distribution of the VAR coefficients (| 2 ) at the posterior mean.
Line 53 evaluates the Gamma posterior for the precision. The function gampdf1 converts the two parameters of
the distribution: the degrees of freedom 0 and scale parameter 0 into the parameters = 0 2 and = 20
as expected by the parameterisation of the Gamma distribution used by Matlab in its built in function gampdf.
28 1. GIBBS SAM PLING FOR LINEAR REGRESSION M ODELS
Figure 21. Matlab code for calculating the marginal likelihood continued
¡ ¢
Line 55 evaluates the log likelihood
¡ ∗ at 2∗the
¢ posterior
¡ ∗ mean.
¢ Lines
¡ 56
¢ to 61 evaluate the term ∗ | 2∗¡ in the
¢
factorisation of the posterior 1
¡ = ¢ | 2∗ × 1 2∗ . Lines 63 to 69 evaluate the term 1 2∗ .
Each iteration in the loop evaluates 1 2∗ | Note that this is simply the Gamma distribution with degrees of
freedom 0 + and scale parameter 0 + 0 where the residuals are calculated using each Gibbs draw of the
regression coefficients .0 and 0 denote the prior degrees of freedom and prior scale parameter respectively. Line
¡ ¢ P ¡ ¢
73 constructs 1 2∗ ≈ 1 =1 12∗ | . The marginal likelihood is calculated using equation 5.2 on line
75 of the code.
CHAPTER 2
This chapter introduces Bayesian simulation methods for Vector Autoregressions (VARs). The estimation of
these models typically involves a large number of parameters. As a consequence, estimates of objects of interest
such as impulse response functions and forecasts can become imprecise in large scale models. By incorporating prior
information into the estimation process, the estimates obtained using Bayesian methods are generally more precise
than those obtained using the standard classical approach. In addition, bayesian simulation methods such as Gibbs
sampling provide an efficient way not only to obtain point estimates but also to characterise the uncertainty around
those point estimates. Therefore we focus on estimation of VARs via Gibbs sampling in this chapter.
Note, however, that under certain prior distributions, analytical expressions exist for the marginal posterior
distribution of the VAR parameters. A more general treatment of Bayesian VARs can be found in Canova (2007)
amongst others. See http://apps.eui.eu/Personal/Canova/Courses.html for F.Canova’s BVAR code.
This chapter focusses on two key issues
• It states the conditional posterior distributions of the VAR parameters required for Gibbs sampling and
discussed the Gibbs sampling algorithm for VARs
• We go through the practical details of setting different type of priors for VAR parameters
• We focus on implementation of Gibbs sampling for VARs in Matlab.
• We discuss how to estimate structural VARs with sign restrictions using Matlab.
1. The Conditional posterior distribution of the VAR parameters and the Gibbs sampling algorithm
Consider the following VAR(p) model
= + 1 −1 + 2 −2 − + (1.1)
(0 ) = Σ if =
(0 ) = 0 if 6=
( ) = 0
where is a × matrix of endogenous variables, denotes a constant term. The VAR can be written compactly
as
= + (1.2)
with = { −1 −2 − }Note that as each equation in the VAR has identical regressors, it can be re-
written as
= ( ⊗ ) + (1.3)
where = ( ) and = () and = ( ).
Assume that the prior for the VAR coefficients is normal and given by
³ ´
()˜ ̃0 (1.4)
where ̃0 is a ( × ( × + 1)) × 1 vector which denotes the prior mean while is a is a [ × ( × + 1)] ×
[ × ( × + 1)] matrix where the diagonal elements denote the variance of the prior. We discuss different ways
of setting ̃0 and in detail below.
It can be shown that the posterior distribution of the VAR coefficients conditional on Σ is normal (see Kadiyala
and Karlsson (1997)) . That is the conditional posterior for the coefficients is given by (|Σ ) ˜ ( ∗ ∗ ) where
¡ ¢−1 ³ −1 ´
∗ = −1 + Σ−1 ⊗ 0 ̃0 + Σ−1 ⊗ 0 ̂ (1.5)
¡ ¢−1
∗ = −1 + Σ−1 ⊗ 0
where ̂ is a ( ³× ( × + 1)) ×´1 vector which denotes the OLS estimates of the VAR coefficients in vectorised
−1
format ̂ = (0 ) (0 ) . The format of the conditional posterior mean in equation 1.5 is very similar
to that discussed for the linear regression model (see section 2.1 in the previous chapter). That is the mean of the
conditional posterior distribution is a weighted average of the OLS estimator ̂ and the prior ̃0 with the weights
given by the inverse of the variance of each (Σ−1 ⊗ 0 is the inverse of ̂ while −1 is the inverse of the variance
of the prior).
29
30 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
The conjugate prior for the VAR covariance matrix is an inverse Wishart distribution with prior scale matrix ̄
and prior degrees of freedom
¡ ¢
(Σ)˜ ̄ (1.6)
Definition 3. If Σ is a × positive definite matrix, it is distributed as an inverse Wishart with the following
Y
||2 ¡ ¢
density (Σ) = |Σ|(++1)2 exp −05Σ−1 where −1 = 22 (−1)4 Γ [( + 1 − ) 2], is the scale
=1
matrix and denotes the degrees of freedom. See Zellner (1971) pp395 for more details.
Informally, one can think of the inverse Wishart distribution as a multivariate version of the inverse Gamma
distribution introduced in the context of the linear regression model in the previous chapter.
¡ Given
¢ the prior in
equation 1.6, the posterior for Σ conditional on is also inverse Wishart (Σ| ) ˜ Σ̄ + where is the
sample size and
0
Σ̄ = ̄ + ( − ) ( − ) (1.7)
Note that denotes the VAR coefficients reshaped into ( × + 1) by matrix.
1.1. Gibbs sampling algorithm for the VAR model. The Gibbs sampling algorithm for the VAR model
consists of the following steps:
Step 1 Set priors for the VAR coefficients and the³covariance
´ matrix. As discussed above, the prior for the VAR
coefficients is normal and given by ()˜ ̃0 . The prior for the covariance matrix of the residuals Σ
¡ ¢
is inverse Wishart and given by ̄ Set a starting value for Σ (e.g. the OLS estimate of Σ).
Step 2 Sample the VAR coefficients from its conditional posterior distribution (|Σ ) ˜ ( ∗ ∗ ) where
¡ ¢−1 ³ −1 ´
∗ = −1 + Σ−1 ⊗ 0 ̃0 + Σ−1 ⊗ 0 ̂ (1.8)
(×( × +1))×1
¡ ¢−1
∗ = −1 + Σ−1 ⊗ 0 (1.9)
( ×(× +1))×( ×( × +1))
Once ∗ and ∗ are calculated, the VAR coefficients are drawn from the normal distribution (see algorithm 1 in
Chapter 1)
" #
1 = ∗ + ̄ × ( ∗ )12 (1.10)
(( ×( × +1))×1) (( ×( × +1))×1) (1×(×( × +1))) ( ×(× +1))×( ×( × +1))
¡ ¢ ¡ ¢0 ¡ ¢
Step 3 Draw Σ from its conditional distribution (Σ| ) ˜ Σ̄ + where Σ̄ = ̄+ − 1 − 1
where 1 is the previous draw of the VAR coefficients reshaped into a matrix with dimensions ( × +1)×
so it is conformable with
Algorithm 3. To draw a matrix Σ̂ from the distribution with v degrees of freedom and scale parameter
draw a matrix with dimensions × , from the multivariate normal (0 −1 ) Then the draw from the inverse
Wishart distribution is given by the following transformation:
à !−1
X
Σ̂ = 0
=1
¡ ¢0 ¡ ¢
Step 3 (continued) With the parameters of inverse Wishart distribution in hand (Σ̄ = ̄ + − 1 − 1 and
+ ) one can use algorithm 3 to draw Σ from the inverse Wishart distribution.
Repeat Steps 2 to 3 times to obtain 1 and (Σ)1 (Σ) . The last values of and Σ from these iterations is used
to form the empirical distribution of these parameters. Note that the draws of the model parameters (after
the burn-in period) are typically used to calculate forecasts or impulse response functions and build the
distribution for these statistics of interest.
In general, the Gibbs sampling algorithm for VARs is very similar to that employed for the linear regression
model in the previous chapter. The key difference turns out to be the fact that setting up the prior in the VAR model
is a more structured process than the linear regression case.
We now turn to a few key prior distributions for VARs that have been proposed in the literature and the
implementation of the Gibbs sampling algorithm in Matlab. To discuss the form of the priors we will use the
following bi-variate VAR(2) model as an example:
µ µ ¶
¶ µ ¶µ ¶ µ ¶µ ¶ µ ¶
1 11 12 −1 11 12 −2 1
= + + + (1.11)
2 21 22 −1 21 22 −2 2
µ ¶ µ ¶
1 Σ11 Σ12
where =Σ=
2 Σ12 Σ22
2. THE M INNESOTA PRIOR 31
Equation 2.1 states that the Minnesota prior incorporates the belief that both and follow an AR(1) process or
a random walk if 011 = 022 = 1. If and are stationary variables then it may be more realistic to incorporate
the prior that they follow an AR(1) process.
³ ´For this example, the mean of the Minnesota prior distribution for the
VAR coefficients (i.e. ̃0 from ()˜ ̃0 ) is given by the vector
⎛ ⎞
0
⎜ 011 ⎟
⎜ ⎟
⎜ 0 ⎟
⎜ ⎟
⎜ 0 ⎟
⎜ ⎟
⎜ 0 ⎟
̃0 = ⎜
⎜
⎟
⎟ (2.2)
⎜ 0 ⎟
⎜ 0 ⎟
⎜ ⎟
⎜ 022 ⎟
⎜ ⎟
⎝ 0 ⎠
0
where the first five rows correspond to the coefficients for the first equation and the second five rows correspond to the
coefficients for the second equation. The variance of the prior is a set in a more structured manner (as compared
to the examples in chapter 1) and is given by the following relations for the VAR coefficients
µ ¶2
1
= (2.3)
3
µ ¶2
1 2
6=
3
where refers to the dependent variable in the equation and to the independent variables in that equation.
Therefore, if = then we are referring to the coefficients on the own lags of variable . and are variances
of error terms from AR regressions estimated via OLS using the variables in the VAR. The ratio of and in
the formulas above controls for the possibility that variable and may have different scales. Note that is the lag
length. The 0 are parameters set by the researcher that control the tightness of the prior:
• 1 controls the standard deviation of the prior on own lags. As 1 → 0 11 22 → 011 022 respectively and
all other lags go to zero in our example VAR in equation 1.11.
• 2 controls the standard deviation of the prior on lags of variables other than the dependent variable i.e.
12 21 etc. As 2 → 0 go to zero. With 2 = 1 there is no distinction between lags of the dependent
variable and other variables.
• 3 controls the degree to which coefficients on lags higher than 1 are likely to be zero. As 3 increases
coefficients on higher lags are shrunk to zero more tightly.
• The prior variance on the constant is controlled by 4 As 4 → 0 the constant terms are shrunk to zero.
It is instructive to look at how the prior variance matrix looks for our example VAR(2) in equation 1.11. This is
shown below in equation 2.4
32 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
⎛ 2
⎞
( 1 4 ) 0 0 0 0 0 0 0 0 0
⎜ 0 (1 )
2
0 0 0 0 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ ⎟
⎜ 0 0 1 1 2
0 0 0 0 0 0 0 ⎟
⎜ 2 ⎟
⎜ ¡ ¢ ⎟
⎜ 1 2 ⎟
⎜ 0 0 0 23
0 0 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ 0 0 0 0 1 1 2
0 0 0 0 0 ⎟
⎜ 2 23 ⎟
=⎜ 2 ⎟
⎜ 0 0 0 0 0 ( 2 4 ) 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ 2 1 2 ⎟
⎜ 0 0 0 0 0 0 1 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 0 0 0 0 0 (1 ) 2
0 0 ⎟
⎜ ³ ´2 ⎟
⎜ ⎟
⎜ 0 0 0 0 0 0 0 0 2 1 2
0 ⎟
⎝ 1 23 ⎠
¡ ¢
1 2
0 0 0 0 0 0 0 0 0 23
(2.4)
The matrix in equation 2.4 is a 10 × 10 matrix, because for this example we have 10 total coefficients in
the VAR model. The diagonal elements of the matrix are the prior variances for each corresponding coefficient.
Consider the the first five elements on the main diagonal correspond to the first equation the VAR model and is
re-produced in equation 2.5.
⎛ 2 ⎞
( 1 4 ) 0 0 0 0
⎜ 0 (1 )
2
0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ ⎟
⎜ 0 0 1 1 2
0 0 ⎟
⎜ 2 ⎟ (2.5)
⎜ ¡ 1 ¢2 ⎟
⎜ 0 0 0 0 ⎟
⎝ 23 ³ ´2 ⎠
1 1 2
0 0 0 0
2 2 3
The first diagonal element ( 1 4 )2 controls the prior on the constant term. The second element (1 )2 controls the
prior on 11 the coefficient on the first lag of Note that this element comes from the first expression in equation
¡ ¢2
2.3 13 with the lag length = 1 as we are dealing with the first lag. The third diagonal element controls the
prior on 12 the coefficient on the first lag of in the equation for Note that this element comes from the second
³ ´2
expression in equation 2.3 i.e. 132 with = 1. The third and the fourth diagonal elements control the prior on
the coefficients 11 and 12 respectively (and again come from the first and second expression in equation 2.3 with
= 2).
Under a strict interpretation of the Minnesota prior, the covariance matrix of the residuals of the VAR Σ is
assumed to be diagonal with the diagonal entries fixed using the error variances from AR regressions . Under
this assumption, the mean of the posterior distribution for the coefficients is available in closed form. For the exact
formula, see Kadiyala and Karlsson (1997) Table 1. However, it is common practice amongst some researchers to
incorporate the Minnesota prior into the Gibbs sampling framework and draw Σ from the inverse Wishart distribution.
We turn to the practical implementation of this algorithm next.
An important question concerns the values of the hyperparameters that control the priors. Canova (2007) pp
380 reports the following values for these parameters typically used in the literature.
1 = 02
2 = 05
3 = 1 2
4 = 105
Some researchers set the value of these parameters by comparing forecast performance of the VAR across a range of
values for these parameters. In addition, the marginal likelihood can be used to select the value of these hyperpa-
rameters. The appendix to this chapter shows how to use the procedure in Chib (1995) to calculate the marginal
likelihood for a VAR model.
2.1. Gibbs sampling and the Minnesota prior. Matlab code. We consider the estimation of a bi-variate
VAR(2) model using quarterly data on annual GDP growth and CPI inflation for the US from 1948Q2 to 2010Q4. We
employ a Minnesota prior which incorporates the belief that both variables follow a random walk. Note that while
annual CPI inflation may be non-stationary (and hence the random walk prior reasonable), annual GDP growth
is likely to be less persistent. Hence one may want to consider incorporating the belief that this variable follows
an AR(1) process in actual applications. Note that, we also incorporate a inverse Wishart prior for the covariance
matrix and hence depart from the strict form of this model where the covariance matrix is fixed and diagonal. The
2. THE M INNESOTA PRIOR 33
model is estimated using the Gibbs sampling algorithm described in section 1.1. The code for this model is in the
file example1.m in the subfolder chapter 2 under the folder code. The code is also shown in figures 1, 2 and 3. We
now go through this code line by line.
Line 5 of the code loads the data for the two variables from an excel file and lines 8 and 9 prepare the matrices
Lines 16 to 24 compute 1 and 2 (to be used to form the Minnesota prior) using AR(1) regressions for each
variable. In this example we use the full sample to compute these regressions. Some researchers use a pre-sample (or
a training sample) to compute 1 and 2 and then estimate the VAR on the remaining data points. The argument
for using a pre-sample is that the full sample should not really be used to set parameters that affect the prior.
34 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Lines 27 to 29 specify the parameters 1 ,2 3 ,4 that control the tightness of the prior and are used to build the
prior covariance. Line 33 specifies ̃0 the prior mean. As mentioned above in this example we simply assume a prior
mean of 1 for the coefficients on own first lags. In practice, this choice should depend on the stationarity properties of
the series. Line 35 forms the 10×10 prior variance matrix . Lines 37 to 47 fill the diagonal elements of this matrix as
shown in equation 2.4. Line 49 specifies the prior scale matrix for the inverse Wishart distribution as an identity matrix
but specifies the prior degrees of freedom as the minimum possible +1 (line 51) hence making this a non-informative
prior. Line 53 sets the starting value for Σ as an identity matrix. We use 10,000 Gibbs replications discarding the
first 5000 as burn-in. Line 62 is the first step of the Gibbs sampler with the calculation of the mean of the conditional
2. THE M INNESOTA PRIOR 35
¡ ¢−1 ³ −1 ´
posterior distribution of the VAR coefficients ∗ = −1 + Σ−1 ⊗ 0 ̃0 + Σ−1 ⊗ 0 ̂
(×( × +1))×1
¡ ¢−1
while line 63 compute the variance of this distribution as ∗ = −1 + Σ−1 ⊗ 0 . On
( ×(× +1))×( ×( × +1))
line 64 we draw the VAR coefficients from the normal distribution using ∗ and ∗ . Line 66 calculates the residuals
of the VAR. Line 68 calculates the posterior scale matrix Σ̄. Line 69 draws the covariance matrix from the inverse
Wishart distribution where the function IWPQ uses the method in algorithm 3. Once past the burn-in period we
build up the predictive density and save the forecast for each variable. The quantiles of the predictive density are
shown in figure 4
36 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Median Forecast
10th percentile
20th percentile
30th percentile 6
6 70th percentile
80th percentile
90th percentile
5
2
3
2
0
−2
−4
−1
−6 −2
1995 2000 2005 2010 2015 1995 2000 2005 2010 2015
Figure 4. Forecast for annual GDP growth and inflation using a VAR with a Minnesota prior
The parameters that make up the diagonal elements of ̄ and ̄ have the following interpretation:
• 0 controls the overall tightness of the prior on the covariance matrix.
• 1 controls the tightness of the prior on the coefficients on the first lag. As 1 → 0 the prior is imposed
more tightly.
• 3 controls the degree to which coefficients on lags higher than 1 are likely to be zero. As 3 increases
coefficients on higher lags are shrunk to zero more tightly.
• The prior variance on the constant is controlled by 4 As 4 → 0 the constant is shrunk to zero.
To consider the interpretation of this prior (i.e. equations 3.1 and 3.2), consider calculating the prior covariance
matrix for the coefficients. This will involve the following operation
̄ ⊗ ̄ (3.8)
That is the matrix or the prior variance of all the VAR coefficients is obtained by a kronecker product in 3.8.
Consider calculating this kronecker product in our bi-variate VAR example
⎛ ⎞
(0 4 )2 0 0 0 0
⎜ ³ ´2 ⎟
⎛ ³ ´ ⎞ ⎜ 0 1 ⎟
2 ⎜ 0 1 0 0 0 ⎟
1
0 ⎜ ³ ´2 ⎟
⎜ 0 ⎟ ⎜ 0 0 0 1
0 0 ⎟
⎝ ³ ´2 ⎠ ⊗ ⎜ 2 ⎟
0 2 ⎜ ³ ´ 2 ⎟
0 ⎜ 0 1 ⎟
⎜ 0 0 0 2 3
0 ⎟
⎝ 1
³ ´2 ⎠
0 1
0 0 0 0 23 2
This kronecker product involves each element of ̄ being multiplied by the entire ̄. If one does one obtains equation
3.9
⎛ 2
⎞
( 1 4 ) 0 0 0 0 0 0 0 0 0
⎜ 0 (1 )
2
0 0 0 0 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ ⎟
⎜ 0 0 1 1
0 0 0 0 0 0 0 ⎟
⎜ 2 ⎟
⎜ ¡ ¢ ⎟
⎜ 1 2 ⎟
⎜ 0 0 0 23
0 0 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ 0 0 0 0 1 1
0 0 0 0 0 ⎟
⎜ 2 23 ⎟
=⎜ 2 ⎟ (3.9)
⎜ 0 0 0 0 0 ( 2 4 ) 0 0 0 0 ⎟
⎜ ³ ´2 ⎟
⎜ 2 1 ⎟
⎜ 0 0 0 0 0 0 1 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 0 0 0 0 0 (1 ) 2
0 0 ⎟
⎜ ³ ´2 ⎟
⎜ ⎟
⎜ 0 0 0 0 0 0 0 0 2 1
0 ⎟
⎝ 1 23 ⎠
¡ ¢
1 2
0 0 0 0 0 0 0 0 0 23
Note that this is just the Minnesota prior variance with the parameter 2 = 1. Therefore the structure of the
natural conjugate prior implies that we treat lags of dependent variable and lags of other variables in each equation
of the VAR in exactly the same manner. This is in contrast to the Minnesota prior where the parameter 2 governs
the tightness of the prior on lags of variables other than the dependent variable.
Given the natural conjugate prior, analytical results exist for the posterior distribution for the coefficients and
the covariance matrix. Therefore one clear advantage of this set up over the Minnesota prior is that it allows the
derivation of these analytical results without the need for a fixed and diagonal error covariance matrix. The exact
formulas for the posteriors are listed in table 1 in Kadiyala and Karlsson (1997).
The Gibbs sampling algorithm for this model is identical to that described in section 2.1. As explained above
the only difference is that the variance of the prior distribution is set equalt to as described in equation 3.9.
3.2. The independent Normal inverse Wishart prior. The restrictions inherent in the natural conjugate
prior may be restrictive in many practical circumstances. That is, in many practical applications one may want
to treat the coefficients of the lagged dependent variables differently from those of other variables. An example is
a situation where the researcher wants impose that some coefficients in a VAR equation are close to zero (e.g. to
impose money neutrality or small open economy type restrictions). This can be acheived via the independent Normal
38 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
inverse Wishart prior. As the name suggests, this prior involves setting the prior for the VAR coefficients and the
error covariance independently (unlike the natural conjugate prior)
³ ´
() ˜ ̃0 (3.10)
0 0 0 0 −2 4
3. THE NORM AL INVERSE W ISHART PRIOR 39
Lines 48 to 53 set the variance around the prior for 12 13 14 and 12 13 14 Note that the variance is set to a
very small number implying that we incorporate the belief that these coefficients equal zero very strongly. Lines 56
to 88 set the prior variance for the remaining VAR coefficients according to equation 3.9. This is an ad hoc way of
incorporating prior information about these coefficients but is important given the small sample. Lines 90 and 92
set the prior for the error covariance as in example 1. Given these priors the Gibbs algorithm is exactly the same
as in the previous example. However, we incorporate one change usually adopted by researchers. On lines 108 to
115 we draw the VAR coefficients from its conditional posterior but ensure that the draw is stable. In other words
the function stability re-writes the VAR coefficient matrix in companion form and checks if the eigenvalues of this
40 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
matrix are less than or equal to1—i.e. that the VAR is stable (see Hamilton (1994) page 259). Once past the burn-in
stage line 123 calculates the structural impact matrix 0 as the Cholesky decomposition of the draw of Σ and lines
124 to 129 calculate the impulse response to a negative shock in the Government bond yield using this 0 We save
the impulse response functions for each remaining draw of the Gibbs sampler. Quantiles of the saved draws of the
impulse response are error bands for the impulse responses.
The resulting median impulse responses and the 68% error bands are shown in figure 8. Note that 68% error
bands are typically shown as the 90% or 95% bands can be misleading if the distribution of the impulse response
function is skewed due to non-linearity. The response of the Federal Funds rate to this shock is close to zero as
4. STEADY STATE PRIORS 41
implied by the Cholesky decomposition and the prior on 12 13 14 and 12 13 14 A 0.3% fall in the Government
bond yield lowers unemployment by 0.1% after 10 months (but the impact is quite uncertain as evident from the
wide error bands). The impact on inflation is much more imprecise with the zero line within the error bands for most
of the impulse horizon.
−4 Response of the Federal Funds rate Response of the Government Bond Yield
x 10
1.5 0
1 −0.05
−0.1
0.5
−0.15
0
−0.2
−0.5 −0.25
−0.3
−1
−0.35
10 20 30 40 50 10 20 30 40 50
0 Median Response
0.2
Upper 84%
−0.02 Lower 16%
0.15
Zero Line
−0.04
0.1
−0.06
0.05
−0.08
−0.1 0
−0.12 −0.05
−0.14 −0.1
−0.16
−0.15
−0.18
−0.2
−0.2
−0.25
10 20 30 40 50 10 20 30 40 50
Horizon
inflation in the long run will be close to the target set by the central bank. This information is a potentially useful
input as a prior.
Note that while the priors introduced above allow the researcher to have an impact on the value of the constant
terms in the VAR, there is no direct way to affect the long run mean (note that forecasts converge to the long run
unconditional mean). Consider our example bi-variate VAR re-produced below
µ ¶ µ ¶ µ ¶µ ¶¶ µµ ¶ ¶µ ¶ µ
1 11 12 −1 1 11
1 12 −2
= + +
=Σ (4.1) +
2 21 22 −1 2 21
2 22 −2
µ ¶
1
The Minnesota and the Normal inverse Wishart priors place a prior on the constants The long run or steady
2
state means for and denoted by 1 and 2 however, is defined as (see Hamilton (1994) page 258)
µ ¶ µµ ¶ µ ¶ µ ¶¶−1 µ ¶
1 1 0 11 12 11 12 1
= − − (4.2)
2 0 1 21 22 21 22 2
Villani (2009) proposes a prior distribution for the unconditional means = {1 2 } along with coefficients of
the VAR model. This requires one to re-write the model in terms of = {1 2 } rather than the constants 1 and
2 This can be done in our example VAR by substituting for 1 and 2 in equation 4.1 using the values of these
constants from equation 4.2 to obtain
µ ¶ µµ ¶ µ ¶ µ ¶¶ µ ¶
1 0 11 12 11 12 1
= − −
0 1 21 22 21 22 2
µ ¶µ ¶ µ ¶µ ¶ µ ¶
11 12 −1 11 12 −2 1
+ + +
21 22 −1 21 22 −2 2
or more compactly in terms of lag operators as
() ( − ) = (4.3)
µ ¶ µ ¶ µ ¶
1 0 11 12 11 12
where = { }, = {1 2 }and () = − − 2
0 1 21 22 21 22
4. STEADY STATE PRIORS 43
where ̃ are the OLS estimates of the VAR coefficients in companion form and denotes the OLS estimates
of the constant terms in a comformable matrix. For the bi-variate VAR in equation 4.1 this looks as follows
⎛⎛ ⎞ ⎛ ⎞⎞−1 ⎛ ⎞
⎛ ⎞ 1 0 0 0 ̂11 ̂12 ˆ11 ˆ12 ̂1
1 ⎜⎜ ⎟ ⎜ ˆ ˆ ⎟⎟ ⎜ ⎟
⎝ 2 ⎠ = ⎜⎜ 0 1 0 0 ⎟ − ⎜ ̂21 ̂22 21 22 ⎟⎟ ⎜ ̂2 ⎟
⎝⎝ 0 0 1 0 ⎠ ⎝ 1 0 0 0 ⎠⎠ ⎝ 0 ⎠
0 0 0 1 0 1 0 0 0
Step 2 Sample the VAR coefficients from their conditonal distribution. Conditional on , equation 4.3 implies that
the model is a VAR in the transformed (or de-meaned) variables
¡ 0 =¢ − . The conditional posterior
distribution of the VAR coefficients is normal distribution ̄|Σ ∗ ˜ ( ∗ ∗ ) where
¡ ¢−1 ³ −1 ´
∗ = −1 + Σ−1 ⊗ 00 0 ̃0 + Σ−1 ⊗ 00 0 ̂ (4.5)
( ×(× ))×1
¡ ¢−1
∗ = −1 + Σ−1 ⊗ 00 0 (4.6)
(×( × ))×( ×(× ))
³¡ ¢−1 ¡ 00 0 ¢´
where 0 = [−1
0 0
− ] and ̂ = 00 0 Note that the dimensions of ∗ and ∗ are different
relative to those shown in section 1.1 because 0 does not contain a constant term. Once ∗ and ∗ are calculated,
the VAR coefficients are drawn from the normal distribution as before.
¡ ¢ ¡ ¢ ¡ ¢0 ¡ ¢
Step 3 Draw Σ from its conditional distribution Σ|̄ ∗ ˜ Σ̄ + where Σ̄ = ̄+ 0 − 0 1 0 − 0 1
where 1 is the previous draw of the VAR coefficients reshaped into a matrix with dimensions ( × ) ×
so it is conformable with ∗
Step 4 Draw¡ from its¢conditional distribution. Villani (2009) shows that the conditional distribution of is given
as |̄ Σ ∗ ∼ (∗ Ω∗ ) where
¡ ¡ 0 ¢ 0 ¢−1
Ω∗ = Σ−1 0
+ ⊗Σ
−1
(4.7)
¡ ¡ ¢ ¢
∗ = Ω∗ 0 Σ−1 0 + Σ−1
0 (4.8)
where is a × ( + 1) matrix = [ −−1 − − ] where is the constant term ( a × 1) vector equal to
one). is a matrix with the following structure
⎛ ⎞
⎜ 1 ⎟
=⎜ ⎝ ⎠
⎟
For our two variable VAR looks as follows
⎛ ⎞
1 0
⎜ 0 1 ⎟
⎜ ⎟
⎜ 11 12 ⎟
=⎜
⎜
⎟
⎟ (4.9)
⎜ 21 22 ⎟
⎝ 11 12 ⎠
21 22
44 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Finally = − 1 −1 − − where denotes the VAR coefficients on the i lag from the previous Gibbs
iteration.
Step 5 Repeat steps 2 to 4 times to obtain 1 and (Σ)1 (Σ) and 1 The last values of
and Σ from these iterations is used to form the empirical distribution of these parameters.
4.2. Gibbs sampling algorithm for the VAR with steady state priors. The matlab code. We estimate
the VAR with steady state priors using the same data used in the first example (quarterly data on annual GDP growth
and CPI inflation for the US from 1948Q2 to 2010Q4) and consider a long term forecast of these variables. The code
4. STEADY STATE PRIORS 45
for the model (example3.m) is presented in figures 9, 10, 11 and 12. The code is identical to the first matlab example
until line 50 where we set the prior for the long run means of the two variables. As an example we set the prior mean
equal to 1 for both 1 and 2 and a tight prior variance. Lines 54 to 58 estimate the VAR coefficients via OLS and
estimate a starting value for 1 and 2 as described in Step 1 of the Gibbs sampling algorithm above. Line 68 is
the first step of the Gibbs algorithm and computes the demeaned data 0 = −¡ and uses¢ this on line 77 and 78
to compute the mean and the variance of the conditional posterior distribution ̄|Σ ∗ and samples the VAR
coefficients from the normal distribution. Lines 81 to 85 draw Σ from the inverse Wishart distribution. Lines 89 to 100
draw 1 and 2 from the normal distribution. On line 89 the code creates the matrix = − 1 −1 − −
46 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Figure 11. Matlab code for VAR with steady state priors (continued)
⎛ ⎞
1 0
⎜ 0 1 ⎟
⎜ ⎟
⎜ 11 12 ⎟
⎜
Lines 90 to 96 create the matrix = ⎜ ⎟ Line 97 creates the matrix = [ −−1 − − ] . Lines
⎟
⎜ 21 22 ⎟
⎝ 11 12 ⎠
21 22
98 and 99 compute the variance and mean of the conditional posterior distribution of (see equation 4.7 and 4.8
)while line 100 draws from the normal distribution. After the burn-in stage the VAR is used to do a forecast for
4. STEADY STATE PRIORS 47
Figure 12. Matlab code for VAR with Steady State priors.
40 quarters. It is convenient to parameterise the VAR in the usual form i.e as in equation 4.1. On line 110 the code
calculates the implied constants in the VAR using the fact that
⎛⎛ ⎞ ⎛ ⎞⎞ ⎛ ⎞ ⎛ ⎞
1 0 0 0 11 12 11 12 1 1
⎜⎜ 0 1 0 0 ⎟ ⎜ 21 22 21 22 ⎟⎟ ⎜ 2 ⎟ ⎜ 2 ⎟
⎜⎜ ⎟ ⎜ ⎟⎟ ⎜ ⎟=⎜ ⎟
⎝⎝ 0 0 1 0 ⎠ − ⎝ 1 0 0 0 ⎠⎠ ⎝ 1 ⎠ ⎝ 0 ⎠
0 0 0 1 0 1 0 0 2 0
and lines 113 to 117 calculate the forecast for each retained draw. The resulting forecast distribution in figure 13 is
centered around the long run mean close to 1 for both variables.
48 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
5
Mean Forecast
Median Forecast
4 10th percentile
20th percentile
4
30th percentile
70th percentile
80th percentile
90th percentile
3
2
0 1
−2
−1
−2
−4
−3
−6 −4
1995 2000 2005 2010 2015 2020 1995 2000 2005 2010 2015 2020
Figure 13. Forecast distribution for the VAR with steady state priors.
5.1. The Normal Wishart (Natural Conjugate) prior using dummy observations. Consider artificial
data denoted and (we consider in detail below how to generate this data) such that
0 −1 0
0 = ( ) ( ) (5.1)
0
= ( − 0 ) ( − 0 )
where ̃0 = (0 ). In other words a regression of on gives the prior mean for the VAR coefficients and sum
of squared residuals give the prior scale matrix for the error covariance matrix. The prior is of the normal inverse
Wishart form
³ ´
0 −1
(|Σ)˜ ̃0 Σ ⊗ ( ) (5.2)
(Σ) ˜ ( − )
where is the length of the artificial data and denotes the number of regressors in each equation.
Given this artificial data, the conditional posterior distributions for the VAR parameters are given by
5. IM PLEM ENTING PRIORS USING DUM M Y OBSERVATIONS 49
−1
(|Σ ) ˜ (( ∗ ) Σ ⊗ ( ∗0 ∗ ) ) (5.3)
(Σ| ) ˜ ( ∗ ∗ )
where ∗ = [ ; ] ∗ = [; ] i.e. the actual VAR left and right hand side variables appended by the artificial
data and ∗ denotes the number of rows in ∗ and
−1
∗ = ( ∗0 ∗ ) ( ∗0 ∗ )
∗ = ( ∗ − ∗ )0 ( ∗ − ∗ )
Note that the conditional posterior distribution has a simple form and the variance of (|Σ ) only involves
the inversion of × + 1 matrix making a Gibbs sampler based on this formulation much more computationally
efficient in large models.
5.1.1. Creating the dummy observations for the Normal Wishart prior. The key question however is, where do
and come from? The artificial observations are formed by the researcher and are created using the following
hyper-parameters:
• controls the overall tightness of the prior
• controls the tightness of the prior on higher lags
• controls the tightness of the prior on constants
• are standard deviation of error terms from OLS estimates of AR regression for each variable in the model
To discuss the creation of the dummy observations we are going to use the bi-variate VAR given below as an
example:
µ ¶ µ ¶ µ ¶µ ¶ µ ¶µ ¶ µ ¶ µ ¶
1 11 12 −1 11 12 −2 1 1
= + + + =Σ (5.4)
2 21 22 −1 21 22 −2 2 2
Consider dummy observations that implement the prior on the coefficients on the first lag of and The
artificial data (denoted by 1 and 1 ) is given by
µ ¶
(1 ) 1 0
1 = (5.5)
0 (1 ) 2
µ ¶
0 (1 ) 1 0 0 0
1 =
0 0 (1 ) 2 0 0
To see the intuition behind this formulation consider the VAR model using the artificial data
⎛ ⎞
1 2
µ ¶ µ ¶⎜ 11 21 ⎟ µ ¶
(1 ) 1 0 0 (1 ) 1 0 0 0 ⎜
⎜
⎟
⎟ 1
= 12 22 ⎟ + (5.6)
0 (1 ) 2 0 0 (1 ) 2 0 0 ⎜
⎝ 11 21 ⎠ 2
1 1
12 22
Proceeding as in equation 5.7 one can show that these dummy variables imply a prior mean of 0 for the second lag
with the prior variance of the same form as in equation 3.9. For example, the prior variance associated with 11 is
2 (1 )
21 2
.
The artificial observations that control the prior on the constants in the model are given by:
µ ¶
0 0
3 = (5.9)
0 0
µ ¶
1 0 0 0 0
3 =
1 0 0 0 0
As → 0 the prior is imlemented more tightly. The dummy observations to implement the prior on the error
covariance matrix are given by
µ ¶
1 0
4 = (5.10)
0 2
µ ¶
0 0 0 0 0
4 =
0 0 0 0 0
with the magnitude of the diagonal elements of Σ controlled by the scale of the diagonal elements of 4 (i.e. larger
diagonal elements implement the prior belief that the variance of 1 and 2 is larger).
The prior is implemented by adding all these dummy observations to the actual data. That is
∗ = [ ; 1 ; 2 ; 3 ; 4 ] ∗ = [; 1 ; 2 ; 3 ; 4 ]
With this appended data in hand, the conditional distributions in equation 5.3 can be used to implement the Gibbs
sampling algorithm. Note that, as discussed in Banbura et al. (2007), these dummy observations for a general
variable VAR with lags are given as
⎛ ( 1 ) ⎞
1
⎛ ⊗(1 ) ⎞
⎜ 0 ⎟
0 ×1
⎜ ×( −1)× ⎟
⎜ ⎟ ⎜ 0× 0 ×1 ⎟
= ⎜ ⎟
⎜ ( 1 ) ⎟ = ⎝
⎜ ⎟
⎠ (5.11)
⎜ ⎟
⎝ ⎠ 01×
01×
where are the prior means for the coefficients on the first lags of the dependent variables (these can be different
from 1) and = (1 )
5.1.2. Creating dummy variables for the sum of coefficients prior. If the variables in the VAR have a unit root,
this information can be reflected via a prior that incorporates the belief that coefficients on lags of the dependent
variable sum to 1 (see Robertson and Tallman (1999)). This prior can be implemented in our example VAR via the
following dummy observations
µ ¶ µ ¶
1 0 0 1 0 1 0
5 = 5 = (5.12)
0 2 0 0 2 0 2
where 1 is the sample mean of and 2 is the sample mean of possibly calculated using an intial sample of
data. Note that these dummy observations imply prior means of the form + = 1 where = 1 2 and controls
the tightness of the prior. As → ∞ the prior is implemented more tightly. Banbura et al. (2007) show that these
dummy observations for a variable VAR with lags are given as
(1 1 ) ³ ´
= = (12 )⊗(
1 1 )
0×1 (5.13)
where = 1 and = 1 are sample means of each variable included in the VAR.
5.1.3. Creating dummy variables for the common stochastic trends prior. One can express the prior belief that
the variables in the VAR have a common stochastic trend via the following dummy observations
¡ ¢ ¡ ¢
6 = 1 2 6 = 1 2 1 2 (5.14)
These dummy observations imply, for example, that 1 = 1 + 1 11 + 2 12 + 1 11 + 2 12 i.e. the mean of the
first variable is a combination of 1 and 2 Note as → ∞ the prior is implemented more tightly and the series in
the VAR share a common stochastic trend.
5.1.4. Matlab code for implementing priors using dummy observations. Figures 14, 15 and 16 show the matlab
code for the bi-variate VAR(2) model using quarterly data on annual GDP growth and CPI inflation for the US from
1948Q2 to 2010Q4 (example4.m). Line 26 of the code calculates the sample means of the data to be used in setting
the dummy observations. Some researchers use a pre-sample to calculate these means and the standard deviations
Lines 28 to 32 specify the parameters that control the prior. Lines 33 to 37 set the dummy observations for
the VAR coefficients on the first lags. Lines 38 to 42 set the dummy observations for the VAR coefficients on the
second lag. Lines 43 to 47 specify the dummy observations for the prior on the constant. Lines 48 to 53 specify
5. IM PLEM ENTING PRIORS USING DUM M Y OBSERVATIONS 51
the dummy observations for the unit root prior. Lines 56 to 57 set out the dummy observations for the common
stochastic trends prior. Lines 59 to 64 specify the dummy observations for the prior on the covariance matrix.
Lines 68 and 69 mix the actual observations with the dummy data creating ∗ = [ ; ] ∗ = [; ]. Line 72
−1
computes the mean of the conditional posterior distribution of the VAR coefficients ∗ = ( ∗0 ∗ ) ( ∗0 ∗ ). Line
∗0 ∗ −1
83 calculates the variance of this posterior distribution Σ⊗( ) and line 85 draws the VAR coefficients from the
normal distribution with this mean and variance. Line 88 calculates the scale matrix for the inverse Wishart density
0
∗ = ( ∗ − ∗ ∗ ) ( ∗ − ∗ ∗ ) and line 89 draws the covariance matrix from the inverse Wishart distribution.
Once past the burn-in stage the code forecasts the two variables in the VAR and builds up the predictive density.
52 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
functions, variance decompositions and historical decompositions. The Gibbs sampling framework is convenient
because it allows one to build a distribution for these objects (i.e. impulse response functions, variance decompositions
and historical decompositions) and thus characterise uncertainty about these estimates.
Strictly speaking, this indirect method of estimating structural VARs—i.e. calculating 0 using a Gibbs draw
of Σ (and not sampling 0 directly) provides the posterior distribution of 0 only if the structural VAR is exactly
identified (for e.g. when 0 is calculated using a Cholesky decomposition as in section 3.2.1) . In the case of over
identification one needs to estimate the posterior of 0 directly ( see Sims and Zha (1998)). We will consider such
an example in Chapter 4.
54 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Recent applications of structural VARs have used sign restrictions to identify structural shocks (for a critical
survey see Fry and Pagan (2007)). Despite the issues raised in Sims and Zha (1998), sign restrictions are implemented
using an indirect algorithm. In other words for each retained draw of Σ one calculates an 0 matrix which results
in impulse responses to a shock of interest with signs that are consistent with theory. For example to identify a
monetary policy shock one may want an 0 matrix that leads to a response of output and inflation that is negative
and a response of the policy interest rate that is positive for a few periods after the shock.
Ramirez et al. (2010) provide an efficient algorithm to find an 0 matrix consistent with impulse responses of
a certain sign consistent with theory. We review this algorithm by considering the following VAR(1) model as an
example ⎛ ⎞ ⎛ ⎞ ⎛ ⎞⎛ ⎞ ⎛ ⎞
1 11 12 13 −1 1
⎝ ⎠ = ⎝ 2 ⎠ + ⎝ 21 22 23 ⎠ ⎝ −1 ⎠ + ⎝ 2 ⎠ (6.1)
3 31 32 33 −1 3
⎛ ⎞
1
where ⎝ 2 ⎠ = Σ is output growth, is inflation and is the interest rate. The aim is to calculate
3
the impulse response to a monetary policy shock. The monetary policy shock is assumed to be one that decreases
and and increases in the period of the shock. As described above, the Gibbs sampling algorithm to estimate
the parameters of the VAR model cycles through two steps, sampling successively from (|Σ ) and (Σ| )
Once past the burn-in stage the following steps are used calculate the required 0 matrix:
Step 1 Draw a × matrix from the standard normal distribution
Step 2 Calculate the matrix from the decomposition of Note that is orthonormal i.e. 0 = .
Step 3 Calculate the Cholesky decomposition of the current draw of Σ = ̃00 ̃0
Step 4 Calculate the candidate 0 matrix as 0 = ̃0 . Note that because 0 = this implies that 00 0
will still equal Σ By calculating the product ̃0 we alter the elements of ̃0 but not the property that
Σ = ̃00 ̃0 The candidate 0 matrix in our 3 variable VAR example will have the following form
⎛ ⎞
11 12 13
0 = ⎝ 21 22 23 ⎠
31 32 33
The third row of this matrix corresponds with the interest rate shock. We need to check if 31 0 and
32 0 and 33 ¡ 0. If this is the¢ case a contemporaneous increase in will lead to a fall in and
as the elements 31 32 33 correspond to the current period impulse response of and
respectively. If 31 0 and 32 0 and 33 0 we stop and use this 0 matrix to compute impulse
responses and other objects of interest. If the restriction is not satisfied we go to step 1 and try with an
new matrix.
Step 5 Repeat steps 1 to 4 for every retained Gibbs draw.
6.1. A Structural VAR with sign restrictions in matlab. We estimate a large scale VAR model for the
US using quarterly data from 1971Q1 to 2010Q4 (example5.m). The VAR model includes the following variables (in
this order): (1) Federal Funds Rate (2) Annual GDP growth (3) Annual CPI Inflation (4) Annual real consumption
growth (5) Unemployment rate (6) change in private investment (7) net exports (8) annual growth in M2 (9) 10 year
government bond yield (10) annual growth in stock prices (11) annual growth in the yen dollar exchange rate. We
identify a monetary policy shock by assuming that a monetary contraction has the following contemporaneous effects
Variable Sign restriction
Federal Funds Rate +
Annual GDP growth -
Annual CPI Inflation -
Annual Real Consumption Growth -
Unemployment rate +
Annual Investment Growth -
Annual Money Growth -
The Matlab code for this example is shown in figures 17, 18 and 19. Line 37 of the code builds the dummy
observations for the normal wishart prior for the VAR using equations 5.11 and the sum of coefficients prior using
equation 5.13 via the function create_dummies.m in function folder. Lines 49 to 55 sample from the conditional
posterior distributions as in the previous example. Once past the burn-in stage, on line 61 we draw a × matrix
from the standard normal distribution. Line 62 takes the QR decomposition of and obtaines the matrix Line
63 calculates the Cholesky decomposition of Σ while line 64 calculates the candidate 0 matrix as 0 = ̃0 . Lines
66 to 72 check if the sign restrictions are satisfied by checking the elements of the first row of the 0 matrix (the row
that corresponds the interest rate). Lines 77 to 83 check if the sign restrictions are satisfied with the sign reversed.
If they are, we multiply the entire first row of the 0 matrix by -1. The code keeps on drawing and calculating
candidate 0 = ̃0 matrices until an 0 matrix is found that satisfies the sign restrictions. Once an 0 matrix is
6. APPLICATION1: STRUCTURAL VARS AND SIGN RESTRICTIONS 55
found that satisfies the sign restrictions, this is used to calculate the impulse response to a monetary policy shock
and the impulse response functions for each retained Gibbs draw are saved. The file example6.m has exactly the
same code but makes the algorithm to find the 0 matrix more efficient by searching all rows of candidate 0 = ̃0
matrix for the one consistent with the policy shock—i.e. with the signs as in the table above. Once this is found we
insert this row into the first row of the candidate 0 matrix. Note that that this re-shuffling of rows does not alter the
property that Σ = 00 0 . Note also that the 0 matrix is not unique. That is, one could find 0 matrices that satisfy
the sign restrictions but have elements of different magnitude. Some researchers deal with this issue by generating
0 matrices that satisfy the sign restrictions for each Gibbs draw and then retaining the 0 matrix that is closest to
56 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
the mean or median of these matrices. This imples that one restricts the distribution of the selected 0 matrices
via a (arbitrary) rule. The file example7.m does this for our example by generating 100 0 matrices for each retained
Gibbs draw and using the 0 matrix closest to the median to compute the impulse response functions. Figure 20
shows the estimated impulse response functions computed using example7.m
Figure 19. A structural VAR with sign restrictions: Matlab code (continued)
that inflation and GDP growth follow future paths fixed at the official central bank forecast. Waggoner and Zha
(1999) provide a convenient framework to calculate not only the conditional forecasts but also the forecast distribution
using a Gibbs sampling algorithm.
To see their approach consider a simple VAR(1) model
= + −1 + 0 (7.1)
58 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
0.4 0.2
0.1 0.2
0.2 0 0.1
−0.1 0
0
−0.2
−0.1
−0.2 −0.3
−0.2
5 10 15 20 25 30 5 10 15 20 25 30 5 10 15 20 25 30
−0.1 0
−1
−0.2 −0.1
−2
5 10 15 20 25 30 5 10 15 20 25 30 5 10 15 20 25 30
0.3
40 0.1
0.2
0
20 0.1
−0.1
0
0 −0.2
−0.1
−0.3
5 10 15 20 25 30 5 10 15 20 25 30 5 10 15 20 25 30
−2 0
−2
−4
5 10 15 20 25 30 5 10 15 20 25 30
Figure 20. Impulse response to a monetary policy shock using sign restrictions
where denotes a × matrix of endogenoeus variables, are the uncorrelated structural shocks and 0 00 = Σ
where Σ denotes the variance of the reduced form VAR esiduals. Iterating equation 7.1 forward times we obtain
X
X
+ = + −1 + 0 +− (7.2)
=0 =0
Equation 7.2 shows that the K period ahead forecast + can be decomposed into components with and without
structural shocks. The key point to note is that if a restriction is placed on the future path of the variable in
, this implies restrictions on the future shocks to the other variables in the system. This can easily be seen by
re-arranging equation 7.2
X
X
+ − − −1 = 0 +− (7.3)
=0 =0
If some of the variables in + are constrained to follow a fixed path, this implies restrictions on the future
innovations on the RHS of equation 7.3. Waggoner and Zha (1999) express these constraints on future innovations as
= (7.4)
where is a ( × ) × 1 vector where are the number of constrained variables and denotes the number of
periods the constraint is applied. The elements of the vector are the path for the constrained variables minus the
unconditional forecast of the constrained variables. is a matrix with dimensions ( × ) × ( × ). The elements
of this matrix are the impulse responses of the constrained variables to the structural shocks at horizon 1 2.
The ( × ) × 1 vector contains the constrained future shocks. We give a detailed example showing the structure
of these matrices below.
Doan et al. (1983) show that a least square solution for the constrained innovations in equation 7.4 is given as
With these constrained shocks ̂ in hand, the conditional forecasts can be calculated by substituting these in
equation7.2.
7. APPLICATION 2: CONDITIONAL FORECASTING USING VARS AND GIBBS SAM PLING 59
7.1. Calculating conditional forecasts. To see the details of this calculation, consider the following VAR
model with two endogenous variables
µ ¶ µ ¶ µ ¶µ ¶ µ ¶µ ¶
1 1 2 −1 11 1
= + + (7.6)
2 3 4 −1 12 22 2
In addition denote as the impulse response of the variable at horizon to the structural shock where
= 1 2. Consider forecasting
⎛ three
⎞ periods
⎛ ⎞in the future using the estimated VAR in equation 7.6. However we
̂+1 1
impose the condition that⎝ ̂+2 ⎠ = ⎝ 1 ⎠, i.e. variable is fixed at 1 over the forecast horizon. In order to
̂+3 1
calculate the forecast for under this condition, the first step involves using equation 7.5 to calculate the restricted
structural shocks. Using equation 7.5 requires building the matrices and . We now describe the structure of these
matrices for our example. First note that the restricted structural shocks (to be calculated) are stacked as
⎛ ⎞
̂1+1
⎜ ̂2+1 ⎟
⎜ ⎟
⎜ ̂1+2 ⎟
⎜
̂ = ⎜ ⎟ (7.7)
⎟
⎜ ̂2+2 ⎟
⎝ ̂1+3 ⎠
̂2+3
The matrix of impulse responses is built to be compatible with ̂ (see equation 7.4) In this example, it has the
following structure ⎛ 1 ⎞
2
12 12 0 0 0 0
= ⎝ 221 2
22 1
12 2
12 0 0 ⎠ (7.8)
1 2 1 2 1 2
32 32 22 22 12 12
The matrix is made of the response of the constrained variable 2 (i.e. ) to the two structural shocks. The
first row of the matrix has the response of to 1 and 2 at horizon 1. Note that this row corresponds to the
first two elements in ̂— it links the constrained shocks 1 period ahead to their responses The second row of has
this impulse response at horizon 2 (first two elements) and then at horizon 1 (third and fourth element). This row
corresponds to the forecast two periods ahead and links the structural shocks at horizon 1 and 2 to their respective
impulse responses. A similar interpretation applies to the subsequent rows of this matrix.
The matrix r is given as
⎛ ⎞
1 − ̃+1
= ⎝ 1 − ̃+2 ⎠ (7.9)
1 − ̃+3
where ̃+ denotes the unconditional forecast of Once these matrices are constructed, the restricted structural
shocks are calculated as ̂ = 0 (0 )−1 . These are then used calculate the conditional forecast by substituting them
in equation 7.6 and iterating forward.
In figures 21 and 22 we show the matlab code for this simple example of calculating a conditional forecast (the
matlab file is example8.m). We estimate a VAR(2) model for US GDP growth and inflation and use the estimated
VAR to forecast GDP growth 3 periods ahead assuming inflation remains fixed at 1% over the forecast horizon.Lines
18 to 21 of the code estimate the VAR coefficients and error covariance via OLS and calculate 0 as the Choleski
decomposition of the error covariance matrix. As shown in Waggoner and Zha (1999) the choice of identifying
restrictions (i.e. the structure of 0 ) does not affect the conditional forecast which depends on the reduced form
VAR. Therefore it is convenient to use the Choleski decomposition to calculate 0 for this application. Lines 25 to
28 estimate the impulse response functions . Line 33 to 39 constructs the unconditional forecast by simulating
the estimated VAR model for three periods. Lines 41 to 43 construct the matrix as specified in equation 7.8.
Line 45 constructs the matrix. With these in hand, the restricted future shocks are calculated on line 50. The
conditional forecast is calculated by simulating the VAR using these restricted shocks 50 to 59 of the code with the
matlab variable yhat2 holding the conditional forecast.
7.2. Calculating the distribution of the conditional forecast. The main contribution of Waggoner and
Zha (1999) is to provide a Gibbs sampling algorithm to construct the distribution of the conditional forecast and
thus allow a straigth forward construction of fan charts whn some forecasts are subject to constraints. In particular,
Waggoner and Zha (1999) show that the distribution of the restricted future shocks is normal with mean ̄ and
variance ̄ where
̄ = 0 (0 )−1 (7.10)
0 0 −1
̄ = − ( )
The Gibbs sampling algorithm to generate the forecast distribution proceeds in the following steps
(1) Initialise the VAR coefficients and the 0 matrix
60 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
(2) Form the matrices and Draw the restricted structural shocks from the (̄ ̄ ) distribution where ̄
and ̄ are calculated as in equation 7.10. This draw of structural shocks is used to calculate the conditional
forecast ̂+
(3) Construct the appended dataset ∗ = [ ; ̂+ ]. This the actual data for the VAR model with the forecasts
added to it. The conditional posterior of the VAR coefficients and covariance matrix is construced using
∗ and new values of the coefficients and covariance matrix are drawn. The 0 matrix can be updated as
the Cholesky decomposition of the new draw of the covariance matrix. Note that by using ∗ we ensure
7. APPLICATION 2: CONDITIONAL FORECASTING USING VARS AND GIBBS SAM PLING 61
Figure 22. Matlab code for computing the conditional forecast (continued)
that the draws of the VAR parameters take into account the restrictions = . This procedure therefore
accounts for parameter uncertainty and the restrictions imposed on the forecasts by the researcher.
(4) Goto step 2 and repeat times. The last draws of ̂+ can be used to construct the distribution of the
forecast.
In order to demonstrate this algorithm we continue our Matlab example above and calculate the distribution of
the GDP growth forecast, leaving the inflation forecast restricted at 1%.
The Matlab code is shown in figures 23 and 24. Note that this is a continuation of the code in the previous example
from line 60. We use 5000 Gibbs iterations and discard the first 3000 as burn in. Line 70 of the code constructs the
62 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Figure 23. Calculating the distribution of the conditional forecast via Gibbs sampling
appended dataset and lines 82 to 90 use this appended data to draw the VAR coefficients and covariance matrix from
their conditional distributions. The impulse responses and unconditional forecasts based on this draw of the VAR
coefficients and the new 0 matrix are used to construct the matrix and the vector on lines 113 to 117. Lines
119 to 122 construct the mean and variance of the restricted structural shocks ̄ and ̄ . On line 124 we draw the
structural shocks from the (̄ ̄ ) distrbution and lines 126 to 136 use these to construct the conditional forecast.
Once past the burn-in stage the conditional forecasts are saved in the matrices out1 and out2.
Running the code produces figure 25. The left panel of the figure shows the forecast distribution for GDP growth.
The right panel shows the forecast for inflation which is restricted at 1% over the forecast horizon.
7. APPLICATION 2: CONDITIONAL FORECASTING USING VARS AND GIBBS SAM PLING 63
Figure 24. Calculating the distribution of the conditional forecast via Gibbs sampling (continued)
7.3. Extensions and other issues. The example above places restrictions on both structural shocks 1 and
2 to produce the conditional forecast. In some applications it may be preferable to produce the conditional forecast
by placing restrictions only on a subset of shocks. For instances one may wish to restrict 1 only in our application.
This can be done easily by modifying the matrix as follows:
⎛ 1 ⎞
12 0 0 0 0 0
= ⎝ 22
1 1
0 12 0 0 0 ⎠ (7.11)
1 1 1
32 0 22 0 12 0
64 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
Median Forecast
10th percentile
20th percentile
5
6 30th percentile
70th percentile
80th percentile
90th percentile
−2
−4
−1
−6 −2
1996 1998 2000 2002 2004 2006 2008 2010 2012 1996 1998 2000 2002 2004 2006 2008 2010 2012
Waggoner and Zha (1999) also discuss a simple method for imposing ‘soft conditions’ on forecasts— i.e. restricting
the forecasts for some variables to lie within a range rather than the ‘hard condition’ we examine in the example
above. Robertson et al. (2005) introduce an alternative method to impose ‘soft conditions’.
8. Further Reading
• A comprehensive general treatment of Bayesian VARs can be found in Canova (2007) Chapter 10.
• An excellent intuitive explanation of priors and conditional forecasting can be found in Robertson and
Tallman (1999).
• A heavily cited article discussing different prior distributions for VARs and methods for calculating posterior
distributions is Kadiyala and Karlsson (1997).
• Banbura et al. (2007) is an illuminating example of implementing the natural conjugate prior via dummy
observations.
• The appendix of Zellner (1971) provides an excellent description of the Inverse Wishart density.
9. Appendix: The marginal likelihood for a VAR model via Gibbs sampling
We can easily apply the method in Chib (1995) to calculate the marginal likelihood for a VAR model. This can
then be used to select prior tightness (see for example Carriero et al. (2010)) or to choose the lag length and compare
different models.
Consider the following VAR model
X
= + − + ( ) = Σ (9.1)
=1
9. APPENDIX: THE M ARGINAL LIKELIHOOD FOR A VAR M ODEL VIA GIBBS SAM PLING 65
The prior for the VAR coefficients = { } is ()˜ (̃ ) and for the covariance matrix (Σ)˜ (̄ ). The
posterior distribution of the model parameters Φ = Σ is defined via the Bayes rule
( |Φ) × (Φ)
(Φ| ) = (9.2)
( )
¯ ¯ P ¡ ¢
where ln ( |Φ) = −2 ln 2 + 2 ln ¯Σ−1 ¯ − 05 =1 Σ−1 0 is the likelihood function with N representing the
number of endogenous variables, (Φ) is the joint prior distribution while ( ) is the marginal likelihood that we
want to compute. Chib (1995) suggests computing the marginal likelihood by re-arranging equation 9.2. Note that
in logs we can re-write equation 9.2 as
ln ( ) = ln ( |Φ) + ln (Φ) − ln (Φ| ) (9.3)
Note that equation 9.3 can be evaluated at any value of the parameters Φ to calculate ln ( ). In practice a
high density point Φ∗ such as the posterior mean or posterior mode is used.
The likelihood function is easy to evaluate. In order to evaluate the priors, the pdf of the normal density and the
inverse Wishart is needed. The latter is given in definition 3 above.
Following Chib (1995) the posterior density (Φ∗ | ) = ( ∗ Σ∗ | )can be factored as
( ∗ Σ∗ | ) = ( ∗ |Σ∗ ) × (Σ∗ | ) (9.4)
The first term on the RHS of equation 9.4 can be evaluated easily as this is simply the conditional posterior distribution
of the VAR coefficients—i.e. a normal distribution with a known mean and covariance matrix.
( ∗ |Σ∗ )˜ ( )
¡ ¢−1 ³ −1 ´
= −1 + Σ∗−1 ⊗ 0 ̃0 + Σ∗−1 ⊗ 0 ̂
¡ ¢−1
= −1 + Σ∗−1 ⊗ 0
The second term on the on the RHS of equation 9.4 can be evaluated by noting that
∗ 1X
(Σ | ) ≈ (Σ∗ | ) (9.5)
=1
where represent = 1 draws of the VAR coefficients from the Gibbs sampler used to estimate the VAR model.
Note that (Σ∗ | ) is the inverse Wishart distribution with scale matrix Σ̄ = ̄ + 0 and degrees of freedom
+ where the residuals are calculated using the draws
Figures 26 and 27 show the matlab code for estimating the marginal likelihood in a simple BVAR with a natural
conjugate prior implemented via dummy observations. On line 44 we calculate the marginal likelihood analytically for
comparison with Chib’s estimate. Analytical computation is possible with the natural conjugate prior (see Bauwens
et al. (1999)), while Chib’s estimator can be used more generally. Lines 46 to 67 estimate the VAR model using Gibbs
sampling with the posterior means calculated on lines 69 and 70. Lines 73 to 76 calculate the prior moments which
are used to evaluate the prior densities on lines 79 and 81. Line 83 evaluates the log likelihood function for the VAR
P
model. Line 86 evaluates the term ( ∗ |Σ∗ ). Lines 88 to 99 evaluate the term (Σ∗ | ) ≈ 1 =1 (Σ∗ | ).
These components are used to calculate the marginal likelihood on line 102.
66 2. GIBBS SAM PLING FOR VECTOR AUTOREGRESSIONS
1. Introduction
State space models have become a key tool for research and analysis in central banks. In particular, they can be
used to detect structural changes in time series relationships and to extract unobserved components from data (such
as the trend in a time series). The state space formulation is also used when calculating the likelihood function for
DSGE models.
The classic approach to state space modelling can be computationally inefficient in large scale models as it is
based on maximising the likelihood function with respect to all parameters. In contrast, Gibbs sampling proceeds
by drawing from conditional distributions which implies dealing with smaller components of the model. In addition,
Gibbs sampling provides an approximation to the marginal posterior distribution of the state variable and therefore
directly provides a measure of uncertainty associated with the estimate of the state variable. The use of prior
information also helps along the dimensions of the model where the data is less informative.
This chapter discusses the Gibbs sampling algorithm for state space models and provides examples of implement-
ing the algorithm in Matlab.
⎛ ⎞
¡ ¢
= 1 1 0 ⎝ ⎠ Observation Equation (2.6)
−1
69
70 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
⎛ ⎞ ⎛
⎞ ⎛ ⎞⎛ −1 ⎞ ⎛ ⎞
1 0 2 −1 1
⎝ ⎠ = ⎝ 0 ⎠ + ⎝ 0 1 0 ⎠⎝ −1 ⎠ + ⎝ 2 ⎠ Transition Equation (2.7)
−1 0 1 0 0 −2 0
⎛ ⎞ ⎛ ⎞
1 11 12 0
where ⎝ 2 ⎠ = = ⎝ 12 22 0 ⎠ Consider the observation equation for this model. Here the
0 0 0 0 ⎛ ⎞
matrix is a coefficient matrix which links the state variables ⎝ ⎠ to . Note that the observation equation
−1
has no error term as we assume that decomposes exactly into the two components.
The left hand side of the transition equation
⎛ has the⎞ state vector at time i.e. . The right hand side contains
−1
the state vector lagged one period i.e. −1 = ⎝ −1 ⎠ The fact that the state vector contains −1 implies that
−2
−1 contains −2 . This gives us a way to incorporate the AR(2) process for into the transition equation. In
general, if the state variable follows an AR(p) process, this implies adding − 1 lags of that state-variable into the
state vector
The first row of the matrix contains the AR coefficients for with the constant in the corresponding row of
. The second row forms the random walk process for Note that the last row of contains a 1 (element (1,1) )
to link −1 on the left hand side and −1 on the right hand side and represents an identity. As a consequence, the
last row of equals zero with corresponding zeros in the matrix.
As a final example of a state space model, consider a dynamic factor model for a panel of series where
= 1 2 represents time and = 1 2 represents the cross-section. Each series in the panel is assumed to depend
on a common component i.e. = −1 + We assume that the common unobserved component follows
an (2) process: = + 1 −1 + 2 −2 + This model has the following state-space representation:
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
1 1 0 µ ¶ 1
⎜ 2 ⎟ ⎜ 1 0 ⎟ ⎜ 2 ⎟
⎜ ⎟ ⎜ ⎟
+⎜ ⎟ Observation Equation
⎝ ⎠=⎝ 0 ⎠ −1 ⎝ ⎠ (2.8)
0
−1
µ ¶ µ ¶ µ ¶µ ¶ µ ¶
1 2 −1 1
= + + Transition Equation (2.9)
−1 0 1 0 −2 0
⎛ ⎞
11 0 0 0 µ ¶
⎜ 0 22 0 0 ⎟ 11 0
where ( ) = = ⎜
⎝
⎟ and ( ) = = . As in the unobserved
0 0 0 ⎠ 0 0
0 0 0
component model, the matrix contains the coefficients linking the data to the state variables The first lag of
appears in the state vector because of our assumption that follows an AR(2) process. The transition equation
of the system incorporates the AR(2) dynamics for the state variable in companion form with appropriate structures
for the and matrices.
See Kim and Nelson (1999) Chapter 2 for further examples of state space models.
distributions of the parameters in this case are known from Chapter 1. This observation indicates the following
general Gibbs algorithm for the state space model in equations 3.1 and 3.2.
Step 1 Conditional on , sample and from their posterior distributions.
Step 2 Conditional on sample and from their posterior distributions.
Step 3 Conditional on the parameters of the state space: and sample the state variable from its
conditional posterior distribution.
Step 4 Repeat steps 1 to 3 until convergence is detected.
As emphasised above, steps 1 to 3 are standard and involve linear regressions and/or VARs with known conditional
posteriors. The new step required for the state space model is step 3 where we sample from its conditional posterior
distribution. We turn to a description of the conditional posterior distribution for next.
3.1. The conditional distribution of the state variable. We follow Kim and Nelson (1999) chapter 8 closely
in this description. Let ̃ = [ 1 2 ] i.e. the time series of from time 1 2
³ . Similarly, let ̃ ´= [1 ]
Recall that we are interested in deriving the conditional posterior distribution ̃ | ̃ i.e. the joint
posterior for 1³
2 ´ As shown by Carter and Kohn (1994), it is convenient to consider a factorisation of the
joint density ̃ |̃ . Note, we drop the conditioning arguments for simplicity in what follows below.
³ ´
We can factor ̃ |̃ into the following conditional distributions
³ ´ ³ ´ ³ ´
̃ |̃ = |̃ × ̃ −1 | ̃ (3.3)
³ ´
Note that the right hand side of 3.3 splits ̃ |̃ into the product of the marginal distribution of the state
variable at time ³ T and the´ distribution
³ of the vector
´ ̃ ³−1 = [ 1 2
´ −1³] conditioned on ´ We can expand
the term ̃ −1 | ̃ as ̃ −1 | ̃ = −1 | ̃ × ̃ −2 | −1 ̃ where ̃ −2 =
[ 1 2 −2 ] . Thus:
³ ´ ³ ´ ³ ´ ³ ´
̃ |̃ = |̃ × −1 | ̃ × ̃ −2 | −1 ̃ (3.4)
³ ´ ³ ´
Continuing in this vein and expanding ̃ −2 | −1 ̃ = −2 | −1 ̃ ×
³ ´
̃ −3 | −1 −2 ̃
³ ´ ³ ´ ³ ´ ³ ´ ³ ´
̃ |̃ = |̃ × −1 | ̃ × −2 | −1 ̃ × ̃ −3 | −1 −2 ̃
Expanding further →
³ ´ ³ ´ ³ ´ ³ ´
̃ |̃ = |̃ × −1 | ̃ × −2 | −1 ̃ (3.5)
³ ´ ³ ´
× −3 | −1 −2 ̃ × 1 | −1 −2 2 ̃
As shown in Kim and Nelson (1999) (page 191) expression 3.5 can be simplified by considering the fact that
follows
³ a first order AR´ or Markov process. Because of this Markov property, given ̃ and −1 , in the term
−2 | −1 ̃ , contains no additional information for −2 . This term can therefore be re-written as
³ ´ ³ ´ ³ ´
−2 | −1 ̃ . Similarly −3 | −1 −2 ̃ can be re-written as −3 | −2 ̃ .
³ ´
A similar argument applies to the data vector ̃ For example, in the term −2 | −1 ̃ , ̃ −2 =
[1³ −2 ] contains´ all the required information
³ for −2 (given
´ −1 ). Therefore, this
³ term can be re-written
´ as
−2 | −1 ̃ −2 . Similarly, the term −3 | −2 ̃ can be re-written as −3 | −2 ̃ −3
Given these simplifications we can re-write expression 3.5 as
³ ´ ³ ´ ³ ´ ³ ´ ³ ´
̃ |̃ = |̃ × −1 | ̃ −1 × −2 | −1 ̃ −2 × −3 | −2 ̃ −3 (3.6)
³ ´
× 1 | 2 ̃1
or more compactly
³ ´ ³ ´ Y
−1 ³ ´
̃ |̃ = |̃ |+1 ̃ (3.7)
=1
Assuming that the disturbances of the state space model and are normally distributed:
³ ´
|̃ ˜ ( | | ) (3.8)
³ ´
|+1 ̃ ˜ ( | +1 | +1 )
where the notation | denotes an estimate of at time given information upto time . The two components on
the right hand side of expression 3.7 are normal distributions. However, to draw from these distributions, we need
to calculate their respective means and variances.
³ ´ To see this calculation we consider each component in turn.
3.1.1. The mean and variance of |̃ . We can compute the mean | and the variance | using the
Kalman filter. The Kalman filter is a recursive algorithm which provides with an estimate of the state variable at
each time period, given information up to that time period—i.e. it provides an estimate of | and its variance | .
To estimate the state variable, the Kalman filter requires knowledge of the parameters of the state space
and . These are available in our Gibbs sampling framework from the previous draw of the Gibbs sampler.
The Kalman filter consists of the following equations which are evaluated recursively through time starting from
an initial value 0|0 and 0|0
In equation 3.10 we consider the hypothetical case where we have three variables 1 2 and 3 . Originally we
were forecasting 3 based on 1 i.e. the term ̂ (3 |1 ) and we want to update this projection using the variable 2 .
According to equation 3.10 the updated projection is the sum of ̂ (3 |1 ) and the error in predicting 2 where the
−1
projection of 2 is based on 1 . The weight attached to this prediction error is 32 22 where is the covariance
between . Consider first the intuition behind the prediction error 2 − ̂ (2 |1 ). If the information contained in
1 and 2 is very similar, it is likely that ̂ (2 |1 ) and 2 will be similar and hence the extra unanticipated information
−1
contained in 2 will be limited. The weight attached to this extra information 32 22 can be interpreted as the
regression coefficient between 3 and 2 . A larger value of this coefficient implies that the information contained in
2 receives a larger weight when updating the forecast ̂ (3 |1 ).
In our application, if we let 3 = , 2 = and 1 = −1 →
h³ ´¡ ¢0 i
| = |−1 + − |−1 − |−1 × (3.11)
h¡ ¢¡ ¢0 i−1
− |−1 − |−1 × |−1
3. THE GIBBS SAM PLING ALGORITHM FOR STATE SPACE M ODELS 73
h¡ ¢¡ ¢0 i
where |−1 = |−1 + . Note that the term − |−1 − |−1 is simply the forecast error variance
³ ´ ³ ´
|−1 Also note that − |−1 = ( + + ) − |−1 + = − |−1 + Thus
h³ ´¡ ∙³ ´0 ¸
¢0 i ´³ ³ ´
− |−1 − |−1 = − |−1 − |−1 +
→ ∙³ ´³ ³ ´´0 ¸
− |−1 − |−1 = |−1 0
Substituting these in equation 3.11 produces the updating equation | = |−1 + |−1 . A similar derivation can be
used to obtain the final updating equation | as shown in Hamilton (1994) page 380. Finally note that the likelihood
P ¯ ¯ P
function is given as a by product of the Kalman filter recursions as − 12 =1 ln 2 ¯|−1 ¯ − 12 =1 0|−1 |−1
−1
|−1
For a stationary transition equation, the initial values for the Kalman filter recursions 0|0 and 0|0 are given
as the unconditional mean and variance. That is 0|0 = ( − )−1 and (0|0 ) = ( − ⊗ )−1 (). If
the transition equation of the system is non-stationary (for e.g. if the state variable follows a random walk) the
unconditional moments do not exist. In this case 0|0 can be set arbitrarily. 0|0 is then set as a diagonal matrix
with large diagonal entries reflecting uncertainty around this initial guess.
To recap, we evaluate the equations of the Kalman ³ filter´ given in 3.9 for periods = 1 . The final recursion
delivers | and | the mean and variance of |̃
³ ´ ³ ´
3.1.2. The mean and variance of | +1 ̃ . The mean and variance of the conditional distribution | +1 ̃
can also be derived using the Kalman filter updating equations. As discussed in Kim and Nelson (1999) page 192,
deriving the mean | +1 can be thought of as updating | (the kalman filter estimate of the state variable) for
information
³ ´ contained in +1 which we treat as observed (for e.g. at time − 1, +1 is given using a draw from
|̃ which we discussed above) Note that this task fits into the framework of the updating equations discussed
in the previous section as we are updating an estimate using new information. In other words, the updating equations
of the Kalman filter apply with parameters and the prediction error chosen to match our problem.
For the purpose of this derivation we can consider a state space system with the observation equation:
+1 = + + +1 (3.12)
This implies that the prediction error is given by ∗+1|
= +1 − + | . The forecast error variance is given
∗
by +1| = | 0 + . Note also that for this observation equation, the matrix that relates the state variable to
the observed data +1 is ∗ = With these definitions in hand we can simply use the updating equations of the
Kalman filter. That is ³ ´
| +1 = | + ∗ +1 − + | (3.13)
| +1 = | − ∗ ∗ | (3.14)
∗ ∗−1
where the gain matrix is = | ∗0 +1| .
Equations 3.13 and 3.14 are evaluated backwards in time starting from period − 1 and iterating backwards to
period 1. This recursion consists of the following steps:
Step 1 Run the Kalman filter from = 1 to obtain the mean | and the variance | of the distribution
³ ´
|̃ Also save | and | for = 1 . Draw from the normal distribution with mean |
and the variance | Denote this draw by ̂ ³ ´
Step 2 At time − 1, use 3.13 to calculate −1| −1 = −1| −1 + ∗ ̂ − + −1| −1 where −1| −1
is the Kalman filter estimate of the state variable (from step 1) at time − 1. Use equation 3.14 to calculate
| +1 . Draw ̂ −1 from the normal distribution with mean −1| −1 and variance | +1
Step 3 Repeat step 2 for = − 2 − 3 1
This backward recursion (The Carter and Kohn algorithm) delivers a draw of ̃ = [ 1 2 ] from its
conditional posterior distribution.
A minor modification to this algorithm is required if the matrix is singular (see the example of the state space
model given in equation 2.6). In this case we evaluate equations 3.13 and 3.14 using ̄ instead of , ̄ instead of
and ̄ instead
µ ¶of where µ ̄ ̄ ̄ correspond
¶ toµthe non-singular
¶ block of In the example given in equation 2.6
1 0 2 11 12
above ̄ = ̄ = and ̄ =
0 0 1 0 12 22
3.2. The Gibbs sampling algorithm. We can now re-state the Gibbs alogrithm for the state space model in
equations 3.1 and 3.2.
Step 1 Conditional on , sample and from their posterior distributions.
Step 2 Conditional on sample and from their posterior distributions.
74 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
Step 3 Conditional on the parameters of the state space: and sample the state variable from its
conditional posterior distribution. That is, run the Kalman filter to obtain | and | for = 1 and
draw Use equations 3.13 and 3.14 to draw 1 2 −1
Step 4 Repeat steps 1 to 3 until convergence is detected.
Implementing this Gibbs sampling algorithm therefore requires programming the Kalman filter and equations
3.13 and 3.14 in matlab. The remainder of this chapter describes this task with the help of several examples.
¡ ¢−1
| +1 = | − | 0 | 0 + | (5.4)
These are computed going backwards in time from period − 1 to 1. We now turn to the implementation of the
algorithm in Matlab
Figures 4 and 5 show the matlab code for the Carter and Kohn algorithm for artificial data on the state space
model shown in equation 4.1) assuming that = 0 = 1 = 0001 = 001 (See example2.m). As alluded to
above, the algorithm works in two steps. As a first step we run the Kalman filter to obtain | | . Lines 21 to
44 of the code are the Kalman filter equations and are identical to the example above. Note that the matrix ptt saves
| for each time period.1 The matrix beta_tt saves | for each time period. Line 47 specifies an empty matrix to
hold the draw of Line 48 specifies a × 1 vector from the (0 1) distribution to be used below. Line 51 draws
1 This is set up as a three dimensional matrix where the first dimension is time and the second two dimensions are the rows and
columns of the covariance matrix \ . In this example this matrix has dimension 500 × 1 × 1
5. THE CARTER AND KOHN ALGORITHM IN M ATLAB 75
Figure 6 plots the result of running this code and shows the draw for
6. THE GIBBS SAM PLING ALGORITHM FOR A VAR W ITH TIM E-VARYING PARAM ETERS 77
estimated β
0.4 t
true βt
0.2
−0.2
−0.4
−0.6
−0.8
−1
−1.2
50 100 150 200 250 300 350 400 450 500
which are just the intercept and AR coefficients in an AR regression for each individual coefficient in
̃ (the conditional distributions for linear regression models are described in chapter 1)
1
Step 4. Sample from its conditional posterior distribution. Conditional on the draw ̃ the posterior of is
³ ³ P ´´0 ³ ³ P ´´
inverse Wishart with scale matrix − 1 + =1 1
− − 1 + =1 1
− + 0 and
degrees of freedom + 0
Step 5. Repeat steps 2 to 4 times and use the last draws for inference. Note that unlike fixed coefficient VAR
models, this state space model requires a large number of draws for convergence (e.g. ≥ 100 000).
80 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
0.2
0.1
−0.1
−0.2
−0.3
−0.4
50 100 150 200 250 300 350 400 450 500
Figure 6. A draw from the conditional posterior distribution of using the Carter and Kohn algorithm
6.1. Matlab code for the time-varying parameter VAR. We consider a time-varying VAR model with
two lags using US data on GDP growth, CPI inflation and the Federal Funds rate over the period 1954Q3 to 2010Q2
(Example3.m). We use the time-varying VAR model to compute the impulse response to a monetary policy shock
at each point in time and see if the response of the US economy to this shock has changed over this period. The
code for this model can be seen in figures 7, 8, 9 and 10. Line 13 of the code sets the training sample equal to
the first 40 observations of the sample and line 16 calculates a fixed coefficient VAR using this training sample to
obtain 0 and 0|0 In calculating 0 on line 21 we set = 3510−4 Lines 25 and 26 set a starting value for
and Lines 29 and 30 remove the training sample from the data—the model is estimated on the remaining sample.
Lines 38 to 88 sample the time-varying coefficients conditional on and using the Carter and Kohn algorithm.
The code for this is exactly the same as in the previous example with some monor differences. First, note that the
VAR is expressed as = ( ⊗ ) ( ) + for each time period = 1 . This is convenient as it allows
us to write the transition equation in terms of ( ) i.e. the VAR coefficients in vectorised form at each point in
time. Therefore, on line 47 x is set equal to ( ⊗ ) . The second practical differences arises in the backward
0 0 0
recursion
³ on lines
´ 64 to 87. In particular (following earlier papers) we draw ̃ = [( 1 ) ( 2 ) ( ) ] for
̃ | ̃ but ensure that the VAR is stable at each point in time. If the stability condition fails for one time
period, the entire matrix ̃ = [( 1 )0 ( 2 )0 ( )0 ] is discarded and the algorithm tries again. With the
draw of ̃ in hand line 89 calculates the residuals of the transition equation Line 90 calculates the scale matrix
³ 1 1
´0 ³ 1 1
´
̃ − ̃ −1 ̃ − ̃ −1 + 0 and line 91 draws from the inverse Wishart distribution. Line 94 draws the VAR
error covariance from the inverse Wishart distribution. Note that we use a flat prior for in this example. One
past the burn-in stage we save the draws for ̃ , and . We use the saved draws to compute the impulse response
to a monetary policy shock and use sign restrictions to identify a monetary policy shock (lines 106 to 180). We
assume that a monetary policy shock is one that increases interest rates, decreases inflation and output growth. The
results for the time-varying impulse response are shown in 11. The 3-D surface charts show the impulse response
horizon on the Y-axis and the time-series on the X-axis. These results show little evidence of significant variation in
the impulse response functions across time for this dataset.
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 81
where is a × matrix containing a panel of macroeconomic and financial variables. denotes the Federal
Funds rate and are the unobserved factors which summarise the information in the data The first equation is
the observation equation of the model, while the second equation is the transition equation. Bernanke et al. (2005)
consider a shock to the interest rate in the transition equation and calculate the impulse response of each variable in
It is instructive to consider the state-space representation of the FAVAR model in more detail. We assume in this
example that the lag length in the transition equation equals 2 and there are 3 unobserved factors = {1 2 3 }
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 83
The left hand side of the observation equation 7.2 contains the dataset with the Funds rate as the last variable
(thus ̃ = { }) . is related to the three factors via the factor loadings where = 1 and
= 1 2 3 is related to the Federal Funds rate via the coefficients Bernanke et al. (2005) assume that are
non-zero only for fast moving financial variables. appears in the state vector (even though it is observed)
as we want it to be part of the transition equation. Therefore the last row of the coefficient matrix describes the
identity = The state vector contains the first lag of all state variables as we want two lags in the VAR
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 85
Note that this is simply a VAR(2) in 1 2 3 and written in first order companion form to make
consistent with the usual form of a transition equation (i.e. the transition equation needs to be in AR(1) form). Note
that: ⎛ ⎞
11 12 13 14 0 0 0 0
⎜ 12 22 23 24 0 0 0 0 ⎟
⎜ ⎟
⎜ 13 23 33 34 0 0 0 0 ⎟
⎜ ⎟
⎜ 14 24 23 44 0 0 0 0 ⎟
( ) = = ⎜⎜ ⎟ (7.5)
⎜ 0 0 0 0 0 0 0 0 ⎟ ⎟
⎜ 0 0 0 0 0 0 0 0 ⎟
⎜ ⎟
⎝ 0 0 0 0 0 0 0 0 ⎠
0 0 0 0 0 0 0 0
where the zeros result from the fact that the last 4 equations in the transition equation describe identities. Therefore
the matrix is singular in this FAVAR model. This implies that the Carter and Kohn recursion has to be generalised
slightly to take this singularity into account as discussed above. This modification implies that we use ∗ ∗ ∗ ∗+1
86 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
in equations 3.13 and 3.14 where ∗ ∗ ∗ ∗+1 denote the first rows of +1 . In our example = 4 as
the top 4 × 4 block of is non-singular and we draw three factors (the equation in the observation equation
is an identity).
The Gibbs sampling algorithm can be discerned by imagining the situation where the factors are observed.
Give the factors, the observation equation is just linear regressions of the form = + + and
the conditional distributions studies in Chapter 1 apply immediately to sample and (i.e. the elements of )
and . Similarly, given the factors, the transition equation is simply a VAR model. The conditional distributions in
Chapter 2 can be used to sample , and . Finally, given a draw for , and the model can be cast into
the state-space form shown in equations 7.2 and 7.4. Then the Carter and Kohn algorithm can be used to draw
from its conditional distribution. The Gibbs sampling algorithm consists of the following steps
Step 1 Set priors and starting values. The prior for the factor loadings is normal. Let = { }. Then
( ) ∼ (0 Σ ). The prior for the diagonal elements of is inverse Gamma and given by ( ) ∼
(0 0 ). The prior for the VAR parameters , and can be set using any of the priors for VARs
considered in the previous chapter. For example, one may consider setting an independent Normal inverse
Wishart prior. Collecting the VAR coefficients in the matrix and the non-zero elements of in the matrix
Ω this prior can be represented as () ⎛ ∼ (0 Σ )⎞and (Ω) ∼ (Ω0 0 ). The Kalman filter requires
1
⎜ ⎟
⎜ 2 ⎟
⎜ ⎟
⎜ 3 ⎟
⎜ ⎟
the initial value of the state vector = ⎜ ⎜ ⎟. One can use principal components to get an initial
⎟
⎜ 1−1 ⎟
⎜ 2−1 ⎟
⎜ ⎟
⎝ 3−1 ⎠
−1
estimate of 1 2 and 3 to set 0|0 The principal component estimate also provides a good starting
value for the factors = 1 2 and 3 One can arbitrarily set = 1 and Ω to an identity matrix to
start the algorithm.
Step 2. Conditional on the factors and sample the factor loadings = { } from their conditional
distributions. For each variable in the factor loadings have a normal conditional posterior (as described
in Chapter 1) ( | ) ∼ (∗ ∗ )
µ ¶−1 µ ¶
1 0 1 0
∗ = Σ−1 + Σ −1
0 +
µ ¶−1
1 0
∗ = Σ−1 +
where = {1 2 ,3 } if the series is a fast moving data series which has a contempora-
neous relationship with the Federal Funds rate (e.g. stock prices) and = {1 2 ,3 } if the series
is a slow moving data series which has no contemporaneous relationship with the Federal Funds rate
(e.g. GDP). Note that as 1 2 ,3 and are both estimated the model is unidentified. Bernanke et al.
(2005) suggest fixing the top 3 × 3 block of to an identity matrix and the top 3 × 1 block of to zero
for identification. See Bernanke et al. (2005) for more details on this issue.
Step 3. Conditional on the factors and the factor loadings = { } sample the variance of the er-
ror terms of the observation equation from the inverse Gamma distribution with scale parameter
( − )0 ( − ) + 0 with degrees of freedom + 0 where is the length of the estimation
sample.
Step 4. Conditional on the factors and the error covariance matrix Ω, the posterior for the VAR coefficients
(recall = { } the coefficients in the transition equation of the model) is normal (see Chapter 2) and
given as (| Ω) ∼ ( ∗ ∗ ) where 0 Σ
¡ ¢−1 ³ −1 ´
∗ = Σ−1 +Ω
−1
⊗ ̄0 ̄ Σ (0 ) + Ω−1 ⊗ ̄0 ̄ (̂)
¡ ¢−1
∗ = Σ−1 +Ω
−1
⊗ ̄0 ̄
where ̄ = {−1 −1 −2 −2 1} and ̂ is the OLS estimate of
Step 5. Conditional on the factors ¡ and the¢ VAR coefficients the error covariance Ω has a inverse Wishart
0¡ ¢
posterior with scale matrix − ̄ − ̄ + Ω0 and degrees of freedom +0
Step 6. Given and Ω the model can be cast into state-space form and then the factors are sampled via
the Carter and Kohn algorithm.
Step 7 Repeat steps 2 to 6 M times and use the last L values for inference
7.1. Matlab code for the FAVAR model. We estimate a FAVAR model using UK data over the period
1970Q1 to 2006Q1. We use 40 Macroeconomic and financial time series along with the Bank of England policy rate
to estimate the model and consider the impact of a monetary policy shock.
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 87
The Matlab code for this example (example4.m) can be seen in figures 12, 13, 14, 15, 16 and 17.
Lines 3 and 4 load the × panel of UK data and the variable names ( = 40). Line 6 reads a variable called
index. The first column is a × 1 vector which equals 1 if the corresponding data series in the panel has to be first
differenced. The second column is a × 1 vector which equals 1 if the corresponding data series is a fast-moving
variable (like an asset price) and will have a contemporaneous relationship with the policy interest rate i.e. 6= 0
for this variable. Lines 10 to 23 transform the data to stationarity and standardises it. Lines 25 to 27 read the bank
rate and standardises it. Line 35 extracts three principal components from the dataset to use as starting values for
the three factors in this example. Line 36 defines 0|0 =[pmat(1,:) z(1) zeros(1,N)]. Notice that there are 8 state
88 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
variables: 3 factors, the interest rate and the first lags of these 4 state variables and thus 0|0 is 1 × 8 Line 38 sets
0|0 as a 8 × 8 identity matrix. We arbitrarily set = 1 and Ω = to start the algorithm on lines 39 and 40. Note
that following Bernanke et al. (2005) we will not use prior distributions for the regression or VAR coefficients which
will imply that the conditional posteriors collapse to OLS formulae.
Lines 48 to 72 sample the factor loadings. The code loops through the 40 data series and selects each as the
dependent variable (line 52) to be regressed on the factors only for slow moving series (line 54) or the factors and
the policy interest rate for fast moving series (line 56). Line 58 calculates the mean of the conditional posterior
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 89
Lines 79 to 83 sample from the inverse Gamma distribution (using the function IG in the functions folder) with
prior degrees of freedom and the prior scale matrix set to 0 (hence using information from the data only). Lines 85
and 86 set up the left hand side and the right hand side variables for the VAR model using the factors (pmat) and the
policy rate. Lines 89 and 90 calculate the mean and variance of the conditional distribution of the VAR coefficients
(without prior information these are just OLS). Line 93 draws the VAR coefficients ensuring stability. Lines 102 and
103 draw the covariance matrix Ω from the inverse Wishart distribution. We now have a draw for all parameters
of the state space representation so we build the matrices necessary to cast the FAVAR into the state space form.
Lines 110 to 112 build the matrix seen in equation 7.2. Line 114 builds the covariance matrix of the error term
7. THE GIBBS SAM PLING ALGORITHM FOR A FACTOR AUGM ENTED VAR 91
Line 116 builds the matrix seen in equation 7.4. Line 118 builds the matrix F, while line 120 builds the matrix
. With the matrices of the state space representation in hand we start the Carter and Kohn algorithm by running
the Kalman filter from lines 123 to 153. Note a minor difference to the previous example is that the observation
equation now does not have a regressor on the right hand side. Hence on line 127 x is set equal to the matrix
Line 156 starts the backward recursion. Recall that the last 5 state variables represent identities and is singular.
Therefore we will only work with the first 3 rows (and columns for covariance matrices) of and +1 Lines
159 to 161 create ∗ ∗ ∗ . Lines 168 and 169 are the modified Carter and Kohn updating equations. Line 172 sets
the factors pmat equal to the last draw using the Carter and Kohn algorithm and we return to the first step of the
92 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
Gibbs sampler. Once past the burn-in period we calculate an impulse response of the factors to a shock to the bank
rate in the transition equation using a Cholesky decomposition of the covariance matrix to form the 0 matrix. Line
182 uses the observation equation of the model to calculate the impulse response of all the underlying data series.
The estimated impulse responses are shown in 18.
8. GIBBS SAM PLING FOR A M IXED FREQUENCY VAR 93
Transport
Consumption Govt −3
Consumption −4
x 10 xConstruction
10 Exports Imports Capital GDP Manufacturing storage & communication Total Output
5 0.02 0.05
0.01 0.05
3 0.05 0 0
0 0 0
0 2 −0.02
0 −0.02 −0.05 0
1 0 −0.04 −0.05 −0.05
−0.01 −0.04
0 −0.06 −0.1 −0.1
−0.06 −0.05
−0.02 −1 −5 −0.1
−0.05 −0.08 −0.08 −0.15
−0.15
0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20
Electricity,
Distribution, All gas, Manuf of Manuf coke/petroleum Manuf of chemicals RPI RPI All items
hotels & catering production industries water supply: food, drink & tobacco prod & man−made fibres Total Production RPI Total Food Total Non−Food other than seasonal Food
0.1
0 0 0.15
0 0.05 0.06 0
0 0.05 0.1
0.04 −0.02 0.1
0 −0.05
−0.05 0.02 −0.04 −0.02 0.05
−0.05 −0.05 0
0 0
−0.1 0 −0.06
−0.1 −0.04 −0.1 −0.05
−0.02 −0.1 −0.05
−0.08
−0.1
0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20
CPI GDP Deflator Wages RPIX RPI M4 Total M4 Households M4 PNFCs M4 OFCs M0
0 0 0 0 0.02
0 0.15
0.1
0.05 0.1 −0.05 0
−0.05 −0.05 −0.05 0
0.05
0 0.05 −0.1
0 −0.05 −0.02 −0.02
0 −0.1 −0.1
−0.05 −0.15 −0.1
−0.05 −0.05 −0.04
−0.1 −0.15 −0.15 −0.04
−0.2
0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20
M4 Lending M4L Households M4L PNFCs House Prices Dividend Yield PE Ratio FTSE ALL Share Index pounds/dollar pounds/euro pounds/yen
0 0 0 0.06 0.08
0 0.02
0.08 0.02 0.06 0.02
−0.05 0.04
−0.05 −0.05 0.06 0
−0.1 0.04 0
0.02 0
−0.1 0.04 −0.02 −0.02
−0.1 −0.2 0 −0.02 0.02
−0.1 0.02 −0.04 −0.04
−0.15 −0.02 0
−0.15 −0.3 0 −0.04 −0.06 −0.06
0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20 0 20
Figure 18. Impulse response of UK Macroeconomic series to a monetary policy shock using a
FAVAR model.
⎛ ⎞
1
⎜ 2 ⎟
⎜ ⎟
⎜ 3 3 ⎟
⎜ ⎟
⎜ 4 ⎟
⎜ ⎟
= ⎜
⎜ 5 ⎟
⎟
⎜ 6 6 ⎟
⎜ ⎟
⎜ ⎟
⎜ ⎟
⎝ ⎠
In other words, if one were to consider the VAR at monthly frequency then has missing observations in two months
of every quarter. However, we can treat these missing observations as unobserved monthly observations on and
re-write the model as a state space model (see Schorfheide and Song (2015)).
94 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
This equation states that when an observation for is available (in period 3 6 9 etc), the quarterly observed data
is an average of the unobserved monthly data. For example, the equation implies that 3 = 13̂ +13̂−1 +13̂−2
where ̂ denotes the unobserved monthly data on . This can be changed to reflect other assumptions. For example
it can be assumed that the observed data is the sum of monthly observations by changing 13 to 1. As is observed
at the monthly frequency the second row of the matrix specifies an identity.
When an observation for is unavailable, the observation equation changes to:
⎛ ⎞
0 1
⎜ 0 2 ⎟ ⎛ ⎞
⎜ ⎟ ̂
⎜ 3 3 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0 4 ⎟ µ ¶⎜ ⎟ µ ¶
⎜ ⎟ ⎜ ⎟
⎜ 0 5 ⎟= 0 0 0 0 0 0 ⎜ ̂−1 ⎟ + for = 1 2 4 (8.2)
⎜ ⎟ 0 1 0 0 0 0 ⎜ −1 ⎟ 0
⎜ 6 6 ⎟ ⎜ ⎟
⎜ ⎟ ⎝ ̂−2 ⎠
⎜ ⎟
⎜ ⎟ −2
⎝ ⎠
where ( ) is large. When observations are missing, the first row of is zero. The variance of is set to a large
number. Recall from the description of the update step of the Kalman filter that this assumption effectively means
that missing observations on are ignored when calculating the updated estimate of ̂ . Therefore, the observation
equation for this model changes over time depending on whether observations on are missing.
The transition equation stays fixed over time and is defined as
⎛ ⎞ ⎛ ⎞ ⎛ ⎞⎛ ⎞ ⎛ ⎞
̂ 1 1 2 3 4 5 6 ̂−1 1
⎜ ⎟ ⎜ 2 ⎟ ⎜ 1 2 3 4 5 6 ⎟⎜ −1 ⎟ ⎜ 2 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟⎜ ⎟ ⎜ ⎟
⎜ ̂−1 ⎟ ⎜ 0 ⎟ ⎜ 1 0 0 0 0 0 ⎟⎜ ̂−2 ⎟ ⎜ 0 ⎟
⎜ ⎟=⎜ ⎟+⎜ ⎟⎜ ⎟+⎜ ⎟ (8.3)
⎜ −1 ⎟ ⎜ 0 ⎟ ⎜ 0 1 0 0 0 0 ⎟⎜ −2 ⎟ ⎜ 0 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟⎜ ⎟ ⎜ ⎟
⎝ ̂ ⎠ ⎝ 0 ⎠ ⎝ 0 0 1 0 0 0 ⎠⎝ ̂ ⎠ ⎝ 0 ⎠
−2 −3
−2 0 0 0 0 1 0 0 −3 0
−1
⎛ ⎞
11 12 0 0 0 0
⎜ 12 22 0 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 0 0 0 0 ⎟
where () = = ⎜ ⎜ ⎟
⎜ 0 0 0 0 0 0 ⎟ ⎟
⎝ 0 0 0 0 0 0 ⎠
0 0 0 0 0 0
If ̂ was observed, then the model collapses to a BVAR. This observation provides the intuition behind the Gibbs
algorithm for this model. The algorithm consists of the following steps:
(1) Set priors and starting values. The prior for the VAR parameters , and can be set using any
of the priors for VARs considered in the previous chapter. For example, one may consider setting an
independent Normal inverse Wishart prior.
µ Collecting
¶ the VAR coefficients in the matrix and the non-
11 12
zero elements of in the matrix Ω = this prior can be represented as () ∼ (0 Σ )
12 22
⎛ ⎞
̂
⎜ ⎟
⎜ ⎟
⎜ ̂−1 ⎟
and (Ω) ∼ (Ω0 0 ). The Kalman filter requires the initial value of the state vector = ⎜ ⎟
⎜ −1 ⎟.
⎜ ⎟
⎝ ̂ ⎠
−2
−2
9. FURTHER READING 95
An initial estimate of ̂ can be obtained by using a simple interpolation method. For example, using
repeated observations to fill in the months with missing data.
(2) Conditional on ̂ and the error covariance matrix Ω, the posterior for the VAR coefficients (recall
= { } the coefficients ³ in the ´transition equation of the model) in vectorised form is normal (see
Chapter 2) and given as |̂ Ω ∼ ( ∗ ∗ ) where 0 Σ
¡ ¢−1 ³ −1 ´
∗ = Σ−1 +Ω
−1
⊗ ̄0 ̄ Σ (0 ) + Ω−1 ⊗ ̄0 ̄ (̂)
¡ ¢−1
∗ = Σ−1 +Ω
−1
⊗ ̄0 ̄
where ̄ = {̂−1 −1 ̂−2 −2 ̂−3 −3 1} and ̂ is the OLS estimate of using ̄ = {̂ } ̄
3. Conditional on ¡ ̂ and the
¢0 ¡ VAR coefficients
¢ the error covariance Ω has a inverse Wishart posterior with
scale matrix ̄ − ̄ ̄ − ̄ + Ω0 and degrees of freedom + 0
(3) Finally, given a draw of the VAR parameters, the state variable ̂ is drawn using the Carter and Kohn
algorithm. As in the previous example, the backward recursion needs a modification to account for the fact
that is singular. This modification implies that we use ∗ ∗ ∗ ∗+1 in equations 3.13 and 3.14 where
∗ ∗ ∗ ∗+1 denote the first rows of +1 . In our example = 2 as the top 2 × 2 block of
is non-singular.
(4) Repeat steps 2 to 4 until convergence
The code for the mixed frequency VAR is provided in figures 19 to 21. This code is based on artificial data
generated for two variables at the monthly frequency. Lines 20 to 26, average the observations of the first variable to
produce a quarterly series . The point of the example is to test if the mixed frequency VAR (that uses quarterly
data for the first variable and monthly data for the second variable) outlined above can be used to recover the
original monthly series. Lines 36 and 37 form an initial estimate of ̂ using repeated observations. Note that the
lag length is set to 3. This is the minimum lag length allowed by the structure of the observation equation. Lines
58 to 79 set the priors for the VAR model via artificial or dummy observations (see Chapter 2). Lines 83 to 87 set
the initial state and its covariance to be used in the Kalman filter. The Gibbs sampler begins on line 90. The first
step of the sampling algorithm is coded on lines 92 to 103 which draws the VAR coefficients. The VAR covariance
is drawn on lines 105 to 107 from the inverse Wishart distribution. The final step of the algorithm using the Carter
Kohn algorithm begins on line 109 with the Kalman filter. The matrices of the transition equation of the state space
system are created on lines 110 to 112. Within the Kalman filter on line 121, we check if is missing and set the
matrix and the variance of error term accordingly. The backward recursion is on lines 154 to 176. Note that once
̂ is drawn the data for the VAR is updated on lines 178 to 182.
Figure 22 shows that the posterior estimates of ̂ are close to the underlying true data.
9. Further reading
• Kim and Nelson (1999) chapter 3 is an excellent intuitive introduction to state space models.
• Hamilton (1994) chapter provides a more formal derivation of the Kalman filter.
• Kim and Nelson (1999) chapter 8 provides a detailed description of Gibbs sampling for state space models.
• Code and a monograph by Gary Koop:
https://sites.google.com/site/garykoop/home/computer-code-2.
96 3. GIBBS SAM PLING FOR STATE SPACE M ODELS
The recent financial crisis has again highlighted the fact that relationships between economic variables may be
subject to sudden shifts. The time-varying parameter model introduced in the previous chapter offers one method
for dealing with such structural change. However, TVP models may be ill suited to deal with this problem if the
structural change is abrupt. This chapter discusses the estimation of Markov switching models that are well equipped
to deal with abrupt regime shifts. As in the case of state-space models, a Gibbs sampling approach to estimation offers
a powerful method to estimate these models. The material in this chapter draws heavily on material in Hamilton
(1994) and Kim and Nelson (1999).
1. Switching regressions
Before considering Markov switching regressions, recall that a basic regression with dummy variables (or a
switching regression) is defined as:
= + ˜ (0 2 ) (1.1)
= 0 (1 − ) + 1
2 = 20 (1 − ) + 21
where for = 1 2 denotes a dummy variable that indicates when a structural (or regime) shift takes place.
If is known then this just a linear regression and methods introduced in Chapter 1 apply. We are interested in
a situation where is unknown — i.e. the researcher has to estimate when the regime change occurred and the
associated regression parameters in each regime. It is instructive to consider how the likelihood function of the model
can be obtained. The likelihood function at time in this case is defined as
1
X
( |−1 ) = ( | = −1 ) × ( = |−1 ) (1.2)
=0
where denotes information at time . Here the first term on the RHS is the likelihood conditional on the
value of ³. The second term´ is the probability of being in the regime. Thus for regime = 1 this equals:
0 0
√ 1 2 exp −( − 12) 2( − 1 ) Pr [ = 1]. Therefore the likelihood function can be written as
2 1 1
probability
Likelihood
µ 0 0¶
1 − ( − 0 ) ( − 0 )
( |−1 ) = p exp Pr [ = 0] (1.3)
2 20 2 20
µ ¶
1 − ( − 1 )0 ( − 1 )0
+p exp Pr [ = 1]
2 21 2 21
P
The log likelihood of the model is =1 ln ( |−1 ). The key thing to note about equation 1.3 is that it represents
a weighted average of the likelihood conditional on each regime with weights given by the probability of being in that
regime at a given time. Thus to calculate the likelihood function one needs to calculate the term Pr [ = ] for each
. As is unobserved, this problem is similar to the estimation of an unobserved state variable dealt with in the
previous chapter. In other words, a filtering algorithm (like the Kalman filter) is required. But before considering
this approach, one needs to define the ‘transition equation’ for . It is this choice which leads to the definition of
Markov switching models.
= 0 (1 − ) + 1
2 = 20 (1 − ) + 21
101
102 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
We assume that is unobserved ( but takes on two values 0 and 1) and follows a first order Markov chain. In other
words, depends on −1 with associated probabilities given by
Pr [ = 0|−1 = 0] = 00
Pr [ = 1|−1 = 0] = 01 = 1 − 00
Pr [ = 1|−1 = 1] = 11
Pr [ = 0|−1 = 1] = 10 = 1 − 11
Thus refers to the probability that the current regime is given that the regime in the previous period was .
Values for 00 11 close to 1 imply that once in one of these regimes, the process is highly likely to remain in the same
regime for some time — i.e. the regimes are persistent. These transition probabilties can be conveniently summarised
in a transition probability matrix µ ¶
00 10
=
01 11
Note that the columns of this matrix sum to 1. Of course the model can be extended to allow for regimes. For
example in the case of three regimes can be equal to 0 1 or 2 with transition probability matrix:
⎛ ⎞
00 10 20
= ⎝ 01 11 21 ⎠
02 12 22
A filtering algorithm to calculate the probability terms Pr [ = | ] is described in Hamilton (1994). Denote this
× 1 vector of probabilities as | where the subscript denotes the estimate at time given information at that
time period . The Hamilton filter proceeds in two steps which are applied at each point in the sample = 1 2 .
Assume that an intial value −1|−1 is available. The following steps are applied time :
(1) Prediction Step: The state variable is predicted one period forward
|−1 = −1|−1 (2.1)
where is the matrix of transition probabilites. Note that |−1 is an estimate of ( = |−1 ). In other
µ ¶
Pr [ = 0|−1 ]
words, in the two regime case it is a 2 × 1 vector |−1 =
Pr [ = 1|−1 ]
(2) Update Step: Update the predicted estimate with information in the data at time , i.e. estimate | =
( | )Note that ( | ) = ( |−1 ). This can be obtained via formula
( | = −1 ) ¯ ( = |−1 )
| = ( |−1 ) = −1
(2.2)
X
( | = −1 ) ¯ ( = |−1 )
=0
where ¯ denotes element⎛by element ³ multiplication. In the ´ two regime case, the numerator of equation
⎞
1 −( − 0 )0 ( − 0 )0
√ exp × Pr [ = 0|−1 ]
⎜ 220 ³
2 20
´ ⎟
2.2 is simply the vector: ⎝ 0 0 ⎠. This vector denotes the
√ 1 2 exp −( − 12) 2( − 1 ) × Pr [ = 1|−1 ]
2 1 1
joint density ( |−1 ). Notice that the denominator of of equation 2.2 sums across the M regimes and
the weighted average is the marginal density ( |−1 ) or the likelihood function. Thus equation 2.2 is
simply a division of a joint density by the marginal to obtain the conditional distribution. | is the input
on the RHS of equation 2.1 in the next time period.
To start the filter, the initial value 0|0 can be calculated as the unconditional probability
0|0 = = (0 )−1 0
µ ¶ µ ¶
− 0×1
where = and = .
11× 1
Applying these two steps provides the likelihood function of the model ( |−1 ) and | or ( |−1 ) for
= 1 2 . These latter probabilities will be used in the Gibbs sampling algorithm for estimating this model.
(1) Conditional on 2 and ̃ sample from its conditional posterior distribution.
(2) Conditional on and ̃ sample 2 from its conditional posterior distribution.
(3) Conditional on 2 and ̃ sample from its conditional posterior distribution.
(4) Conditional on 2 and sample ̃ from its conditional posterior distribution.
With a value of ̃ in hand, the model collapses to a set of linear regressions on subsamples:
0 = 0 0 + 0 0 ˜ (0 20 )
1 = 1 1 + 1 1 ˜ (0 21 )
where 0 0 and 1 1 represent the data selected when ̃ = 0 and ̃ = 1 respectively.
With a normal prior for 0 and 1 , the conditional posterior in step 1 is also normal and is simply the posterior
for the linear regression model conditional on knowing the error variance (see equation 2.10). The only difference is
that given two regressions apply, one in each sub-sample. Similarly, with an Inverse Gamma prior for 20 and 21 ,
the conditional posterior in step 2 is also Inverse Gamma, i.e. the posterior for the error variance of a regression with
known coefficients (see equation 2.16).
Therefore the first two steps of the algorithm are standard. Steps 3 and 4 require new concepts. We turn to
these next.
3.1. The conditional posterior for . A conjugate prior for each column of is the Dirichlet distribution.
With regimes, this distribution depends on parameters for = 1 2 . The PDF is: (1 −1 1 )
P
∝ Π −1
=1 with given implicitly by 1− −1
=1 . The mean of the distribution is given as ̃ while the variance
P
can be calculated as ̃2(̃− )
(̃+1)
where ̃ = =1 .
µ ¶
00 10
Consider the case of two regimes with = . Then an example of the Dirichlet prior might be
01 11
(00 ) ˜ (00 01 ) and (11 ) ˜ (11 10 ) where () represents the Dirichlet distribution. Suppose that we
choose 00 = 15 and 01 = 1. This implies that the mean of the prior for 00 equals 094, while the variance is 0003.
Therefore, this prior would represent the strong belief that regime 0 is quite persistent.
Combining this prior with the likelihood results in a conditional posterior which is also Dirichlet
¡ ¢
( | ) ˜ 1 + 1 2 + 2 + (3.2)
where = 1 2 refers to the column numbers of the transition probability matrix. Thus, in the two regime
example (00 |) ˜ (00 + 00 01 + 01 ) and (11 |) ˜ (11 + 11 10 + 10 )
The parameter refers to the number of times regime is followed by regime . This can be counted using the
draw of ̃ . Given this state variable, this conditional posterior does not depend on the data or the other parameters
in the model.
Random numbers can be drawn from the Dirichlet distribution using the following algorithm:
Algorithm 4. To draw from a M dimensional Dirichlet distribution (1 −1 1 ), first draw 1
from the Gamma distribution with shape parameter 1 . Then the quantity provides a draw from the
=1
Dirichlet distribution.
3.2. The conditional posterior of ̃ . This conditional posterior can be derived using the same method
used to derive the Carter and Kohn³recursion for state-space ´ models (see Kim and Nelson (1999)). We want to
derive the conditional distribution ̃ | 2 ̃ where ̃ = [1 ] and the data is denoted by the matrix
̃ = [1 1 1 1 ] ³ ´ ³ ´
This distribution can be simplified in the following way. First note that ̃ |̃ = 1 2 |̃ where
³ ´
we suppress conditioning on model parameters to make the notation simpler. The joint density 1 2 |̃
can be factored as
³ ´
1 2 |̃
³ ´ ³ ´
= |̃ 1 2 −1 |̃
³ ´ ³ ´ ³ ´
= |̃ −1 | ̃ 1 2 −2 |̃ −1
³ ´ ³ ´ ³ ´ ³ ´
= |̃ −1 | ̃ −2 | −1 ̃ 1 |2 −1 ̃
and the state variable at other time periods do not contain any additional information about 1 . This implies that
the last line can be written as
³ ´ ³ ´ ³ ´ ³ ´
|̃ −1 | ̃ −1 −2 | −1 ̃ −2 1 |2 ̃1
³ ´ Y
−1 ³ ´
= |̃ |+1 ̃
=1
Therefore, the conditional posterior for the state variable is given by:
³ ´ ³ ´ Y
−1 ³ ´
̃ | 2 ̃ = |̃ |+1 ̃ (3.3)
=1
The key task is to sample ̃ from this density. As in the case of state-space models in the previous chapter this
sampling proceeds in two steps:
³ ´
(1) Drawing from |̃ : Run the Hamilton filter to obtain the probability | or ( |−1 ) for
³ ´
= 1 2 At time , one can draw from the discrete distribution |̃ using | as the
³ ´
probability associated with each value takes. In the two regime case, one calculates Pr = 0|̃ =
³ ´
( =0|−1 )
−1 and draws ˜ (0 1). If ≥ Pr = 0|̃ , then = 1, else = 0.
=0 ( =|−1 )
³ ´ ³ ´ ( +1 |̃ )
(2) Drawing from |+1 ̃ . Note that |+1 ̃ = |̃ . The numerator can again be
³ ´ (³ +1 ) ´ ³ ´
factored into a ‘conditional’ and ‘marginal’: +1 |̃ = +1 | ̃ |̃ . Note that as all
information
³ about the ´ states contained ³in ̃ is´ present in , ̃ can be removed from the first term on the
RHS: +1 |̃ = (+1 | ) |̃ . Therefore
³ ´
³ ´ (+1 | ) |̃
|+1 ̃ = ³ ´
+1 |̃
³ ´
∝ (+1 | ) |̃
³ ´
As discussed in Kim and Nelson (1999), (+1 | ) is just the transition probability while |̃ refers
³ ´
to the ‘filter’ probability | = ( |−1 ). Drawing from (+1 | ) |̃ proceeds backwards
in time
µ starting from
¶ − 1 and going back to period 1. Consider the two regime case. Recall that
00 10
= and denote the two elements of ( |−1 ) as Pr [ = 0| ] and Pr [ = 1| ]. If
01 11
+1 = 0, then at time one calculates
h i
Pr = 0|+1 = 0 ̃ = 00 × Pr [ = 0| ]
h i
Pr = 1|+1 = 0 ̃ = 10 × Pr [ = 1| ]
This is repeated for − 1 − 2 1 to deliver a draw from the conditional posterior of ̃ .
3.2.1. Label switching. The labels attached to each regime (for e.g. regime 0 and 1 in the 2 regime model) can
switch during this algorithm. This is because the value of the likelihood is unaffected by switching the labels of the
regime. Therefore, without some identifying restrictions, the marginal posteriors obtained from this algorithm can
be be multi-modal. A simple way to proceed is to assume that one regime is associated with a higher (lower) value
of a particular parameter. For example, one can assume that 20 21 and use rejection sampling to ensure that the
saved draws are consistent with this condition.
4. THE HAM ILTON FILTER IN M ATLAB 105
obtain ( | = −1 ) ¯ ( = |−1 ) (see equation 2.2). Line 44 sums this object across regimes and finally the
updated estimates | are obtained on line 45. Note that the output from line 45 is used as input in the RHS of the
prediction equation (line 41) in the next time period. Also note that in this demonstration, the filter is run using the
true values of the model parameters. This will change when we run the full Gibbs algorithm below.
7. Extensions
In this section, we consider several extensions of the basic MS model described above.
Figure 7. Output from 10,000 iterations of the Gibbs Sampler for the MS model
(1) Conditional posterior of ̃ . When running the Hamilton filter, the conditional likelihood for observation
¡ ¢05 ¡ 0 ¢
changes to ( | = −1 ) = 2 −05 det Ω−1 exp −05 ( − ) Ω−1
( − ) where de-
notes the lags and the intercept while is the matrix of coefficients including and the intercepts in one
+ 1 × matrix (see equation 2.2). There is no change in the backward recursion or the draw of the
transition probabilties.
(2) Conditional posterior of the VAR parameters. Given ̃ , the VAR parameters are drawn in each regime
by simply using the conditional posterior distributions ³ ´discussed in Chapter 2. In particular, given a
Normal prior for the VAR coefficients ( )˜ ̃0 and a inverse Wishart prior for the error co-
¡ ¢
variance (Ω )˜ ̄ ¡ the conditional
¢ posterior in each regime is Normal for the VAR ¡ coefficients
¢
( |Ω ) ˜ ∗ ∗ and inverse Wishart for Ω : (Ω | ) ˜ Ω̄ + .
Note that:
¡ ¢−1 ³ −1 ´
∗ = −1 + Ω−1 ⊗ 0
̃ 0 + Ω−1
⊗ 0
̂ (7.1)
¡ ¢−1
∗ = −1 + Ω−1 0
⊗
where ̂ is the OLS estimate in regime = and denotes selected when ̃ = . In addition
³ ´0 ³ ´
Ω̄ = − ̂ − ̂ + ̄ . Note that this draw can also be implemented if the priors on
the VAR parameters are implemented via dummy observations. As discussed in Chapter 2, the conditional
posteriors are simpler in this case.
The code for this algorithm is shown in figures 8 to 11. First note that lines 95 and 96 uses the multivariate
normal density as mentioned above. The second change is on line 160 onwards. Once the sample is divided into the
two regimes, the VAR parameters are drawn separately. Note that in this code, the prior on the VAR parameters is
implemented via dummy observations. The conditional posteriors have a simpler form in this case (see Chapter 2).
7.2. AR model with switching mean and variance. Consider the following two regime MS model
³ ´
− ∗ = −1 − −1
∗ + ˜ (0 2 ) (7.2)
µ ¶
∗ ∗ 00 10
where = 0 1 denotes the state variable with transition probability matrix = . Note that unlike
01 11
∗
the simple MS model considered above, a lag of the state variable −1 appears on the RHS. Therefore, in order to
calculate the likelihood function using the procedure described in equation 1.3, one needs to be able to track both
∗ and −1
∗
. As described in Hamilton (1994), this easily achieved by defining a new state variable that takes on
112 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
values 1 2 3 4
= 1 if ∗ =0 and ∗
−1 =0
= 2 if ∗ =1 and ∗
−1 =0
= 3 if ∗ =0 and ∗
−1 =1
= 4 if ∗ =1 and ∗
−1 =1
7. EXTENSIONS 113
The transition probability matrix for the new state variable is given as:
⎛ ⎞
11 21 31 41
⎜ 12 22 32 42 ⎟
= ⎜
⎝ 13
⎟ (7.3)
23 33 43 ⎠
14 24 34 44
⎛ ⎞
00 0 00 0
⎜ 01 0 01 0 ⎟
= ⎜
⎝ 0
⎟
10 0 10 ⎠
0 11 0 11
114 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
This matrix implies, for example that Pr [ = 1|−1 = 2] = 0. Given that when = 2 the regime is defined as
∗ = 1 and −1
∗
= 0 and when = 1 the regime is defined as ∗ = 0 and −1 ∗
= 0, regime 2 cannot be followed by
regime 1 as it would imply a contradiction. We describe the Gibbs algorithm for this 4 regime model below. Note that
as the lags in this model increase, the number of (artificial) regimes increase. Similarly, if ∗ follows 2 regimes,
then the re-parameterised model is more complex. While estimation becomes more computationally intensive, the
algorithm described below still applies.
Gibbs sampling algorithm. The algorithm involves sampling from the following conditional posterior distributions:
¡ ¢
(1) Sample from | 2 . As before, this involves the following two steps:
7. EXTENSIONS 115
(a) Run the Hamilton filter to obtain | . Note that the conditional likelihoods ( | ) in the update
step are given by:
³ 0
´
(( −0 )−(−1 −0 ))0
( | = 1) = √ 1 2 exp −(( −0 )−(−1 −0 )) 2
2 0
2 0 ³ ´
1 −(( −1 )−(−1 −0 ))0 (( −1 )−(−1 −0 ))0
( | = 2) = √ 2 exp 2
2 1
2 1 ³ ´
1 −(( −0 )−(−1 −1 ))0 (( −0 )−(−1 −1 ))0
( | = 3) = √ 2 exp 2
2 0
2 0 ³ ´
1 −(( −1 )−(−1 −1 ))0 (( −1 )−(−1 −1 ))0
( | = 4) = √ 2 exp 2 2
2 1 1
Apart from this change, the remaining steps of the filter are unchanged.
116 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
³ ´ ³ ´ Y−1 ³ ´
(b) Draw from ̃ | 2 ̃ = |̃ |+1 ̃
=1 ³ ´
(i) At time , one can draw from the discrete distribution |̃ using | as the probabil-
³ ´
ity associated with each value takes. In the four regime case, one calculates Pr = 1|̃ =
³ ´
( =1|−1 )
4 and draws ˜ (0 1). If ≤ Pr = 1| ̃ , then = 1. Otherwise cal-
=1 ( =|
³ −1 ) ´ ³ ´
( =2|−1 )
culate Pr = 2|̃ = 4 ( =| ) and draw ˜ (0 1). If ≤ Pr = 2|̃ , then
=2
³ −1 ´
= 2. Otherwise calculate Pr = 3|̃ = 4 (( =3|−1 )
and draw ˜ (0 1). If
=|−1 )
³ ´ =3
∗ = −1
∗
+ ˜ (0 1)
where
− ∗ −1 − −1
∗ = −1 =
This transformed regression has fixed coefficients an error term with a variance of 1. Given an normal prior
for , the conditional posterior is simply the one for a linear regression with a known error variance (see
Chapter 1). ¡ ¢
(4) Sample from | 2 . Re-write the AR(1) model as
³ h i ´ ³ h i ´
∗ ∗
− −1 0 ̃ = 0 (1 − ) 1 ̃ = 1 (1 − )
= + + ˜ (0 1)
This simply a linear regression of −
−1
on 2 dummy variables. As in step 3, with a normal prior for
0 1 , the posterior of regression with a known error variance applies. ³ ´
¡ ¢
(5) Sample from 2 | We assume an inverse Gamma prior for 20 21 : Γ−1 ̃20 20 We calculate
¡ ¢ ∗
the residuals = − − − −1 . These residuals can be split into the two µ regimes given by ̃¶ and
[0]0 [0]
0 0 +
the draw for 20 and 21 is made seperately from inverse Gamma distributions: Γ−1 ̃0 + 2 2 and
7. EXTENSIONS 117
µ ¶
[1]0 [1]
̃0 +1 0 + []
Γ−1 2 2 where denotes the residual selected in regime i, while denotes the number of
observations in regime i.
Figures 12 to 15 present the Matlab code for this model (example5.m). This example is based on artificial data
generated on lines 4 to 31. Lines 34 to 59 set priors and starting values. The Hamilton filter can be seen on lines 72
to 98. Note the 4 conditional likelihoods that enter the update step on line 93. The backward recursion is coded on
lines 101 to 178. As discussed above, as the model has four regimes, minor changes are required to the way the state
variable is drawn. These are highlighted in the figure.
µ Line 185 ¶ constructs the original state variable and lines 189
00 10
to 199 draw the transition probability matrix ∗ = . The function matf (based on James Hamilton’s
01 11
118 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
⎛ ⎞
00 0 00 0
⎜ 01 0 01 0 ⎟
code), constructs = ⎜ ⎟ ∗ ∗
⎝ 0 10 0 10 ⎠ from . Its arguments are number of regimes and number of
0 11 0 11
lagged states. Lines 203 to 210 draw the AR coefficient, while 0 and 1 are drawn on lines 213 to 219. Lines 221
onwards split the residual series into the two regimes and draws 20 and 21 .
7.3. Markov switching model with time varying transition probabilties. In the MS models considered
so far, the transition probabilties are fixed. Following Filardo and Gordon (1998) amongst others, this assumption
can be relaxed and the transition probabilities can be made functions of exogenous regressors. Consider the two
7. EXTENSIONS 119
regime MS model:
= + ˜ (0 2 ) (7.4)
= 0 (1 − ) + 1
2= 20 (1 − ) + 21
µ ¶
00 ( ) 10 ( )
The transition probabilties are now given by = where denotes a set of regressors.
01 ( ) 11 ( )
The evolution of the state variable can be described using a Probit model
( = 0) = ∗ 0
∗ = 0 + −1 + 1 −1 + ˜ (0 1)
120 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
where ∗ is an unobserved latent variable. Given the normality of the transition probabilties can be calculating
using the normal CDF.
Pr [ = 0|−1 = 0] = Pr [ − 0 − −1 − 1 −1 ]
Recall that −1 = 0 and denote the normal CDF by Φ () :
Pr [ = 0|−1 = 0] = Φ (− 0 − −1 )
Similarly
Pr [ = 1|−1 = 1] = Pr [ ≥ − 0 − −1 − 1 −1 ]
= 1 − Φ (− 0 − −1 − 1 −1 )
7. EXTENSIONS 121
The Gibbs sampling algorithm for this model thus involves extra steps to draw ∗ and the coefficients Γ = [ 0 1 ]
There are only minor modifications to the remaining steps of the algorithm. The algorithm samples from the following
conditional posterior distributions:
¡ ¢
(1) Sample from | 2 Γ ∗ . The only modification required to this step is the fact that there is a
different transition probability matrix at each point in time. This needs to be taken into account while
running the Hamilton filter—the prediction step is now given as
|−1 = −1|−1
³ ´
Similarly, the backward recursion using (+1 | ) |̃ is modified as (+1 | ) = +1 is different
for = − 1 ¡− 2 1. ¢
(2) Sample from ∗ | 2 Γ . Following Albert and Chib (1993), ∗ can be sampled from the following
truncated normal distributions for = 1 2
∗ ˜ ( 1) if = 1
∗ ˜ ( 1) if = 0
where ( 1) is the ( 1) distribution left truncated at zero, while ( 1) is the ( 1) distri-
bution right truncated
¡ at zero. Note ¢ that = 0 + −1 + 1 −1 .
(3) Sample from Γ|∗ 2 . Given ∗ the probability equation is a simple linear regression with a
known error variance:
∗ = 0 + −1 + 1 −1 + ˜ (0 1)
Given a normal prior (Γ0 ΣΓ ) the conditional posterior is also Normal ( )
¡ ¢−1
= Σ−1 0
Γ + ̃ ̃
¡ ¢
= Σ−1 0 ∗
Γ Γ0 + ̃
7.4. A regression with Markov switching coefficients and a structural break in the variance. As a
final extension we consider the following model:
As we condition on this change is simple to implement. There is no change in the backward recursion
used to draw .
7. EXTENSIONS 123
¡ ¢
(2) Sample from | 2 . As in step 1, the conditional likelihoods in the Hamilton filter need
to take into account the fact that the regression coefficients switch independently. Hence the conditional
likelihoods are:
³ 0 0
´
( | = 0) = √ 1 2 exp −( − 02) 2( − 0 )
2 0 ³ 0
´ if = 0
1 −( − 0 )0 ( − 0 )0
( | = 1) = √ 2 exp 2 12
2 1
³ 0 0
´
( | = 0) = √ 1 2 exp −( − 12) 2( − 1 )
2 0 ³ 0
´ if = 1
1 −( − 1 )0 ( − 1 )0
( | = 1) = √ 2 exp 2 2
2 1 1
124 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
µ ¶
1
We also assume that 1|1 = . There is no change to be made in the backward recursion.
¡ ¢ 0
(3) | 2 . Given a Dirichlet prior, the columns of are drawn as in section 3.1.
¡ ¢
(4) | 2 . Given a Dirichlet prior, the first columns of is drawn as in section 3.1.
¡ ¢
(5) | 2 . Define = [ = 0] 0 + [ = 1] 1 and write the regression as
= + ˜ (0 1)
Given , a regression with unit error variance applies when = 0 and = 1 and is drawn easily from
its (Normal) conditional posterior.
8. FURTHER READING 125
8. Further reading
• A classic paper on the Bayesian approach to MS models: Chib (1996).
126 4. GIBBS SAM PLING FOR M ARKOV SW ITCHING M ODELS
1. Introduction
The Gibbs sampling algorithm relies on the availability of conditional distributions to be operational. In many
cases (of practical relevance) conditional distributions are not available in closed form. An important example of
such a situation is the estimation of Dynamic Stochastic General Equilibrium (DSGE) models where the conditional
distribution of different parameter blocks is unavailable. In such cases an algorithm more general than the Gibbs
sampler is required to approximate the posterior distribution. The Metropolis Hastings algorithm offers such an
alternative. This chapter introduces this algorithm and discusses its implementation in Matlab for a number of
important cases. The algorithm is applied to DSGE models in the next chapter.
1 This essentially means that we accept the draw with probability if this experiment is repeated many times. For e.g if = 01
and if we 1000 replications we should expect 100 of the 1000 draws to have
133
134 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
+1
where ∼ (0 Σ) is a ס1 vector. Note
¢ that ¡=Φ − Φ¢ is normally distributed.
¡ As ¢the normal
¡ distribution
¢
+1 +1
is symmetric, the density Φ − Φ equals Φ − Φ . In other words, Φ+1 |Φ = Φ |Φ+1 under
this random walk candidate density and the formula for the acceptance probability in equation 2.2 simplifies to
à ¡ ¢ !
Φ+1
= min 1 (3.2)
(Φ )
The random walk MH algorithm, therefore, works in the following steps:
Step 1 Specify a starting value for the parameters Φ denoted by Φ0 and fix Σ the variance of shock to the random
walk candidate generating density.
Step 2 Draw a new value for the parameters Φ using
Φ = Φ + (3.3)
where Φ = Φ0 for the first draw
Step 3 Compute the acceptance probability
à ¡ ¢ !
Φ
= min 1 (3.4)
(Φ )
If v (0 1), then retain Φ and set Φ = Φ , otherwise retain Φ
Step 4 Repeat steps 2 and 3 M times and use the last L draws for inference.
Note that Σ the variance of is set by the researcher. A higher value for Σ could mean a lower rate of acceptances
across the MH iterations (i.e. the acceptance rate is defined as the number of accepted MH draws divided by the
total number of MH draws) but would mean that the algorithm explores a larger parameter space. In contrast, a
lower value for Σ would mean a larger acceptance rate with the algorithm considering a smaller number of possible
parameter values. The general recommendation is to choose Σ such that the acceptance rate is between 20% to 40%.
We consider the choice of Σ in detail in the examples described below.2
3.1. Estimating a non-linear regression via the random walk MH algorithm. As an example, consider
the estimation of the following non-linear regression model
³ ´
= 1 2 + ∼ (0 2 ) (3.5)
and for the moment assume no prior information is used in estimating 1 2 and 2 so the posterior distribution
coincides with the likelihood function. Our aim is to draw samples from the marginal posterior distribution of the
parameters. As the model is non linear, the results on the conditional distributions of the regression coefficients
shown in Chapter 1 do not apply and the MH algorithm is needed. We proceed in the following steps:
Step 1 Set starting values for Φ = {1 2 , 2 } These starting values could be set, for e.g, by estimating a log
linearised version of equation 3.5 via OLS. The variance of the candidate generating density Σ can be set
as the OLS coefficient covariance matrix Ω̂ times a scaling factor i.e Σ = Ω̂ × Note that Ω̂ provides a
rough guess of how volatile each parameter is. The scaling factor lets the researcher control the acceptance
rate (a higher value for would mean a lower acceptance rate). Note that in this simple model the choice
of starting values may not be very important and the algorithm would probably converge rapidly to the
posterior. However, in the more complicated (and realistic) models considered below this choice can be
quite important.
Step 2 Draw a new set of parameters from the random walk candidate density
Φ = Φ + (3.6)
µ ¶
(Φ )
Step 3 Compute the acceptance probability = min (Φ ) 1 Note that the target density () is the likeli-
hood function in this example. The log likelihood function for this regression model is given by
⎛³ ³ ´´0 ³ ³ ´´ ⎞
2 2
⎜ − 1 − 1 ⎟
ln ( |Φ) = − ln 2 − 2 − 05 ⎝ ⎠ (3.7)
2 2 2
Figures 1 and 2 show the matlab code for this example (example1.m). Lines 3 to 9 generate artificial data for the
non-linear regression model assuming that 1 = 4 2 = 2, 2 = 1 Line 11 sets the starting
⎛ values for these parameters
⎞
1 0 0
arbitrarily. Lines 20 to 22 set the variance of the random walk candidate density as Σ = ⎝ 0 2 0 ⎠ ×scaling
0 0 01
1 2
factor where and are OLS estimates of the variance of 1 and 2 . Line 29 sets the variable naccept which
will count the number of accepted draws. Hence the acceptance rate is naccept/REPS. Line 30 starts the loop for the
2 See Chib and Ramamurthy (2010) for a more efficient version of the basic algorithm described above.
136 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
MH algorithm. Line 32 draws the new value of the parameters from the random walk candidate density. Note that
there is nothing intrinsic in this step that stops the new value of 2 from being less than zero. Therefore lines 35 to
37 set the value of the log likelihood to a very small number if a negative 2 is drawn thus ensuring that this draw
is not going to be accepted. Alternatively one can set the acceptance probability to 0 when a negative value for 2
is drawn. Lines 44 to 46 calculate the log likelihood at the old draw. Line 49 calculates the acceptance probability.
Line 53 checks if the acceptance probability is bigger than a number from the standard uniform distribution. If
this is the case we retain the new draw of the parameters.Figure 3 shows all the draws of the model parameters. The
algorithm is close to the true values of these parameters after a few hundred draws.
3. THE RANDOM WALK M ETROPOLIS HASTINGS ALGORITHM 137
B1
B
2
0 σ2
True B1
True B2
2
True σ
−1
0 5000 10000 15000
Metropolis Hastings Draws
3.2. Estimating a non-linear regression via the random walk MH algorithm (incorporating prior
distributions). We consider the same non-linear regression model examined in the previous section but now incor-
porate prior distribution for the regression parameters. We assume that the regression coefficients = {1 2 }
have a normal prior () ∼ (0 Σ0 ). For convenience, we set a prior for the precision, the reciprocal of the
¡ ¢ 0 0
variance. The Gamma prior 1 2 with a prior scale parameter 2 and degrees of freedom 2 . The random walk
MH algorithm now consists of the following steps:
algorithm is needed. We proceed in the following steps:
¡ ¢
Step 1 Set the parameters of the prior distributions () and 1 2 Set starting values for Φ = {1 2 , 2 }
Finally set the variance of the candidate generating density Σ.
Step 2 Draw a new set of parameters from the random walk candidate density
Φ = Φ + (3.9)
µ ¶
(Φ )
Step 3 Compute the acceptance probability = min (Φ ) 1 Note that the target density () is the pos-
terior density in this example as we have prior distributions for our parameters. Recall from chapter 1
that the Bayes law states that he posterior distribution is proportional to the likelihood times the prior.
Therefore we need to evaluate the likelihood and the prior distributions at the drawn value of the parameters
and multiply them together. The log likelihood function for this regression model is given by
⎛³ ³ ´´0 ³ ³ ´´ ⎞
2 2
⎜ − 1 − 1 ⎟
ln ( |Φ) = − ln 2 − 2 − 05 ⎝ 2 ⎠ (3.10)
2 2
The prior density for the regression coefficients is just a normal density given by
−2 −1 £ 0 ¤
() = (2) |Σ0 | 2 exp −05 ( − 0 ) Σ−10 ( − 0 ) (3.11)
Note that this is evaluated at the new draw of the regression coefficients. If the new draw is very far from the prior
mean 0 and the prior is tight (the diagonal elements of Σ0 are small) then () will evaluate to a small number.
138 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Similarly, the log prior density for 1 2 is a Gamma distribution with a density function given by
0 µ ¶
¡ ¢ 1 2 −1 − 0
1 2 = ∗ 2 exp (3.12)
2 2
1
where ∗ = 0 and Γ () denotes the Gamma function. The log posterior is given by
0
Γ( 2 )( 20 ) 2
¡ ¢
ln (Φ| ) ∝ ln ( |Φ) + ln () + ln 1 2
Therefore the acceptance probability is simply the likelihood ratio
¡ ¡ ¡ ¢ ¡ ¢¢ ¢
= min exp ln Φ | − ln Φ | 1 (3.13)
¡ ¢ 2
¡ ¢
where ln Φ | is the log posterior evaluated at the new draw of 1 2 , and ln Φ | is the log
posterior evaluated at the old draw. If ∼ (0 1) we retain the new draw and set Φ = Φ .
Step 4 Repeat steps 2 and 3 M times. The last L draws of 1 2 , 2 provide an approximation to the marginal
posterior distributions.
Figures 4 and 5 show the code for this example (example2.m). Relative to the previous example there are only
two changes. First on lines 12 to 15 we set the parameters of the prior distributions for and 1 2 Second, line 45
evaluates the log prior density for at the new draw. Similarly, line 46 evaluates the log prior density for 1 2 at
the new draw. The log posterior at the new draw is calculated on line 47. Lines 50 to 55 calculate the log posterior
at the old draw of the parameters. The remaining code is identical to the previous example.
3. THE RANDOM WALK M ETROPOLIS HASTINGS ALGORITHM 139
3.3. The random walk MH algorithm for a state space model. In this section we consider the estimation
of a regression with time-varying parameters using the MH algorithm. Note that Gibbs sampling is feasible for this
model. Our reason for using the MH algorithm is related to the fact the steps involved in dealing with this model
are very similar to those required when estimating a DSGE model. In particular, the choice of starting values is no
longer trivial.
The model we consider has the following state space representation
= + + ˜ (0 ) (3.14)
µ ¶ µ ¶ µ ¶µ ¶ µ ¶
1 1 0 −1 1
= + +
2 0 2 −1 2
µ ¶ µµ ¶ µ ¶¶
1 0 1 0
˜
2 0 0 2
The random walk MH algorithm for this model works exactly as before. At each iteration we calculate the log
posterior for the model at the old and the new draw of the parameters Φ = {1 2 1 2 1 2 }. Calculation of
the posterior involves evaluating the prior distributions and the log likelihood of the model. Note that the likelihood
function of this state space model is evaluated using the Kalman filter. As discussed in Hamilton (1994) (page 385)
if the shocks to the state space model ( 1 2 ) are distributed normally, then the density of the data ( | ) is
given as ³ ´
−12 ¯¯ ¯−12
( | ) = (2) |−1 ¯ × exp −05 0 −1 |−1
|−1 |−1 (3.15)
for = 1 with the log likelihood of the model given by
X
ln ( |Φ) = ln ( | ) (3.16)
=1
Here |−1 is the prediction error from the prediction step of the Kalman filter and |−1 is the variance of the
prediction error (see Chapter 3).
Figures 6 and 7 show a matlab function (likelihoodTVP.m) which uses the Kalman filter to calculate the likelihood
for this model and will be used in the main code discussed below. Line 4 checks if the variances (stored as the last
three elements of theta) are positive and 1 and 2 µ do not sum¶to a number greater than 1 ( this is aµrough¶way to
1 0 1
check for stability). Lines 5 to 7 form the matrix while lines 8 to 10 form the matrix . Line
0 2 µ ¶ 2
1 0
13 specifies the matrix R while lines 14 to 16 specify the matrix The Kalman filter recursions on lines
0 2
20 to 39 are as described in Chapter 3. Line 40 ³ uses the prediction error ´ and the variance of the prediction error to
−12 ¯¯ ¯−12
¯ 0 −1
calculate ( | ) = (2) |−1 × exp −05 |−1 |−1 |−1 Line 42 adds this for each observation (if
there are no numerical problems). Line 47 returns the negative of the likelihood function (we are going to minimise
this below).
The MH algorithm for this model is given by the following steps:
Step 1 Set priors for the coefficients and variances of the state space model. We assume that 1 2 1 2 have a
normal prior while the reciprocal of 1 2 have a Gamma prior.
Step 2 Set a starting value for the parameters Φ = {1 2 1 2 1 2 } and the variance of the shock to
the random walk candidate generating density. We set the starting value for Φ as the estimate Φ by
numerically maximising the log posterior. The mode of the posterior provides a reasonable point to the start
the MH algorithm and implies that fewer iterations may be required for the algorithm to converge.3 The
estimate of the covariance of Φ can be used to set the variance of the random walk candidate density.
Note that the covariance of Φ is given by the inverse of the hessian of the log posterior with respect to
the model parameters. Denoting this estimated variance by Ω̂ the variance of the shock to the candidate
generating density is set as Σ = Ω̂ × where is a scaling factor chosen by the researchers such that the
acceptance rate is between 20% and 40%.
Step 3 Draw a new set of parameters from the random walk candidate density
Φ = Φ + (3.17)
µ
¶
(Φ )
Step 4 Compute the acceptance probability = min (Φ ) 1 As in the previous example the target density is
the posterior distribution. The log of the posterior distribution is calculated as the sum of the log likelihood
and the sum of the log priors. As described above, the log likelihood is calculated by using the Kalman
filter. If ∼ (0 1) then we keep Φ otherwise we retain the old draw.
3 Note also that if the posterior is multi-modal (which may be the case for complicated models) the numerical maximum will be a
rough approximation to the posterior mode.
142 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Figure 6. The log likelihood for the time-varying parameter model in Matlab
Step 5 Repeat steps 3 and 4 M times. The last L draws of Φ provide an approximation to the marginal posterior
distributions.
Figures 8, 9 and 10 show the code for the MH algorithm for this model. Line 2 of the code adds the optimization
software csminwel written by Chris Sims and freely available at http://sims.princeton.edu/yftp/optimize/mfiles/.
This matlab function minimises a user supplied function. Lines 5 to 23 create artificial data for the state space
model assuming that 1 = 01 2 = −01 1 = 095 2 = 095 = 2 1 = 01 2 = 01. Lines 25 to 36 set the
parameters for the prior distributions. Lines 37 to 39 maximise the log posterior of the model using csminwel. Line
39 called csminwel using the code:
3. THE RANDOM WALK M ETROPOLIS HASTINGS ALGORITHM 143
Figure 7. The log likelihood for the time-varying parameter model in Matlab (continued)
[FF,AA,gh,hess,itct,fcount,retcodeh] =
csminwel(‘posterior’,theta0,eye(length(theta0))*.1,[],1e-15,1000,y,x,F0,VF0,MU0,VMU0,R0,VR0,Q0,VQ0);
The inputs to the function are (1) the name of the function that calculates the log posterior. This is called
posterior.m in our example. Note that this example evaluates the log likelihood using likelihoodTVP.m. The function
then evaluates the log prior for each parameter. The sum of these is the log joint prior. The sum of the log joint prior
and the log likelihood is the log posterior. Note that posterior.m returns the negative of the log posterior. Therefore
csminwel minimises the minimum of the negative log posterior which is equivalent to maximising the log posterior.
(2) the initial values of the model parameters theta0. (3) the initial hessian matrix which can be left as default. (4) a
144 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
function for calculating analytical derivitives. If this is unavailable then we enter an empty matrix [] as done above.
(5) The tolerance level to stop the iterative procedure. This should be left as default. (6) The maximum number of
iterations set to a 1000 in the example above. All the remaining arguments (y,x,F0,VF0,MU0,VMU0,R0,VR0,Q0,VQ0)
are passed directly to the function posterior.m and are inputs for that function. The function returns (1) FF the
value of the function at the minimum. (2) AA the value of the parameters at the minimum and (3) hess the inverse
hessian of the function being minimised
Line 42 of the code sets the variance of the random walk candidate generating density as a scalar times the
parameter variance obtained from the optimisation using csminwel. Line 43 sets the initial value of the parameters
as the posterior mode estimates.
Line 51 calculates the log likelihood at the initial value of the parameters. Lines 59 to 68 evaluate the log prior
distributions for the parameters of the state space model. Line 69 calculates the log joint prior as the sum of these
prior distributions. Line 70 calculates the log posterior as the sum of the log likelihood and the log joint prior.
Line 74 draws the new value of the parameters from the random walk candidate generating density. Line 82
calculates the log likelihood at the new draw (assuming that the drawn variances are positive and the elements of
sum to less than 1). Lines 83 to 100 evaluate the log joint prior at the new draw and line 101 calculates the posterior.
Line 109 calculates the acceptance probability. If this is bigger than a number from the standard uniform distribution
then the new draw of the parameters is accepted. In this case Line 115 also sets posteriorOLD to posteriorNEW— it
automatically updates the value of the posterior at the old draw eliminating the need to compute the posterior at
the old draw at every iteration (as we have done in the examples above).
Line 120 computes the acceptance rate (the ratio of the number of accepted draws and the total draws). Once
past the burn-in stage we save the draws of the model parameters. Figure 11 shows the retained draws of the
parameters along with the true values.
3.4. The random walk MH algorithm used in a Threshold VAR model. In this section, we consider
how the MH algorithm is used in the estimation of a Threshold VAR model (TVAR). The TVAR is defined as
X
= 1 + 1 − + ( ) = Ω1 if ≤ ∗
=1
X
= 2 + 2 − + ( ) = Ω2 if ∗
=1
where is a matrix of endogenous variables, = − (i.e. a lag of one of the endogenous variables) is the
threshold variable and ∗ is the threshold level. Note that if ∗ and are known, then the TVAR is simply two
146 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
F F μ
1 2 1
1 1 0.4
0.95 0.3
0.95
0.9 0.2
0.9
0.85 0.1
0.85
0.8 0
0
2.5 0.3
−0.1
2 0.2
−0.2
1.5 0.1
−0.3
−0.4 1 0
0 0.5 1 1.5 2 0 0.5 1 1.5 2 0 0.5 1 1.5 2
4 4 4
x 10 x 10 x 10
Q
2
0.5
0.4
MH draws
0.3 True value
0.2
0.1
0
0 0.5 1 1.5 2
4
x 10
MH draws
VAR models defined over the appropriate data samples using ≤ ∗ and ∗ . This observation allows us to
devise a Gibbs algorithm (with a MH step). In what follows below we assume the delay parameter to be known.
See Chen and Lee (1995) for the extension of the algorithm to the case where is estimated.
Step 1 Set Priors. In the application below, we assume ( ∗ ) ˜ (̄ ∗ ∗ ). We set a natural conjugate prior for
the VAR parameters in both regimes using dummy observations. See the prior used in section 6. Set an
initial value for ∗ . One way to do this is to use the mean or median of − .
Step 2 Seperate the data into two regimes. The first regime includes all observations such that ≤ ∗ . Call this
sample 1 The second regime includes all observations such that ∗ . Call this sample 2
Step 3 Sample the VAR parameters = { } and Ω in each regime = 1 2. Let denote the right hand side
variables of the VAR. The conditional distribution is exactly as defined in chapter 2 above and is given by
−1
( |Ω ∗ ) ˜ ((∗ ) Ω ⊗ (∗0 ∗ ) ) (3.18)
(Ω | ∗ ) ˜ (∗ ∗ )
where
−1
∗ = (∗0 ∗ ) (∗0 ∗ )
0
= − ∗ (∗
(∗ − ∗ ) ∗ )
where ∗ = [ ; ] and ∗ = [ ; ] with the dummy observations that define the prior for
the left and the right hand side of the VAR respectively.
Step 4 Use a MH step to sample ∗ . Draw a new value of the threshold from the random walk
∗ ∗
= + ˜ (0 Σ)
Then compute the acceptance probability
∗ ∗
( | Ω ) ( )
= ∗ ∗
( | Ω ) ( )
∗
where ( | Ω ) is the likelihood of the VAR computed as the product of the likelihoods in the two
regimes. The log likelihood in each regime (ignoring constants) is
µ ¶ X ∙³ ´0 ³ ´¸
¯ −1 ¯
¯ ¯
log Ω − 05 − ̃ Ω−1
− ̃
2 =1
∗
with ̃ equivalent to reshaped to be conformable with . Then draw ˜ (0 1). If accept
∗
else retain . The scale Σ can be tuned to ensure an acceptance rate between 20% and 40%.
As an example we consider a TVAR where contains US data on GDP growth, CPI inflation, a short term
interest rate and a financial conditions index (FCI) calculated by the Chicago Fed. The threshold variable is assumed
to be the second lag of FCI and examine the impulse response of the variables to a unit increase in FCI (a deterioration
3. THE RANDOM WALK M ETROPOLIS HASTINGS ALGORITHM 147
of financial conditions) in the two regimes. The matlab code is in the file named thresholdvarNFCI.m and displayed
in figures 12, 13 and 14. In this example the prior ( ∗ ) ˜ (̄ ∗ ∗ ) is set by using the mean of NFCI as ̄ ∗ and
∗ = 10 (line 28). Lines 30 to 53 set the natural conjugate prior for the VAR parameters. Lines 80 to 87 seperate
the sample into two regimes. Lines 89 to 128 draw the VAR coefficients and covariance in each regime. The MH step
to draw the threshold variable starts on line 134 with a draw from the random walk candidate density. Then the log
∗ ∗ ∗ ∗
posterior ln ( ( | Ω ) ( )) is computed on line 136 while ln ( ( | Ω ) ( )) is computed on
line 137. The acceptance probability is computed on line 138.
148 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
The top right panel of figure 15 plots the estimated threshold and the threshold variable and shows that regime 2
persisted in the 1980s, the early 1990s and then during the recent recession. There is some evidence that the negative
impact of an FCI shock on GDP growth is larger in regime 2.
3.5. The random walk MH algorithm used in a STVAR model. A related model is the smooth transition
(ST)VAR given by:
⎛ ⎞
X
= (1 − ( ∗ )) ⎝1 + 1 − ⎠ +
=1
⎛ ⎞
X
( ∗ ) ⎝2 + 2 − ⎠ +
=1
( ) = Ω
∗ 1
where ( ) = 1+exp(−( ∗
− ))
. Here is a matrix of endogenous variables, = − (i.e. a lag of one
of the endogenous variables) is the threshold variable and ∗ is the threshold level and 0 is the smoothness
parameter that determines the smoothness of the regime shifts. The Gibbs algorithm proceeds in the following steps
(see Lopes and Salazar (2006))
(1) Set Priors. We assume ( ∗ ) ˜ (̄ ∗ ∗ ). We assume a Gamma prior for : () ˜Γ ( 0 0 ) . We set a
natural conjugate prior for the VAR parameters = { } for = 1 2. in both regimes using dummy
observations. See the prior used in section 6. Set an initial value for ∗ . One way to do this is to use the
mean or median of − .
(2) Sample from (1 |2 Ω ∗ ). Write the VAR model as
⎛ ⎞ ⎛ ⎞
X
X
− ( ∗ ) ⎝2 + 2 − ⎠ = ( ∗ ) ⎝2 + 2 − ⎠ +
=1 =1
or
̄ = 1 ̄ +
³ P ´
where ̄ = − ( ∗ ) 2 + =1 2 −
and ̄ = { ( ∗ ) ( ∗ ) (−1 ) ( ∗ ) (− )} This is a standard VAR model and
the conditional posterior is as described for the coefficients of the Threshold VAR above:
−1
(1 |2 Ω ∗ ) ˜ ((∗ ) Ω ⊗ (∗0 ∗ ) ) (3.19)
where ∗
= [̄ ; ] with the dummy observations that define the prior for the left and the right
hand side of the VAR respectively.
(3) Sample from (2 |1 Ω ∗ ). ³ ´
P
We proceed exactly as in step 2 by defining ̄ = − (1 − ( ∗ )) 1 + =1 1 −
3. THE RANDOM WALK M ETROPOLIS HASTINGS ALGORITHM 151
(4) Sample from (Ω|2 1 Ω ∗ ) As in the previous example this conditional posterior is ( ∗ ∗ )
0
where ∗ = ( − 1 ∗ ) ( − 1 ∗ ) with = [̄ ; ]
(5) Sample from ( |1 2 Ω) We use a random walk MH step to sample Ξ = ∗ . Draw a new value
∗
³ ´
via OLS (Lines 28 to 31). The log posterior ( − ) ln det () − 05 (̂)0 is then maximised. Note that
given a starting value of the 5 free parameters, the log posterior is evaluated by the function getML which returns the
negative of this function. This negative posterior is then minimised first via Simplex on lines 41 using 500 iterations
in the Matlab function fminsearch. This refines the starting values to be input into the main minimisation routine
CSMINWEL which was introduced above. The values of at the mode of the log posterior (minimum of minus log
posterior) are given Theta2 and the covariance by hess. The former is used as the initial values in the metropolis step
while the latter is used to calibrate the variance of the candidate distribution. The MCMC algorithm begins on line
58. Line 60 draws from the candidate density. Lines 63 and 64 evaluate the posterior at the new and old draws
with the acceptance probability calculated on line 66. The draw of is normalised 75 to 77. The commented lines
78 to 80 show the normalisation rule proposed in Waggoner and Zha (2003). Finally, the VAR coefficients are drawn
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 153
on 83 to 85. Running the code provides a comparison of the true impulse responses and the estimated posterior
distribution.
à ¡ ¢ ¡ ¢ !
Φ+1 Φ+1 |Φ
= min 1 (4.2)
(Φ ) (Φ |Φ+1 )
The independence MH algorithm is therefore more general than the random walk MH algorithm. Unlike the
random walk MH algorithm, the candidate generating density in the independence MH algorithm has to be tailored
to the particular problem at hand. We examine an application to stochastic volatility models below.
Apart from the change in the form of the candidate generating density the steps of the algorithm remain the
same:
Step 1 Set starting values for the model parameters. ¡ ¢
Step 2 Draw a candidate value of the parameters Φ+1 from the candidate generating density Φ+1
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 155
Step 4 If ∼ (0 1) is less than retain Φ+1 . Otherwise retain the old draw.
Step 5 Repeat steps 2 to 4 times and base inference on the last draws. In other words, the empirical
distribution using the last draws is an approximation to target density.
4.1. Estimation of stochastic volatility models via the independence MH algorithm. A simple sto-
chastic volatility model for a × 1 data series is given by
156 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
p
= exp (ln ) (4.4)
ln = ln −1 +
˜ (0 )
where is time-varying variance. Note that this is a state space model where the observation equation is non-linear
in the state variable and therefore the Carter and Kohn algorithm does not apply. Jacquier et al. (2004) instead
suggest applying an independence MH algorithm at each point in time to sample from the conditional distribution of
which is given by ( |− ) where the subscript − denotes all other dates than Jacquier et al. (2004) argue
that because the transition equation of the model is a random walk, the knowledge of +1 and −1 contains all
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 157
= ln −1 (4.15)
=
The algorithm for the stochastic volatility model consists of the following steps4 :
4 Note that the Jacquier et al. (2004) algorithm is a single-move algorithm— the stochastic volatility is drawn one period at a time.
This may mean that this algorithm requires a large number of draws before convergence occurs. Kim et al. (1998) develop an algorithm
to sample the entire time-series of the stochastic volatility jointly and show that this multi-move algorithm is more efficient.
158 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Step 1 Obtain a starting value for = 0 as ̂2 and set the prior ̄ ̄ (e.g ̄ could be the log of OLS estimate
of the variance of and ̄ could be set to a big ¡ number¢ to reflect the uncertainty in this initial guess). Set
an inverse Gamma prior for i.e. () ∼ 20 20 Set a starting value for
Step 2 Time 0 Sample the initial value of denoted by 0 from the log normal density
à !
−1 − (ln 0 − 0 )2
(0 |1 ) = 0 exp
2 0
³ ´
where the mean 0 = 0 ̄̄ + ln1 and 0 = ̄+ ̄
.
Algorithm 5. To sample from the log normal density ∼ log ( ) sample 0 from the normal density
( ) Then = exp (0 )
ep 2 Time 1 to T-1 For each date t=1 to T-1 draw a new value for from the candidate density (call the draw )
à !
¡ +1 ¢ −1 − (ln − )2
Φ = exp
2
(ln +1 +ln −1 )
where = 2 and = 2 Compute the acceptance probability
⎛ ³ ´ ⎞
−2
−05
exp 2
= min ⎝ ³ ´ 1⎠
−05 −2
exp 2
Draw ˜ (0 1). If set h = . Otherwise retain the old draw.
Step 2 Time T For the last time period = compute = ln −1 and = and draw from the candidate density
à !
2
¡ +1 ¢ − (ln − )
Φ = −1
exp
2
Compute the acceptance probability
⎛ ³ ´ ⎞
−2
−05
exp 2
= min ⎝ ³ ´ 1⎠
−2
−05
exp 2
Draw ˜ (0 1). If set h = . Otherwise retain the old draw.
Step 3 Given a draw for compute the residuals of the transition equation = ln − ln −1 Draw from the
0 +
inverse Gamma distribution with scale parameter 2 0 and degrees of freedom + 2 Note that this is an
0
Figure 22. Matlab code for the stochastic volatility model continued
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 161
Figure 23. Matlab code for the stochastic volatility model continued
162 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
600
Estimated posterior median
20 lower bound
upper bound
15
500
10
5
400
−5 300
−10
200
−15
−20
100
−25
−30
1920 1930 1940 1950 1960 1970 1980 1990 2000 2010 1920 1930 1940 1950 1960 1970 1980 1990 2000 2010
The right panel of figure 24 plots the estimated stochastic volatility of UK inflation.
We now a consider an extended version of this stochastic volatility model for inflation. The model now assumes
a time-varying AR(1) specification for inflation with stochastic volatility in the error term. This model is given as
p
= + −1 + exp (ln ) (4.16)
Letting = { } the coefficients in the regression evolve as
= −1 + (4.17)
where ∼ (0 ). As before, the variance of the error term evolves as
ln = ln −1 + (4.18)
˜ (0 )
This model can be easily estimated by combining the Carter and Kohn algorithm with the Metropolis algorithm
described above. The steps are as follows:
Step 1 Set a inverse Wishart prior for . The prior scale matrix can be set as 0 = × × 0 where 0 is the
length of training sample, is the variance covariance matrix of obtained via OLS using the traning
sample and is a scaling factor set to a small number. Obtain a starting value for = 0 as ̂2 and
set the prior ̄ ̄ (e.g ̄ could be the log of OLS estimate of the variance of using the training sample
and ̄ could be set to a big number to reflect the uncertainty in this initial guess). Set an inverse Gamma
prior for i.e. () ∼ (0 0 ) Set a starting value for and
Step 2 Time 0 Conditional on and sample the initial value of denoted by 0 from the log normal density
à !
2
− (ln 0 − 0 )
(0 |1 ) = −1
0 exp
2 0
³ ´
where the mean 0 = 0 ̄̄ + ln1 and 0 = ̄+ ̄
.
ep 2 Time 1 to T-1 For each date t=1 to T-1 draw a new value for (conditional on and ) from the candidate density
(call the draw ) Ã !
2
¡ +1 ¢ −1 − (ln − )
Φ = exp
2
where = (ln +1 +ln
2
−1 )
and = 2 Compute the acceptance probability (note that the residuals are
used in the expression below rather than as in the previous example)
⎛ ³ ´ ⎞
−2
−05
exp 2
= min ⎝ ³ ´ 1⎠
−05 −2
exp 2
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 163
Draw ˜ (0 1). If set h = . Otherwise retain the old draw.
Step 2 Time T For the last time period = compute = ln −1 and = and draw from the candidate density
à !
2
¡ +1 ¢ −1 − (ln − )
Φ = exp
2
Compute the acceptance probability
⎛ ³ ´ ⎞
−2
−05
exp 2
= min ⎝ ³ ´ 1⎠
−2
−05
exp 2
Draw ˜ (0 1). If set h = . Otherwise retain the old draw.
Step 3 Given a draw for compute the residuals of the transition equation = ln − ln −1 Draw from the
0 +
inverse Gamma distribution with scale parameter 2 0 and degrees of freedom + 2 Note that this is an
0
Figure 25. Matlab code for the time-varying parameter AR model with stochastic volatility
The matlab code for this example (example5.m) is shown in figures 25, 26 and 27. Lines 18 to 23 of the
code estimate an AR(1) model via OLS on a training sample of 10 observations. Line 24 sets ̄ as the log of the
error variance using this OLS residuals. Lines 27 and 28 set the initial value of the time varying coefficients and
the associated variance as the OLS estimates. Line 30 sets the prior scale matrix 0 using the OLS estimate of the
coefficient covariance. Lines 37 to 40 set an initial value for and and line 47 starts the algorithm. Lines 48 to 101
sample using the independence MH step described in the previous example. ³ The2 only
´ change is that ³ the2 residuals
´
− −
from the observation equation are used to evaluate the densities −05
exp 2 and −05
exp 2 when
calculating the acceptance probability. Line 104 samples from the inverse Gamma distribution. Line 108 samples
4. THE INDEPENDENCE M ETROPOLIS HASTINGS ALGORITHM 165
Figure 26. Matlab code for the time-varying parameter AR model with stochastic volatility continued
the time-varying coefficients using the Carter Kohn algorithm. For simplicity, the code for this algorithm is moved
into a seperate function carterkohn1.m saved in the functions folder. This code is identical to the examples discussed
in the previous chapter apart from the minor difference that the value of the variance of the errors of the observation
equation at time is set to . See line 18 in carterkohn1.m. Note that this function also returns the updated value of
the error term The inputs to this function are as follows: (1) the initial state 0|0 (2) Variance of the initial state
(3) the time-varying variance of shock to the observation equation (4) (5) the dependent variable and (6)
the independent variables. Conditional on a value for line 112 samples from the inverse Wishart distribution.
Figure 28 shows the estimated stochastic volatility and the time-varying coefficients.
166 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Figure 27. Matlab code for the time-varying parameter AR model with stochastic volatility continued
80
0.92
70
60 0.91
50
0.9
40
30 0.89
20
0.88
10
0.87
1920 1930 1940 1950 1960 1970 1980 1990 2000 2010 1920 1930 1940 1950 1960 1970 1980 1990 2000 2010
0.4 0
0.3 −5
−10
0.2
−15
0.1
−20
0
−25
−0.1 −30
1920 1930 1940 1950 1960 1970 1980 1990 2000 2010 1920 1930 1940 1950 1960 1970 1980 1990 2000 2010
Figure 28. Estimates from the time-varying AR model with stochastic volatility
and the subscript 0 denotes the fact that this is the training sample. The scale matrix 0 is set equal to
0|0 × 0 × where is a scaling factor chosen by the researcher. Some studies set = 3510−4 i.e. a small
number to reflect the fact that the training sample in typically short and the resulting estimates of 0|0
maybe imprecise. Note that one can control the apriori amount of time-variation in the model by varying
. Set a starting value for The initial state is set equal to 0|0 = ( 0 )0 and the intial state covariance
is given by 0|0
Step 1b Set the prior for 1 and 2 . The prior for 1 is inverse Gamma (1 ) ∼ (10 0 ) and the prior
for
µ 2 is inverse ¶Wishart (2 ) ∼ (20 0 ). Benati and Mumtaz (2006) set 10 = 0001 and 20 =
0001 0 12
Let = Σ0 and let 0 denote the inverse of the matrix with the diagonal normalised
0 0001
to 1. The initial values for (i.e. the initial state 0|0 ) are the non-zero elements of 0 with the variance
of the initial state set equal to ( ) × 10 (as in Benati and Mumtaz (2006)). Set a starting value for
2
Step 1c Obtain a starting value for = 0 and = 13 as ̂ and set the prior ̄ ̄. ̄ can be set equal to
the log of the diagonal element of Σ0 and ̄ to a large number. Set an inverse Gamma prior for i.e.
( ) ∼ (0 0 ). Set a starting value for
Step 2 Conditional on , and draw using the Carter and Kohn algorithm. The algorithm exactly as
described for the time-varying VAR without stochastic volatility in Chapter 3 with the difference that the
variance of changes at each point in time and this needs to be taken into account when running the
Kalman filter.
Step 3 Using the draw for calculate the residuals of the transition equation − −1 = and sample from
the inverse Wishart distribution using the scale matrix 0 + 0 and degrees of freedom + 0
Step 4 Draw the elements of using the Carter and Kohn algorithm (conditional on 1 and 2 ). The
state space formulation for 12 is
2 = −12 1 + 2 (2 ) = 2
12 = 12−1 + 1 (1 ) = 1
The state space formulation for 13 and 23 is
3 = −13 1 − 23 2 + 3 (3 ) = 3
µ ¶ µ ¶ µ ¶ µ ¶
13 13−1 2 2
= + ( ) = 2
23 23−1 3 3
Note that these two formulations are just time-varying regressions in the residuals and the Carter and Kohn
algorithm is applied to each seperately to draw 12 13 and 23
Step 5. Conditional on a draw for 12 13 and 23 calculate the residuals 1 , 2 and 3 Draw 1 from the
0 +
inverse Gamma distribution with scale parameter 1 12 10 and degrees of freedom + 2 . Draw 2 from
0
0
the inverse Wishart distribution with scale matrix 2 2 + 20 ⎛ and degrees
⎞ of freedom + 0
1
Step 6 Using the draw of from step 4 calculate = where = ⎝ 2 ⎠. Note that are contemporane-
3
ously uncorrelated. We can therefore draw for = 13 separately by simply applying the independence
MH algorithm described above for each (conditional on a draw for )
Step 7 Conditional on a draw for for = 13 draw from the inverse Gamma distribution with scale parameter
(ln −ln −1 )0 (ln −ln −1 )+0
2 and degrees of freedom +
2
0
Step 8 Repeat steps 2 and 7 M times. The last L draws provide an approximation to the marginal posterior
distributions of the model parameters.
5. A VAR W ITH TIM E-VARYING COEFFICIENTS AND STOCHASTIC VOLATILITY 169
Figure 29. Matlab code for the time-varying VAR with stochastic volatility
The matlab code for estimating this model (example6.m) is shown in figures 29, 30, 31 and 32. We consider a
time-varying VAR model with two lags using US data on GDP growth, CPI inflation and the Federal Funds rate over
the period 1954Q3 to 2010Q2 in this code. Lines 25 to 27 set the initial values for the elements of by calculating
the matrix 0. Lines 30 amd 31 set the variance around these initial values. Lines 32 and 33 set the prior scale
matrices 10 and 20 Lines 38 to 45 set the priors and starting values for the stochastic volatility models for the
transformed VAR residuals Lines 59 to 112 contain the Carter and Kohn algorithm to sample the VAR coefficients
. The only change relative to the example in chapter 3 is on lines 69 to 72. Line 70 using the function chofac.m
to reshape the value of at time into a lower triangular matrix. Line 72 calculates the VAR error covariance
170 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Figure 30. Matlab code for the time-varying VAR with stochastic volatility
matrix for that time period and this is used in Kalman filter equations. Line 116 samples from the inverse Wishart
distribution. Line 121 uses the Carter and Kohn algorithm to sample 12 (where for simplicity the code for the
algorithm is in the function carterkohn1.m). Line 122 samples 13 and 23 using the same function. Lines 124
and 125 sample 1 from the inverse Gamma distribution. Lines 127 and 128 sample 2 from the inverse Wishart
distribution. Lines 131 to 136 calculate = . Lines 138 to 142 use the independence MH algorithm to draw
= 13 using these The code for the algorithm is identical to the two previous examples but is included in
the function getsvol.m for simplicity. This function takes in the following inputs (1) the previous draw of (2)
(3) ̄ (4) ̄ (5) and returns a draw for Lines 145 to 148 draw from the inverse Gamma distribution.
6. CONVERGENCE OF THE M H ALGORITHM 171
Figure 31. Matlab code for the time-varying VAR with stochastic volatility
Figure 33 plots the estimated impulse response to a monetary policy shock (identified via sign restrictions) and
the estimated stochastic volatility.
Figure 32. Matlab code for the time-varying VAR with stochastic volatility
6. CONVERGENCE OF THE M H ALGORITHM 173
Figure 33. Response to a monetary policy shock from the time-varying VAR with stochastic volatil-
ity (Top panel) and the estimated stochastic volatility (bottom panel)
174 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Figure 34. Recursive mean for key parameters of the time-varying VAR model
Most of the methods for checking convergence of the Gibbs sampler (see Chapter 1) can be applied immediately
to output from the MH algorithm. Several studies present simple statistics such as recursive means of the MH draws
and the autocorrelation functions to test if the algorithm has converged. As an example we present the recursive
means of the retained draws for the time-varying parameter VAR considered in the previous section. As described
above this model is estimated using a mixture of Gibbs and MH steps. Figure 34 presents the recursive means
calculated every 20 draws for and . The X-axis of each panel represents these parameterised vectorised.
The Y-axis represents the draws. The recursive means usggest convergence for but indicate some variation
in the means for possibly suggesting that more draws are required for this model.
Gelman and Rubin (1992) suggest a diagnostic for monitoring the convergence of multiple MH chains (for esti-
mating the same model) started from different starting values. For every parameter of interest Gelman and Rubin
(1992) calculate the within chain variance as
1 X 1 X¡ ¢2
= ̄ − ̄
=1 =1
1X 1 X
̄ = ̄ = ̄
=1 =1
They argue that underestimates the variance of (before convergence) as the MH algorithm has not explored
the parameter space. In contrast, 2 = −1 + overestimates this variance due to dispersed starting values. If
the MH algorithm has converged then and 2 should be similar. Gelman and Rubin (1992) suggest calculating
the statistic
2 +
=
− 2
2
2 2 +
where = and checking if this is close to 1 which would indicate convergence of the MH algorithm.
7. Further Reading
• Koop (2003) chapter 5 provides an excellent description of the Metropolis Hastings algorithm.
8. APPENDIX: COM PUTING THE M ARGINAL LIKELIHOOD USING THE GELFAND AND DEY M ETHOD 175
8. Appendix: Computing the marginal likelihood using the Gelfand and Dey method
Gelfand and Dey (1994) introduce a method for computing the marginal likelihood that is particularly convenient
to use when employing the Metropolis Hastings algorithm. This method is based on the following result.
∙ ¸
(Φ) 1
| = (8.1)
( |Φ) × (Φ) ( )
where ( |Φ) denotes the likelihood function, (Φ) is the prior distribution, ( ) is the marginal likelihood and
(Φ) is any pdf with support Θ defined within the region of the posterior. The proof of equation 8.1 can be obtained
h i Z
(Φ) (Φ)
by noting that ( |Φ)× (Φ) | = ( |Φ)× (Φ) × (Φ| ) Φ where (Φ| ) is the posterior distribution. Note
( |Φ)× (Φ)
that (Φ| ) = ( ) and the density (Φ) integrates to 1 leaving us with the right hand side in equation
8.1.
X
1 (Φ )
We can approximate the marginal likelihood as ( |Φ )× (Φ ) where Φ denotes draws of the parameters
=1
from Metropolis Hastings algorithm and ( |Φ ) × (Φ ) is the posterior evaluated at each draw. Geweke (1998)
recommends using a truncated normal distribution for (Φ). This distribution is truncated at the tails to ensure
that (Φ) is bounded from above, a requirement in Gelfand and Dey (1994). In particular, Geweke (1998) suggest
using ∙
1 ¯ ¯−12 ³ ´ ³ ´0 ¸ ³ ´
¯ ¯ −1
(Φ) = ¯ Σ̂¯ exp −05 Φ − Φ̂ Σ̂ Φ − Φ̂ × Φ ∈ Θ̂ (8.2)
(2)2
where Φ̂ is³ the posterior
´ mean, Σ̂ is the posterior covariance and k is the number of parameters. The indicator
function Φ ∈ Θ̂ takes a value of 1 if
∙³ ´ ³ ´0 ¸
−1
Φ − Φ̂ Σ̂ Φ − Φ̂ ≤ 21− ()
where 21− () is the inverse 2 cumulative distribution function with degrees of freedom and probability Thus
21− () denotes the value that exceeds 1 − % of the samples from a 2 distribution with degrees of freedom. The
³ ´
indicator function Φ ∈ Θ̂ therefore removes ‘extreme’ values of Φ For more details, see Koop (2003) page 104.
In figures 35 and 36 we estimate the marginal likelihood for a linear regression model via the Gelfand and Dey
method. The model is exactly used in the appendix to Chapter 1 and is based on artifical data. A simple random walk
Metropolis Hastings algorithm is used to approximate the posterior on lines 35 to 62 and we save the log posterior
evaluated at each draw and each draw of the parameters. Lines 65 and 66 calculate the posterior mean and variance.
We set 1 − = 01 on line 68. In practice, different value of 1 − can be tried to check robustness of the estimate.
2
On line 70³we evaluate
´ the
³ inverse
´ CDF. Line 71 to 78, loop through the saved draws of the parameters. On 73 we
0
(Φ )
calculate Φ − Φ̂ Σ̂−1 Φ − Φ̂ If this is less than or equal to 21− () we evaluate ( |Φ )× (Φ ) in logs, adding
the constant lpost_mode to prevent overflow.
176 5. AN INTRODUCTION TO THE THE M ETROPOLIS HASTINGS ALGORITHM
Figure 35. Matlab code to calculate the marginal likelihood via the Gelfand and Dey Method
8. APPENDIX: COM PUTING THE M ARGINAL LIKELIHOOD USING THE GELFAND AND DEY M ETHOD 177
Figure 36. Matlab code to calculate the marginal likelihood via the Gelfand and Dey Method (continued)
CHAPTER 6
This chapter considers the Bayesian estimation of Dynamic Stochastic General Equilibrium (DSGE) models using
the random walk metropolis hastings algorithm. These models are popular in academia and central banks for policy
analysis. Several menu driven computer packages (e.g. DYNARE) are now available for the estimation of these
models and several papers and books discuss the econometrics of DSGE models (see An and Schorfheide (2007)).
The focus of the chapter is practical — it offers a step by step guide to DSGE estimation from a Bayesian perspective
and tries to clarify the practical aspects of the problem. It, therefore, offers a useful starting point for researchers
who are new to DSGE estimation but are familiar with the economics behind these models.
1.1. Solving the model. The model in equation 1.1 cannot be estimated in its current form as it includes
unobserved variables dated in the future on the right hand side of the first two equations. One way to proceed is to
solve the model so that it can be re-written in a VAR form:
= −1 + (1.2)
⎛ ⎞
⎜ ⎟ ⎛ 2 ⎞
⎜ ⎟ 1 0 0
⎜ ⎟
where = ⎜
⎜
⎟ and are iid shocks with covariance matrix ⎝ 0 22 0 ⎠. The elements of the coefficient
⎜ ⎟
⎟
⎝ 0 0 23
⎠
matrix and the contemporaneous impact matrix are functions of the model parameters Θ. If we treat as state
variables, then equation 1.2 is a transition equation. As described below, once this is combined with an observation
equation we have a linear state space model that can be easily estimated as the Kalman filter can be used to calculate
the likelihood of the model.
Therefore, model solution is a key step in the estimation process. There are several solution algorithms available.
In this application we use the algorithm developed in Sims (2002). To use this algorithm and Chris Sims’ code, the
model needs to be written in matrix form:
0 ̃ = 1 ̃ −1 + + ̄
⎛ ⎞
⎜ ⎟
⎜ ⎟
⎜ ⎟
⎜ ⎟
⎜ ⎟
̃ = ⎜
⎜
⎟
⎟
⎜ ⎟
⎜ ⎟
⎜ ⎟
⎝ +1 ⎠
+1
For the model in equation 1.1 these matrices are defined as:
⎛ ⎞
1 0 1 −1 0 0 −1 −1 IS curve
⎜ − 1 0 0 −1 0 0 − ⎟ Phillips curve
⎜ ⎟
⎜ 0 − 1 0 0 −1 0 0 ⎟ Policy rule
⎜ ⎟
⎜ 0 0 0 1 0 0 0 0 ⎟ Demand shock
0 = ⎜⎜ 0
⎟ (1.3)
⎜ 0 0 0 1 0 0 0 ⎟ ⎟ Supply shock
⎜ 0 0 0 0 0 1 0 0 ⎟
⎜ ⎟ Policy shock
⎝ 1 0 0 0 0 0 0 0 ⎠ Exp. error 1
0 1 0 0 0 0 0 0 Exp. error 2
⎛ ⎞
0 0 0 0 0 0 0 0 IS curve
⎜ 0 0 0 0 0 0 0 0 ⎟ Phillips curve
⎜ ⎟
⎜ 0 0 0 0 0 0 0 0 ⎟ Policy rule
⎜ ⎟
⎜ 0 0 0 1 0 0 0 0 ⎟ Demand shock
1 = ⎜
⎜ 0 0 0 0 2 0 0 0 ⎟ Supply shock
⎟ (1.4)
⎜ ⎟
⎜ 0 0 0 0 0 3 0 0 ⎟ Policy shock
⎜ ⎟
⎝ 0 0 0 0 0 0 1 0 ⎠ Exp. error 1
0 0 0 0 0 0 0 1 Exp. error 2
⎛ ⎞
0 0 0 IS curve
⎜ 0 0 0 ⎟ Phillips curve
⎜ ⎟
⎜ 0 0 0 ⎟ Policy rule
⎜ ⎟
⎜ 1 0 0 ⎟ Demand shock
=⎜ ⎟
⎜ 0 1 0 ⎟ Supply shock (1.5)
⎜ ⎟
⎜ 0 0 1 ⎟ Policy shock
⎜ ⎟
⎝ 0 0 0 ⎠ Exp. error 1
0 0 0 Exp. error 2
⎛ ⎞
0 0 IS curve
⎜ 0 0 ⎟ Phillips curve
⎜ ⎟
⎜ 0 0 ⎟ Policy rule
⎜ ⎟
⎜ 0 0 ⎟ Demand shock
̄ = ⎜ ⎟
⎜ 0 0 ⎟ Supply shock (1.6)
⎜ ⎟
⎜ 0 0 ⎟ Policy shock
⎜ ⎟
⎝ 1 0 ⎠ Exp. error 1
0 1 Exp. error 2
Equation 1.3 shows 0 which is an 8 × 8 matrix in our case. The dimensions reflect the number of variables in
the model including the 2 expectational errors. The dimensions of 1 are also 8 × 8 while the number of columns of
and reflect the fact that the model has three iid structural shocks and two expectational errors.
The file example1.m demonstrates the solution of the model using the Sims (2002) method. For this example,
we use assume that the parameters have the following values:
= 1 = 15 = 3 = 15 1 = 07 2 = 07 3 = 07
Line 16 calls the main function model_solve.m that sets up 0 1 ̄ and calls the function to solve the model.
Figures 1 and 2 display the code for this function. The input to the function is the vector of parameters which
are extracted on lines 7 to 16. In order to set up 0 1 ̄ while minimising coding errors, it is helpful to set up
indices of equations, variables and shocks. These are set up on lines 21 to 44. Line 60 to 65 modifies the first row
of 0 to insert the coefficients of the IS equation. Lines 68 to 71 insert the coefficients of the Phillips curve in the
second row while lines 75 to 77 deal with the policy rule. Lines 83 to 85 modify 0 1 and to reflect the coefficients
of the demand shock equation = 1 −1 + 1 . Lines 87 to 93 do exactly the same for the supply and policy shocks.
Finally, the expectational errors are dealt with on lines 97 to 104. The function written by Sims to solve the model
is called gensys. The inputs to this function are 0 1 a vector C that specifies constants in the model (as shown
on line 52, this is a vector of zeros as all variables are in deviations from the steady state in the example model), ̄
and a number div which specifies the criteria for stable roots. Typically div=1. In other words, the function call is
1. THE DSGE M ODEL 181
gensys( 0, 1,C, , ,div). The function is called on line 114. The first key output from this function
0 1 ̄
is RC which is a 2 × 1 vector. If the first element of this vector equals 1, then a solution to the model exists. If the
second element equals 1, the solution is unique. The top 6 × 6 block of the return T1 is the matrix in the solution
(see equation 1.2). Similarly, the top 6 rows of T0 correspond to the matrix in equation 1.2. The final two rows
(and columns in case of T1) correspond to the expectational errors which are not directly relevant for estimation.
182 6. BAYESIAN ESTIM ATION OF LINEAR DSGE M ODELS
As discussed above, the solution in equation 1.7 is in the form of a VAR(1). It is now straightforward to produce
impulse responses to any of the 3 structural shocks and calculate objects such as variance decomposition. Moreover,
the solution can be used to calculate the likelihood function of the model via the Kalman filter. We turn to this next.
1.2. Calculating the log likelihood and log posterior. We treat as our vector of unobserved state
(model) variables and consider the model solution = −1 + as a transition equation of a state-space model.
We can obtain data on ‘real world’ counterparts of the model variables ̃ (output gap) , ̃ (inflation) and ̃ (short-
term interest rate). Typically the output gap would be measured as de-trended GDP, inflation as the log difference
of CPI and interest rate as the policy rate set by the central bank. As we do not allow for constants in the model,
the data is demeaned.
The observation equation of the model is then given as:
⎛ ⎞
⎛ ⎞ ⎛ ⎞⎜ ⎟
̃ 1 0 0 0 0 0 ⎜ ⎜
⎟
⎟
⎝ ̃ ⎠ = ⎝ 0 1 0 0 0 0 ⎠⎜ ⎟ (1.8)
⎜ ⎟
̃ 0 0 1 0 0 0 ⎜ ⎝ ⎠
⎟
⎛ ⎞
̃
Notice that we have three observable variables ⎝ ̃ ⎠ and three structural shocks in this model. If the number
̃
of shocks is less than the number of observables, then the model is stochastically singular and the Kalman filter
cannot be used to calculate the likelihood function (see Ruge-Murcia (2007) for further explanations on this point).
Equations 1.8 and 1.2 thus gives a linear state-space model:
=
= −1 + ̃
(̃ ) = ( ( )) 0
⎛ ⎞
21 0 0
where ( ) = ⎝ 0 22 0 ⎠. The file example2.m demonstrates the calculation of the likelihood using ar-
0 0 23
tificial data generated from the calibrated model in example1.m. On line 8, the function likelihood is called to
carry out ¡this calculation. This function ¢ is shown in figure 3. The input to the function is the parameter vec-
tor Θ = 1 2 3 21 22 23 . Note from line 6, that when the model is solved, only the parameters
1 2 3 are required. With the solution of the model at hand, the next step is to build the matrices of the
state-space. However, this is only useful if the model solution exists and is unique. Therefore, lines 8 to 10 are used
to terminate the likelihood calculation (and to set the log likelihood to minus infinity), if existence or uniqueness are
rejected. The matrices of the state-space are created on lines 16 to 22. Lines 25 and 26 set the initial state vector and
its covariance. The Kalman filter begins on line 29. Note that on line 37, the code calculates the reciprocal condition
number of the variance of the prediction error. The program terminates if the reciprocal condition number is small
indicating that the variance may not have an inverse (which is used to calculate the log likelihood on line 48). The
function returns the log likelihood of the model in the scalar out.
Note from Bayes rule that the log posterior is proportional to the log likelihood plus the log prior:
ln (Θ| ) ∝ ln (Θ| ) + ln (Θ)
log p osterior log likelihood log prior
Therefore to calculate the log posterior we have to evaluate the log prior ln (Θ). For simplicity we assume that there
is an independent prior on each parameter and thus ln (Θ) = ln () + ln () + ln () + ln () + ln (1 ) +
ln (2 ) + ln (3 ) + ln ( 21 ) + ln ( 22 ) + ln ( 23 ), i.e. the sum of the log prior on each parameter. The question
now arises: what prior distributions should be used? In the current example, the following choices for the prior
distributions seem reasonable:
Parameter Distribution(mean,variance) 95% interval
Gamma(1,1) [002 376]
Gamma(1.5,1) [021 400]
Gamma(3,1) [140 525]
Gamma(1.5,1) [021 400]
1 Beta(0.5,0.2) [013 087]
2 Beta(0.5,0.2) [013 087]
3 Beta(0.5,0.2) [013 087]
21 Inverse Gamma(1,0.5) [034 268]
22 Inverse Gamma(1,0.5) [034 268]
23 Inverse Gamma(1,0.5) [034 268]
184 6. BAYESIAN ESTIM ATION OF LINEAR DSGE M ODELS
The prior for is assumed to be a Gamma distribution with a mean and variance of 1. The Gamma distribution
is appropriate for this parameter as we expect to be greater than zero. Note that this (and other) distributions can
be parameterised via their means and variances or parameters such as scale, degrees of freedom etc. As discussed in
the appendix to Bauwens et al. (1999), the mean and the variance of a Gamma( ) distribution are given by
= and = 2 . It is then easy to calculate that = and = . This transformation is useful as functions
to simulate and evaluate the Gamma distribution in Matlab require the user to provide values for and which
can be backed out from and . The Beta distribution is chosen as a prior for the autoregressive parameters to
ensure that they remain between 0 and 1. Note that for a Beta( ) distribution, the mean and variance is defined
as: = + = (+)2(++1)
. As before, we can specify the prior in terms of and and back out the implied
parameters . Finally, we use an Inverse Gamma prior for the shock variances, following the practice in reduced
form econometric models. As described in Bauwens et al. (1999) (page 292), the Inverse Gamma-2 distribution
2. M ETROPOLIS HASTINGS ALGORITHM 185
2
( ) has the following first two moments: = −2 = −4 2 . This allows us to solve for given an value for
the mean and the variance. The matlab code logprior.m evaluates these prior distributions at a given value of the
model parameters and prior means and variances and returns ln (Θ).
Note that one may want to check the shape of the prior distributions implied by these choices. This can be
easily done by simulating random numbers from the prior distribution and examining the percentiles of the resulting
distribution (see matlab file simprior.m). The final column of the table above lists the 5th and 95th percentile for
each chosen prior. In practice, this interval can provide information about whether the prior distribution covers the
range of estimates of these parameters reported in previous papers.
Note, secondly that due to the asymmetry of the posterior distributions, it is likely that the posterior mean does not
coincide with the maximum posterior estimates. This may also reflect the fact that the maximum of the posterior
found by CSMINWEL is a local maximum.
Figure 7 compares the estimated distribution of the response to monetary policy shocks with the response
calculated at the true parameter values and shows that the algorithm performs reasonably well. Finally, note that
the last part of example3.m (see actual code), calculates the marginal likelihood of the model using the Gelfand and
Dey Harmonic mean method (see appendix to previous chapter). This simply requires one to store the value of the
log posterior and the parameters for each draw. Then the marginal likelihood ( ) is approximated by using the
2. M ETROPOLIS HASTINGS ALGORITHM 187
X ¯ ¯−12 ∙ ³ ´ ³ ´0 ¸ ³ ´
1 1 (Θ ) 1 ¯ ¯ −1
equation ( )= where (Θ ) =
(Θ | ) ¯Σ̂¯
(2)2
exp −05 Θ − Θ̂ Σ̂ Θ − Θ̂ × Θ ∈ Θ̃
=1
³ ´
where Θ̂ and Σ̂ denote the mean and covariance of the draws of the DSGE parameters and Θ ∈ Θ̃ is an indicator
³ ´ ³ ´0
function that equals 1 if −05 Θ − Θ̂ Σ̂−1 Θ − Θ̂ ≤ 21− () with equal to number of parameters.
The estimated log marginal likelihood for this model is -945.61. The file example4.m estimates a version of the
model that restricts 1 = 0. The estimated log marginal likelihood of this restricted model is -1072.34. Unsurprisingly,
188 6. BAYESIAN ESTIM ATION OF LINEAR DSGE M ODELS
the (artificial) data favour the benchmark model. More formally one can consider the posterior odds of the benchmark
model 0 against the restricted model 1 . The posterior odds ratio is defined as
0 ( |0 )
01 = ×
1 ( |1 )
The first term in this expression is the prior odds ratio, i.e. the ratio of the prior probabilities (weights) attached
to the two models. If we assume that 0 = 1 , then the posterior odds ratio (collapses to the Bayes Factor) and
evaluates to 01 = exp(−94561 − (−107234)) = 1 09 × 1055 suggesting overwhelming evidence in favour of 0 .
Lubik and Schorfheide (2007) provide an interesting application of model comparison across DSGE models.
3. Further Reading
• Canova and Sala (2009) provide a detailed discussion of identification issues in DSGE models.
• A book on Bayesian estimation of DSGE models by Herbst and Schorfheide (2015)
Part 3
Further Topics
CHAPTER 7
1. Introduction
A number of recent papers have shown that time-variation in the parameters and shock variances of state-space
models can be useful in many empirical contexts. Some recent examples include Negro and Otrok (2008) and Mumtaz
and Surico (2012) who estimate dynamic factor models (DFM) with time-varying parameters and stochastic volatility,
while STOCK and WATSON (2007) introduce stochastic volatility in an unobserved component model. This chapter
considers the Gibbs sampling algorithm for such models in detail. In particular, it describes the algorithm and code
for estimating the dynamic factor model of Mumtaz and Surico (2012). The methods reviewed in this chapter can
be applied to several variants of these extended state-space models considered in the literature.
˜ (0 1) (2.3)
¡£ ¤¢
This AR model features time-varying coefficients and stochastic volatility. The coefficients Φ
= 1
are assumed to evolve as random walks:
¡ ¢12
Φ = Φ −1 +
˜ (0 1)
Exactly the same formulation is used for the country factors, with each of them described by an AR(p) process with
time-varying parameters and stochastic volatility . That is the country factors follow:
X 1
= + −
+ ( ) 2 (2.4)
=1
˜ (0 1) (2.5)
¡£ ¤¢
The coefficients Φ = 1 are assumed to evolve as random walks:
12
Φ = Φ−1 + ( )
˜ (0 1)
where:
³ ´
12
= −1 + ˜ (0 1)
3.1. Priors and starting values. We start the description by explaining how the starting values and priors
are set for each parameter. The code for this is shown in figures 1 to 3. Note that this example uses artificial data
that is generated from this model by running generate_data.m. This data is loaded on line 8 and standardised on
line 9. Line 11 sets a training sample of 20 to calibrate some priors as discussed below.
(1) Factor loadings : In order to set the prior, we obtain estimates of by using a principal component
estimator. With an estimate of the factors ̂ ̂ in hand , estimates of the factor loadings can easily be
obtained by treating the equation =
̂ + ̂ + as N linear regressions and obtain the factor
loadings via OLS. The prior we use is then given as () ˜ (̂ ) where ̂ is the OLS estimate of
and . In figures 1 to 3 this is done starting line 27 that estimates the world factor. The country factor
is then estimated via principal components on the residual data (after removing the impact of the world
factor) for each country on lines 30 to 34. Lines 37 to 44 run the OLS regression for each series storing the
prior mean in FLOAD0. Line 45 sets the prior variance . Note that the residuals from these regressions
are stored in the matrix res.
(2) Priors for : The prior is assumed to be inverse Wishart. We follow the approach developed in
papers by Cogley and Sargent and use a training sample to estimate the scale matrices. Given 0 training
P
sample observations for ̂ we can estimate an AR model via OLS: ̂ = ̂ + =1 ̂
̂− + . Call
¡ ¢ ¡ ¢
the coefficient covariance from this regression ̂ . Then the prior for is set as ˜ 0 0
where 0 = ̂
× 0 × where = 35 × 10−4 in our application. The prior for for = 1 2
is set in exactly the same manner using the principal component estimates of the country factors. The same
procedure is used to set the prior for . The scale matrices obtained from this procedure are also used as
initial values for these variances. For example is initialised by assuming that =
0 . For 0 these
calculations are implemented on lines 49 to 51. Line 49 prepares the dependent and independent variables
for an AR(2) regression. Line 50 runs the regression obtaining the coefficient covariance p00w which is used
to calculate the scale matrix on line 51. Exactly the same procedure is repeated for each country factor
on lines 53 to 64 and for each idiosyncratic factor on lines 66 to 76. Note that these OLS regressions are
also used to obtain the initial conditions for the time-varying coefficients and their variances. For example
3. PRIORS AND THE GIBBS SAM PLING ALGORITHM 193
³ ´
Φ0 ˜ Φ
0|0 Φ where Φ 0|0 is set to the OLS estimates of the coefficients while Φ is the coefficient
covariance obtained via OLS.
(3) Starting values for
: Lines 78 to 82 remove the training sample from the data and the initial
P
estimates of the factors. Line 86 and 87 again conducts the OLS regression ̂ = ̂ + =1 ̂
̂− +
¡ ¢
2
using the estimation sample. The starting value for is set as + 00001. As explained in Chapter
5 and the description below, this starting value is used in the Metropolis Step devised by Jacquier et al.
194 7. STATE-SPACE M ODELS W ITH TIM E-VARYING PARAM ETERS
(2004) to sample from the conditional posterior of the stochastic volatilities. In exactly the same manner,
the starting values for and are set on lines 90 to 107.
(4) Priors for : The priors for these variances is inverse Gamma: (0 ). In our application we
set = 1 0 = 001 (line 109). Note that the metropolis algorithm to draw the stochastic volatility uses a
prior for the initial condition. For example one needs to set ln 0 ˜ (̄ ̄). We set ̄ = 10 (line 108) and
use the OLS estimate of the error variance (in step 2) to set the prior mean ̄ (i.e. s00w on line 50). The
prior for the initial condition for all stochastic volatilties is set this way.
3. PRIORS AND THE GIBBS SAM PLING ALGORITHM 195
3.2. The Gibbs sampling algorithm. The Gibbs algorithm involves sampling from the following conditional
posterior distributions:
¡ ¢
(1) Sample from Φ |Ξ : Here Ξ all remaining parameters and states in the model. Given a draw of the
world factor, the stochastic volatility
and the variance , this step involves a TVP regression with a
3. PRIORS AND THE GIBBS SAM PLING ALGORITHM 197
This is a linear state-space model and the Carter Kohn algorithm is used to draw Φ . As described in
Chapter 3, this involves running the Kalman filter and a backward recursion. As the variance of the error
to the observation equation is time-varying (i.e. ), a slight modification to the Kalman filter is required
to ensure that this time-variation is taken into account — i.e. the different value for the variance is selected
in each recursion of the Kalman filter. The code for this step is shown in figure 4 lines 9 to 16. ¡Line 19¢
creates the left and the right hand size variables in this TVP regression. Line 10 draws from Φ |Ξ
given previous values of and the initial conditions Φ
0|0 Φ . The function carterKohnAR
has the following inputs: 1) dependent variable, 2) independent variable, 3) , 4)
, 5) Φ0|0 , 6) Φ ,
7) P, 8) CHECK, 9) maxdraws, 10) EX. If CHECK =1, then at most maxdraws attempts are made to find a
stable draw. If no stable draw is found problemw is set to 1. Finally EX =1 implies there is one exogenous
regressor, i.e. the intercept. The function returns the draw from conditional posterior using the Carter Kohn
algorithm: beta2w, the residuals of the regression errorw, a dummy variable that equals 1 at a particular
time period if the stability condition is violated at that observation
³¡ (rootsw ) and problemw. ´
¡ ¢ ¢0 ¡ ¢
(2) Sample from |Ξ : This conditional posterior is Φ − Φ
−1 Φ − Φ
−1 + 0 + 0 .
Lines 18 to 20 in figure 4 display the draw of this parameter.
(3) Sample from (Φ |Ξ) for = 1 2 : This involves exactly the same calculation as step 1. The only
change is that we need to conduct this draw using each country factor seperately. Lines 22 to 33 in figure
4 show the application of the Carter Kohn algorithm using each .
(4) Sample from ( |Ξ) for = 1 2 : The draw from this inverse Wishart conditional posterior is carried
out on lines 35 to¡ 38 exactly
¢ as in step 2.
(5) Sample from |Ξ for = 1 2 : Given a value for the residuals , the stochastic volatilties
and variances this is imply an application of the Carter and Kohn algorithm to the TVP-AR model that
applies to each , i.e.
1
= −1 + ( ) 2
³ ´
12
= −1 +
The same code as in step 1 is again used (lines 41 to 52), looping over the N residuals. Not that EX=0
when calling carterkohnAR as the regression has no intercept. Not the use of parfor, the parallel for loop
which can speed up this step if N is large.
(6) Sample from ( |Ξ) for = 1 2 : Lines 54 to 58 show this draw from the inverse Wishart distribution.
¡ ¢
(7) Sample from |Ξ : Given the residuals of the transition equation 2.2, the following stochastic volatility
model applies:
¡ ¢ 12
=
¡ ¢12
ln
= ln
−1 +
Given and initial conditions, the independence Metropolis algorithm of Jacquier et al. (2004) can be
used to draw as explained in Chapter 5. The code for this step is shown in figure 5 (lines 3 and 4).
Line 3 calls a function getsvol. The inputs to this function are: 1) hlastw, the last draw of
, 2) , 3)
the prior mean for the initial volatility ̄, 4) The variance of the prior for the initial volatility and 5) the
residuals , i.e. the observed data in the observation equation of the stochastic volatility model. It returns
a ( + 1) × 1 vector containing the draw of . Line 4 updates hlastw for the next draw.
¡ ¢ ¡ ¢0 ¡ ¢
(8) Sample from |Ξ : This conditional posterior is inverse Gamma: ( ln
− ln −1 ln
− ln −1 +
0 + ). Lines 5 and 6 conduct this draw.
(9) Sample from ( |Ξ) for = 1 2 : As in step 7, this draw is a simple application of the Jacquier et al.
(2004) using the residuals from the transition equation of each country factor. This is done on lines 9 to 11
in figure 5. ¡ ¢
(10) Sample from |Ξ for = 1 2 : This is simply a series of draws from the inverse Gamma distrib-
utions as in step 8. See lines 12 and 13 of the code.
198 7. STATE-SPACE M ODELS W ITH TIM E-VARYING PARAM ETERS
(11) Sample from ( |Ξ) for = 1 2 : This requires nothing more than an application of the Jacquier
et al. (2004) algorithm to the series of stochastic volatility models given by
1
= ( ) 2
ln = ln −1 + ( )12
where denotes the residuals = − −1 for = 1 2 collected when conducting step 5 above.
See lines 16 to 19 of the code.
(12) Sample from ( |Ξ) for = 1 2 : Line 20 and 21 of figure 5 show the draw of this variance from the
inverse Gamma distribution for each i.
3. PRIORS AND THE GIBBS SAM PLING ALGORITHM 199
(13) Sample from (|Ξ): The observation equation of the factor model is
=
+ +
1
= −1 + ( ) 2
For each i, given and the model can be easily transformed so that the error term has no serial
correlation or heteroscedasticity:
³ ´−1 ³ ´
∗ = −1 + 0 −1 ̂ + 0 (3.1)
³ ´−1
∗ = −1 + 0
The code for this step is shown in figure 6. Line 5 starts a loop across the countries. Lines 6 to 9, select the
data, the sereial correlation coefficients , the error variances and the prior means ̂ for each country.
The first two sets of factor loadings for each country are fixed, i.e. the first two rows of the factor loading
matrix for each country is set equal to an identity matrix to deal with rotational indeterminancy of the
factor model. Line 18 loops over the remaining data series. Lines 19 to 26 remove the serial correlation and
heteroscedasticity and line 31 draws the laodings from the conditional posterior. The inputs to the function
getreg are: 1) , 2) , 3) ̂ 4) and 5) Variance of the error of the regression which equals 1 here. Note
that the residuals are updated on line 34.
(14) Sample from ( |Ξ): The final step is a draw of the factors from their conditional posterior. This is
simply an application of the Carter and Kohn algorithm. However, it is instructive to consider the model
in state-space form. Consider, for a simplicity an example where = 8 and = 2 and = 2. The
observation equation is then given by:
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
1 − 1 1−1
1 11 0 −1
1 −1 11 0 ̃1
⎜ ⎟ ⎜ 12 0 −2 −2 12 0 ⎟⎛ ⎞
⎜ ⎟
⎜ 2 − 2 2−1 ⎟ ⎜ 2 2 ⎟ ⎜ ̃2 ⎟
⎜ ⎟ ⎜ 13 −3 −3 13 ⎟⎜ ⎟ ⎜ ⎟
⎜ 3 − 3 3−1 ⎟ ⎜ 3 0 3 0 ⎟⎜ 1 ⎟ ⎜ ̃3 ⎟
⎜ ⎟ ⎜ ⎟⎜ ⎟ ⎜ ⎟
⎜ 4 − 4 4−1 ⎟ = ⎜
4 14 0 −4
4 −4 14 0 ⎟⎜ 2 ⎟+⎜ ̃4 ⎟
⎜ ⎟ ⎜ 2 ⎟⎜ ⎟ ⎜ ⎟
⎜ 5 − 5 5−1 ⎟ ⎜
5 0 21 −5
5 0 −5 1 ⎟⎜
−1 ⎟ ⎜ ̃5 ⎟
⎜ ⎟ ⎜ ⎟⎝ 1 ⎠ ⎜ ⎟
⎜ 6 − 6 6−1 ⎟ ⎜
⎜
6 0 22 −6
6 0 −6 22 ⎟
⎟
−1 ⎜ ̃6 ⎟
⎝ 7 − 7 7−1 ⎠ ⎝ 0 23 −7 0 −7 23 ⎠
2
−1 ⎝ ̃7 ⎠
7 7
8 − 8 8−1 0 24 −8 0 −8 24
̃8
8 8
̃
⎛ ⎞
1 0 0 0 0 0 0 0
⎜ 0 2 0 0 0 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 3 0 0 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 0 4 0 0 0 0 ⎟
(̃ ) = = ⎜
⎜
⎟
⎟
⎜ 0 0 0 0 5 0 0 0 ⎟
⎜ 0 0 0 0 0 6 0 0 ⎟
⎜ ⎟
⎝ 0 0 0 0 0 0 7 0 ⎠
0 0 0 0 0 0 0 8
for this in the Kalman filter. The transition equation of the model is defined as:
⎛ ⎞ ⎛ ⎞⎛ ⎞⎛ ⎞ ⎛ ⎞
1 0 0 2 0 0 −1 ̃1
⎜ 1 ⎟ ⎜ 1 ⎟⎜ 0 11 0 0 1
0 ⎟ ⎜ 1 ⎟ ⎜ ̃2 ⎟
⎜ ⎟ ⎜ 2 ⎟⎜ 2 ⎟ ⎜ −1 ⎟ ⎜ ⎟
⎜ 2 ⎟ ⎜ ⎟⎜ 0 2 2 ⎟⎜ −1 2 ⎟ ⎜ ⎟
⎜ ⎟ = ⎜ ⎟⎜ 0 1 0 0 2 ⎟⎜ ⎟ ⎜ ̃3 ⎟
⎜ −1 ⎟ ⎜ 0 ⎟⎜ 1 0 ⎟ ⎜ ⎟+⎜ 0 ⎟
⎜ 1 ⎟ ⎜ ⎟⎜ 0 0 0 0 ⎟⎜ −2 ⎟ ⎜ ⎟
⎝ −1 ⎠ ⎝ 0 ⎠⎝ 0 1 0 0 0 0 ⎠⎝ −2 1 ⎠ ⎝ 0 ⎠
2 2
−1 0 0 0 1 0 0 0 −2 0
−1 ̃
⎛ ⎞
0 0 0 0 0
⎜ 0 1 0 0 0 0 ⎟
⎜ ⎟
⎜ 0 0 2 0 0 0 ⎟
⎜
(̃ ) = = ⎜ ⎟
0 0 0 0 0 0 ⎟
4. FURTHER READING 201
Again, the parameters of the transition equation are time-varying. This needs to be accounted for in the
Kalman filter and the backward recursion. The code for this step is shown in figures 7 and 8. Lines 2 to 5
calculate − −1 using the function remSC which takes arguments and . The Kalman filter
iteration begins on line 13. Notice that the matrices of the state-space have to be built within the loop as
they are time-varying. Lines 18 to 36 construct the matrix shown in the observation equation. Line 38
constructs . The matrices of the transition equation are constructed on lines 42 to 47. Lines 50 to 53
are the equations of the prediction and the update steps of the Kalman filter. The backward recursion of
the Carter and Kohn algorithm begins on line 66. As in the Kalman filter, the matrices of the transition
equation are constructed at each time period. One important point is the fact that the backward recursion
is derived by assuming the following ‘observation equation’ (see Chapter 3):
+1 = +1 + +1 + +1
(+1 ) = +1
Notice that the matrices of the state-space are dated at time + 1. Thus, in the Carter and Kohn backward
recusrsion when = − 2, for example, the matrices of the state space are −1 −1 −1 . These
matrices are constructed on lines 77 to 82. Line 84 to 86, select the rows corresponding to the non-singular
block of . The remaining lines calculate the mean and variance of the conditional distribution and draw
the state-variables (see Chapter 3). The factors are extracted on lines 94 to 96.
As mentioned above, the example uses artificial data. Based on a run using 20,000 iterations and a burn-in of
10,000 replications we can compare the estimated contribution of the world factor to the variance of each series with
the true value assumed in the data generating process.
Figure 9 shows this comparison for the first 40 series of artificially generated data. The black line shown the true
time-varying contribution of the world factor to the variance of the series. The red line shows the posterior estimate
obtained by running the algorithm described above.
4. Further reading
• Time-varying factor models are also estimated in Bianchi et al. (2009), Baumeister et al. (2013), Liu et al.
(2014), Ellis et al. (2014) and Mumtaz and Theodoridis (2017). The algorithms used in these papers is very
similar to the one described in this chapter.
• Kim and Nelson (1999) consider state-space models with Markov Switching in Chapter 10.
202 7. STATE-SPACE M ODELS W ITH TIM E-VARYING PARAM ETERS
Figure 9. A comparison of the true contribution of the world factor (black line) with the estimated
posterior median contribution (red line)
CHAPTER 8
c
Appendix: Introduction to Matlab°
1. Introduction
This appendix provides a basic introduction to Matlab and introduces the key concept needed in dealing with
the codes used in this book. Note that a number of alternative guides to Matlab are available on the web and these
may be used to supplement the material here.
2. Getting started
Figure 1 shows a basic screen shot of Matlab. There are two main windows: (1) the editor window which is
docked on top and the (2) command window which is docked at the bottom. The editor is where we type our code.
Once the code is typed it can be saved as a file which has the extension .m. The code is run by clicking on the green
run button or by simply typing in the name of the program file in the command window. The command window is
where the output from running the code appears. Or alternatively, each line of the code can be run by typing it in
the command window and pressing enter.
In figure 2 we show how to create a generic first program called helloworld.m which prints out the words Hello
World. The code simply consists of the line ’Hello World’ where the single quotes signify that this a string variable
as opposed to a numeric variable (or a number). By clicking on run, the output appears in the command window.
Alternatively one can run the line of the program containing the code by highlighting the line and pressing F9.
µ ¶
2 3 4 5
=
7 8 9 10
Figure 3 shows the matlab code required to do this (example1.m). Note that the numbers in the first column are
line numbers which are shown here for convenience and will not appear in the actual code. Note also that any code
starting with % is a comment and is ignored by matlab. Line 2 shows how X is created.1 The numbers have square
brackets around them. Each column of the matrix is seperated by a space and rows by a semi-colon. Lines 2 and 3
finish with a semi-colon. This stops Matlab from priniting out X and Z when the code is run. Instead we print the
matrices by typing X and Z without a semi-colon on lines 5 and 6.
3.1.2. Entering a matrix using built in commands. Line 2 in figure 4 creates a 10 × 10 matrix of zeros (the file is
example2.m). The first argument in the function zeros denotes the number of rows, the second denotes the number
of columns required. Line 4 creates a 10 × 20 matrix of ones. Line 6 creates a 10 × 10 identity matrix. Line 8 creates
a 10 × 10 matrix where each element is drawn from a N(0,1) distribution. Similarly, line 9 creates a matrix from the
standard uniform distribution.
3.1.3. Reading in data from an excel file. In practice we will read in matrices (i.e. data) from an outside source.
As an example we have stored data on UK GDP growth and inflation in an excel file called data.xls in the folder
called data. Figure 5 shows the matlab code used to read the excel file. The command xlsread reads the data into a
matrix called data. The variable names are read into a string variable called names. Note that text files can be read
by using the command load. Suppose one wants to read in data from a text file dalled data.txt, one simply needs to
type load data.txt. Type ‘help load’ in the command window for further information.
205
c
206 8. APPENDIX: INTRODUCTION TO M ATLAB °
3.2. Manipulating matrices. Lines 2 and 3 in figure 6 create two matrices X and Z shown in example 1 above.
Line 5 sets the element (2,1), i.e. the element on the second row first column to 30. Line 7 creates a new 4 × 4 matrix
by vertically concatenating X and Z. This done by the command M=[X;Z] where the semi-colon denotes vertical
concatenation. That is, it creates
⎛ ⎞
1 2 3 4
⎜ 30 6 7 8 ⎟
=⎜ ⎝ 2 3 4 5 ⎠
⎟
7 8 9 10
Line 9 of the code creates a new 2 × 8 matrix N by horizontally concatenating X and Z. This done by the command
N=[X Z] where the space denotes horizontal concatenation. Finally Line 11 of the code shows how to set the entire
second row of N equal to -10. Note that argument 1:end in N(2,1:end)=-10 selects columns 1 to 10. One can delete
elements by setting them equal to [ ]. For example N(2,:)=[ ] deletes the entire second row.
3.3. Matrix Algebra. Matlab supports numerous matrix functions. We demonstrate some of these by writing
code to estimate an OLS regression using data in the file data.xls used in example 3 above (example5.m). Recall the
formulas for an OLS regression = + are
−1
̂ = ( 0 ) ( 0 )
( − )0 ( − )
() = =
−
³ ´
0 −1
̂ = × ( )
Where T denotes the number of observations and K are the number of regressors. We assume is the first column
of data.xls and is the second column of data.xls and a constant term. Line 3 of the code shown in Figure 7 loads
the data. Line 5 shows the function size to find the number of rows in the data. Line 7 assigns the first column of
3. M ATRIX PROGRAM M ING LANGUAGE 207
Figure 3. Example 1
data to a × 1 matrix called Y. Line 9 creates the × 2 matrix where the first column is the constant and the
second column is the second column of data.
−1
Line 11 calculates the OLS esimate of the coefficients using the formula ̂ = ( 0 ) ( 0 ). This line shows
three matrix functions. First the the transpose of X in matlab is simply X’. One can multiply conformable matrices
by using the * key. The inverse of matrix is calculated in matlab using the inv function. Line 13 calculates the
c
208 8. APPENDIX: INTRODUCTION TO M ATLAB °
Figure 4. Example 2
Figure 5. Example 3
Figure 6. Example 4
³ ´
residuals while Line 15 calculates () and line 17 calculates ̂ = × ( 0 )−1 Note that is a scalar
−1
and ( 0 ) is a matrix. In general a scalar can be multiplied by each element of matrix by the ‘.*’ key which
denotes element by element multiplication. So this can also be used for element by element multiplication of two
matrices. Finally, the standard errors of the coefficients are given as the square root of the diagonal of the matrix
4. PROGRAM CONTROL 209
Figure 7. Example 5
Figure 8. Example 6
−1
× ( 0 ) Line 19 uses the diag command to extract the diagonal and then takes the square root by raising them
to the power 0.5. This is done by the command .^ which raises each element of a matrix to a specified power.
Other useful matrix functions include the Cholesky decomposition (command chol, type help chol in the command
window for details). A list of matrix functions can be seen by typing help matfun in the command window.
4. Program control
4.1. For Loops. One of the main uses of programming is to use the code to repeat tasks. This is used intensively
in Bayesian simulation methods. The main type of loop we use is the For loop. To take a trivial example suppose we
need to create a 100 × 1 matrix called Z where each element is equal to row number raised to power 2. So Z(1,1)=1,
Z(2,1)=2^2, Z(3,1)=3^2 and so on. The code is shown in figure 8. Line 2 sets the total number of rows in the matrix
Z. Line 3 creates the empty matrix Z.
c
210 8. APPENDIX: INTRODUCTION TO M ATLAB °
Figure 9. Example 7
Line 4 begins the for loop and instructs matlab to repeat the instruction on line 5 REPS times. That is the loop
starts with i=1 and repeats the instruction below (instructions on lines before the end statement) until i=REPS. It
increases i by 1 in each iteration. On line 5 the ith row of Z is set equal to i squared. Therefore when i=1, Z(1,1)=1,
when i=2, Z(2,1)=22 , when i=3, Z(3,3)=32 and so on. The instruction end on line 6 closes the for loop. Note if
we had typed on line 4 for i=1:-1:REPS, i would be decreased by 1 in each iteration. If we had typed on line 4 for
i=1:2:REPS, i would be increased by 2 in each iteration.
Figure 9 shows a second example where we simulate an (1) process for 1000 periods = −1 + =
11000. Where ˜ (0 1). Line 3 of the code creates a × 1 matrix of zeros Y Line 4 draws the error term from
the standard normal distribution. Line 5 sets the AR coefficient = 099. Line 6 starts the loop from period 2 going
up to period T=1000. Line 7 simulates each value of = 11000 and line 8 ends the for loop. Line 9 plots the
simulated series.
4.2. Conditional statements. Conditional statements instruct Matlab to carry out commands if a condition
is met. Suppose, in example 7 above, we want to simulate the AR model but only want to retain values for Y which
are greater than or equal to zero. We can do this by using the if statement in matlab. Figure 10 shows the matlab
code. Lines 1 to 5 are exactly as before. Line 6 begins the for loop. Line 7 sets a variable = −1 + Line 8
4. PROGRAM CONTROL 211
begins the if statement and instructs matlab to carry out the command on line 9 if the condition temp=0 is true.
Line 10 ends the if statement. Line 11 ends the for loop. If we wanted to retain negative values only, line 8 would
change to if temp0. If we wanted to retain values equal to zero, line 8 would change to if temp==0. If we wanted
to retain all values not equal to zero, line 8 would change to if temp~=0. If we wanted to retain values greater than
0 but less than 1, line 8 would change to if temp0 & & temp1. If we wanted to retain values greater than 0 or
greater than 1 line 8 would change to if temp0 || temp 1.
4.3. While Loops. While loops are loops which repeat matlab statements until a condition is met. The code
in figure 11 re-formulates the for loop in example 7 using the while loop. Line 6 sets the starting point of the lopp at
i=2. Line 7 starts the while loop and instructs matlab to perform tasks before the end statement until the condition
iT is true. On line 8 we simulate the AR(1) model as before. Line 9 increase the value of i by 1 in each iteration.
Note that unlike the For loop, the index variable is not incremented automatically and this has to be done manually.
4.4. Functions. As our code becomes longer and more complicated, it is good practice to transfer parts of the
code into seperate files called functions which can then be called from a main program in exactly the same way as
built in matlab functions (like inv() etc) . Suppose we want to create a function called AR which takes as inputs the
value of AR coefficient and number of observations and returns as output simulated × 1 matrix of data from
this AR model. We can convert the code from example 7 into this function in a simple way. The code is shown in
figure 12. The function begins with the word function. Then one specifies the output of the function (Y), the name
of the function (AR) and the inputs (RHO,T). Lines 2 to 6 remain exactly the same. This function should be saved
with the file name AR.m. The function (for e.g. with = 099 = 100)can be called from the command window
(or from another piece of code) as Y=AR(0.99,100).
c
212 8. APPENDIX: INTRODUCTION TO M ATLAB °
5. Numerical optimisation
A key tool in Matlab is the ability find the maximum/minimum of a function numerically. This is important as
we need these numerical tools to find the maximum of the likelihood functions. There are several built in optmising
routines in Matlab. Here we focus on a minimisation routine written by Chris Sims called CSMINWEL (available
from http://sims.princeton.edu/yftp/optimize/mfiles/. We have saved these files in the folder func). This routine
has been known to work well for the type of models we consider in this course.
As an example we are going to maximise the likelihood function for the linear regression model considered in
example 5. The log likelihood function is given by
µ ¶
¡ ¢ 1 ( − )0 ( − )
ln = − 2 ln 2 2 −
2 2
We need to maximise this with respect to and 2 To use CSMINWEL we proceed in two steps
STEP1: We first need to write a function that takes in as input a set of values for and 2 and returns the
value of ln at that particular value of the parameters. The code to calculate the likelihood function is shown
in 13. The function is called loglikelihood. It takes as input, the parameters theta, and the data series Y and X.
The parameter vector needs to be the first argument. Line 5 extracts the regression coefficients. Line 6 extracts the
standard deviation of the error term and squares it. Thus we optimise with respect to (and not 2 ). Line 6
ensures that the value of 2 will always be positive. Lines 7 and 8 calculate the likelihood function for a given and
2 The consitional statement on line 9 checks for numerical problems. In particular, it checks if the log likelihood is
not a number (isnan(lik), or it equals infinity (isinf(lik)) or is a complex number (1-isreal(lik) — isreal(lik) equals 1 if
lik is real and thus 1-isreal(lik) equals 1 if lik is not a real number). In case of numerical problems, the negative of
the log likelihood is set to a large number. If there are no numerical problems the function returns the negative of
the calculated log likelihood. The function returns the negative of the log likelihood as CSMINWEL is a minimiser
(i.e. we minimise the negative of the log likelihood and this is equivalent to maximising the log likelihood).
STEP2: We use CSMINWEL to minimise the negative log likelihood calculated by loglikelihood.m. This code
can be seen in figure 14. Line 2 ensures that the files required for CSMINWEL in the folder func can be found by
Matlab. Lines 4 to 10 load the data and create the Y and X matrix. Line 12 specifies the initial values of the
parameters. Line 14 calls the function csminwel. The first input argument is the name of the function that calculates
the log likelihood. The second input are the starting values. The tird input is the starting value of the inverse hessian.
This can be left as default as a × matrix with diagonal elements equal to 0.5. If the next argument is set equal
to [ ] csminwel uses numerical derivitives in the optimisation. This is the default un all out applications. The next
argument is the convergence tolerance which should be left as defaults. The next input argument are the number of
maximum iterations. The remaining inputs are passed directly to the function loglikelihood.m after the parameter
values. The function returns the minimum of the negative log likelihood in fhat, the values of the parameters at the
5. NUM ERICAL OPTIM ISATION 213
minimum in xhat and the inverse hessian as Hhat.Retcodehat=0 if convergence occurs. Running this code produce
the same value of as the OLS formula in the examples above.
Bibliography
Albert, James H. and Siddhartha Chib, 1993, Bayesian Analysis of Binary and Polychotomous Response Data,
Journal of the American Statistical Association 88(422), 669—679.
URL: http://www.tandfonline.com/doi/abs/10.1080/01621459.1993.10476321
An, Sungbae and Frank Schorfheide, 2007, Bayesian Analysis of DSGE Models, Econometric Reviews 26(2-4), 113—
172.
URL: http://dx.doi.org/10.1080/07474930701220071
Banbura, Marta, Domenico Giannone and Lucrezia Reichlin, 2007, Bayesian VARs with Large Panels, CEPR Dis-
cussion Papers 6326, C.E.P.R. Discussion Papers.
URL: http://ideas.repec.org/p/cpr/ceprdp/6326.html
Baumeister, Christiane, Philip Liu and Haroon Mumtaz, 2013, Changes in the effects of monetary policy on disag-
gregate price dynamics, Journal of Economic Dynamics and Control 37(3), 543—560.
URL: https://ideas.repec.org/a/eee/dyncon/v37y2013i3p543-560.html
Bauwens, Luc, Michel Lubrano and Jean François Richard, 1999, Bayesian inference in dynamic econometric models,
Oxford University Press.
Benati, Luca and Haroon Mumtaz, 2006, US evolving Macroeconomic Dynamics: A structural investigation., Mimeo
European Central Bank.
Bernanke, Ben, Jean Boivin and Piotr S. Eliasz, 2005, Measuring the Effects of Monetary Policy: A Factor-augmented
Vector Autoregressive (FAVAR) Approach, The Quarterly Journal of Economics 120(1), 387—422.
URL: http://ideas.repec.org/a/tpr/qjecon/v120y2005i1p387-422.html
Bianchi, Francesco, Haroon Mumtaz and Paolo Surico, 2009, The great moderation of the term structure of UK
interest rates, Journal of Monetary Economics 56(6), 856—871.
URL: https://ideas.repec.org/a/eee/moneco/v56y2009i6p856-871.html
Canova, Fabio, 2007, Methods for Applied Macroeconomic Research, Princeton University Press, Princeton.
Canova, Fabio and Luca Sala, 2009, Back to square one: Identification issues in DSGE models, Journal of Monetary
Economics 56(4), 431—449.
URL: https://ideas.repec.org/a/eee/moneco/v56y2009i4p431-449.html
Carriero, Andrea, George Kapetanios and Massimiliano Marcellino, 2010, Forecasting Government Bond Yields with
Large Bayesian VARs, Working Papers 662, Queen Mary, University of London, School of Economics and Finance.
URL: http://ideas.repec.org/p/qmw/qmwecw/wp662.html
Carter, C. K. and R. Kohn, 1994, On Gibbs sampling for state space models, Biometrika 81(3), 541—553.
URL: http://biomet.oxfordjournals.org/content/81/3/541.abstract
Casella, George and Edward I. George, 1992, Explaining the Gibbs Sampler, The American Statistician 46(3), pp.
167—174.
URL: http://www.jstor.org/stable/2685208
Chen, Cathy W. S. and Jack C. Lee, 1995, BAYESIAN INFERENCE OF THRESHOLD AUTOREGRESSIVE
MODELS, Journal of Time Series Analysis 16(5), 483—492.
URL: http://dx.doi.org/10.1111/j.1467-9892.1995.tb00248.x
Chib, Siddhartha, 1993, Bayes regression with autoregressive errors : A Gibbs sampling approach, Journal of Econo-
metrics 58(3), 275—294.
URL: http://ideas.repec.org/a/eee/econom/v58y1993i3p275-294.html
Chib, Siddhartha, 1995, Marginal Likelihood from the Gibbs Output, Journal of the American Statistical Association
90(432), 1313—1321.
URL: http://www.jstor.org/stable/2291521
Chib, Siddhartha, 1996, Calculating posterior distributions and modal estimates in Markov mixture models, Journal
of Econometrics 75(1), 79 — 97.
URL: http://www.sciencedirect.com/science/article/pii/0304407695017704
Chib, Siddhartha, 1998, Estimation and comparison of multiple change-point models, Journal of Econometrics
86(2), 221 — 241.
URL: http://www.sciencedirect.com/science/article/pii/S0304407697001152
Chib, Siddhartha and Srikanth Ramamurthy, 2010, Tailored randomized block MCMC methods with application to
DSGE models, Journal of Econometrics 155(1), 19—38.
URL: http://ideas.repec.org/a/eee/econom/v155y2010i1p19-38.html
215
216 Bibliography
Cogley, Timothy and Thomas J. Sargent, 2002, Evolving Post-World War II U.S. Inflation Dynamics, NBER Macro-
economics Annual 2001, Volume 16, NBER Chapters, National Bureau of Economic Research, Inc, pp. 331—388.
URL: http://ideas.repec.org/h/nbr/nberch/11068.html
Doan, Thomas, Robert B. Litterman and Christopher A. Sims, 1983, Forecasting and Conditional Projection Using
Realistic Prior Distributions, NBER Working Papers 1202, National Bureau of Economic Research, Inc.
URL: http://ideas.repec.org/p/nbr/nberwo/1202.html
Ellis, Colin, Haroon Mumtaz and Pawel Zabczyk, 2014, What Lies Beneath? A Time-varying FAVAR Model for the
UK Transmission Mechanism, The Economic Journal 124(576), 668—699.
URL: http://dx.doi.org/10.1111/ecoj.12147
Filardo, Andrew J. and Stephen F. Gordon, 1998, Business cycle durations, Journal of Econometrics 85(1), 99—123.
URL: https://ideas.repec.org/a/eee/econom/v85y1998i1p99-123.html
Fry, Renee and Adrian Pagan, 2007, Some Issues in Using Sign Restrictions for Identifying Structural VARs, NCER
Working Paper Series 14, National Centre for Econometric Research.
URL: http://ideas.repec.org/p/qut/auncer/2007-8.html
Gelfand, A. E. and D. K. Dey, 1994, Bayesian Model Choice: Asymptotics and Exact Calculations, Journal of the
Royal Statistical Society. Series B (Methodological) 56(3), pp. 501—514.
URL: http://www.jstor.org/stable/2346123
Gelman, Andrew and Donald B. Rubin, 1992, Inference from Iterative Simulation Using Multiple Sequences, Statistical
Science 7(4), pp. 457—472.
URL: http://www.jstor.org/stable/2246093
Geweke, John, 1991, Evaluating the accuracy of sampling-based approaches to the calculation of posterior moments,
Technical report.
Geweke, John, 1998, Using simulation methods for Bayesian econometric models: inference, development, and com-
munication, Technical report.
Hamilton, J. D., 1994, Time series analysis, Princeton University Press, Princeton.
Herbst, E. and F. Schorfheide, 2015, Bayesian Estimation of DSGE Models, Princeton University Press, Princeton.
Jacquier, E, N Polson and P Rossi, 2004, Bayesian analysis of stochastic volatility models, Journal of Business and
Economic Statistics 12, 371—418.
Kadiyala, K Rao and Sune Karlsson, 1997, Numerical Methods for Estimation and Inference in Bayesian VAR-Models,
Journal of Applied Econometrics 12(2), 99—132.
URL: http://ideas.repec.org/a/jae/japmet/v12y1997i2p99-132.html
Kim, C-J. and C. R. Nelson, 1999, State-space models with regime switching, MIT Press, Cambridge, Massachusetts.
Kim, Sangjoon, Neil Shephard and Siddhartha Chib, 1998, Stochastic Volatility: Likelihood Inference and Comparison
with ARCH Models, Review of Economic Studies 65(3), 361—93.
URL: http://ideas.repec.org/a/bla/restud/v65y1998i3p361-93.html
Koop, Gary, 2003, Bayesian Econometrics, Wiley.
Liu, Philip, Haroon Mumtaz and Angeliki Theophilopoulou, 2014, The transmission of international shocks to the
UK. Estimates based on a time-varying factor augmented VAR, Journal of International Money and Finance
46(C), 1—15.
URL: https://ideas.repec.org/a/eee/jimfin/v46y2014icp1-15.html
Lopes, Hedibert F. and Esther Salazar, 2006, Bayesian Model Uncertainty In Smooth Transition Autoregressions,
Journal of Time Series Analysis 27(1), 99—117.
URL: http://dx.doi.org/10.1111/j.1467-9892.2005.00455.x
Lubik, Thomas A. and Frank Schorfheide, 2007, Do central banks respond to exchange rate movements? A structural
investigation, Journal of Monetary Economics 54(4), 1069 — 1087.
URL: http://www.sciencedirect.com/science/article/pii/S0304393206002108
Mumtaz, Haroon and Konstantinos Theodoridis, 2017, Common and country specific economic uncertainty, Journal
of International Economics 105, 205 — 216.
URL: http://www.sciencedirect.com/science/article/pii/S0022199617300090
Mumtaz, Haroon and Paolo Surico, 2012, EVOLVING INTERNATIONAL INFLATION DYNAMICS: WORLD AND
COUNTRYSPECIFIC FACTORS, Journal of the European Economic Association 10(4).
Negro, Marco Del and Christopher Otrok, 2008, Dynamic factor models with time-varying parameters: measuring
changes in international business cycles, Technical report.
Negro, Marco Del and Frank Schorfheide, 2004, Priors from General Equilibrium Models for VARS, International
Economic Review 45(2), 643—673.
URL: http://ideas.repec.org/a/ier/iecrev/v45y2004i2p643-673.html
Primiceri, G, 2005, Time varying structural vector autoregressions and monetary policy, The Review of Economic
Studies 72(3), 821—852.
Ramirez, Juan Rubio, Daniel Waggoner and Tao Zha, 2010, Structural Vector Autoregressions: Theory of Identifi-
cation and Algorithms for Inference, Review of Economic Studies 77(2), 665—696.
URL: http://ideas.repec.org/a/bla/restud/v77y2010i2p665-696.html
Bibliography 217
Robertson, John C. and Ellis W. Tallman, 1999, Vector autoregressions: forecasting and reality, Economic Review
(Q1), 4—18.
URL: http://ideas.repec.org/a/fip/fedaer/y1999iq1p4-18nv.84no.1.html
Robertson, John C, Ellis W Tallman and Charles H Whiteman, 2005, Forecasting Using Relative Entropy, Journal
of Money, Credit and Banking 37(3), 383—401.
URL: http://ideas.repec.org/a/mcb/jmoncb/v37y2005i3p383-401.html
Ruge-Murcia, Francisco J., 2007, Methods to estimate dynamic stochastic general equilibrium models, Journal of
Economic Dynamics and Control 31(8), 2599 — 2636.
URL: http://www.sciencedirect.com/science/article/pii/S0165188906001758
Schorfheide, Frank and Dongho Song, 2015, Real-Time Forecasting With a Mixed-Frequency VAR, Journal of Busi-
ness & Economic Statistics 33(3), 366—380.
URL: https://ideas.repec.org/a/taf/jnlbes/v33y2015i3p366-380.html
Sims, Christopher A., 2002, Solving Linear Rational Expectations Models, Computational Economics 20(1), 1—20.
URL: http://dx.doi.org/10.1023/A:1020517101123
Sims, Christopher A., Daniel F. Waggoner and Tao Zha, 2008, Methods for inference in large multiple-equation
Markov-switching models, Journal of Econometrics 146(2), 255—274.
URL: https://ideas.repec.org/a/eee/econom/v146y2008i2p255-274.html
Sims, Christopher A and Tao Zha, 1998, Bayesian Methods for Dynamic Multivariate Models, International Economic
Review 39(4), 949—68.
URL: http://ideas.repec.org/a/ier/iecrev/v39y1998i4p949-68.html
Sims, Christopher A. and Tao Zha, 1999, Error Bands for Impulse Responses, Econometrica 67(5), 1113—1156.
URL: https://ideas.repec.org/a/ecm/emetrp/v67y1999i5p1113-1156.html
STOCK, JAMES H. and MARK W. WATSON, 2007, Why Has U.S. Inflation Become Harder to Forecast?, Journal
of Money, Credit and Banking 39, 3—33.
URL: http://dx.doi.org/10.1111/j.1538-4616.2007.00014.x
Villani, Mattias, 2009, Steady-state priors for vector autoregressions, Journal of Applied Econometrics 24(4), 630—
650.
URL: http://dx.doi.org/10.1002/jae.1065
Waggoner, Daniel F. and Tao Zha, 1997, Normalization, probability distribution, and impulse responses, Technical
report.
Waggoner, Daniel F. and Tao Zha, 1999, Conditional Forecasts In Dynamic Multivariate Models, The Review of
Economics and Statistics 81(4), 639—651.
URL: http://ideas.repec.org/a/tpr/restat/v81y1999i4p639-651.html
Waggoner, Daniel F. and Tao Zha, 2003, Likelihood preserving normalization in multiple equation models, Journal
of Econometrics 114(2), 329 — 347.
URL: http://www.sciencedirect.com/science/article/pii/S0304407603000873
Zellner, Arnold, 1971, An introduction to Bayesian Inference in Econometrics, Wiley.