Title: | Bayesian Inference of Vector Autoregressive and Error Correction Models |
---|---|
Description: | Assists in the set-up of algorithms for Bayesian inference of vector autoregressive (VAR) and error correction (VEC) models. Functions for posterior simulation, forecasting, impulse response analysis and forecast error variance decomposition are largely based on the introductory texts of Chan, Koop, Poirier and Tobias (2019, ISBN: 9781108437493), Koop and Korobilis (2010) <doi:10.1561/0800000013> and Luetkepohl (2006, ISBN: 9783540262398). |
Authors: | Franz X. Mohr [aut, cre] (0009-0003-8890-7781) |
Maintainer: | Franz X. Mohr <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.2.4.9000 |
Built: | 2024-10-11 03:31:55 UTC |
Source: | https://github.com/franzmohr/bvartools |
Add Priors to Bayesian Models
A generic function used to generate prior specifications for a list of models. The function invokes particular methods which depend on the class of the first argument.
add_priors(object, ...)
add_priors(object, ...)
object |
an object of class |
... |
arguments passed forward to method. |
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Obtain data matrices model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in draws should be much higher. # Add prior specifications model <- add_priors(model)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Obtain data matrices model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in draws should be much higher. # Add prior specifications model <- add_priors(model)
Adds prior specifications to a list of models, which was produced by
function gen_var
.
## S3 method for class 'bvarmodel' add_priors( object, coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01), sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04), ssvs = NULL, bvs = NULL, ... )
## S3 method for class 'bvarmodel' add_priors( object, coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01), sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04), ssvs = NULL, bvs = NULL, ... )
object |
a list, usually, the output of a call to |
coef |
a named list of prior specifications for the coefficients of the
models. For the default specification all prior means are set to zero and the diagonal elements of
the inverse prior variance-covariance matrix are set to 1 for coefficients corresponding to non-deterministic
and structural terms. For deterministic coefficients the prior variances are set to 10 via |
sigma |
a named list of prior specifications for the error variance-covariance matrix of the models. For the default specification of an inverse Wishart distribution the prior degrees of freedom are set to the number of endogenous variables and the prior variances to 1. See 'Details'. |
ssvs |
optional; a named list of prior specifications for the SSVS algorithm. Not allowed for TVP models. See 'Details'. |
bvs |
optional; a named list of prior specifications for the BVS algorithm. See 'Details'. |
... |
further arguments passed to or from other methods. |
The arguments of the function require named lists. Possible specifications are described in the following. Note that it is important to specify the priors in the correct list. Otherwise, the provided specification will be disregarded and default values will be used.
Argument coef
can contain the following elements
v_i
a numeric specifying the prior precision of the coefficients. Default is 1.
v_i_det
a numeric specifying the prior precision of coefficients corresponding to deterministic terms. Default is 0.1.
coint_var
a logical specifying whether the prior mean of the first own lag of an
endogenous variable should be set to 1. Default is FALSE
.
const
a numeric or character specifying the prior mean of coefficients, which correspond
to the intercept. If a numeric is provided, all prior means are set to this value.
If const = "mean"
, the mean of the respective endogenous variable is used as prior mean.
If const = "first"
, the first values of the respective endogenous variable is used as prior mean.
minnesota
a list of length 4 containing parameters for the calculation of
the Minnesota prior, where the element names must be kappa0
, kappa1
, kappa2
and kappa3
.
For the endogenous variable the prior variance of the
th lag of regressor
is obtained as
where is the residual standard deviation of variable
of an unrestricted
LS estimate. For exogenous variables
is the sample standard deviation.
max_var
a numeric specifying the maximum prior variance that is allowed for non-deterministic coefficients.
shape
a numeric specifying the prior shape parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 3.
rate
a numeric specifying the prior rate parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 0.0001.
rate_det
a numeric specifying the prior rate parameter of the error term of the state equation for coefficients, which correspond to deterministic terms. Only used for models with time varying parameters. Default is 0.01.
If minnesota
is specified, v_i
and v_i_det
are ignored.
Argument sigma
can contain the following elements:
df
an integer or character specifying the prior degrees of freedom of the error term. Only
used, if the prior is inverse Wishart.
Default is "k"
, which indicates the amount of endogenous variables in the respective model.
"k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If an integer
is provided, the degrees of freedom are set to this value in all models.
scale
a numeric specifying the prior error variance of endogenous variables. Default is 1.
shape
a numeric or character specifying the prior shape parameter of the error term. Only
used, if the prior is inverse gamma or if time varying volatilities are estimated.
For models with constant volatility the default is "k"
, which indicates the amount of endogenous
variables in the respective country model. "k + 3"
can be used to set the prior to the amount of
endogenous variables plus 3. If a numeric is provided, the shape parameters are set to this value in all
models. For models with stochastic volatility this prior refers to the error variance of the state
equation.
rate
a numeric specifying the prior rate parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with stochastic volatility this prior refers to the error variance of the state equation.
mu
numeric of the prior mean of the initial state of the log-volatilities. Only used for models with time varying volatility.
v_i
numeric of the prior precision of the initial state of the log-volatilities. Only used for models with time varying volatility.
sigma_h
numeric of the initial draw for the variance of the log-volatilities. Only used for models with time varying volatility.
constant
numeric of the constant, which is added before taking the log of the squared errors. Only used for models with time varying volatility.
covar
logical indicating whether error covariances should be estimated. Only used
in combination with an inverse gamma prior or stochastic volatility, for which shape
and
rate
must be specified.
df
and scale
must be specified for an inverse Wishart prior. shape
and rate
are required for an inverse gamma prior. For structural models or models with stochastic volatility
only a gamma prior specification is allowed.
Argument ssvs
can contain the following elements:
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
tau
a numeric vector of two elements containing the prior standard errors
of restricted variables () as its first element and unrestricted variables (
)
as its second.
semiautomatic
an numeric vector of two elements containing the
factors by which the standard errors associated with an unconstrained least squares
estimate of the model are multiplied to obtain the prior standard errors
of restricted () and unrestricted (
) variables, respectively.
This is the semiautomatic approach described in George et al. (2008).
covar
logical indicating if SSVS should also be applied to the error covariance matrix as in George et al. (2008).
exclude_det
logical indicating if deterministic terms should be excluded from the SSVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
Either tau
or semiautomatic
must be specified.
The argument bvs
can contain the following elements
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
covar
logical indicating if BVS should also be applied to the error covariance matrix.
exclude_det
logical indicating if deterministic terms should be excluded from the BVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
If either ssvs$minnesota
or bvs$minnesota
is specified, prior inclusion probabilities
are calculated in a Minnesota-like fashion as
|
for own lags of endogenous variables, |
|
for other endogenous variables, |
|
for exogenous variables, |
|
for deterministic variables, |
for lag with
,
,
,
as the first, second,
third and forth element in
ssvs$minnesota
or bvs$minnesota
, respectively.
A list of country models.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
data("e1") e1 <- diff(log(e1)) * 100 model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) model <- add_priors(model)
data("e1") e1 <- diff(log(e1)) * 100 model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) model <- add_priors(model)
Adds prior specifications to a list of models, which was produced by
function gen_vec
.
## S3 method for class 'bvecmodel' add_priors( object, coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01), coint = list(v_i = 0, p_tau_i = 1, shape = 3, rate = 1e-04, rho = 0.999), sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04), ssvs = NULL, bvs = NULL, ... )
## S3 method for class 'bvecmodel' add_priors( object, coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01), coint = list(v_i = 0, p_tau_i = 1, shape = 3, rate = 1e-04, rho = 0.999), sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04), ssvs = NULL, bvs = NULL, ... )
object |
a list, usually, the output of a call to |
coef |
a named list of prior specifications for coefficients that do not
determine the cointegration space. For the default specification all prior means are set to zero
and the diagonal elements of the inverse prior variance-covariance matrix are set to 1
for coefficients corresponding to non-deterministic terms. For deterministic coefficients the prior
variances are set to 10 by |
coint |
a named list of prior specifications for coefficients determining the cointegration space of VEC models. See 'Details'. |
sigma |
a named list of prior specifications for the error variance-covariance matrix of the models. For the default specification of an inverse Wishart distribution the prior degrees of freedom are set to the number of endogenous variables plus the rank of the cointegration matrix. The prior variance is to 1. See 'Details'. |
ssvs |
optional; a named list of prior specifications for the SSVS algorithm. Not allowed for TVP models. See 'Details'. |
bvs |
optional; a named list of prior specifications for the BVS algorithm. See 'Details'. |
... |
further arguments passed to or from other methods. |
The arguments of the function require named lists. Possible specifications are described in the following. Note that it is important to specify the priors in the correct list. Otherwise, the provided specification will be disregarded and default values will be used.
Argument coef
contains the following elements
v_i
a numeric specifying the prior precision of the coefficients. Default is 1.
v_i_det
a numeric specifying the prior precision of coefficients that correspond to deterministic terms. Default is 0.1.
const
a character specifying the prior mean of coefficients, which correspond
to the intercept. If const = "mean"
, the means of the series of endogenous variables
are used as prior means. If const = "first"
, the first values of the series of endogenous
variables are used as prior means.
minnesota
a list of length 4 containing parameters for the calculation of
the Minnesota prior, where the element names must be kappa0
, kappa1
, kappa2
and kappa3
.
For the endogenous variable the prior variance of the
th lag of regressor
is obtained as
where is the residual standard deviation of variable
of an unrestricted
LS estimate. For exogenous variables
is the sample standard deviation.
The function only provides priors for the non-cointegration part of the model. However,
the residual standard errors are based on an unrestricted LS regression of the
endogenous variables on the error correction term and the non-cointegration regressors.
max_var
a numeric specifying the maximum prior variance that is allowed for non-deterministic coefficients.
shape
a numeric specifying the prior shape parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 3.
rate
a numeric specifying the prior rate parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 0.0001.
rate_det
a numeric specifying the prior rate parameter of the error term of the state equation for coefficients, which correspond to deterministic terms. Only used for models with time varying parameters. Default is 0.01.
If minnesota
is specified, elements v_i
and v_i_det
are ignored.
Argument coint
can contain the following elements:
v_i
numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. Default is 0.
p_tau_i
numeric of the diagonal elements of the inverse prior matrix of
the central location of the cointegration space . Default is 1.
shape
an integer specifying the prior degrees of freedom of the error term of the state equation
of the loading matrix . Default is 3.
rate
a numeric specifying the prior variance of error term of the state equation
of the loading matrix . Default is 0.0001.
rho
a numeric specifying the autocorrelation coefficient
of the state equation of . It must be smaller than 1. Default is 0.999.
Note that in contrast to Koop et al. (2011)
is not drawn in the Gibbs sampler of
this package yet.
Argument sigma
can contain the following elements:
df
an integer or character specifying the prior degrees of freedom of the error term. Only
used, if the prior is inverse Wishart.
Default is "k"
, which indicates the amount of endogenous variables in the respective model.
"k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If an integer
is provided, the degrees of freedom are set to this value in all models.
In all cases the rank of the cointegration matrix is automatically added.
scale
a numeric specifying the prior error variance of endogenous variables. Default is 1.
shape
a numeric or character specifying the prior shape parameter of the error term. Only
used, if the prior is inverse gamma or if time varying volatilities are estimated.
For models with constant volatility the default is "k"
, which indicates the amount of endogenous
variables in the respective country model. "k + 3"
can be used to set the prior to the amount of
endogenous variables plus 3. If a numeric is provided, the shape parameters are set to this value in all
models. For models with stochastic volatility this prior refers to the error variance of the state
equation.
rate
a numeric specifying the prior rate parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with stochastic volatility this prior refers to the error variance of the state equation.
mu
numeric of the prior mean of the initial state of the log-volatilities. Only used for models with time varying volatility.
v_i
numeric of the prior precision of the initial state of the log-volatilities. Only used for models with time varying volatility.
sigma_h
numeric of the initial draw for the variance of the log-volatilities. Only used for models with time varying volatility.
constant
numeric of the constant, which is added before taking the log of the squared errors. Only used for models with time varying volatility.
covar
logical indicating whether error covariances should be estimated. Only used
in combination with an inverse gamma prior or stochastic volatility, for which shape
and
rate
must be specified.
df
and scale
must be specified for an inverse Wishart prior. shape
and rate
are required for an inverse gamma prior. For structural models or models with stochastic volatility
only a gamma prior specification is allowed.
Argument ssvs
can contain the following elements:
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
tau
a numeric vector of two elements containing the prior standard errors
of restricted variables () as its first element and unrestricted variables (
)
as its second.
semiautomatic
an numeric vector of two elements containing the
factors by which the standard errors associated with an unconstrained least squares
estimate of the model are multiplied to obtain the prior standard errors
of restricted () and unrestricted (
) variables, respectively.
This is the semiautomatic approach described in George et al. (2008).
covar
logical indicating if SSVS should also be applied to the error covariance matrix as in George et al. (2008).
exclude_det
logical indicating if deterministic terms should be excluded from the SSVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
Either tau
or semiautomatic
must be specified.
The argument bvs
can contain the following elements
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
covar
logical indicating if BVS should also be applied to the error covariance matrix.
exclude_det
logical indicating if deterministic terms should be excluded from the BVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
If either ssvs$minnesota
or bvs$minnesota
is specified, prior inclusion probabilities
are calculated in a Minnesota-like fashion as
|
for own lags of endogenous variables, |
|
for other endogenous variables, |
|
for exogenous variables, |
|
for deterministic variables, |
for lag with
,
,
,
as the first, second,
third and forth element in
ssvs$minnesota
or bvs$minnesota
, respectively.
A list of models.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Get data data("e6") # Create model model <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add priors model <- add_priors(model)
# Get data data("e6") # Create model model <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add priors model <- add_priors(model)
Adds prior specifications to a list of models, which was produced by
function gen_dfm
.
## S3 method for class 'dfmodel' add_priors( object, lambda = list(v_i = 0.01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = 0.01), sigma_v = list(shape = 5, rate = 4), ... )
## S3 method for class 'dfmodel' add_priors( object, lambda = list(v_i = 0.01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = 0.01), sigma_v = list(shape = 5, rate = 4), ... )
object |
a list, usually, the output of a call to |
lambda |
a named list of prior specifications for the factor loadings in the measurement equation. For the default specification the diagonal elements of the inverse prior variance-covariance matrix are set to 0.01. The variances need to be specified as precisions, i.e. as inverses of the variances. |
sigma_u |
a named list of prior specifications for the error variance-covariance matrix. See 'Details'. |
a |
a named list of prior specifications for the coefficients of the transition equation. For the default specification the diagonal elements of the inverse prior variance-covariance matrix are set to 0.01. The variances need to be specified as precisions, i.e. as inverses of the variances. |
sigma_v |
a named list of prior specifications for the error variance-covariance matrix. See 'Details'. |
... |
further arguments passed to or from other methods. |
Argument lambda
can only contain the element v_i
, which is a numeric specifying the prior
precision of the loading factors of the measurement equation. Default is 0.01.
The function assumes an inverse gamma prior for the errors of the measurement equation.
Argument sigma_u
can contain the following elements:
shape
a numeric or character specifying the prior shape parameter of the error terms of the measurement equation. Default is 5.
rate
a numeric specifying the prior rate parameter of the error terms of the measurement equation. Default is 4.
Argument a
can only contain the element v_i
, which is a numeric specifying the prior
precision of the coefficients of the transition equation. Default is 0.01.
The function assumes an inverse gamma prior for the errors of the transition equation.
Argument sigma_v
can contain the following elements:
shape
a numeric or character specifying the prior shape parameter of the error terms of the transition equation. Default is 5.
rate
a numeric specifying the prior rate parameter of the error terms of the transition equation. Default is 4.
A list of models.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 5000, burnin = 1000) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4))
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 5000, burnin = 1000) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4))
The data set contains quarterly time series for 196 US macroeconomic variables from 1959Q3 to 2015Q3. It was produced from file "freddata_Q.csv" of the data sets associated with Chan, Koop, Poirier and Tobias (2019). Raw data are available at https://web.ics.purdue.edu/~jltobias/second_edition/Chapter18/code_for_exercise_4/freddata_Q.csv.
data("bem_dfmdata")
data("bem_dfmdata")
A named time-series object with 225 rows and 196 variables. A detailed explanation of the variables can be found in McCracken and Ng (2016).
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
McCracken, M. W., & Ng, S. (2016). FRED-MD: A monthly database for macroeconomic research. Journal of Business & Economic Statistics 34(4), 574-589. doi:10.1080/07350015.2015.1086655
bvar
is used to create objects of class "bvar"
.
A plot function for objects of class "bvar"
.
Forecasting a Bayesian VAR object of class "bvar"
with credible bands.
bvar( data = NULL, exogen = NULL, y, x = NULL, A0 = NULL, A = NULL, B = NULL, C = NULL, Sigma = NULL ) ## S3 method for class 'bvar' plot(x, ci = 0.95, type = "hist", ...) ## S3 method for class 'bvar' predict(object, ..., n.ahead = 10, new_x = NULL, new_d = NULL, ci = 0.95)
bvar( data = NULL, exogen = NULL, y, x = NULL, A0 = NULL, A = NULL, B = NULL, C = NULL, Sigma = NULL ) ## S3 method for class 'bvar' plot(x, ci = 0.95, type = "hist", ...) ## S3 method for class 'bvar' predict(object, ..., n.ahead = 10, new_x = NULL, new_d = NULL, ci = 0.95)
data |
the original time-series object of endogenous variables. |
exogen |
the original time-series object of unmodelled variables. |
y |
a time-series object of endogenous variables with |
x |
an object of class |
A0 |
either a |
A |
either a |
B |
either a |
C |
either a |
Sigma |
a |
ci |
a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95. |
type |
either |
... |
additional arguments. |
object |
an object of class |
n.ahead |
number of steps ahead at which to predict. |
new_x |
an object of class |
new_d |
a matrix of new deterministic variables. Must have |
For the VARX model
the function collects the S draws of a Gibbs sampler (after the burn-in phase) in a standardised object,
where is a K-dimensional vector of endogenous variables,
is a
matrix of structural coefficients.
is a
coefficient matrix of lagged endogenous variabels.
is an M-dimensional vector of unmodelled, non-deterministic variables
and
its corresponding coefficient matrix.
is an N-dimensional vector of deterministic terms
and
its corresponding coefficient matrix.
is an error term with
.
For time varying parameter and stochastic volatility models the respective coefficients and error covariance matrix of the above model are assumed to be time varying, respectively.
The draws of the different coefficient matrices provided in A0
, A
,
B
, C
and Sigma
have to correspond to the same MCMC iterations.
For the VAR model
with the function produces
n.ahead
forecasts.
An object of class "bvar"
containing the following components, if specified:
data |
the original time-series object of endogenous variables. |
exogen |
the original time-series object of unmodelled variables. |
y |
a |
x |
a |
A0 |
an |
A0_lambda |
an |
A0_sigma |
an |
A |
an |
A_lambda |
an |
A_sigma |
an |
B |
an |
B_lambda |
an |
B_sigma |
an |
C |
an |
C_lambda |
an |
C_sigma |
an |
Sigma |
an |
Sigma_lambda |
an |
Sigma_sigma |
an |
specifications |
a list containing information on the model specification. |
A time-series object of class "bvarprd"
.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Get data data("e1") e1 <- diff(log(e1)) e1 <- window(e1, end = c(1978, 4)) # Generate model data data <- gen_var(e1, p = 2, deterministic = "const") # Add priors model <- add_priors(data, coef = list(v_i = 0, v_i_det = 0), sigma = list(df = 0, scale = .00001)) # Set RNG seed for reproducibility set.seed(1234567) iterations <- 400 # Number of iterations of the Gibbs sampler # Chosen number of iterations and burnin should be much higher. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin # Total number of MCMC draws y <- t(model$data$Y) x <- t(model$data$Z) tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables m <- k * nrow(x) # Number of estimated coefficients # Priors a_mu_prior <- model$priors$coefficients$mu # Vector of prior parameter means a_v_i_prior <- model$priors$coefficients$v_i # Inverse of the prior covariance matrix u_sigma_df_prior <- model$priors$sigma$df # Prior degrees of freedom u_sigma_scale_prior <- model$priors$sigma$scale # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values u_sigma_i <- diag(1 / .00001, k) # Data containers for posterior draws draws_a <- matrix(NA, m, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters a <- post_normal(y, x, u_sigma_i, a_mu_prior, a_v_i_prior) # Draw variance-covariance matrix u <- y - matrix(a, k) %*% x # Obtain residuals u_sigma_scale_post <- solve(u_sigma_scale_prior + tcrossprod(u)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) # Store draws if (draw > burnin) { draws_a[, draw - burnin] <- a draws_sigma[, draw - burnin] <- solve(u_sigma_i) } } # Generate bvar object bvar_est <- bvar(y = model$data$Y, x = model$data$Z, A = draws_a[1:18,], C = draws_a[19:21, ], Sigma = draws_sigma) # Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Plot draws plot(object) # Load data data("e1") e1 <- diff(log(e1)) * 100 e1 <- window(e1, end = c(1978, 4)) # Generate model data model <- gen_var(e1, p = 0, deterministic = "const", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Generate forecasts bvar_pred <- predict(object, n.ahead = 10, new_d = rep(1, 10)) # Plot forecasts plot(bvar_pred)
# Get data data("e1") e1 <- diff(log(e1)) e1 <- window(e1, end = c(1978, 4)) # Generate model data data <- gen_var(e1, p = 2, deterministic = "const") # Add priors model <- add_priors(data, coef = list(v_i = 0, v_i_det = 0), sigma = list(df = 0, scale = .00001)) # Set RNG seed for reproducibility set.seed(1234567) iterations <- 400 # Number of iterations of the Gibbs sampler # Chosen number of iterations and burnin should be much higher. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin # Total number of MCMC draws y <- t(model$data$Y) x <- t(model$data$Z) tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables m <- k * nrow(x) # Number of estimated coefficients # Priors a_mu_prior <- model$priors$coefficients$mu # Vector of prior parameter means a_v_i_prior <- model$priors$coefficients$v_i # Inverse of the prior covariance matrix u_sigma_df_prior <- model$priors$sigma$df # Prior degrees of freedom u_sigma_scale_prior <- model$priors$sigma$scale # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values u_sigma_i <- diag(1 / .00001, k) # Data containers for posterior draws draws_a <- matrix(NA, m, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters a <- post_normal(y, x, u_sigma_i, a_mu_prior, a_v_i_prior) # Draw variance-covariance matrix u <- y - matrix(a, k) %*% x # Obtain residuals u_sigma_scale_post <- solve(u_sigma_scale_prior + tcrossprod(u)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) # Store draws if (draw > burnin) { draws_a[, draw - burnin] <- a draws_sigma[, draw - burnin] <- solve(u_sigma_i) } } # Generate bvar object bvar_est <- bvar(y = model$data$Y, x = model$data$Z, A = draws_a[1:18,], C = draws_a[19:21, ], Sigma = draws_sigma) # Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Plot draws plot(object) # Load data data("e1") e1 <- diff(log(e1)) * 100 e1 <- window(e1, end = c(1978, 4)) # Generate model data model <- gen_var(e1, p = 0, deterministic = "const", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Generate forecasts bvar_pred <- predict(object, n.ahead = 10, new_d = rep(1, 10)) # Plot forecasts plot(bvar_pred)
Produces draws from the posterior distributions of Bayesian VAR models.
bvarpost(object)
bvarpost(object)
object |
an object of class |
The function implements commonly used posterior simulation algorithms for Bayesian VAR models with both constant and time varying parameters (TVP) as well as stochastic volatility. It can produce posterior draws for standard BVAR models with independent normal-Wishart priors, which can be augmented by stochastic search variable selection (SSVS) as proposed by Geroge et al. (2008) or Bayesian variable selection (BVS) as proposed in Korobilis (2013). Both SSVS or BVS can also be applied to the covariances of the error term.
The implementation follows the descriptions in Chan et al. (2019), George et al. (2008) and Korobilis (2013). For all approaches the SUR form of a VAR model is used to obtain posterior draws. The algorithm is implemented in C++ to reduce calculation time.
The function also supports structural BVAR models, where the structural coefficients are estimated from contemporary endogenous variables, which corresponds to the so-called (A-model). Currently, only specifications are supported, where the structural matrix contains ones on its diagonal and all lower triangular elements are freely estimated. Since posterior draws are obtained based on the SUR form of the VAR model, the structural coefficients are drawn jointly with the other coefficients.
An object of class "bvar"
.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
# Get data data("e1") e1 <- diff(log(e1)) * 100 # Create model model <- gen_var(e1, p = 2, deterministic = "const", iterations = 50, burnin = 10) # Number of iterations and burnin should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- bvarpost(model)
# Get data data("e1") e1 <- diff(log(e1)) * 100 # Create model model <- gen_var(e1, p = 2, deterministic = "const", iterations = 50, burnin = 10) # Number of iterations and burnin should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- bvarpost(model)
'bvec' is used to create objects of class "bvec"
.
A plot function for objects of class "bvec"
.
bvec( y, alpha = NULL, beta = NULL, beta_x = NULL, beta_d = NULL, r = NULL, Pi = NULL, Pi_x = NULL, Pi_d = NULL, w = NULL, w_x = NULL, w_d = NULL, Gamma = NULL, Upsilon = NULL, C = NULL, x = NULL, x_x = NULL, x_d = NULL, A0 = NULL, Sigma = NULL, data = NULL, exogen = NULL ) ## S3 method for class 'bvec' plot(x, ci = 0.95, type = "hist", ...)
bvec( y, alpha = NULL, beta = NULL, beta_x = NULL, beta_d = NULL, r = NULL, Pi = NULL, Pi_x = NULL, Pi_d = NULL, w = NULL, w_x = NULL, w_d = NULL, Gamma = NULL, Upsilon = NULL, C = NULL, x = NULL, x_x = NULL, x_d = NULL, A0 = NULL, Sigma = NULL, data = NULL, exogen = NULL ) ## S3 method for class 'bvec' plot(x, ci = 0.95, type = "hist", ...)
y |
a time-series object of differenced endogenous variables,
usually, a result of a call to |
alpha |
a |
beta |
a |
beta_x |
a |
beta_d |
a |
r |
an integer of the rank of the cointegration matrix. |
Pi |
a |
Pi_x |
a |
Pi_d |
a |
w |
a time-series object of lagged endogenous variables in levels, which enter the
cointegration term, usually, a result of a call to |
w_x |
a time-series object of lagged unmodelled, non-deterministic variables in levels, which enter the
cointegration term, usually, a result of a call to |
w_d |
a time-series object of deterministic terms, which enter the
cointegration term, usually, a result of a call to |
Gamma |
a |
Upsilon |
an |
C |
an |
x |
an object of class |
x_x |
a time-series object of |
x_d |
a time-series object of |
A0 |
either a |
Sigma |
a |
data |
the original time-series object of endogenous variables. |
exogen |
the original time-series object of unmodelled variables. |
ci |
interval used to calculate credible bands for time-varying parameters. |
type |
either |
... |
further graphical parameters. |
For the vector error correction model with unmodelled exogenous variables (VECX)
the function collects the draws of a Gibbs sampler in a standardised object,
where
is a K-dimensional vector of differenced endogenous variables
and
is a
matrix of structural coefficients.
is
the coefficient matrix of the error correction term, where
,
and
are the first lags of endogenous,
exogenous variables in levels and restricted deterministic terms, respectively.
,
, and
are the corresponding coefficient matrices, respectively.
is a coefficient matrix of lagged differenced endogenous variabels.
is an M-dimensional vector of unmodelled, non-deterministic variables
and
its corresponding coefficient matrix.
is an
-dimensional vector of unrestricted deterministics and
the corresponding coefficient matrix.
is an error term with
.
For time varying parameter and stochastic volatility models the respective coefficients and error covariance matrix of the above model are assumed to be time varying, respectively.
The draws of the different coefficient matrices provided in alpha
, beta
,
Pi
, Pi_x
, Pi_d
, A0
, Gamma
, Ypsilon
,
C
and Sigma
have to correspond to the same MCMC iteration.
An object of class "gvec"
containing the following components, if specified:
data |
the original time-series object of endogenous variables. |
exogen |
the original time-series object of unmodelled variables. |
y |
a time-series object of differenced endogenous variables. |
w |
a time-series object of lagged endogenous variables in levels, which enter the cointegration term. |
w_x |
a time-series object of lagged unmodelled, non-deterministic variables in levels, which enter the cointegration term. |
w_d |
a time-series object of deterministic terms, which enter the cointegration term. |
x |
a time-series object of |
x_x |
a time-series object of |
x_d |
a time-series object of |
A0 |
an |
A0_lambda |
an |
A0_sigma |
an |
alpha |
an |
beta |
an |
beta_x |
an |
beta_d |
an |
Pi |
an |
Pi_x |
an |
Pi_d |
an |
Gamma |
an |
Gamma_lamba |
an |
Gamma_sigma |
an |
Upsilon |
an |
Upsilon_lambda |
an |
Upsilon_sigma |
an |
C |
an |
C_lambda |
an |
C_sigma |
an |
Sigma |
an |
Sigma_lambda |
an |
Sigma_sigma |
an |
specifications |
a list containing information on the model specification. |
# Load data data("e6") # Generate model data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted") # Obtain data matrices y <- t(data$data$Y) w <- t(data$data$W) x <- t(data$data$X) # Reset random number generator for reproducibility set.seed(1234567) iterations <- 400 # Number of iterations of the Gibbs sampler # Chosen number of iterations should be much higher, e.g. 30000. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin r <- 1 # Set rank tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables k_w <- nrow(w) # Number of regressors in error correction term k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms k_alpha <- k * r # Number of elements in alpha k_beta <- k_w * r # Number of elements in beta k_gamma <- k * k_x # Set uninformative priors a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix v_i <- 0 p_tau_i <- diag(1, k_w) u_sigma_df_prior <- r # Prior degrees of freedom u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values beta <- matrix(c(1, -4), k_w, r) u_sigma_i <- diag(1 / .0001, k) g_i <- u_sigma_i # Data containers draws_alpha <- matrix(NA, k_alpha, iterations) draws_beta <- matrix(NA, k_beta, iterations) draws_pi <- matrix(NA, k * k_w, iterations) draws_gamma <- matrix(NA, k_gamma, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i, v_i = v_i, p_tau_i = p_tau_i, g_i = g_i, gamma_mu_prior = a_mu_prior, gamma_v_i_prior = a_v_i_prior) alpha <- temp$alpha beta <- temp$beta Pi <- temp$Pi gamma <- temp$Gamma # Draw variance-covariance matrix u <- y - Pi %*% w - matrix(gamma, k) %*% x u_sigma_scale_post <- solve(tcrossprod(u) + v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) u_sigma <- solve(u_sigma_i) # Update g_i g_i <- u_sigma_i # Store draws if (draw > burnin) { draws_alpha[, draw - burnin] <- alpha draws_beta[, draw - burnin] <- beta draws_pi[, draw - burnin] <- Pi draws_gamma[, draw - burnin] <- gamma draws_sigma[, draw - burnin] <- u_sigma } } # Number of non-deterministic coefficients k_nondet <- (k_x - 4) * k # Generate bvec object bvec_est <- bvec(y = data$data$Y, w = data$data$W, x = data$data$X[, 1:6], x_d = data$data$X[, 7:10], Pi = draws_pi, Gamma = draws_gamma[1:k_nondet,], C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),], Sigma = draws_sigma) # Load data data("e6") # Generate model model <- gen_vec(data = e6, p = 2, r = 1, const = "unrestricted", iterations = 20, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Plot draws plot(object)
# Load data data("e6") # Generate model data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted") # Obtain data matrices y <- t(data$data$Y) w <- t(data$data$W) x <- t(data$data$X) # Reset random number generator for reproducibility set.seed(1234567) iterations <- 400 # Number of iterations of the Gibbs sampler # Chosen number of iterations should be much higher, e.g. 30000. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin r <- 1 # Set rank tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables k_w <- nrow(w) # Number of regressors in error correction term k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms k_alpha <- k * r # Number of elements in alpha k_beta <- k_w * r # Number of elements in beta k_gamma <- k * k_x # Set uninformative priors a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix v_i <- 0 p_tau_i <- diag(1, k_w) u_sigma_df_prior <- r # Prior degrees of freedom u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values beta <- matrix(c(1, -4), k_w, r) u_sigma_i <- diag(1 / .0001, k) g_i <- u_sigma_i # Data containers draws_alpha <- matrix(NA, k_alpha, iterations) draws_beta <- matrix(NA, k_beta, iterations) draws_pi <- matrix(NA, k * k_w, iterations) draws_gamma <- matrix(NA, k_gamma, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i, v_i = v_i, p_tau_i = p_tau_i, g_i = g_i, gamma_mu_prior = a_mu_prior, gamma_v_i_prior = a_v_i_prior) alpha <- temp$alpha beta <- temp$beta Pi <- temp$Pi gamma <- temp$Gamma # Draw variance-covariance matrix u <- y - Pi %*% w - matrix(gamma, k) %*% x u_sigma_scale_post <- solve(tcrossprod(u) + v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) u_sigma <- solve(u_sigma_i) # Update g_i g_i <- u_sigma_i # Store draws if (draw > burnin) { draws_alpha[, draw - burnin] <- alpha draws_beta[, draw - burnin] <- beta draws_pi[, draw - burnin] <- Pi draws_gamma[, draw - burnin] <- gamma draws_sigma[, draw - burnin] <- u_sigma } } # Number of non-deterministic coefficients k_nondet <- (k_x - 4) * k # Generate bvec object bvec_est <- bvec(y = data$data$Y, w = data$data$W, x = data$data$X[, 1:6], x_d = data$data$X[, 7:10], Pi = draws_pi, Gamma = draws_gamma[1:k_nondet,], C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),], Sigma = draws_sigma) # Load data data("e6") # Generate model model <- gen_vec(data = e6, p = 2, r = 1, const = "unrestricted", iterations = 20, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Plot draws plot(object)
An object of class "bvec"
is transformed to a VAR in level representation.
bvec_to_bvar(object)
bvec_to_bvar(object)
object |
an object of class |
An object of class "bvar"
.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("e6") # Generate model data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted") # Obtain data matrices y <- t(data$data$Y) w <- t(data$data$W) x <- t(data$data$X) # Reset random number generator for reproducibility set.seed(1234567) iterations <- 100 # Number of iterations of the Gibbs sampler # Chosen number of iterations should be much higher, e.g. 30000. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin r <- 1 # Set rank tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables k_w <- nrow(w) # Number of regressors in error correction term k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms k_alpha <- k * r # Number of elements in alpha k_beta <- k_w * r # Number of elements in beta k_gamma <- k * k_x # Set uninformative priors a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix v_i <- 0 p_tau_i <- diag(1, k_w) u_sigma_df_prior <- r # Prior degrees of freedom u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values beta <- matrix(c(1, -4), k_w, r) u_sigma_i <- diag(1 / .0001, k) g_i <- u_sigma_i # Data containers draws_alpha <- matrix(NA, k_alpha, iterations) draws_beta <- matrix(NA, k_beta, iterations) draws_pi <- matrix(NA, k * k_w, iterations) draws_gamma <- matrix(NA, k_gamma, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i, v_i = v_i, p_tau_i = p_tau_i, g_i = g_i, gamma_mu_prior = a_mu_prior, gamma_v_i_prior = a_v_i_prior) alpha <- temp$alpha beta <- temp$beta Pi <- temp$Pi gamma <- temp$Gamma # Draw variance-covariance matrix u <- y - Pi %*% w - matrix(gamma, k) %*% x u_sigma_scale_post <- solve(tcrossprod(u) + v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) u_sigma <- solve(u_sigma_i) # Update g_i g_i <- u_sigma_i # Store draws if (draw > burnin) { draws_alpha[, draw - burnin] <- alpha draws_beta[, draw - burnin] <- beta draws_pi[, draw - burnin] <- Pi draws_gamma[, draw - burnin] <- gamma draws_sigma[, draw - burnin] <- u_sigma } } # Number of non-deterministic coefficients k_nondet <- (k_x - 4) * k # Generate bvec object bvec_est <- bvec(y = data$data$Y, w = data$data$W, x = data$data$X[, 1:6], x_d = data$data$X[, 7:10], Pi = draws_pi, Gamma = draws_gamma[1:k_nondet,], C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),], Sigma = draws_sigma) # Thin posterior draws bvec_est <- thin(bvec_est, thin = 5) # Transfrom VEC output to VAR output bvar_form <- bvec_to_bvar(bvec_est)
# Load data data("e6") # Generate model data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted") # Obtain data matrices y <- t(data$data$Y) w <- t(data$data$W) x <- t(data$data$X) # Reset random number generator for reproducibility set.seed(1234567) iterations <- 100 # Number of iterations of the Gibbs sampler # Chosen number of iterations should be much higher, e.g. 30000. burnin <- 100 # Number of burn-in draws draws <- iterations + burnin r <- 1 # Set rank tt <- ncol(y) # Number of observations k <- nrow(y) # Number of endogenous variables k_w <- nrow(w) # Number of regressors in error correction term k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms k_alpha <- k * r # Number of elements in alpha k_beta <- k_w * r # Number of elements in beta k_gamma <- k * k_x # Set uninformative priors a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix v_i <- 0 p_tau_i <- diag(1, k_w) u_sigma_df_prior <- r # Prior degrees of freedom u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom # Initial values beta <- matrix(c(1, -4), k_w, r) u_sigma_i <- diag(1 / .0001, k) g_i <- u_sigma_i # Data containers draws_alpha <- matrix(NA, k_alpha, iterations) draws_beta <- matrix(NA, k_beta, iterations) draws_pi <- matrix(NA, k * k_w, iterations) draws_gamma <- matrix(NA, k_gamma, iterations) draws_sigma <- matrix(NA, k^2, iterations) # Start Gibbs sampler for (draw in 1:draws) { # Draw conditional mean parameters temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i, v_i = v_i, p_tau_i = p_tau_i, g_i = g_i, gamma_mu_prior = a_mu_prior, gamma_v_i_prior = a_v_i_prior) alpha <- temp$alpha beta <- temp$beta Pi <- temp$Pi gamma <- temp$Gamma # Draw variance-covariance matrix u <- y - Pi %*% w - matrix(gamma, k) %*% x u_sigma_scale_post <- solve(tcrossprod(u) + v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha)) u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k) u_sigma <- solve(u_sigma_i) # Update g_i g_i <- u_sigma_i # Store draws if (draw > burnin) { draws_alpha[, draw - burnin] <- alpha draws_beta[, draw - burnin] <- beta draws_pi[, draw - burnin] <- Pi draws_gamma[, draw - burnin] <- gamma draws_sigma[, draw - burnin] <- u_sigma } } # Number of non-deterministic coefficients k_nondet <- (k_x - 4) * k # Generate bvec object bvec_est <- bvec(y = data$data$Y, w = data$data$W, x = data$data$X[, 1:6], x_d = data$data$X[, 7:10], Pi = draws_pi, Gamma = draws_gamma[1:k_nondet,], C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),], Sigma = draws_sigma) # Thin posterior draws bvec_est <- thin(bvec_est, thin = 5) # Transfrom VEC output to VAR output bvar_form <- bvec_to_bvar(bvec_est)
Produces draws from the posterior distributions of Bayesian VEC models.
bvecpost(object)
bvecpost(object)
object |
an object of class |
The function implements posterior simulation algorithms proposed in Koop et al. (2010)
and Koop et al. (2011), which place identifying restrictions on the cointegration space.
Both algorithms are able to employ Bayesian variable selection (BVS) as proposed in Korobilis (2013).
The algorithm of Koop et al. (2010) is also able to employ stochastic search variable selection (SSVS)
as proposed by Geroge et al. (2008).
Both SSVS and BVS can also be applied to the covariances of the error term. However, the algorithms
cannot be applied to cointegration related coefficients, i.e. to the loading matrix or
the cointegration matrix
.
The implementation primarily follows the description in Koop et al. (2010). Chan et al. (2019), George et al. (2008) and Korobilis (2013) were used to implement the variable selection algorithms. For all approaches the SUR form of a VEC model is used to obtain posterior draws. The algorithm is implemented in C++ to reduce calculation time.
The function also supports structural BVEC models, where the structural coefficients are estimated from contemporary endogenous variables, which corresponds to the so-called (A-model). Currently, only specifications are supported, where the structural matrix contains ones on its diagonal and all lower triangular elements are freely estimated. Since posterior draws are obtained based on the SUR form of the VEC model, the structural coefficients are drawn jointly with the other coefficients. No identifying restrictions are made regarding the cointegration matrix.
An object of class "bvec"
.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
# Get data data("e6") # Create model model <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- bvecpost(model)
# Get data data("e6") # Create model model <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- bvecpost(model)
bvs
employs Bayesian variable selection as proposed by Korobilis (2013)
to produce a vector of inclusion parameters for the coefficient matrix
of a VAR model.
bvs(y, z, a, lambda, sigma_i, prob_prior, include = NULL)
bvs(y, z, a, lambda, sigma_i, prob_prior, include = NULL)
y |
a |
z |
a |
a |
an M-dimensional vector of parameter draws. If time varying parameters are used,
an |
lambda |
an |
sigma_i |
the inverse variance-covariance matrix. If the variance-covariance matrix
is time varying, a |
prob_prior |
an M-dimensional vector of prior inclusion probabilities. |
include |
an integer vector specifying the positions of variables, which should be
included in the BVS algorithm. If |
The function employs Bayesian variable selection as proposed
by Korobilis (2013) to produce a vector of inclusion parameters, which are
the diagonal elements of the inclusion matrix for the VAR model
where .
is a K-dimensional vector of endogenous variables and
is a
matrix of regressors with
as a vector of regressors.
A matrix of inclusion parameters on its diagonal.
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
# Load data data("e1") data <- diff(log(e1)) * 100 # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Prior for inclusion parameter prob_prior <- matrix(0.5, m) # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) lambda <- diag(1, m) z_bvs <- z %*% lambda a <- post_normal_sur(y = y, z = z_bvs, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) lambda <- bvs(y = y, z = z, a = a, lambda = lambda, sigma_i = sigma_i, prob_prior = prob_prior)
# Load data data("e1") data <- diff(log(e1)) * 100 # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Prior for inclusion parameter prob_prior <- matrix(0.5, m) # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) lambda <- diag(1, m) z_bvs <- z %*% lambda a <- post_normal_sur(y = y, z = z_bvs, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) lambda <- bvs(y = y, z = z, a = a, lambda = lambda, sigma_i = sigma_i, prob_prior = prob_prior)
Convenience function, which generates the input data for posterior simulation of covariance parameters.
covar_prepare_data(y, omega_i, k, tt, tvp)
covar_prepare_data(y, omega_i, k, tt, tvp)
y |
a |
omega_i |
a |
k |
an integer of the number of endogenous variables. |
tt |
an integer of the number of observations. |
tvp |
logical indicating if the SUR matrix with the values of regressors should be prepared for the estimation of constant or time varying parameters. |
For the model
with and
as a diagonal matrix of error variances, the function produces
the input data for the posterior simulation of the lower triangular covariance coefficients
of
as presented in Primiceri (2005).
A list with three elements:
y |
The prepared vector of endogenous variables. |
z |
The prepared matrix of regressors. |
omega_i |
The prepared diagonal matrix of measurement error variances. |
All matrices are returned as sparse matrices.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
# Create artificial data k <- 3 tt <- 4 u <- matrix(1:(k * tt)) omega_i <- Matrix(diag(1:3, k)) # Generate input data (constant parameters) covar_prepare_data(u, omega_i, k, tt, FALSE) # Generate input data (time varying parameters) covar_prepare_data(u, omega_i, k, tt, TRUE)
# Create artificial data k <- 3 tt <- 4 u <- matrix(1:(k * tt)) omega_i <- Matrix(diag(1:3, k)) # Generate input data (constant parameters) covar_prepare_data(u, omega_i, k, tt, FALSE) # Generate input data (time varying parameters) covar_prepare_data(u, omega_i, k, tt, TRUE)
Convenience function, which takes the vector of draws of lower triangular covariance coefficients and transforms it into a matrix with ones on the main diagonal. In case of time varying parameters the resulting matrix will be block diagonal.
covar_vector_to_matrix(psi, k, tt)
covar_vector_to_matrix(psi, k, tt)
psi |
a |
k |
the number |
tt |
the number |
A sparse, block diagonal matrix.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
# Create artificial data k <- 5 tt <- 4 n_covar <- (k - 1) * k / 2 # Constant parameters psi <- matrix(1:(n_covar)) covar_vector_to_matrix(psi, k, tt) # Time varying parameters psi <- matrix(1:(n_covar * tt)) covar_vector_to_matrix(psi, k, tt)
# Create artificial data k <- 5 tt <- 4 n_covar <- (k - 1) * k / 2 # Constant parameters psi <- matrix(1:(n_covar)) covar_vector_to_matrix(psi, k, tt) # Time varying parameters psi <- matrix(1:(n_covar * tt)) covar_vector_to_matrix(psi, k, tt)
dfm
is used to create objects of class "dfm"
.
A plot function for objects of class "dfm"
.
dfm(x, lambda = NULL, fac, sigma_u = NULL, a = NULL, sigma_v = NULL) ## S3 method for class 'dfm' plot(x, ci = 0.95, ...)
dfm(x, lambda = NULL, fac, sigma_u = NULL, a = NULL, sigma_v = NULL) ## S3 method for class 'dfm' plot(x, ci = 0.95, ...)
x |
an object of class |
lambda |
an |
fac |
an |
sigma_u |
an |
a |
a |
sigma_v |
an |
ci |
interval used to calculate credible bands. |
... |
further graphical parameters. |
The function produces a standardised object from S draws of a Gibbs sampler (after the burn-in phase) for the dynamic factor model (DFM) with measurement equation
where
is an
vector of observed variables,
is an
vector of unobserved factors and
is the corresponding
matrix of factor loadings.
is an
error term.
The transition equation is
where
is an
coefficient matrix and
is an
error term.
An object of class "dfm"
containing the following components, if specified:
x |
the standardised time-series object of observable variables. |
lambda |
an |
factor |
an |
sigma_u |
an |
a |
an |
sigma_v |
an |
specifications |
a list containing information on the model specification. |
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- dfmpost(model) # Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- draw_posterior(model) # Plot factors plot(object)
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- dfmpost(model) # Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- draw_posterior(model) # Plot factors plot(object)
Produces draws from the posterior distributions of Bayesian dynamic factor models.
dfmpost(object)
dfmpost(object)
object |
an object of class |
The function implements the posterior simulation algorithm for Bayesian dynamic factor models.
The implementation follows the description in Chan et al. (2019) and C++ is used to reduce calculation time.
An object of class "dfm"
.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- dfmpost(model)
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- dfmpost(model)
Forwards model input to posterior simulation functions. This is a generic function.
draw_posterior(object, ...)
draw_posterior(object, ...)
object |
a list of model specifications. Usually, the output of a call to
|
... |
arguments passed forward to method. |
Forwards model input to posterior simulation functions.
## S3 method for class 'bvarmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
## S3 method for class 'bvarmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each model in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
result in an object of class "bvar"
.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
Forwards model input to posterior simulation functions for vector error correction models.
## S3 method for class 'bvecmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
## S3 method for class 'bvecmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each list element in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
be "bvec"
.
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
# Load data data("e6") e6 <- e6 * 100 # Generate model model <- gen_vec(e6, p = 1, r = 1, const = "restricted", iterations = 10, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
# Load data data("e6") e6 <- e6 * 100 # Generate model model <- gen_vec(e6, p = 1, r = 1, const = "restricted", iterations = 10, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
Forwards model input to posterior simulation functions.
## S3 method for class 'dfmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
## S3 method for class 'dfmodel' draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each list element in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
be "bvar"
, "bvec"
or "dfm"
.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in should be much higher. # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model)
The data set contains quarterly, seasonally adjusted time series for West German fixed investment, disposable income, and consumption expenditures in billions of DM from 1960Q1 to 1982Q4. It was produced from file E1 of the data sets associated with Lütkepohl (2007). Raw data are available at http://www.jmulti.de/download/datasets/e1.dat and were originally obtained from Deutsche Bundesbank.
data("e1")
data("e1")
A named time-series object with 92 rows and 3 variables:
fixed investment.
disposable income.
consumption expenditures.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
The data set contains quarterly, seasonally unadjusted time series for German long-term interest and inflation rates from 1972Q2 to 1998Q4. It was produced from file E6 of the data sets associated with Lütkepohl (2007). Raw data are available at http://www.jmulti.de/download/datasets/e6.dat and were originally obtained from Deutsche Bundesbank and Deutsches Institut für Wirtschaftsforschung.
data("e6")
data("e6")
A named time-series object with 107 rows and 2 variables:
nominal long-term interest rate (Umlaufsrendite).
log of GDP deflator.
The data cover West Germany until 1990Q2 and all of Germany aferwards. The values refer to the last month of a quarter.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
A plot function for objects of class "bvarfevd".
fevd(object, ...) ## S3 method for class 'bvarfevd' plot(x, ...)
fevd(object, ...) ## S3 method for class 'bvarfevd' plot(x, ...)
object |
an object of class |
... |
further graphical parameters. |
x |
an object of class "bvarfevd", usually, a result of a call to |
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain FEVD vd <- fevd(object, response = "cons") # Plot plot(vd)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain FEVD vd <- fevd(object, response = "cons") # Plot plot(vd)
Produces the forecast error variance decomposition of a Bayesian VAR model.
## S3 method for class 'bvar' fevd( object, response = NULL, n.ahead = 5, type = "oir", normalise_gir = FALSE, period = NULL, ... )
## S3 method for class 'bvar' fevd( object, response = NULL, n.ahead = 5, type = "oir", normalise_gir = FALSE, period = NULL, ... )
object |
an object of class |
response |
name of the response variable. |
n.ahead |
number of steps ahead. |
type |
type of the impulse responses used to calculate forecast error variable decompositions.
Possible choices are orthogonalised |
normalise_gir |
logical. Should the GIR-based FEVD be normalised? |
period |
integer. Index of the period, for which the variance decomposition should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
The function produces forecast error variance decompositions (FEVD) for the VAR model
with . For non-structural models matrix
is set to the identiy matrix
and can therefore be omitted, where not relevant.
If the FEVD is based on the orthogonalised impulse resonse (OIR), the FEVD will be calculated as
where is the forecast error impulse response for the
th period,
is the lower triangular Choleski decomposition of the variance-covariance
matrix
,
is a selection vector for the response variable and
a selection vector for the impulse variable.
If type = "sir"
, the structural FEVD will be
calculated as
where is the diagonal element of the
th variable of the variance covariance matrix.
If type = "gir"
, the generalised FEVD will be
calculated as
where is the diagonal element of the
th variable of the variance covariance matrix.
If type = "sgir"
, the structural generalised FEVD will be
calculated as
.
Since GIR-based FEVDs do not add up to unity, they can be normalised by setting normalise_gir = TRUE
.
A time-series object of class "bvarfevd"
.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Pesaran, H. H., & Shin, Y. (1998). Generalized impulse response analysis in linear multivariate models. Economics Letters, 58, 17-29.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate models model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain FEVD vd <- fevd(object, response = "cons") # Plot FEVD plot(vd)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate models model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Add priors model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain FEVD vd <- fevd(object, response = "cons") # Plot FEVD plot(vd)
gen_dfm
produces the input for the estimation of a dynamic factor model (DFM).
gen_dfm(x, p = 2, n = 1, iterations = 50000, burnin = 5000)
gen_dfm(x, p = 2, n = 1, iterations = 50000, burnin = 5000)
x |
a time-series object of stationary endogenous variables. |
p |
an integer vector of the lag order of the measurement equation. See 'Details'. |
n |
an integer vector of the number of factors. See 'Details'. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
The function produces the variable matrices of dynamic factor models (DFM) with measurement equation
where
is an
vector of observed variables,
is an
vector of unobserved factors and
is the corresponding
matrix of factor loadings.
is an
error term.
The transition equation is
where
is an
coefficient matrix and
is an
error term.
If integer vectors are provided as arguments p
or n
, the function will
produce a distinct model for all possible combinations of those specifications.
An object of class 'dfmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 5000, burnin = 1000)
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 5000, burnin = 1000)
gen_var
produces the input for the estimation of a vector autoregressive (VAR) model.
gen_var( data, p = 2, exogen = NULL, s = NULL, deterministic = "const", seasonal = FALSE, structural = FALSE, tvp = FALSE, sv = FALSE, fcst = NULL, iterations = 50000, burnin = 5000 )
gen_var( data, p = 2, exogen = NULL, s = NULL, deterministic = "const", seasonal = FALSE, structural = FALSE, tvp = FALSE, sv = FALSE, fcst = NULL, iterations = 50000, burnin = 5000 )
data |
a time-series object of endogenous variables. |
p |
an integer vector of the lag order (default is |
exogen |
an optional time-series object of external regressors. |
s |
an optional integer vector of the lag order of the external regressors (default is |
deterministic |
a character specifying which deterministic terms should
be included. Available values are |
seasonal |
logical. If |
structural |
logical indicating whether data should be prepared for the estimation of a structural VAR model. |
tvp |
logical indicating whether the model parameters are time varying. |
sv |
logical indicating whether time varying error variances should be estimated by employing a stochastic volatility algorithm. |
fcst |
integer. Number of observations saved for forecasting evaluation. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
The function produces the data matrices for vector autoregressive (VAR) models, which can also include unmodelled, non-deterministic variables:
where
is a K-dimensional vector of endogenous variables,
is a
coefficient matrix of contemporaneous endogenous variables,
is a
coefficient matrix of endogenous variables,
is an M-dimensional vector of exogenous regressors and
its corresponding
coefficient matrix.
is an N-dimensional vector of deterministic terms and
its corresponding
coefficient matrix.
is the lag order of endogenous variables,
is the lag
order of exogenous variables, and
is an error term.
If an integer vector is provided as argument p
or s
, the function will
produce a distinct model for all possible combinations of those specifications.
If tvp
is TRUE
, the respective coefficients
of the above model are assumed to be time varying. If sv
is TRUE
,
the error covariance matrix is assumed to be time varying.
An object of class 'bvarmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("e1") e1 <- diff(log(e1)) # Generate model data data <- gen_var(e1, p = 0:2, deterministic = "const")
# Load data data("e1") e1 <- diff(log(e1)) # Generate model data data <- gen_var(e1, p = 0:2, deterministic = "const")
gen_vec
produces the input for the estimation of a vector error correction (VEC) model.
gen_vec( data, p = 2, exogen = NULL, s = 2, r = NULL, const = NULL, trend = NULL, seasonal = NULL, structural = FALSE, tvp = FALSE, sv = FALSE, fcst = NULL, iterations = 50000, burnin = 5000 )
gen_vec( data, p = 2, exogen = NULL, s = 2, r = NULL, const = NULL, trend = NULL, seasonal = NULL, structural = FALSE, tvp = FALSE, sv = FALSE, fcst = NULL, iterations = 50000, burnin = 5000 )
data |
a time-series object of endogenous variables. |
p |
an integer vector of the lag order of the series in the (levels) VAR. Thus, the
resulting model's lag will be |
exogen |
an optional time-series object of external regressors. |
s |
an optional integer vector of the lag order of the exogenous variables of the series
in the (levels) VAR. Thus, the resulting model's lag will be |
r |
an integer vector of the cointegration rank. See 'Details'. |
const |
a character specifying whether a constant term enters the error correction
term ( |
trend |
a character specifying whether a trend term enters the error correction
term ( |
seasonal |
a character specifying whether seasonal dummies should be included in the error
correction term ( |
structural |
logical indicating whether data should be prepared for the estimation of a structural VAR model. |
tvp |
logical indicating whether the model parameters are time varying. |
sv |
logical indicating whether time varying error variances should be estimated by employing a stochastic volatility algorithm. |
fcst |
integer. Number of observations saved for forecasting evaluation. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
The function produces the variable matrices of vector error correction (VEC) models, which can also include exogenous variables:
where
is a
vector of differenced endogenous variables,
is a
vector of cointegration variables,
is a
matrix of cointegration parameters,
is a
coefficient matrix of endogenous variables,
is a
vector of differenced exogenous regressors,
is a
coefficient matrix of exogenous regressors,
is a
vector of deterministic terms, and
is a
coefficient matrix of deterministic terms
that do not enter the cointegration term.
is the lag order of endogenous variables and
is the lag
order of exogenous variables of the corresponding VAR model.
is a
error term.
If an integer vector is provided as argument p
, s
or r
, the function will
produce a distinct model for all possible combinations of those specifications.
If tvp
is TRUE
, the respective coefficients
of the above model are assumed to be time varying. If sv
is TRUE
,
the error covariance matrix is assumed to be time varying.
An object of class 'bvecmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("e6") # Generate model data data <- gen_vec(e6, p = 4, const = "unrestricted", season = "unrestricted")
# Load data data("e6") # Generate model data data <- gen_vec(e6, p = 4, const = "unrestricted", season = "unrestricted")
Prior inclusion probabilities as required for stochastic search variable selection (SSVS) à la George et al. (2008) and Bayesian variable selection (BVS) à la Korobilis (2013).
inclusion_prior( object, prob = 0.5, exclude_deterministics = TRUE, minnesota_like = FALSE, kappa = c(0.8, 0.5, 0.5, 0.8) )
inclusion_prior( object, prob = 0.5, exclude_deterministics = TRUE, minnesota_like = FALSE, kappa = c(0.8, 0.5, 0.5, 0.8) )
object |
an object of class |
prob |
a numeric specifying the prior inclusion probability of all model parameters. |
exclude_deterministics |
logical. If |
minnesota_like |
logical. If |
kappa |
a numeric vector of four elements containing the prior inclusion probabilities
of coefficients that correspond to own lags of endogenous variables, to endogenous variables,
which do not correspond to own lags, to exogenous variables and deterministic terms, respectively.
Only used if |
If minnesota_like = TRUE
, prior inclusion probabilities
are calculated as
|
for own lags of endogenous variables, |
|
for other endogenous variables, |
|
for exogenous variables, |
|
for deterministic variables, |
for lag with
,
,
,
as the first, second,
third and forth element in
kappa
, respectively.
For vector error correction models the function generates prior inclusion probabilities for differenced variables and unrestricted deterministc terms as described above. For variables in the error correction term prior inclusion probabilites are calculated as
|
fow own levels of endogenous variables, |
|
for levels of other endogenous variables, |
|
for levels of exogenous variables, |
|
for deterministic variables. |
A list containing a matrix of prior inclusion probabilities and an integer vector specifying the positions of variables, which should be included in the variable selction algorithm.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
# Prepare data data("e1") # Generate model input object <- gen_var(e1) # Obtain inclusion prior pi_prior <- inclusion_prior(object)
# Prepare data data("e1") # Generate model input object <- gen_var(e1) # Obtain inclusion prior pi_prior <- inclusion_prior(object)
A plot function for objects of class "bvarirf".
irf(x, ...) ## S3 method for class 'bvarirf' plot(x, ...)
irf(x, ...) ## S3 method for class 'bvarirf' plot(x, ...)
x |
an object of class "bvarirf", usually, a result of a call to |
... |
further graphical parameters. |
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Optain posterior draws object <- draw_posterior(model) # Calculate IR ir <- irf(object, impulse = "invest", response = "cons") # Plot IR plot(ir)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Optain posterior draws object <- draw_posterior(model) # Calculate IR ir <- irf(object, impulse = "invest", response = "cons") # Plot IR plot(ir)
Computes the impulse response coefficients of an object of class "bvar"
for
n.ahead
steps.
## S3 method for class 'bvar' irf( x, impulse = NULL, response = NULL, n.ahead = 5, ci = 0.95, shock = 1, type = "feir", cumulative = FALSE, keep_draws = FALSE, period = NULL, ... )
## S3 method for class 'bvar' irf( x, impulse = NULL, response = NULL, n.ahead = 5, ci = 0.95, shock = 1, type = "feir", cumulative = FALSE, keep_draws = FALSE, period = NULL, ... )
x |
an object of class |
impulse |
name of the impulse variable. |
response |
name of the response variable. |
n.ahead |
number of steps ahead. |
ci |
a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95. |
shock |
size of the shock. |
type |
type of the impulse response. Possible choices are forecast error |
cumulative |
logical specifying whether a cumulative IRF should be calculated. |
keep_draws |
logical specifying whether the function should return all draws of
the posterior impulse response function. Defaults to |
period |
integer. Index of the period, for which the IR should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
The function produces different types of impulse responses for the VAR model
with .
Forecast error impulse responses are obtained by recursions
with .
Orthogonalised impulse responses are calculated as
,
where P is the lower triangular Choleski decomposition of
.
Structural impulse responses are calculated as
.
(Structural) Generalised impulse responses for variable , i.e.
are calculated as
, where
is the variance
of the
diagonal element of
and
is a selection vector containing
one in its
element and zero otherwise. If the
"bvar"
object does not contain draws
of , it is assumed to be an identity matrix.
A time-series object of class "bvarirf"
and if keep_draws = TRUE
a simple matrix.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Pesaran, H. H., Shin, Y. (1998). Generalized impulse response analysis in linear multivariate models. Economics Letters, 58, 17-29.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain IR ir <- irf(object, impulse = "invest", response = "cons") # Plot IR plot(ir)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Obtain IR ir <- irf(object, impulse = "invest", response = "cons") # Plot IR plot(ir)
An implementation of the Kalman filter and backward smoothing algorithm proposed by Durbin and Koopman (2002).
kalman_dk(y, z, sigma_u, sigma_v, B, a_init, P_init)
kalman_dk(y, z, sigma_u, sigma_v, B, a_init, P_init)
y |
a |
z |
a |
sigma_u |
the constant |
sigma_v |
the constant |
B |
an |
a_init |
an M-dimensional vector of initial states. |
P_init |
an |
The function uses algorithm 2 from Durbin and Koopman (2002) to produce
a draw of the state vector for
for a state space model
with measurement equation
and transition equation
where and
.
is a K-dimensional vector of endogenous variables and
is a
matrix of regressors with
as a vector of regressors.
The algorithm takes into account Jarociński (2015), where a possible missunderstanding in the implementation of the algorithm of Durbin and Koopman (2002) is pointed out. Following that note the function sets the mean of the initial state to zero in the first step of the algorithm.
A matrix of state vector draws.
Durbin, J., & Koopman, S. J. (2002). A simple and efficient simulation smoother for state space time series analysis. Biometrika, 89(3), 603–615.
Jarociński, M. (2015). A note on implementing the Durbin and Koopman simulation smoother. Computational Statistics and Data Analysis, 91, 1–3. doi:10.1016/j.csda.2015.05.001
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR k <- nrow(y) tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) a_Q <- diag(.0001, m) # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values for Kalman filter y_init <- y * 0 a_filter <- matrix(0, m, tt + 1) # Initialise the Kalman filter for (i in 1:tt) { y_init[, i] <- y[, i] - z[(i - 1) * k + 1:k,] %*% a_filter[, i] } a_init <- post_normal_sur(y = y_init, z = z, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) y_filter <- matrix(y) - z %*% a_init y_filter <- matrix(y_filter, k) # Reshape # Kalman filter and backward smoother a_filter <- kalman_dk(y = y_filter, z = z, sigma_u = sigma, sigma_v = a_Q, B = diag(1, m), a_init = matrix(0, m), P_init = a_Q) a <- a_filter + matrix(a_init, m, tt + 1)
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR k <- nrow(y) tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) a_Q <- diag(.0001, m) # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values for Kalman filter y_init <- y * 0 a_filter <- matrix(0, m, tt + 1) # Initialise the Kalman filter for (i in 1:tt) { y_init[, i] <- y[, i] - z[(i - 1) * k + 1:k,] %*% a_filter[, i] } a_init <- post_normal_sur(y = y_init, z = z, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) y_filter <- matrix(y) - z %*% a_init y_filter <- matrix(y_filter, k) # Reshape # Kalman filter and backward smoother a_filter <- kalman_dk(y = y_filter, z = z, sigma_u = sigma, sigma_v = a_Q, B = diag(1, m), a_init = matrix(0, m), P_init = a_Q) a <- a_filter + matrix(a_init, m, tt + 1)
Calculates the log-likelihood of a multivariate normal distribution.
loglik_normal(u, sigma)
loglik_normal(u, sigma)
u |
a |
sigma |
a |
The log-likelihood is calculated for each vector in period as
,
where .
# Load data data("e1") e1 <- diff(log(e1)) # Generate VAR model data <- gen_var(e1, p = 2, deterministic = "const") y <- t(data$data$Y) x <- t(data$data$Z) # LS estimate ols <- tcrossprod(y, x) %*% solve(tcrossprod(x)) # Residuals u <- y - ols %*% x # Residuals # Covariance matrix sigma <- tcrossprod(u) / ncol(u) # Log-likelihood loglik_normal(u = u, sigma = sigma)
# Load data data("e1") e1 <- diff(log(e1)) # Generate VAR model data <- gen_var(e1, p = 2, deterministic = "const") y <- t(data$data$Y) x <- t(data$data$Z) # LS estimate ols <- tcrossprod(y, x) %*% solve(tcrossprod(x)) # Residuals u <- y - ols %*% x # Residuals # Covariance matrix sigma <- tcrossprod(u) / ncol(u) # Log-likelihood loglik_normal(u = u, sigma = sigma)
Calculates the Minnesota prior for a VAR model.
minnesota_prior( object, kappa0 = 2, kappa1 = 0.5, kappa2 = NULL, kappa3 = 5, max_var = NULL, coint_var = FALSE, sigma = "AR" )
minnesota_prior( object, kappa0 = 2, kappa1 = 0.5, kappa2 = NULL, kappa3 = 5, max_var = NULL, coint_var = FALSE, sigma = "AR" )
object |
an object of class |
kappa0 |
a numeric specifying the prior variance of coefficients that correspond to own lags of endogenous variables. |
kappa1 |
a numeric specifying the size of the prior variance of endogenous
variables, which do not correspond to own lags, relative to argument |
kappa2 |
a numeric specifying the size of the prior variance of non-deterministic exogenous
variables relative to argument |
kappa3 |
a numeric specifying the size of the prior variance of deterministic
terms relative to argument |
max_var |
a positive numeric specifying the maximum prior variance that is allowed for
coefficients of non-deterministic variables. If |
coint_var |
a logical specifying whether the model is a cointegrated VAR model, for which the prior means of first own lags should be set to one. |
sigma |
either |
The function calculates the Minnesota prior of a VAR model. For the endogenous variable
the prior variance of the
th lag of regressor
is obtained as
where is the residual standard deviation of variable
of an unrestricted
LS estimate. For exogenous variables
is the sample standard deviation.
For VEC models the function only provides priors for the non-cointegration part of the model. The
residual standard errors are based on an unrestricted LS regression of the
endogenous variables on the error correction term and the non-cointegration regressors.
A list containing a matrix of prior means and the precision matrix of the cofficients and the inverse variance-covariance matrix of the error term, which was obtained by an LS estimation.
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2020). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("e1") data <- diff(log(e1)) # Generate model input object <- gen_var(data) # Obtain Minnesota prior prior <- minnesota_prior(object)
# Load data data("e1") data <- diff(log(e1)) # Generate model input object <- gen_var(data) # Obtain Minnesota prior prior <- minnesota_prior(object)
A plot function for objects of class "bvarlist"
.
## S3 method for class 'bvarlist' plot(x, ci = 0.95, type = "hist", model = NULL, ...)
## S3 method for class 'bvarlist' plot(x, ci = 0.95, type = "hist", model = NULL, ...)
x |
an object of class |
ci |
interval used to calculate credible bands for time-varying parameters. |
type |
either |
model |
numeric or integer indicating for which models in argument |
... |
further graphical parameters. |
A plot function for objects of class "bvarprd"
.
## S3 method for class 'bvarprd' plot(x, n.pre = NULL, ...)
## S3 method for class 'bvarprd' plot(x, n.pre = NULL, ...)
x |
an object of class "bvarprd", usually, a result of a call to |
n.pre |
number of plotted observations that precede the forecasts. If |
... |
further graphical parameters. |
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Calculate forecasts pred <- predict(object, new_d = rep(1, 10)) # Plot forecasts plot(pred)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate model data model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Calculate forecasts pred <- predict(object, new_d = rep(1, 10)) # Plot forecasts plot(pred)
Produces a draw of coefficients for cointegration models with a prior on the cointegration space as proposed in Koop et al. (2010) and a draw of non-cointegration coefficients from a normal density.
post_coint_kls( y, beta, w, sigma_i, v_i, p_tau_i, g_i, x = NULL, gamma_mu_prior = NULL, gamma_v_i_prior = NULL )
post_coint_kls( y, beta, w, sigma_i, v_i, p_tau_i, g_i, x = NULL, gamma_mu_prior = NULL, gamma_v_i_prior = NULL )
y |
a |
beta |
a |
w |
a |
sigma_i |
an inverse of the |
v_i |
a numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. |
p_tau_i |
an inverted |
g_i |
a |
x |
a |
gamma_mu_prior |
a |
gamma_v_i_prior |
an inverted |
The function produces posterior draws of the coefficient
matrices ,
and
for the model
where is a K-dimensional vector of differenced endogenous variables.
is an
vector of variables in the cointegration term,
which include lagged values of endogenous and exogenous variables in levels and
restricted deterministic terms.
is an N-dimensional vector of
differenced endogenous and exogenous explanatory variabes as well as unrestricted
deterministic terms. The error term is
.
Draws of the loading matrix are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix is
and the posterior mean by
where is a
matrix of differenced endogenous variables and
with
as an
matrix of
variables in the cointegration term.
For a given prior mean vector and prior covariance matrix
the posterior covariance matrix of non-cointegration coefficients in
is obtained by
and the posterior mean by
where is an
matrix of
explanatory variables, which do not enter the cointegration term.
Draws of the cointegration matrix are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix of the unrestricted cointegration
matrix
is
and the posterior mean by
where and
.
The final draws of and
are calculated using
and
.
A named list containing the following elements:
alpha |
a draw of the |
beta |
a draw of the |
Pi |
a draw of the |
Gamma |
a draw of the |
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224-242. doi:10.1080/07474930903382208
# Load data data("e6") # Generate model data temp <- gen_vec(e6, p = 1, r = 1) y <- t(temp$data$Y) ect <- t(temp$data$W) k <- nrow(y) # Endogenous variables tt <- ncol(y) # Number of observations # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values of beta beta <- matrix(c(1, -4), k) # Draw parameters coint <- post_coint_kls(y = y, beta = beta, w = ect, sigma_i = sigma_i, v_i = 0, p_tau_i = diag(1, k), g_i = sigma_i)
# Load data data("e6") # Generate model data temp <- gen_vec(e6, p = 1, r = 1) y <- t(temp$data$Y) ect <- t(temp$data$W) k <- nrow(y) # Endogenous variables tt <- ncol(y) # Number of observations # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values of beta beta <- matrix(c(1, -4), k) # Draw parameters coint <- post_coint_kls(y = y, beta = beta, w = ect, sigma_i = sigma_i, v_i = 0, p_tau_i = diag(1, k), g_i = sigma_i)
Produces a draw of coefficients for cointegration models in SUR form with a prior on the cointegration space as proposed in Koop et al. (2010) and a draw of non-cointegration coefficients from a normal density.
post_coint_kls_sur( y, beta, w, sigma_i, v_i, p_tau_i, g_i, x = NULL, gamma_mu_prior = NULL, gamma_v_i_prior = NULL, svd = FALSE )
post_coint_kls_sur( y, beta, w, sigma_i, v_i, p_tau_i, g_i, x = NULL, gamma_mu_prior = NULL, gamma_v_i_prior = NULL, svd = FALSE )
y |
a |
beta |
a |
w |
a |
sigma_i |
the inverse of the constant |
v_i |
a numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. |
p_tau_i |
an inverted |
g_i |
a |
x |
a |
gamma_mu_prior |
a |
gamma_v_i_prior |
an inverted |
svd |
logical. If |
The function produces posterior draws of the coefficient
matrices ,
and
for the model
where is a K-dimensional vector of differenced endogenous variables.
is an
vector of variables in the cointegration term,
which include lagged values of endogenous and exogenous variables in levels and
restricted deterministic terms.
is an N-dimensional vector of
differenced endogenous and exogenous explanatory variabes as well as unrestricted
deterministic terms. The error term is
.
Draws of the loading matrix are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix is
and the posterior mean by
where is a
matrix of differenced endogenous variables and
with
as an
matrix of
variables in the cointegration term.
For a given prior mean vector and prior covariance matrix
the posterior covariance matrix of non-cointegration coefficients in
is obtained by
and the posterior mean by
where is an
matrix of
explanatory variables, which do not enter the cointegration term.
Draws of the cointegration matrix are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix of the unrestricted cointegration
matrix
is
and the posterior mean by
where and
.
The final draws of and
are calculated using
and
.
A named list containing the following elements:
alpha |
a draw of the |
beta |
a draw of the |
Pi |
a draw of the |
Gamma |
a draw of the |
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224-242. doi:10.1080/07474930903382208
# Load data data("e6") # Generate model data temp <- gen_vec(e6, p = 1, r = 1) y <- t(temp$data$Y) ect <- t(temp$data$W) k <- nrow(y) # Endogenous variables tt <- ncol(y) # Number of observations # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values of beta beta <- matrix(c(1, -4), k) # Draw parameters coint <- post_coint_kls_sur(y = y, beta = beta, w = ect, sigma_i = sigma_i, v_i = 0, p_tau_i = diag(1, nrow(ect)), g_i = sigma_i)
# Load data data("e6") # Generate model data temp <- gen_vec(e6, p = 1, r = 1) y <- t(temp$data$Y) ect <- t(temp$data$W) k <- nrow(y) # Endogenous variables tt <- ncol(y) # Number of observations # Initial value of Sigma sigma <- tcrossprod(y) / tt sigma_i <- solve(sigma) # Initial values of beta beta <- matrix(c(1, -4), k) # Draw parameters coint <- post_coint_kls_sur(y = y, beta = beta, w = ect, sigma_i = sigma_i, v_i = 0, p_tau_i = diag(1, nrow(ect)), g_i = sigma_i)
Produces a draw of the constant diagonal error variance matrix of the measurement equation of a state space model using an inverse gamma posterior density.
post_gamma_measurement_variance(u, shape_prior, rate_prior, inverse)
post_gamma_measurement_variance(u, shape_prior, rate_prior, inverse)
u |
a |
shape_prior |
a |
rate_prior |
a |
inverse |
logical. If |
For a model with measurement equation
with
the function produces a draw of the constant diagonal error variance matrix
.
A matrix.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
k <- 10 # Number of endogenous variables tt <- 1000 # Number of observations set.seed(1234) # Set RNG seed # Generate artificial error series with N(0, 1) u <- matrix(rnorm(k * tt)) # Define priors shape_prior <- matrix(1, k) rate_prior <- matrix(.0001, k) # Obtain posterior draw post_gamma_measurement_variance(u, shape_prior, rate_prior, inverse = FALSE)
k <- 10 # Number of endogenous variables tt <- 1000 # Number of observations set.seed(1234) # Set RNG seed # Generate artificial error series with N(0, 1) u <- matrix(rnorm(k * tt)) # Define priors shape_prior <- matrix(1, k) rate_prior <- matrix(.0001, k) # Obtain posterior draw post_gamma_measurement_variance(u, shape_prior, rate_prior, inverse = FALSE)
Produces a draw of the constant diagonal error variance matrix of the state equation of a state space model using an inverse gamma posterior density.
post_gamma_state_variance(a, a_init, shape_prior, rate_prior, inverse)
post_gamma_state_variance(a, a_init, shape_prior, rate_prior, inverse)
a |
a |
a_init |
a |
shape_prior |
a |
rate_prior |
a |
inverse |
logical. If |
For the state space model with state equation
and measurement equation
with and
the function produces a draw of the constant diagonal error variances matrix of the
state equation
.
A matrix.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
k <- 10 # Number of artificial coefficients tt <- 1000 # Number of observations set.seed(1234) # Set RNG seed # Generate artificial data according to a random walk a <- matrix(rnorm(k), k, tt + 1) for (i in 2:(tt + 1)) { a[, i] <- a[, i - 1] + rnorm(k, 0, sqrt(1 / 100)) } a_init <- matrix(a[, 1]) # Define initial state a <- matrix(a[, -1]) # Drop initial state from main sample and make vector # Define priors shape_prior <- matrix(1, k) rate_prior <- matrix(.0001, k) # Obtain posterior draw post_gamma_state_variance(a, a_init, shape_prior, rate_prior, inverse = FALSE)
k <- 10 # Number of artificial coefficients tt <- 1000 # Number of observations set.seed(1234) # Set RNG seed # Generate artificial data according to a random walk a <- matrix(rnorm(k), k, tt + 1) for (i in 2:(tt + 1)) { a[, i] <- a[, i - 1] + rnorm(k, 0, sqrt(1 / 100)) } a_init <- matrix(a[, 1]) # Define initial state a <- matrix(a[, -1]) # Drop initial state from main sample and make vector # Define priors shape_prior <- matrix(1, k) rate_prior <- matrix(.0001, k) # Obtain posterior draw post_gamma_state_variance(a, a_init, shape_prior, rate_prior, inverse = FALSE)
Produces a draw of coefficients from a normal posterior density.
post_normal(y, x, sigma_i, a_prior, v_i_prior)
post_normal(y, x, sigma_i, a_prior, v_i_prior)
y |
a |
x |
an |
sigma_i |
the inverse of the |
a_prior |
a |
v_i_prior |
the inverse of the |
The function produces a vectorised posterior draw of the
coefficient matrix
for the model
where is a K-dimensional vector of endogenous variables,
is an M-dimensional vector of explanatory variabes
and the error term is
.
For a given prior mean vector and prior covariance matrix
the posterior covariance matrix is obtained by
and the posterior mean by
where is a
matrix of the endogenous variables and
is an
matrix of
the explanatory variables.
A vector.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) x <- t(temp$data$Z) k <- nrow(y) tt <- ncol(y) m <- k * nrow(x) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Initial value of inverse Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal(y = y, x = x, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) x <- t(temp$data$Z) k <- nrow(y) tt <- ncol(y) m <- k * nrow(x) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Initial value of inverse Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal(y = y, x = x, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
Produces posterior draws of constant error covariance coefficients.
post_normal_covar_const(y, u_omega_i, k, prior_mean, prior_covariance_i)
post_normal_covar_const(y, u_omega_i, k, prior_mean, prior_covariance_i)
y |
a |
u_omega_i |
matrix of error variances of the measurement equation.
Either a |
k |
number of endogenous variables. |
prior_mean |
vector of prior means. In case of TVP, this vector is used as initial condition. |
prior_covariance_i |
inverse prior covariance matrix. In case of TVP, this matrix is used as initial condition. |
For the multivariate model with
the function produces a draw of the lower triangular part of
similar as in
Primiceri (2005), i.e., using
where
and denotes the first to
th elements of the vector
.
A matrix.
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies, 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
# Load example data data("e1") y <- log(t(e1)) k <- nrow(y) y <- matrix(y) # Generate artificial draws of other matrices u_omega_i <- Matrix(diag(1, k)) prior_mean <- matrix(0, 3) prior_covariance_i <- Matrix(diag(0, 3)) # Obtain posterior draw post_normal_covar_const(y, u_omega_i, k, prior_mean, prior_covariance_i)
# Load example data data("e1") y <- log(t(e1)) k <- nrow(y) y <- matrix(y) # Generate artificial draws of other matrices u_omega_i <- Matrix(diag(1, k)) prior_mean <- matrix(0, 3) prior_covariance_i <- Matrix(diag(0, 3)) # Obtain posterior draw post_normal_covar_const(y, u_omega_i, k, prior_mean, prior_covariance_i)
Produces posterior draws of time varying error covariance coefficients.
post_normal_covar_tvp(y, u_omega_i, k, v_sigma_i, psi_init)
post_normal_covar_tvp(y, u_omega_i, k, v_sigma_i, psi_init)
y |
a |
u_omega_i |
matrix of error variances of the measurement equation.
Either a |
k |
number of endogenous variables. |
v_sigma_i |
matrix of error variances of the state equation.
Either an |
psi_init |
a vector of inital values of the state equation. |
For the multivariate model with
the function produces a draw of the lower triangular part of
similar as in
Primiceri (2005), i.e., using
where
and denotes the first to
th elements of the vector
.
The algorithm of Chan and Jeliazkov (2009) is used to obtain time varying coefficients.
A matrix.
Chan, J., & Jeliazkov, I. (2009). Efficient simulation and integrated likelihood estimation in state space models. International Journal of Mathematical Modelling and Numerical Optimisation, 1(1/2), 101–120. doi:10.1504/IJMMNO.2009.030090
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies, 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
# Load example data data("e1") y <- log(t(e1)) k <- nrow(y) y <- matrix(y) # Generate artificial draws of other matrices u_omega_i <- Matrix(diag(1, 3)) v_sigma_i <- Matrix(diag(1000, 3)) psi_init <- matrix(0, 3) # Obtain posterior draw post_normal_covar_tvp(y, u_omega_i, k, v_sigma_i, psi_init)
# Load example data data("e1") y <- log(t(e1)) k <- nrow(y) y <- matrix(y) # Generate artificial draws of other matrices u_omega_i <- Matrix(diag(1, 3)) v_sigma_i <- Matrix(diag(1000, 3)) psi_init <- matrix(0, 3) # Obtain posterior draw post_normal_covar_tvp(y, u_omega_i, k, v_sigma_i, psi_init)
Produces a draw of coefficients from a normal posterior density for a model with seemingly unrelated regresssions (SUR).
post_normal_sur(y, z, sigma_i, a_prior, v_i_prior, svd = FALSE)
post_normal_sur(y, z, sigma_i, a_prior, v_i_prior, svd = FALSE)
y |
a |
z |
a |
sigma_i |
the inverse of the constant |
a_prior |
a |
v_i_prior |
the inverse of the |
svd |
logical. If |
The function produces a posterior draw of the coefficient vector for the model
where .
is a K-dimensional vector of endogenous variables and
is a
matrix of regressors with
as a vector of regressors.
For a given prior mean vector and prior covariance matrix
the posterior covariance matrix is obtained by
and the posterior mean by
A vector.
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR k <- nrow(y) tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Initial value of inverse Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal_sur(y = y, z = z, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) z <- temp$data$SUR k <- nrow(y) tt <- ncol(y) m <- ncol(z) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(0.1, m) # Initial value of inverse Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal_sur(y = y, z = z, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
ssvs
employs stochastic search variable selection as proposed by George et al. (2008)
to produce a draw of the precision matrix of the coefficients in a VAR model.
ssvs(a, tau0, tau1, prob_prior, include = NULL)
ssvs(a, tau0, tau1, prob_prior, include = NULL)
a |
an M-dimensional vector of coefficient draws. |
tau0 |
an M-dimensional vector of prior standard deviations for restricted
coefficients in vector |
tau1 |
an M-dimensional vector of prior standard deviations for unrestricted
coefficients in vector |
prob_prior |
an M-dimensional vector of prior inclusion probabilites for the coefficients
in vector |
include |
an integer vector specifying the positions of coefficients in vector |
The function employs stochastic search variable selection (SSVS) as proposed
by George et al. (2008) to produce a draw of the diagonal inverse prior covariance matrix
and the corresponding vector of inclusion parameters
of the vectorised coefficient matrix
for the VAR model
where is a K-dimensional vector of endogenous variables,
is a vector of explanatory variabes
and the error term is
.
A named list containing two components:
v_i |
an |
lambda |
an M-dimensional vector of inclusion parameters. |
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) x <- t(temp$data$Z) k <- nrow(y) tt <- ncol(y) m <- k * nrow(x) # Obtain SSVS priors using the semiautomatic approach priors <- ssvs_prior(temp, semiautomatic = c(0.1, 10)) tau0 <- priors$tau0 tau1 <- priors$tau1 # Prior for inclusion parameter prob_prior <- matrix(0.5, m) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(c(tau1^2), m) # Initial value of Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal(y = y, x = x, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) # Run SSVS lambda <- ssvs(a = a, tau0 = tau0, tau1 = tau1, prob_prior = prob_prior)
# Load data data("e1") data <- diff(log(e1)) # Generate model data temp <- gen_var(data, p = 2, deterministic = "const") y <- t(temp$data$Y) x <- t(temp$data$Z) k <- nrow(y) tt <- ncol(y) m <- k * nrow(x) # Obtain SSVS priors using the semiautomatic approach priors <- ssvs_prior(temp, semiautomatic = c(0.1, 10)) tau0 <- priors$tau0 tau1 <- priors$tau1 # Prior for inclusion parameter prob_prior <- matrix(0.5, m) # Priors a_mu_prior <- matrix(0, m) a_v_i_prior <- diag(c(tau1^2), m) # Initial value of Sigma sigma_i <- solve(tcrossprod(y) / tt) # Draw parameters a <- post_normal(y = y, x = x, sigma_i = sigma_i, a_prior = a_mu_prior, v_i_prior = a_v_i_prior) # Run SSVS lambda <- ssvs(a = a, tau0 = tau0, tau1 = tau1, prob_prior = prob_prior)
Calculates the priors for a Bayesian VAR model, which employs stochastic search variable selection (SSVS).
ssvs_prior(object, tau = c(0.05, 10), semiautomatic = NULL)
ssvs_prior(object, tau = c(0.05, 10), semiautomatic = NULL)
object |
an object of class |
tau |
a numeric vector of two elements containing the prior standard errors of restricted
variables ( |
semiautomatic |
an optional numeric vector of two elements containing the factors by which
the standard errors associated with an unconstrained least squares estimate of the VAR model are
multiplied to obtain the prior standard errors of restricted ( |
A list containing the vectors of prior standard deviations for restricted and unrestricted variables, respectively.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
# Prepare data data("e1") data <- diff(log(e1)) # Generate model input object <- gen_var(data) # Obtain SSVS prior prior <- ssvs_prior(object, semiautomatic = c(.1, 10))
# Prepare data data("e1") data <- diff(log(e1)) # Generate model input object <- gen_var(data) # Obtain SSVS prior prior <- ssvs_prior(object, semiautomatic = c(.1, 10))
Produces a draw of log-volatilities.
stoch_vol(y, h, sigma, h_init, constant)
stoch_vol(y, h, sigma, h_init, constant)
y |
a |
h |
a |
sigma |
a numeric of the variance of the log-volatilites. |
h_init |
a numeric of the initial state of log-volatilities. |
constant |
a numeric of the constant that should be added to |
The function is a wrapper for function stochvol_ksc1998
.
A vector of log-volatility draws.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Kim, S., Shephard, N., & Chib, S. (1998). Stochastic volatility. Likelihood inference and comparison with ARCH models. Review of Economic Studies 65(3), 361–393. doi:10.1111/1467-937X.00050
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stoch_vol(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stoch_vol(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
Produces a draw of log-volatilities.
stochvol_ksc1998(y, h, sigma, h_init, constant)
stochvol_ksc1998(y, h, sigma, h_init, constant)
y |
a |
h |
a |
sigma |
a |
h_init |
a |
constant |
a |
For each column in y
the function produces a posterior
draw of the log-volatility for the model
where and
is assumed to evolve according to a random walk
with .
The implementation is based on the algorithm of Kim, Shephard and Chip (1998) and performs the following steps:
Perform the transformation .
Obtain a sample from the seven-component normal mixture for
approximating the log- distribution.
Obtain a draw of log-volatilities.
The implementation follows the code provided on the website to the textbook by Chan, Koop, Poirier, and Tobias (2019).
A vector of log-volatility draws.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Kim, S., Shephard, N., & Chib, S. (1998). Stochastic volatility. Likelihood inference and comparison with ARCH models. Review of Economic Studies 65(3), 361–393. doi:10.1111/1467-937X.00050
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stochvol_ksc1998(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stochvol_ksc1998(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
Produces a draw of log-volatilities based on Omori, Chib, Shephard and Nakajima (2007).
stochvol_ocsn2007(y, h, sigma, h_init, constant)
stochvol_ocsn2007(y, h, sigma, h_init, constant)
y |
a |
h |
a |
sigma |
a |
h_init |
a |
constant |
a |
For each column in y
the function produces a posterior
draw of the log-volatility for the model
where and
is assumed to evolve according to a random walk
with .
The implementation follows the algorithm of Omori, Chib, Shephard and Nakajima (2007) and performs the following steps:
Perform the transformation .
Obtain a sample from the ten-component normal mixture for
approximating the log- distribution.
Obtain a draw of log-volatilities.
The implementation is an adaption of the code provided on the website to the textbook by Chan, Koop, Poirier, and Tobias (2019).
A vector of log-volatility draws.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Omori, Y., Chib, S., Shephard, N., & Nakajima, J. (2007). Stochastic volatiltiy with leverage. Fast and efficient likelihood inference. Journal of Econometrics 140(2), 425–449. doi:10.1016/j.jeconom.2006.07.008
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stochvol_ocsn2007(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
data("us_macrodata") y <- matrix(us_macrodata[, "r"]) # Initialise log-volatilites h_init <- matrix(log(var(y))) h <- matrix(rep(h_init, length(y))) # Obtain draw stochvol_ocsn2007(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
summary method for class "bvar"
.
## S3 method for class 'bvar' summary(object, ci = 0.95, period = NULL, ...) ## S3 method for class 'summary.bvar' print(x, digits = max(3L, getOption("digits") - 3L), ...)
## S3 method for class 'bvar' summary(object, ci = 0.95, period = NULL, ...) ## S3 method for class 'summary.bvar' print(x, digits = max(3L, getOption("digits") - 3L), ...)
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
period |
integer. Index of the period, for which the summary statistics should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
x |
an object of class |
digits |
the number of significant digits to use when printing. |
summary.bvar
returns a list of class "summary.bvar"
,
which contains the following components:
coefficients |
A list of various summary statistics of the posterior draws of the VAR coefficients. |
sigma |
A list of various summary statistics of the posterior draws of the variance-covariance matrix. |
specifications |
a list containing information on the model specification. |
summary method for class "bvarlist"
.
## S3 method for class 'bvarlist' summary(object, ...)
## S3 method for class 'bvarlist' summary(object, ...)
object |
an object of class |
... |
further arguments passed to or from other methods. |
The log-likelihood for the calculation of the information criteria is obtained by
,
where . The Akaike, Bayesian and Hannan–Quinn (HQ) information criteria are calculated as
,
and
, respectively,
where is the number of endogenous variables,
the number of lags of endogenous variables,
the number of exogenous variables,
the number of lags of exogenous variables,
the number of deterministic terms and
the number of observations.
summary.bvarlist
returns a table of class "summary.bvarlist"
.
summary method for class "bvec"
.
## S3 method for class 'bvec' summary(object, ci = 0.95, period = NULL, ...) ## S3 method for class 'summary.bvec' print(x, digits = max(3L, getOption("digits") - 3L), ...)
## S3 method for class 'bvec' summary(object, ci = 0.95, period = NULL, ...) ## S3 method for class 'summary.bvec' print(x, digits = max(3L, getOption("digits") - 3L), ...)
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
period |
integer. Index of the period of a TVP VEC, for which a summary should be generated.
Only used for TVP models. Default is |
... |
further arguments passed to or from other methods. |
x |
an object of class |
digits |
the number of significant digits to use when printing. |
summary.bvec
returns a list of class "summary.bvec"
,
which contains the following components:
coefficients |
A list of various summary statistics of the posterior draws of the VAR coefficients. |
sigma |
A list of various summary statistics of the posterior draws of the variance-covariance matrix. |
specifications |
a list containing information on the model specification. |
summary method for class "dfm"
.
## S3 method for class 'dfm' summary(object, ci = 0.95, ...)
## S3 method for class 'dfm' summary(object, ci = 0.95, ...)
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
... |
further arguments passed to or from other methods. |
summary.dfm
returns a list of class "summary.dfm"
,
which contains the following components:
lambda |
A list of various summary statistics of the posterior draws of the factor loadings. |
factor |
A list of various summary statistics of the posterior draws of the factors. |
sigma_u |
A list of various summary statistics of the posterior draws of the variance matrix of the measurement equation. |
a |
A list of various summary statistics of the posterior draws of the factor loadings. |
sigma_v |
A list of various summary statistics of the posterior draws of the variance matrix of the transition equation. |
specifications |
a list containing information on the model specification. |
Transforms a dense matrix of dimensions into a sparse block
diagonal matrix of dimensions
.
sur_const_to_tvp(z, k, tt)
sur_const_to_tvp(z, k, tt)
z |
a |
k |
integer of the number of endogenous variables. |
tt |
integer of the number of observations. |
A sparse block diagonal matrix of dimensions .
# Specify the dimensions of the dense matrix k <- 2 tt <- 5 m <- 3 # Generate artificial data z <- matrix(NA, k * tt, m) for (i in 1:tt) { z[(i - 1) * k + 1:k, ] <- i } # Perform transformation sur_const_to_tvp(z, k, tt)
# Specify the dimensions of the dense matrix k <- 2 tt <- 5 m <- 3 # Generate artificial data z <- matrix(NA, k * tt, m) for (i in 1:tt) { z[(i - 1) * k + 1:k, ] <- i } # Perform transformation sur_const_to_tvp(z, k, tt)
Thins the MCMC posterior draws in an object of class "bvar"
.
## S3 method for class 'bvar' thin(x, thin = 10, ...)
## S3 method for class 'bvar' thin(x, thin = 10, ...)
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
An object of class "bvar"
.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Obtain data matrices model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in draws should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) object <- thin(object)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Obtain data matrices model <- gen_var(e1, p = 2, deterministic = 2, iterations = 100, burnin = 10) # Chosen number of iterations and burn-in draws should be much higher. # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) object <- thin(object)
Thins the MCMC posterior draws in an object of class "bvarlist"
.
## S3 method for class 'bvarlist' thin(x, thin = 10, ...)
## S3 method for class 'bvarlist' thin(x, thin = 10, ...)
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
An object of class "bvarlist"
.
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate multiple model matrices model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Thin object <- thin(object)
# Load data data("e1") e1 <- diff(log(e1)) * 100 # Generate multiple model matrices model <- gen_var(e1, p = 1:2, deterministic = 2, iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Thin object <- thin(object)
Thins the MCMC posterior draws in an object of class "bvec"
.
## S3 method for class 'bvec' thin(x, thin = 10, ...)
## S3 method for class 'bvec' thin(x, thin = 10, ...)
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
An object of class "bvec"
.
# Load data data("e6") # Generate model data model <- gen_vec(e6, p = 2, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Thin object <- thin(object)
# Load data data("e6") # Generate model data model <- gen_vec(e6, p = 2, r = 1, const = "unrestricted", seasonal = "unrestricted", iterations = 100, burnin = 10) # Add prior specifications model <- add_priors(model) # Obtain posterior draws object <- draw_posterior(model) # Thin object <- thin(object)
Thins the MCMC posterior draws in an object of class "dfm"
.
## S3 method for class 'dfm' thin(x, thin = 10, ...)
## S3 method for class 'dfm' thin(x, thin = 10, ...)
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
An object of class "dfm"
.
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- draw_posterior(model) # Plot factors object <- thin(object, thin = 2)
# Load data data("bem_dfmdata") # Generate model data model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1, iterations = 20, burnin = 10) # Number of iterations and burnin should be much higher. # Add prior specifications model <- add_priors(model, lambda = list(v_i = .01), sigma_u = list(shape = 5, rate = 4), a = list(v_i = .01), sigma_v = list(shape = 5, rate = 4)) # Obtain posterior draws object <- draw_posterior(model) # Plot factors object <- thin(object, thin = 2)
The data set contains quarterly time series for the US CPI inflation rate, unemployment rate, and Fed Funds rate from 1959Q2 to 2007Q4. It was produced from file "US_macrodata.csv" of the data sets associated with Chan, Koop, Poirier and Tobias (2019). Raw data are available at https://web.ics.purdue.edu/~jltobias/second_edition/Chapter20/code_for_exercise_1/US_macrodata.csv.
data("us_macrodata")
data("us_macrodata")
A named time-series object with 195 rows and 3 variables:
CPI inflation rate.
unemployment rate.
Fed Funds rate.
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.