**Authors:** *François Le Grand* and *Xavier Ragot*

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.ipynb`

is the main notebook (the present one). It computes the steady-state and creates the file`todynare_Truncation.mat`

for simulating the model with aggregate shocks. It also contains links to the other notebooks.`Structures.ipynb`

contains the various structures that we use.`Utils.ipynb`

contains some useful functions (computing the Gini coefficient, etc.).`SolveAiyagari.ipynb`

solves for the Aiyagari model.`Truncation.ipynb`

computes the truncated model.

The aggregate shock part relies on three files.

`Dynare_Truncation.m`

is 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-stoch-simul.m`

and`post-dynare-perfect-foresight.m`

are 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`@unpack`

;`NBInclude`

: to include packages;`QuantEcon`

: to use the function`rouwenhorst`

to discretize the AR(1) package;`LinearAlgebra`

: to use the `dot function (inner product);`Roots`

: to use the`findzero`

function (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.

In [1]:

```
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):

`Structures.ipynb`

contains the various structures that we use.`Utils.ipynb`

contains some useful functions (computing the Gini coefficient, etc.).`SolveAiyagari.ipynb`

solves for the Aiyagari model.`Truncation.ipynb`

computes the truncated model.

In [2]:

```
@nbinclude("Structures.ipynb");
@nbinclude("Utils.ipynb");
@nbinclude("SolveAiyagari.ipynb");
@nbinclude("Truncation.ipynb");
```

We use the structure `Params`

of the notebook `Parameters`

and a special constructor that provides a calibration device.

For further details on the mechanism of the calibration, please refer to the notebook `Parameters`

.

In [3]:

```
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
);
```

The function `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 `Rmin`

and `Rmax`

parameters are optional bounds for the dichotomy algorithm.
For further details on:

- the structures, refer to the notebook
`Structures`

; - the Aiyagari solution, refer to the notebook
`SolveAiyagari`

.

In [4]:

```
solution = steady(params,Rmin=1.0086444151,Rmax=1.0086444153);
```

Markets clear! in: 1 iterations

We check the consistency of `solution`

(using `check_solution`

) and provide a description of `solution`

(using `describe_solution`

). These two functions are defined in the notebook `SolveAiyagari`

.

In [5]:

```
@assert check_solution(solution, params)
dc,_ = describe_solution(solution,params)
for (k,v) in dc
println(k,": ", v)
end
```

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 `Truncation`

.

In [6]:

```
# 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.