This source simulates a linear ratinal expectation model as following,
$$ E \mathbb{E}t x{t+1} = A x_{t} + B u_{t}, \ \ x_{t} \in \mathbb{R}^{n}, \ \ u_{t} \in \mathbb{R}^{m}. $$ $E$ need not be a reversible matrix. $x_{t} = (x^{1}{t}, \ x^{2}{t})^{t}$ and $x^{1}{t}$ is the predetermined vector, $x^{2}{t}$ is the un-predetermined vector. There must exist some z such that $\det(E z - A) \neq 0$.
We transform this system into the homogeneous system when the shock follows the AR process $u_{t + 1} = \Phi u_{t} + \epsilon_{t + 1}$
$$ \begin{bmatrix} I & O \ O & E \end{bmatrix} \mathbb{E}t \begin{bmatrix} u{t + 1} \ x_{t + 1} \end{bmatrix} = \begin{bmatrix} \Phi & O \ B & A \end{bmatrix} \begin{bmatrix} u_{t} \ x_{t} \end{bmatrix} $$
We replicate the example in https://cdn.rawgit.com/rokko-ed17q1/final/64730073/lrem-hansen-rbc.html.
The parameters are
alpha = 0.33 beta = 0.99 delta = 0.023 chi = 1.75 rho = 0.95
q0 = (1 - beta + beta * delta) / alpha / beta q1 = q0 ^ (1 / (1 - alpha)) q2 = q0 - delta kbar = (1 - alpha) * q1 ^ (- alpha) kbar = kbar / ((1 - alpha) * q0 + chi * q2) cbar = q2 * kbar nbar = q1 * kbar zbar = 1
The matrices are
E = matrix(0, 3, 3) A = matrix(0, 3, 3) B = matrix(0, 3, 1) Phi = matrix(rho, 1, 1) E[1, 1] = alpha * (alpha - 1) * q0 E[1, 2] = alpha * q0 E[1, 3] = - (1 - delta + alpha * q0) E[2, 1] = 1 A[1, 3] = E[1, 3] A[2, 1] = - A[1, 3] A[2, 2] = (1 - alpha) * q0 A[2, 3] = - q2 A[3, 1] = alpha A[3, 2] = (- alpha - (1 - alpha) * nbar) / (1 - nbar) A[3, 3] = -1 B[1, 1] = - alpha * q0 * rho B[2, 1] = q0 B[3, 1] = 1
The simulation is following
library("lrem") library("ggplot2")
policy <- lre_ar(A, E, B, Phi, 0, 1) # Simulation period steps <- 100 # Path of the growth rates out <- simulate(policy$g, policy$h, 0, 1, steps) out0 <- matrix(0, nrow = 1, ncol = ncol(out)) out <- rbind(out0, out) # Steady State ss <- c(zbar, kbar, nbar, cbar) # Convert percentage changes to levels for (i in 1:ncol(out)) { out[, i] <- ss[i] * out[, i] + ss[i] }
result <- data.frame(out) names(result) <- c("Z", "K", "N", "C") result["t"] <- 0:(steps)
# Plotting ggplot(result) + geom_line(aes(x = t, y = Z))
ggplot(result) + geom_line(aes(x = t, y = K))
ggplot(result) + geom_line(aes(x = t, y = N))
ggplot(result) + geom_line(aes(x = t, y = C))
We simulate the following New Keynesian model.
Phillips curve
$$ \pi_{t} = \beta \mathbb{E}{t} \pi{t + 1} + \kappa x_{t} + u^{S}_{t} $$
IS curve
$$ x_{t} = \mathbb{E}{t} x{t + 1} - \frac{1}{\sigma} (i_{t} - \mathbb{E}{t} \pi{t + 1}) + u^{D}_{t} $$
Monetary policy rule
$$ i_{t} = \alpha \pi_{t} + \iota x_{t} $$
Assume AR shock with $|\rho_{S}|, |\rho_{D}| < 1$ and $\mathbb{E}{t} \epsilon^{S}{t + 1} = \mathbb{E}{t} \epsilon^{D}{t + 1}$;
$$ u^{S}{t + 1} = \rho{S} u^{S}{t} + \epsilon^{S}{t + 1} \ u^{D}{t + 1} = \rho{D} u^{D}{t} + \epsilon^{D}{t + 1} $$
Rewriting this system into the above matrix form, we obtain
$$ \begin{bmatrix} I & O \ O & E \end{bmatrix} \mathbb{E}t \begin{bmatrix} u{t + 1} \ \pi_{t + 1} \ x_{t + 1} \ i_{t + 1} \end{bmatrix} = \begin{bmatrix} \Phi & O \ B & A \end{bmatrix} \begin{bmatrix} u_{t} \ \pi_{t} \ x_{t} \ i_{t} \end{bmatrix} $$ where
$$ A = \begin{bmatrix} 1 & - \kappa & 0 \ 0 & 1 & 1 / \sigma \ \alpha & \iota & - 1 \end{bmatrix}, \ E = \begin{bmatrix} \beta & 0 & 0 \ 1 / \sigma & 1 & 0 \ 0 & 0 & 0 \end{bmatrix}, \ B = \begin{bmatrix} - 1 & 0 \ 0 & - 1 \ 0 & 0 \end{bmatrix}, \ \Phi = \begin{bmatrix} \rho_{S} & 0 \ 0 & \rho_{D} \end{bmatrix}, \ u_{t} = \begin{bmatrix} u^{S}{t} \ u^{D}{t} \end{bmatrix}. $$
The parameters are
alpha <- 2 iota <- 0 beta <- 0.99 sigma <- 1 kappa <- 0.132 rhoS <- 0.9 rhoD <- 0.9
The matrices are
A <- matrix(c(1, - kappa, 0, 0, 1, 1 / sigma, alpha, iota, - 1), nrow = 3, byrow = TRUE) E <- matrix(c(beta, 0, 0, 1 / sigma, 1, 0, 0, 0, 0), nrow = 3, byrow = TRUE) B <- matrix(c(- 1, 0, 0, - 1, 0, 0), nrow = 3, byrow = TRUE) Phi <- matrix(c(rhoS, 0, 0, rhoD), nrow = 2, byrow = TRUE)
The values of these variables at the steady state are
rhoSbar <- 0 rhoDbar <- 0 pibar <- 0 xbar <- 0 ibar <- 0 ss <- c(rhoSbar, rhoDbar, pibar, xbar, ibar)
That's all of preparation of simulation.
# Initial shock is (us, ud) = (1, 1) policy <- lre_ar(A, E, B, Phi, NULL, c(1, 1)) # Simulation period steps <- 100 out <- simulate(policy$g, policy$h, NULL, c(1, 1), steps) out0 <- matrix(ss, nrow = 1, ncol = ncol(out)) out <- rbind(out0, out)
result <- data.frame(out) names(result) <- c("rhoS", "rhoD", "pi", "x", "i") result["t"] <- 0:(steps)
# Plotting ggplot(result) + geom_line(aes(x = t, y = rhoS))
ggplot(result) + geom_line(aes(x = t, y = rhoD))
ggplot(result) + geom_line(aes(x = t, y = pi))
ggplot(result) + geom_line(aes(x = t, y = x))
ggplot(result) + geom_line(aes(x = t, y = i))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.