This project makes available the Python version of Chris Sims (2001) gensys algorithm to broader community. I also show how the algorithm can be used to solve a New Keynesian model and further analyse the economy.
- The file REmodelToolkit.py includes the Python version of Chris Sims gensys algorithm.
- The file NKmodelSimulations.ipynb sets up a three equation New Keynesian model in the form that is consistent with the gensys alogorithm. It then uses the algorithm to solve the model.
You can either run the project on your machine using softwares like VS Studio, Anaconda etc. or on Google Colab.
Have both files in the same folder before running the script NKmodelSimulations.ipynb. Make sure that the toolkit file has exactly the same name i.e. REmodelToolkit.py. You do not need to run the cells under SET PATH FOR TOOLKIT IN GOOGLE DRIVE. Instead, start from IMPORT PYTHON LIBRARIES.
To run the NKmodelSimulations.ipynb file on Google Colab, follow these steps:
- Go to your google drive (online).
- Under My Drive, there will be a folder called Colab Notebooks. If not, create one.
- Upload both the files to this folder. Make sure that you upload the toolkit file with exactly the same name i.e. REmodelToolkit.py.
- Open the NKmodelSimulations.ipynb file.
- Connect and run the cells under SET PATH FOR TOOLKIT IN GOOGLE DRIVE.
# This line imports the necessary functionality to interact with your Google Drive from within the Colab notebook.
from google.colab import drive
# This line makes sure that your Google Drive files are accessible within the notebook
drive.mount('/content/drive')
# This command lists the files and directories inside the "Colab Notebooks" folder located in your Google Drive. You should see REmodelToolkit.py here.
!ls "/content/drive/My Drive/Colab Notebooks"
# These two lines ensure Python can find and import custom modules (e.g REmodelToolkit.py) located within your 'Colab Notebooks' folder in Google Drive, making them available to use in the notebook.
import sys
sys.path.append('/content/drive/My Drive/Colab Notebooks')
If you get an error, it could be because:
- You did not give all the permissions when you were prompted to do so.
- You uploaded the toolkit file with a different name than REmodelToolkit.py.
- You did not upload the toolkit file in the Colab Notebooks folder in My Drive.
Fix it. You are now good to go!
The function gensys
solves a linear rational expectations model of the form:
where
where:
G1_sol
is the transition matrix mapping past states to current states.C_vec
is the vector of constants.Impact
determines how shocks influence the system.
The solution also includes gev
which contains the generalised eigenvalues and eu
which reports whether the solution exits and, if it exists, whether it is unique.
The NKmodelSimulations.ipynb file uses the gensys algorithm to solve a standard three equation New Keynesian model of the form:
-
IS curve:
$x_{t} = \beta E_{t}x_{t+1} - \varphi(i_{t} - E_{t}\pi_{t+1} - \bar{r}) - a_{t}$ -
NKPC curve:
$\pi_{t} - \bar{\pi} = \beta E_{t}(\pi_{t+1} - \bar{\pi}) + \lambda x_{t} + u_{t}$ -
Taylor rule:
$i_{t} = \bar{r} + \bar{\pi} + \chi_{\pi}(\pi_{t} - \bar{\pi}) + \chi_{x}x_{t} + m_{t}$
where
-
cost shock:
$u_{t} = \rho_{u}u_{t-1} + \epsilon_{t}^{u}$ -
monetary shock:
$m_{t} = \rho_{m}m_{t-1} + \epsilon_{t}^{m}$ -
productivity shock:
$a_{t} = \rho_{a}a_{t-1} + \epsilon_{t}^{a}$
where
The NKmodelSimulations.ipynb file is divided into several sections. Arguably, the most critical is section 1. Recall, gensys requires the model to be in the following form:
Section 1 is where you setup the matrices that define your model i.e. G0, G1,
To transform the model, you should start with defining an auxiliary variable for each of the forward looking variables:
The transformed version of the model now has 8 equations: 3 for
-
Auxiliary variable 1:
$x_{t} = z_{t-1}^{x} + \eta_{t}^{x}$ -
Auxiliary variable 2:
$\pi_{t} = z_{t-1}^{\pi} + \eta_{t}^{\pi}$ -
IS curve:
$x_{t} + \varphi i_{t} + a_{t} - \beta z_{t}^{x} - \varphi z_{t}^{\pi} = \varphi\bar{r}$ -
NKPC curve:
$-\lambda x_{t} + \pi_{t} - u_{t} - \beta z_{t}^{\pi} = (1-\beta)\bar{\pi}$ -
Taylor rule:
$-\chi_{x}x_{t} - \chi_{\pi}\pi_{t} + i_{t} - m_{t} = \bar{r} + (1 - \chi_{pi})*\bar{\pi}$ -
Cost shock:
$u_{t} = \rho_{u}u_{t-1} + \epsilon_{t}^{u}$ -
Prod shock:
$a_{t} = \rho_{a}a_{t-1} + \epsilon_{t}^{a}$ -
Monetary shock:
$m_{t} = \rho_{m}m_{t-1} + \epsilon_{t}^{m}$
Note, the two equations for auxiliary variables also include expectation errors,
We can now write this system of equations in matrix form as is required by the gensys algorithm. Specifically,
where
Section 2 uses the gensys algorithm to solve the model. Section 3 explicitly reports the solution for each endogenous variable in the model. Section 4 reports the variance-covariance matrix. Section 5 plots impulse responses to each of the shock considered in the model. Section 6 simulates the model economy by generating a series of i.i.d. shocks for all three shocks considered here. The i.i.d. shocks are drawn from a normal distribution with mean zero and standard deviation,