gEcon : Calibration of RBC or DSGE model with IRFs

[This article was first published on K & L Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post gives an short introduction to the gEcon R package, which provides a variety of functionalities for DSGE (Dynamic Stochastic General Equilibrium) or CGE (Computational General Equilibrium) model. It is typical that the former is solved by Matlab with Dynare, the latter by GAMS. In this post, we take a simple example of basic RBC model to understand how to use gEcon R package.



Introduction


Using gEcon (https://gecon.r-forge.r-project.org/) R package, we can perform the calibration or (Bayesian) estimation of DSGE model as well as RBC model. These days, New Keynesian DSGE (NK DSGE) model is widely used for economic policy analysis and simulation (or forecast). For monetary policy analysis, price stickiness is needed and reflected in NK DSGE model.

Authors of gEcon published the following paper, frmo which we can learn a lot of advanced methodologies.


But this post deals with a basic RBC model which is a simpler version of NK DSGE model because our focus is to learn how to use gEcon package not investigate policy effects. But when we understand how to estimate or calibrate RBC model, we can apply this approach to NK DSGE model. This is possible since the same solution technique is used for these two types of model although NK DSGE model is so complicated in nature.


Installing


Installation of gEcon package is not available in CRAN. In particular, you need to install nleqslv R package in advance. If you haven’t installed the following packages, you are recommended to install these packages as a prerequisite.
  • MASS
  • Matrix
  • Rcpp

After installing the above prerequisite packages, type the following command at console.

install.packages(“gEcon”, repos=”http://R-Forge.R-project.org”)

After a long display of installation process, we can find that gEcon package is installed.


Model Structure File (*.gcn)


gEcon estimates a macro model with two files. Firstly, we need a model structure file (*.gcn) to describe a model structure (objective function, constraints, control variables, and so on). Secondly, we need a model running file (*.r file) to run this model structure file.

You can find lots of example code at https://gecon.r-forge.r-project.org/models.html.

As an example of a model structure file, a basic RBC model code(rbc_shlee.gcn) is written as follows. (this example is also found at the above link.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# ############################################################################
# (c) Chancellery of the Prime Minister 20122015                            #
#                                                                            #
# Authors: Grzegorz Klima, Karol Podemski, Kaja RetkiewiczWijtiwiak         #
# ############################################################################
# Basic RBC model
# ############################################################################
 
options {
    output logfile = TRUE; output LaTeX = TRUE;
    output LaTeX landscape = TRUE;
};
 
tryreduce { 
    pi[], K_d[], L_d[];                             
};
 
block CONSUMER {
  definitions{ u[] = (C[]^mu*(1L_s[])^(1mu))^(1eta)/(1eta);};
  controls   { K_s[], C[], L_s[], I[];                         };
  objective  { U[] = u[] + beta*E[][U[1]];                     };
  constraints{ C[] + I[] = pi[] + r[]*K_s[1+ W[]*L_s[];
               K_s[] = (1  delta)*K_s[1+ I[];              };
  calibration{ delta = 0.025; beta = 0.99; eta = 2; mu = 0.3;  };
};
 
block FIRM {
  controls   { K_d[], L_d[], Y[];                              };
  objective  { pi[] = Y[]  L_d[]*W[]  r[]*K_d[];             };
  constraints{ Y[] = Z[]*K_d[]^alpha * L_d[]^(1  alpha);      };
  calibration{ r[ss] * K_d[ss] = 0.36 * Y[ss] > alpha;        };
};
 
block EQUILIBRIUM {
  identities { K_d[] = K_s[1]; L_d[] = L_s[];                 };
};
 
block EXOG {
  identities { Z[] = exp(phi*log(Z[1]) + epsilon_Z[]);        };
  shocks     { epsilon_Z[];                                    };
  calibration{ phi = 0.95;                                     };
};
 
cs


At first, \(X\) variable has the following timing.

  • X[] : current variable such as \(X_t\)
  • X[-n] : predetermined variable such as \(X_{t-n}\)
  • X[n] : forward-looking variable such as \(X_{t+n}\)


Now let’s investigate the above RBC model code (*.gcn) line by line.

block CONSUMER


block CONSUMER describes the following the consumer utility maximization problem with two constraints: budget constraint and capital accumulation.
\begin{align} \max_{K^{\mathrm{s}}_{t}, C_{t}, L^{\mathrm{s}}_{t}, I_{t} } U_{t} &= {\beta} {\mathrm{E}_{t}\left[U_{t+1}\right]} + \left(1 – \eta\right)^{-1} {\left({{C_{t}}^{\mu}} {\left(1 – L^{\mathrm{s}}_{t}\right)^{1 – \mu}}\right)^{1 – \eta}}\\ \mathrm{s.t.}&\nonumber\\ C_{t} + I_{t} &= \pi_{t} + {K^{\mathrm{s}}_{t-1}} {r_{t}} + {L^{\mathrm{s}}_{t}} {w_{t}}\\ K^{\mathrm{s}}_{t} &= I_{t} + {K^{\mathrm{s}}_{t-1}} \left(1 – \delta\right) \end{align}

block FIRM


block FIRM describes the following the firm profit(\(\pi_t\)) maximization problem with the constraint of the Cobb-Douglas production function.
\begin{align} \max_{K_{t}, L_{t}, Y_{t} } \pi_{t} &= Y_{t} – {w_{t}}{L_{t}} – {r_{t}} {K_{t}}\\ \mathrm{s.t.}&\nonumber\\ Y_{t} &= {Z_{t}} {{K_{t}}^{\alpha}} {{L_{t}}^{1 – \alpha}} \end{align}

block EQUILIBRIUM


The equilibrium occurs where the quantity demanded is equal to the quantity supplied for the labor and capital.
\begin{align} K_{t} &= K^{\mathrm{s}}_{t-1} \\ L_{t} &= L^{\mathrm{s}}_{t} \end{align}

block EXOG


In this RBC model, business cycles result from the aggregate productivity shock (\(Z_{t}\)) which follows an exogenous stochastic process. \begin{equation} Z_{t} = e^{\epsilon^{\mathrm{Z}}_{t} + {\phi} {\log{Z_{t-1}}}} \end{equation}

Model Running File (*.R)


This model running file consists of three parts.

  • Load model structure file (*.gcn)
  • Find the steady-state of the model
  • Log-linearize the model in the neighborhood of the steady-state
  • Estimate or Calibrate model parameters
  • Calculate IRF(impulse-response function) and other statistics or Perform simulation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#=========================================================================#
# Financial Econometrics & Derivatives, ML/DL using R, Python, Tensorflow  
# by Sang-Heon Lee 
#
# https://kiandlee.blogspot.com
#————————————————————————-#
# RBC model calibration by using gEcon
#=========================================================================#
 
# load gEcon package
library(gEcon)
 
setwd(“D:/SHLEE/blog/gEcon/RBC”)
 
# make and load the model
rbc < make_model(“rbc_shlee.gcn”)
 
# find and print steady-state values
rbc < steady_state(rbc)
get_ss_values(rbc, to_tex = TRUE)
 
# find and print perturbation solution
rbc < solve_pert(model = rbc, loglin = TRUE)
get_pert_solution(rbc, to_tex = TRUE)
 
# set and print the shock distribution parameters
rbc < set_shock_cov_mat(rbc, cov_matrix = matrix(c(0.01), 11),
                         shock_order = c(“epsilon_Z”))
shock_info(rbc, all = TRUE)
 
# compute and print correlations
rbc < compute_model_stats(rbc, ref_var = “Y”, n_leadlags = 5)
get_model_stats(model = rbc, basic_stats = TRUE, 
                corr = TRUE, autocorr = TRUE, 
                var_dec = FALSE, to_tex = TRUE)
 
# compute and print the IRFs
v.str.response_var = c(“r”“C”“K_s”“Y”“L_s”“I”)
rbc_irf < compute_irf(rbc, variables = v.str.response_var)
 
# plot IRF
v.str.palette < c(“#ff0000”“#00ff00”“#0000ff”
                   “#0072B2”“#00ffff”“#999999”)
x11();
matplot(t(as.matrix(rbc_irf@sim[,,1])), type=“l”
        ylab=“% deviations from the steady-state”
        main = “IRFs to epsilon_Z shock”
        col = v.str.palette, lwd = 4)
legend(“topright”, legend = rbc_irf@variables,
       fill = v.str.palette, ncol = 1, cex = 1)
 
# print summary of the model results
summary(rbc)
 
cs


Results


After running the above R code, we can get the following results: 1) IRFs, 2) calibrated model results with statistics and moments.

The following figures shows the impulse response functions of endogenous variables to the exogenous shock (\(\epsilon^{\mathrm{Z}}_{t}\))
gEcon Impulse Response Functions

The following output shows calibrated model results with statistics and moments. Since we don’t use observable data such as GDP, investment flow, and inflation rate, these output are based on calibration not estimation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 # print summary of the model results
> summary(rbc)
 
Steady state:
               
r      0.035101
C      0.742200
I      0.255921
K_s   10.236846
L_s    0.269467
U   136.237220
W      2.370598
Y      0.998121
Z      1.000000
 
 
Parameter values:
           
alpha 0.360
beta  0.990
delta 0.025
eta   2.000
mu    0.300
phi   0.950
 
 
Linearisation:
x_t = P x_{t1+ Q epsilon_t
y_t = R x_{t1+ S epsilon_t
 
P:
       K_s[1]    Z[1]
K_s[] 0.963137 0.096151
Z[]   0.000000 0.950000
 
Q:
    epsilon_Z
K_s  0.101212
Z    1.000000
 
R:
        K_s[1]    Z[1]
r[]   0.755858 1.352068
C[]    0.491944 0.492105
I[]   0.474511 3.846059
L_s[] 0.181028 0.628232
U[]    0.041796 0.064415
W[]    0.425170 0.723837
Y[]    0.244142 1.352068
 
S:
    epsilon_Z
r    1.423230
C    0.518005
I    4.048483
L_s  0.661297
U    0.067806
W    0.761933
Y    1.423230
 
cs

Besides the above output, shock covariance matrix, correlation matrix, autocorrelations, cross correlations are also produced (These are not reported).

Conclusion


This post deals with the calibration of a basic RBC model using gEcon R package. Authors of gEcon also provides the gEcon.estimation package which deals with the Bayesian estimation for DSGE. It will be interesting to learn how to do Bayesian estimation with these packages in R. \(\blacksquare\)

To leave a comment for the author, please follow the link and comment on their blog: K & L Fintech Modeling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)