Code should execute sequentially if run in a Jupyter notebook

- See the set up page to install Jupyter, Julia and all necessary libraries
- Please direct feedback to contact@quantecon.org or the discourse forum

# The Aiyagari Model¶

## Overview¶

In this lecture we describe the structure of a class of models that build on work by Truman Bewley [Bew77]

We begin by discussing an example of a Bewley model due to `Rao Aiyagari`

The model features

- Heterogeneous agents
- A single exogenous vehicle for borrowing and lending
- Limits on amounts individual agents may borrow

The Aiyagari model has been used to investigate many topics, including

## The Economy¶

### Households¶

Infinitely lived households / consumers face idiosyncratic income shocks

A unit interval of *ex ante* identical households face a common borrowing constraint

The savings problem faced by a typical household is

subject to

where

- \(c_t\) is current consumption
- \(a_t\) is assets
- \(z_t\) is an exogenous component of labor income capturing stochastic unemployment risk, etc.
- \(w\) is a wage rate
- \(r\) is a net interest rate
- \(B\) is the maximum amount that the agent is allowed to borrow

The exogenous process \(\{z_t\}\) follows a finite state Markov chain with given stochastic matrix \(P\)

The wage and interest rate are fixed over time

In this simple version of the model, households supply labor inelastically because they do not value leisure

## Firms¶

Firms produce output by hiring capital and labor

Firms act competitively and face constant returns to scale

Since returns to scale are constant the number of firms does not matter

Hence we can consider a single (but nonetheless competitive) representative firm

The firm’s output is

where

- \(A\) and \(\alpha\) are parameters with \(A > 0\) and \(\alpha \in (0, 1)\)
- \(K_t\) is aggregate capital
- \(N\) is total labor supply (which is constant in this simple version of the model)

The firm’s problem is

The parameter \(\delta\) is the depreciation rate

From the first-order condition with respect to capital, the firm’s inverse demand for capital is

Using this expression and the firm’s first-order condition for labor, we can pin down the equilibrium wage rate as a function of \(r\) as

### Equilibrium¶

We construct a *stationary rational expectations equilibrium* (SREE)

In such an equilibrium

- prices induce behavior that generates aggregate quantities consistent with the prices
- aggregate quantities and prices are constant over time

In more detail, an SREE lists a set of prices, savings and production policies such that

- households want to choose the specified savings policies taking the prices as given
- firms maximize profits taking the same prices as given
- the resulting aggregate quantities are consistent with the prices; in particular, the demand for capital equals the supply
- aggregate quantities (defined as cross-sectional averages) are constant

In practice, once parameter values are set, we can check for an SREE by the following steps

- pick a proposed quantity \(K\) for aggregate capital
- determine corresponding prices, with interest rate \(r\) determined by (1) and a wage rate \(w(r)\) as given in (2)
- determine the common optimal savings policy of the households given these prices
- compute aggregate capital as the mean of steady state capital given this savings policy

If this final quantity agrees with \(K\) then we have a SREE

## Code¶

Let’s look at how we might compute such an equilibrium in practice

To solve the household’s dynamic programming problem we’ll use the DiscreteDP type from QuantEcon.jl

Our first task is the least exciting one: write code that maps parameters for a household problem into the `R`

and `Q`

matrices needed to generate an instance of `DiscreteDP`

Below is a piece of boilerplate code that does just this

It comes from the file aiyagari_household.jl

In reading the code, the following information will be helpful

`R`

needs to be a matrix where`R[s, a]`

is the reward at state`s`

under action`a`

`Q`

needs to be a three dimensional array where`Q[s, a, s']`

is the probability of transitioning to state`s'`

when the current state is`s`

and the current action is`a`

(For a detailed discussion of `DiscreteDP`

see this lecture)

Here we take the state to be \(s_t := (a_t, z_t)\), where \(a_t\) is assets and \(z_t\) is the shock

The action is the choice of next period asset level \(a_{t+1}\)

The type also includes a default set of parameters that we’ll adopt unless otherwise specified

```
#=
Filename: aiyagari_household.jl
Author: Victoria Gregory
Date: 8/29/2016
This file defines the Household type (and its constructor)
for setting up an Aiyagari household problem.
=#
using QuantEcon
"""
Stores all the parameters that define the household's
problem.
##### Fields
- `r::Float64` : interest rate
- `w::Float64` : wage
- `beta::Float64` : discount factor
- `z_chain::MarkovChain` : MarkovChain for income
- `a_min::Float64` : minimum on asset grid
- `a_max::Float64` : maximum on asset grid
- `a_size::Int64` : number of points on asset grid
- `z_size::Int64` : number of points on income grid
- `n::Int64` : number of points in state space: (a, z)
- `s_vals::Array{Float64}` : stores all the possible (a, z) combinations
- `s_i_vals::Array{Int64}` : stores indices of all the possible (a, z) combinations
- `R::Array{Float64}` : reward array
- `Q::Array{Float64}` : transition probability array
"""
type Household
r::Float64
w::Float64
beta::Float64
z_chain::MarkovChain{Float64,Array{Float64,2},Array{Float64,1}}
a_min::Float64
a_max::Float64
a_size::Int64
a_vals::Vector{Float64}
z_size::Int64
n::Int64
s_vals::Array{Float64}
s_i_vals::Array{Int64}
R::Array{Float64}
Q::Array{Float64}
end
"""
Constructor for `Household`
##### Arguments
- `r::Float64(0.01)` : interest rate
- `w::Float64(1.0)` : wage
- `beta::Float64(0.96)` : discount factor
- `z_chain::MarkovChain` : MarkovChain for income
- `a_min::Float64(1e-10)` : minimum on asset grid
- `a_max::Float64(18.0)` : maximum on asset grid
- `a_size::Int64(200)` : number of points on asset grid
"""
function Household(;r::Float64=0.01, w::Float64=1.0, beta::Float64=0.96,
z_chain::MarkovChain{Float64,Array{Float64,2},Array{Float64,1}}
=MarkovChain([0.9 0.1; 0.1 0.9], [0.1; 1.0]), a_min::Float64=1e-10,
a_max::Float64=18.0, a_size::Int64=200)
# set up grids
a_vals = linspace(a_min, a_max, a_size)
z_size = length(z_chain.state_values)
n = a_size*z_size
s_vals = gridmake(a_vals, z_chain.state_values)
s_i_vals = gridmake(1:a_size, 1:z_size)
# set up Q
Q = zeros(Float64, n, a_size, n)
for next_s_i in 1:n
for a_i in 1:a_size
for s_i in 1:n
z_i = s_i_vals[s_i, 2]
next_z_i = s_i_vals[next_s_i, 2]
next_a_i = s_i_vals[next_s_i, 1]
if next_a_i == a_i
Q[s_i, a_i, next_s_i] = z_chain.p[z_i, next_z_i]
end
end
end
end
# placeholder for R
R = fill(-Inf, n, a_size)
h = Household(r, w, beta, z_chain, a_min, a_max, a_size,
a_vals, z_size, n, s_vals, s_i_vals, R, Q)
setup_R!(h, r, w)
return h
end
"""
Update the reward array of a Household object, given
a new interest rate and wage.
##### Arguments
- `h::Household` : instance of Household type
- `r::Float64(0.01)` : interest rate
- `w::Float64(1.0)` : wage
"""
function setup_R!(h::Household, r::Float64, w::Float64)
# set up R
R = h.R
for new_a_i in 1:h.a_size
a_new = h.a_vals[new_a_i]
for s_i in 1:h.n
a = h.s_vals[s_i, 1]
z = h.s_vals[s_i, 2]
c = w * z + (1 + r) * a - a_new
if c > 0
R[s_i, new_a_i] = log(c)
end
end
end
h.r = r
h.w = w
h.R = R
h
end
```

In the following examples our import statements assume that this code is stored as `aiyagari_household.jl`

in the present working directory

(Or you can copy it into a Jupyter notebook cell and delete the corresponding import statement)

As a first example of what we can do, let’s compute and plot an optimal accumulation policy at fixed prices

```
#=
Filename: aiyagari_compute_policy.jl
Author: Victoria Gregory
Date: 8/29/2016
Computes and plots the optimal policy of a household
from the Aiyagari model, given prices.
=#
using LaTeXStrings
using Plots
pyplot()
# Example prices
r = 0.03
w = 0.956
# Create an instance of Household
am = Household(a_max=20.0, r=r, w=w)
# Use the instance to build a discrete dynamic program
am_ddp = DiscreteDP(am.R, am.Q, am.beta)
# Solve using policy function iteration
results = solve(am_ddp, PFI)
# Simplify names
z_size, a_size = am.z_size, am.a_size
z_vals, a_vals = am.z_chain.state_values, am.a_vals
n = am.n
# Get all optimal actions across the set of
# a indices with z fixed in each column
a_star = Array{Float64}(a_size, z_size)
s_i_vals = gridmake(1:a_size, 1:z_size)
for s_i in 1:n
a_i = s_i_vals[s_i, 1]
z_i = s_i_vals[s_i, 2]
a_star[a_i, z_i] = a_vals[results.sigma[s_i]]
end
labels = [string(L"$z = $", z_vals[1]); string(L"$z = $", z_vals[2])]
plot(a_vals, a_star, label=reshape(labels,1,length(labels)), lw=2, alpha=0.6)
plot!(a_vals, a_vals, label="", color=:black, linestyle=:dash)
plot!(xlabel="current assets", ylabel="next period assets", grid=false)
```

Here’s the output

The plot shows asset accumulation policies at different values of the exogenous state

Now we want to calculate the equilibrium

Let’s do this visually as a first pass

The following code draws aggregate supply and demand curves

The intersection gives equilibrium interest rates and capital

```
#=
Filename: aiyagari_compute_equilibrium.jl
Author: Victoria Gregory
Date: 8/30/2016
Draws the aggregate supply and demand curves for
the Aiyagari model.
=#
# Firms' parameters
A = 1
N = 1
alpha = 0.33
beta = 0.96
delta = 0.05
"""
Compute wage rate given an interest rate, r
"""
function r_to_w(r::Float64)
return A * (1 - alpha) * (A * alpha / (r + delta)) ^ (alpha / (1 - alpha))
end
"""
Inverse demand curve for capital. The interest rate
associated with a given demand for capital K.
"""
function rd(K::Float64)
return A * alpha * (N / K) ^ (1 - alpha) - delta
end
"""
Map prices to the induced level of capital stock.
##### Arguments
- `am::Household` : Household instance for problem we want to solve
- `r::Float64` : interest rate
##### Returns
- The implied level of aggregate capital
"""
function prices_to_capital_stock(am::Household, r::Float64)
# Set up problem
w = r_to_w(r)
setup_R!(am, r, w)
aiyagari_ddp = DiscreteDP(am.R, am.Q, am.beta)
# Compute the optimal policy
results = solve(aiyagari_ddp, PFI)
# Compute the stationary distribution
stationary_probs = stationary_distributions(results.mc)[:, 1][1]
# Return K
return sum(am.s_vals[:, 1] .* stationary_probs)
end
# Create an instance of Household
z_chain = MarkovChain([0.67 0.33; 0.33 0.67], [0.5, 1.5])
am = Household(z_chain=z_chain, beta=beta, a_max=20.0)
# Create a grid of r values at which to compute demand and supply of capital
num_points = 20
r_vals = linspace(0.02, 1/beta - 1, num_points)
# Compute supply of capital
k_vals = Array{Float64}(num_points, 1)
for i in 1:num_points
k_vals[i] = prices_to_capital_stock(am, r_vals[i])
end
# Plot against demand for capital by firms
demand = [rd(k) for k in k_vals]
labels = ["demand for capital"; "supply of capital"]
plot(k_vals, [demand r_vals], label=labels', lw=2, alpha=0.6)
plot!(xlabel="capital", ylabel="interest rate")
```

Here’s the corresponding plot