This page presents the implementation and the codes for the paper Optimal Policies with Heterogeneous Agents: Truncation and Transitions. The codes are in Julia (for the steady state) and Matlab and Dynare (for the simulations in the presence of aggregate shocks).
The steady-state part in Julia relies on five notebooks (alll notebooks can be clicked). This part must be run first since it creates a mat-file called
todynare_Truncation.mat that is required to launch the Dynare solver.
Main.ipynbis the main notebook (the present one). It computes the steady-state and creates the file
todynare_Truncation.matfor simulating the model with aggregate shocks. It also contains links to the other notebooks.
Structures.ipynbcontains the various structures that we use.
Utils.ipynbcontains some useful functions (computing the Gini coefficient, etc.).
SolveAiyagari.ipynbsolves for the Aiyagari model.
Truncation.ipynbcomputes the truncated model.
The aggregate shock part relies on three files.
Dynare_Truncation.mis the main file for aggregate shocks. It writes a Dynare mod file (called
DyTruncation.mod) and runs it. This file computes IRFs and second-order moments. It can also simulate a transition path (see the file for further explanation).
post-dynare-perfect-foresight.mare two files for data post-processing. These files are launched automatically and not meant to be launched on their own.
We now proceed with the
Main file. We start with packager imports.
We include a number of packages:
Parameters: to use
NBInclude: to include packages;
QuantEcon: to use the function
rouwenhorstto discretize the AR(1) package;
LinearAlgebra: to use the `dot function (inner product);
Roots: to use the
findzerofunction (root search);
SparseArrays: to manipulate sparse vectors and sparrse matrices;
IterativeSolvers: to use the function
powm!(find the largest eigenvalue of a matrix);
Plots: to draw plots;
MAT: to save file under he Matlab .mat format (needed for the interaction with Dynare);
FileIO: loading and saving files;
JLD2: to manipulate JDL2 file formats.
using Parameters # @unpack using NBInclude # including notebooks instead of jl files using QuantEcon # rouwenhorst using LinearAlgebra # dot using Roots # findzero using SparseArrays # SparseMatrixCSC using IterativeSolvers # powm! using Plots # Plots using MAT # matopen (interaction with Dynare) using FileIO # saving file using JLD2 # manipulating JDL2 file format
We include the following notebooks (their names are clickable):
@nbinclude("Structures.ipynb"); @nbinclude("Utils.ipynb"); @nbinclude("SolveAiyagari.ipynb"); @nbinclude("Truncation.ipynb");
params = Params( KsY=10.26, α=0.36, δ=0.025, γ=1.0001, θ = 1.0,# to be changed endogenously τ=0.082, #it is public consumption over GDP na=100, a_min=1e-9, a_max=1000.0, curv_a=4.0, ny=5, ρy=0.9962034337990648, σy=0.04383761259237633 );
steady takes an input a set of economy parameters
params (as a
Params structure) and returns the solution of the Aiyagari model (as an
AiyagariSolution structure). The
Rmax parameters are optional bounds for the dichotomy algorithm.
For further details on:
solution = steady(params,Rmin=1.0086444151,Rmax=1.0086444153);
Markets clear! in: 1 iterations
We check the consistency of
check_solution) and provide a description of
describe_solution). These two functions are defined in the notebook
@assert check_solution(solution, params) dc,_ = describe_solution(solution,params) for (k,v) in dc println(k,": ", v) end
Share of constrained agents: 0.2046123793950992 K/Y: 2.6750353502949253 C/Y: 0.6524111963063527 Gini: 0.708524565574431 G/Y: 0.08008526866415488 B/Y: 1.4461024087501642e-8 Transfers/Y: -0.08008526916609163 L: 1.0 MPC: 0.22000364785462587 I/Y: 0.2675035350294925
We set the truncation length to 2 and compute the truncated model using the function
Project_plan. We then check that the truncated model is consistent using the function
check_solution_trunc. Both functions are defined in the notebook
# Setting the truncation length N = 2 # Computing the truncated model trunc = Truncate(N,solution,params); # Checking the consistency of the projected model @assert check_truncated_model(trunc,solution,params) # Writing the results in the file todynare_Truncation.mat for running Dynare Write_Dynare(trunc,solution,params)
The model needs then to be run on Dynare.