In [ ]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm
%matplotlib inline
Consider the following business cycle model: \begin{align} Y_t & = A_t K_t^{\alpha} \tag{1}\\ C_t & = (1-s)Y_t \tag{2}\\ I_t & = K_{t+1} - ( 1- \delta) \tag{3}\\ Y_t & = C_t + I_t \tag{4} \end{align} where: \begin{align} \log A_{t+1} & = \rho \log A_t + \epsilon_t, \tag{5} \end{align} reflects exogenous fluctuation in TFP. The endogenous variables in the model are $K_t$, $Y_t$, $C_t$, $I_t$, and $A_t$ and $\epsilon_t$ is an exogenous white noise shock process with standard deviation $\sigma$. $K_t$ and $A_t$ are called state variables because their values in period $t$ affect the equilibrium of the model in period $t+1$.
In the (non-stochastic) steady state: \begin{align} \epsilon_t & = 0 \end{align} and \begin{align} K_t & = 0 \end{align} for all $t$. So we drop the $t$ subscripts and write the steady state solution solution to the model as: \begin{align} A & = 1\\ K & = \left(\frac{sA}{\delta}\right)^{\frac{1}{1-\alpha}}\\ Y & = AK^{\alpha}\\ I & = Y - C \end{align}
In [ ]:
# Define parameters
s = 0.1
delta = 0.025
alpha = 0.35
# Compute the steady state values of the endogenous variables
Kss = (s/delta)**(1/(1-alpha))
Yss = Kss**alpha
Css = (1-s)*Yss
Iss = Yss - Css
print('Steady states:\n')
print('capital: ',round(Kss,5))
print('output: ',round(Yss,5))
print('consumption:',round(Css,5))
print('investment: ',round(Iss,5))
Now, you will simulate how the model behaves in the presence of a set of random TFP shocks. The simulation will run for $T+1$ periods from $t = 0,\ldots, T$. Suppose that $T = 100$.
Initialize an array for $\epsilon_t$ called eps
that is equal to a $T\times 1$ array of normally distributed random variables with mean 0 and standard deviation $\sigma = 0.006$. Set the seed for the Numpy random number generator to 192. Plot $\epsilon_t$.
Initialize an array for $\log A_t$ called logA
that is equal to a $(T+1)\times 1$ array of zeros. Set $\rho = 0.75$ and use the simulated values for $\epsilon_t$ to compute $\log A_1, \log A_2, \ldots, \log A_T$. Plot $\log A_t$.
Create a new variable called A
that stores simulated values of $A_t$ (Note: $A_t = e^{\log A_t}$). Plot $A_t$.
Initialize an array for $K_t$ called K
that is a $(T+1)\times 1$ array of zeros. Set the first value in the array equal to steady state capital. Then compute the subsequent values for $K_t$ using the computed values for $A_t$. Plot $K_t$.
Create variables called Y
, C
, and I
that store simulated values for $Y_t$, $C_t$, and $I_t$.
Construct a $2\times2$ grid of subplots of the simulated paths of capital, output, consumption, and investment.
Compute the log deviation of each variable from its steady state ($(X_t - X_{ss})/X_{ss}$) and store the results in variables called: k_dev
, y_dev
, c_dev
, and i_dev
.
Construct a $2\times2$ grid of subplots of the impulse responses of capital, output, consumption, and investment to the technology shock with each variable expressed as a deviation from steady state.
Save the simulated data in a DataFrame called data_simulated
with columns output
, consumption
, investment
, and tfp
.
In [ ]:
# Step 1: simulate eps
In [ ]:
# Step 2: simulate and plot log(TFP) logA
In [ ]:
# Step 3: compute and plot TFP A
In [ ]:
# Step 4: Compulte and plot capital K
In [ ]:
# Step 5: Compute Y, C, and I
In [ ]:
# Step 6: Create a 2x2 plot of y, c, i, and k
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(2,2,1)
ax.plot(Y,lw=3,alpha = 0.7)
ax.set_title('$Y_t$')
ax.grid()
plt.tight_layout()
In [ ]:
# Step 7: Compute y_dev, c_dev, i_dev, and k_dev to be the log deviations from steady state of the
# respective variables
y_dev = np.log(Y) - np.log(Yss)
In [ ]:
# Step 8: Create a 2x2 plot of y_dev, c_dev, i_dev, and k_dev
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(2,2,1)
ax.plot(100*y_dev,lw=3,alpha = 0.7)
ax.set_title('$\hat{y}_t$')
ax.grid()
ax.set_ylabel('% dev from steady state')
plt.tight_layout()
In [ ]:
# Step 9: Save the simulated data in a DataFrame called data_simulated
In [ ]:
# Create a DataFrame with actual cyclical components of output, consumption, investment, and TFP
data_actual = pd.read_csv('http://www.briancjenkins.com/teaching/winter2017/econ129/data/Econ129_Rbc_Data.csv',index_col=0)
data_actual = pd.DataFrame({'output':np.log(data_actual.gdp/data_actual.gdp_trend),
'consumption':np.log(data_actual.consumption/data_actual.consumption_trend),
'investment':np.log(data_actual.investment/data_actual.investment_trend),
'tfp':np.log(data_actual.tfp/data_actual.tfp_trend)})
data_actual.head()
In [ ]:
# Compute the standard deviations of the actual business cycle data
print(data_actual.std())
In [ ]:
# Compute the standard deviations of the simulated business cycle data
In [ ]:
# Compute the coeficients of correlation for the actual business cycle data
print(data_actual.corr())
In [ ]:
# Compute the coeficients of correlation for the actual business cycle data