In [ ]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import linearsolve as ls
%matplotlib inline
A representative household lives for an infinite number of periods. The expected present value of lifetime utility to the household from consuming $C_0, C_1, C_2, \ldots $ is denoted by $U_0$:
\begin{align} U_0 & = \log (C_0) + \beta E_0 \log (C_1) + \beta^2 E_0 \log (C_2) + \cdots\\ & = E_0\sum_{t = 0}^{\infty} \beta^t \log (C_t), \end{align}where $0<\beta<1$ is the household's subjective discount factor. $E_0$ denotes the expectation with respect to all information available as of date 0.
The household enters period 0 with capital $K_0>0$. Production in period $t$:
\begin{align} F(A_t,K_t) & = A_t K_t^{\alpha} \end{align}where TFP $A_t$ is stochastic:
\begin{align} \log A_{t+1} & = \rho \log A_t + \epsilon_{t+1} \end{align}Capital depreciates at the constant rate $\delta$ per period and so the household's resource constraint in each period $t$ is:
\begin{align} C_t + K_{t+1} & = A_t K_{t}^{\alpha} + (1-\delta)K_t \end{align}In period 0, the household solves:
\begin{align} & \max_{C_0,K_1} \; E_0\sum_{t=0}^{\infty}\beta^t\log (C_t) \\ & \; \; \; \; \; \; \; \; \text{s.t.} \; \; \; \; C_t + K_{t+1} = A_t K_{t}^{\alpha} + (1-\delta)K_t \end{align}which can be written as a choice of $K_1$ only:
\begin{align} \max_{K_1} \; E_0\sum_{t=0}^{\infty}\beta^t\log \left( A_t K_{t}^{\alpha} + (1-\delta)K_t - K_{t+1}\right) \end{align}So given $K_0>0$ and $A_0$, the equilibrium paths for consumption, capital, and TFP are described described by:
\begin{align} \frac{1}{C_t} & = \beta E_t \left[\frac{\alpha A_{t+1}K_{t+1}^{\alpha - 1} + 1 - \delta}{C_{t+1}}\right]\\ C_t + K_{t+1} & = A_{t} K_t^{\alpha} + (1-\delta) K_t\\ \log A_{t+1} & = \rho \log A_t + \epsilon_{t+1} \end{align}For computation purposes, assume the following values for the parameters of the model:
\begin{align} \beta & = 0.99\\ \rho & = .75\\ \sigma & = 0.006\\ \alpha & = 0.35\\ \delta & = 0.025 \end{align}The steady state:
\begin{align} A & = 1\\ K & = \left(\frac{\alpha A}{\beta^{-1} - 1 + \delta} \right)^{\frac{1}{1-\alpha}}\\ C & = AK^{\alpha} - \delta K \end{align}
In [ ]:
# 1. Input model parameters and print
In [ ]:
# 2. Compute the steady state of the model directly
In [ ]:
# 3. Define a function that evaluates the equilibrium conditions
def equilibrium_equations(variables_forward,variables_current,parameters):
# Parameters
p = parameters
# Variables
fwd = variables_forward
cur = variables_current
# Resource constraint
resource = cur.a*cur.k**p.alpha + (1-p.delta)* cur.k - fwd.k - cur.c
# Exogenous tfp
# Euler equation
# Stack equilibrium conditions into a numpy array
return np.array([
])
In [ ]:
# 4. Initialize the model
model = ls.model(equations = equilibrium_equations,
nstates=,
varNames=[], # Any order as long as the state variables are named first
shockNames=[], # Name a shock for each state variable *even if there is no corresponding shock in the model*
parameters = parameters)
In [ ]:
# 5. Set the steady state of the model directly.
model.set_ss([])
In [ ]:
# 6. Find the log-linear approximation around the non-stochastic steady state and solve
model.approximate_and_solve()
In [ ]:
# 7. Print the approximated model in terms of log-deviations from the stady state
print(model.approximated())
In [ ]:
# 8(a) Compute impulse responses and print the computed impulse responses
model.impulse(T=41,t0=5,shock=None)
print(model.irs['eA'].head(10))
In [ ]:
# 8(b) Plot the computed impulse responses to a TFP shock
fig = plt.figure(figsize=(12,4))
ax1 = fig.add_subplot(1,2,1)
model.irs['eA'][['a','k','c']].plot(lw='5',alpha=0.5,grid=True,ax = ax1).legend(loc='upper right',ncol=3)
ax2 = fig.add_subplot(1,2,2)
model.irs['eA'][['eA','a']].plot(lw='5',alpha=0.5,grid=True,ax = ax2).legend(loc='upper right',ncol=2)
In [ ]:
# 9(a) Compute stochastic simulation and print the simulated values
model.stoch_sim(seed=192,covMat= [[parameters['sigma']**2,0],[0,0]])
print(model.simulated.head(10))
In [ ]:
# 9(b) Plot the computed stochastic simulation
fig = plt.figure(figsize=(12,4))
ax1 = fig.add_subplot(1,2,1)
model.simulated[['a','c','k']].plot(lw=5,alpha=0.5,grid=True,ax = ax1).legend(loc='upper right',ncol=3)
ax2 = fig.add_subplot(1,2,2)
model.simulated[['eA','a']].plot(lw=5,alpha=0.5,grid=True,ax = ax2).legend(loc='upper right',ncol=2)
Recall the three equilibrium conditions fo the model: \begin{align} \frac{1}{C_t} & = \beta E_t \left[\frac{\alpha A_{t+1}K_{t+1}^{\alpha - 1} + 1 - \delta}{C_{t+1}}\right]\\ C_t + K_{t+1} & = A_{t} K_t^{\alpha} + (1-\delta) K_t\\ \log A_{t+1} & = \rho \log A_t + \epsilon_{t+1} \end{align} Append two more equations that determine output and investment: \begin{align} Y_t & = A_t K_t^{\alpha}\\ I_t & = K_{t+1} - (1-\delta)K_t \end{align} Recompute the model using the same parameters from above
In [ ]:
# 1. Compute the steady state values of Y and I
In [ ]:
# 2. Define a function that evaluates the equilibrium conditions
In [ ]:
# 3. Initialize the model
In [ ]:
# 4. Set the steady state of the model directly.
In [ ]:
# 5. Find the log-linear approximation around the non-stochastic steady state and solve
In [ ]:
# 6(a) Compute stochastic simulation and print the simulated values
model.stoch_sim(seed=192,covMat= [[parameters['sigma'],0],[0,0]])
print(model.simulated.head(10))
In [ ]:
# 6(b) Plot the computed stochastic simulation
In [ ]:
# Compute the standard deviations of A, Y, C, and I
In [ ]:
# Compute the coefficients of correlation for A, Y, C, and I