Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll...

30
Online Appendix: Numerical Methods for “Income and Wealth Distribution in Macroeconomics: A Continuous-Time Approach” 1 Yves Achdou, Jiequn Han, Jean-Michel Lasry, Pierre-Louis-Lions, Benjamin Moll This is an online Appendix to Achdou et al. (2017). It is concerned with the numerical solution using a finite difference method of the continuous time heterogeneous agent models presented in that paper. Also see the discussion in Section 3 of the paper, particulary the material on the conditions of Barles and Souganidis (1991). Huggett Economy We start by solving a continuous time version of Huggett (1993) which is arguably the simplest heterogeneous agent model that captures many of the features of richer models. The economy can be represented by the following system of equations which we aim to solve numerically: ρv 1 (a) = max c u(c)+ v 0 1 (a)(z 1 + ra - c)+ λ 1 (v 2 (a) - v 1 (a)) (1) ρv 2 (a) = max c u(c)+ v 0 2 (a)(z 2 + ra - c)+ λ 2 (v 1 (a) - v 2 (a)) (2) 0= - d da [s 1 (a)g 1 (a)] - λ 1 g 1 (a)+ λ 2 g 2 (a) (3) 0= - d da [s 2 (a)g 2 (a)] - λ 2 g 2 (a)+ λ 1 g 1 (a) (4) 1= Z a g 1 (a)da + Z a g 2 (a)da (5) 0= Z a ag 1 (a)da + Z a ag 2 (a)da S (r) (6) where z 1 <z 2 and s j (a)= z j + ra - c j (a) and c j (a)=(u 0 ) -1 (v j (a)) are optimal savings and consumption. Finally, there is a state constraint a a. The first order condition u 0 (c j ( a)) = v 0 j ( a) still holds at the borrowing constraint. However, in order to respect the constraint we need s j ( a)= z j + ra - c j ( a) 0. Combining this with the FOC, the state constraint motivates a boundary condition v 0 j ( a) u 0 (z j + r a), j =1, 2 (7) 1 We thank SeHyoun Ahn for fantastic research assistance. Matthieu Gomez provided extremely useful comments and in particular showed us how to clearly think about non-uniform grids in Section 7. 1

Transcript of Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll...

Page 1: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Online Appendix: Numerical Methods for

“Income and Wealth Distribution in Macroeconomics:

A Continuous-Time Approach”1

Yves Achdou, Jiequn Han, Jean-Michel Lasry, Pierre-Louis-Lions, Benjamin Moll

This is an online Appendix to Achdou et al. (2017). It is concerned with the numerical solution

using a finite difference method of the continuous time heterogeneous agent models presented

in that paper. Also see the discussion in Section 3 of the paper, particulary the material on

the conditions of Barles and Souganidis (1991).

Huggett Economy

We start by solving a continuous time version of Huggett (1993) which is arguably the simplest

heterogeneous agent model that captures many of the features of richer models. The economy

can be represented by the following system of equations which we aim to solve numerically:

ρv1(a) = maxc

u(c) + v′1(a)(z1 + ra− c) + λ1(v2(a)− v1(a)) (1)

ρv2(a) = maxc

u(c) + v′2(a)(z2 + ra− c) + λ2(v1(a)− v2(a)) (2)

0 = − d

da[s1(a)g1(a)]− λ1g1(a) + λ2g2(a) (3)

0 = − d

da[s2(a)g2(a)]− λ2g2(a) + λ1g1(a) (4)

1 =

∫ ∞a

g1(a)da+

∫ ∞a

g2(a)da (5)

0 =

∫ ∞a

ag1(a)da+

∫ ∞a

ag2(a)da ≡ S(r) (6)

where z1 < z2 and sj(a) = zj + ra − cj(a) and cj(a) = (u′)−1(vj(a)) are optimal savings and

consumption. Finally, there is a state constraint a ≥ a. The first order condition u′(cj(a)) =

v′j(a) still holds at the borrowing constraint. However, in order to respect the constraint we

need sj(a) = zj + ra− cj(a) ≥ 0. Combining this with the FOC, the state constraint motivates

a boundary condition

v′j(a) ≥ u′(zj + ra), j = 1, 2 (7)

1We thank SeHyoun Ahn for fantastic research assistance. Matthieu Gomez provided extremely usefulcomments and in particular showed us how to clearly think about non-uniform grids in Section 7.

1

Page 2: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

We use a finite difference method. A useful reference is Candler (1999). We first explain

how to solve the Hamilton-Jacobi-Bellman (HJB) equation (1) and (2), and then turn to the

Kolmogorov Forward (Fokker-Planck) equation (3) and (4).

Section 4 explains how the setup and the solution method can be generalized to an en-

vironment where productivity z is continuous and follows a diffusion rather than a two-state

Poisson process. Finally, a useful “warm-up problem” is to solve the HJB equation with no un-

certainty, λj = 0. See http://www.princeton.edu/~moll/HACTproject/HACT_Additional_

Codes.pdf. All algorithms are available as matlab codes from http://www.princeton.edu/

~moll/HACTproject.htm. We are especially indebted to SeHyoun Ahn for showing us how to

use matlab’s sparse matrix routines to increase speed by an order of magnitude.

1 HJB Equation

We use a finite difference method and approximate the functions (v1, v2) at I discrete points

in the space dimension, ai, i = 1, ..., I. We use equispaced grids, denote by ∆a the distance

between grid points, and use the short-hand notation vi,j ≡ vj(ai) and so on. The derivative

v′i,j = v′j(ai) is approximated with either a forward or a backward difference approximation

v′j(ai) ≈vi+1,j − vi,j

∆a≡ v′i,j,F

v′j(ai) ≈vi−1,j − vi,j

∆a≡ v′i,j,B

(8)

The finite difference approximation to (1) and (2) is

ρvi,j = u(ci,j) + v′i,j(zj + rai − ci,j) + λj(vi,−j − vi,j), j = 1, 2

ci,j = (u′)−1(v′i,j)(9)

where v′i,j is either the forward or the backward difference approximation. There are two

complications. The first question is when to use a forward and when a backward difference

approximation. It turns out that this is actually quite important for the stability properties

of the scheme. The second is that the HJB equations are highly non-linear, and therefore so

is the system of equations (9). It therefore has to be solved using an iterative scheme (rather

than simply inverting a matrix).

There are two options that differ in how the value function is updated: a so-called “explicit”

method and an “implicit” method. As a general rule, the implicit method is the preferred

approach because it is both more efficient and more stable/reliable. However, the explicit

method is easier to explain so we turn to it first.

2

Page 3: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

1.1 Explicit Method

See matlab program HJB_stateconstraint_explicit.m. One starts with an initial guess

v0j = (v0

1,j, ..., v0I,j), j = 1, 2 and then updates vnj , n = 1, ... according to

vn+1i,j − vni,j

∆+ ρvni,j = u(cni,j) + (vni,j)

′(zj + rai − cni,j) + λj(vni,−j − vni,j) (10)

where cni,j = (u′)−1[(vni,j)′]. The parameter ∆ is the step size of the explicit method. It can

be shown that the explicit method only converges if ∆ is not too large (it has to satisfy the

so-called “CFL condition”, see e.g. p.181 in Candler (1999)). An advantage of implicit methods

discussed in section 1.2 is that the step size ∆ can be arbitrarily large.

Upwind Scheme. As already mentioned, it is important whether and when a forward or a

backward difference approximation is used. The correct way of doing this is to use a so-called

“upwind scheme.” The rough idea is to use a forward difference approximation whenever the

drift of the state variable (here, savings sni,j = zj + rai− cni,j) is positive and to use a backwards

difference whenever it is negative.2 In practice, this is done as follows: first compute savings

according to both the backwards and forward difference approximations v′i,j,F and v′i,j,B

si,j,F = zj + rai − (u′)−1(v′i,j,F ), si,j,B = zj + rai − (u′)−1(v′i,j,B)

where we suppress n superscripts for notational simplicity. Then use the following approxima-

tion for v′i,j:

v′i,j = v′i,j,F1{si,j,F>0} + v′i,j,B1{si,j,B<0} + v′i,j1{si,j,F≤0≤si,j,B} (11)

where 1{·} denotes the indicator function. The meaning of the last term is as follows. First

note that since v is concave in a, we have v′i,j,F < v′i,j,B and so si,j,F < si,j,B. Therefore, for some

grid points i, si,j,F ≤ 0 ≤ si,j,B. At these grid points, we set savings equal to zero and hence

set the derivative of the value function equal to v′i,j = u′(zj + rai). The fact that v is concave

also means that we do not have to worry about the case where both si,j,F > 0 and si,j,B < 0:

because concavity implies si,j,F < si,j,B, this cannot happen.

Under some circumstances (and in more general applications e.g. problems with non-

convexities) it can happen that the value function is not concave. The question then arises

what to do when both si,j,F > 0 and si,j,B < 0. The following upwind scheme works well in

practice in this case. Define an indicator for the problematic case in which both si,j,F > 0

2Note that treatments of finite difference methods concerned with solving PDEs forward in time usually definean “upwind scheme” in the opposite way (forward difference when the drift is negative, backwards differencewhenever it is positive). See e.g. https://en.wikipedia.org/wiki/Upwind_scheme. The difference is thatsolving HJB equations (even stationary ones) amounts to solving PDEs backwards in time given a terminalcondition (rather than forward in time given an initial condition). The two seemingly different definitions ofthe term “upwind scheme” are the same when taking this difference into account.

3

Page 4: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

and si,j,B < 0, 1bothi,j := 1{si,j,B≤0≤si,j,F }, and an indicator for the unproblematic case 1uniquei,j =

1{si,j,F<0 and si,j,B>0} + 1{si,j,F<0 and si,j,B<0}. Next, define the forward and backward Hamilto-

nians Hi,j,F := u(ci,j,F ) + v′i,j,F si,j,F and similarly for Hi,j,B. Finally, use the upwind scheme

v′i,j = v′i,j,F (1{si,j,F>0}1uniquei,j + 1{Hi,j,F≥Hi,j,B}1

bothi,j )

+ v′i,j,B (1{si,j,B<0}1uniquei,j + 1{Hi,j,F<Hi,j,B}1

bothi,j )

+ v′i,j1{si,j,F≤0≤si,j,B}

(12)

Intuitively, in the problematic case when both si,j,F > 0 and si,j,B < 0, this upwind scheme uses

as the “tie breaker” the rule to use the derivative in the direction in which the gain according

to the Hamiltonians Hi,j,B and Hi,j,F is larger.

State Constraint. The state constraint (7) is enforced by setting

v′1,j,B = u′(zj + ra1), j = 1, 2

From (11) it can then be seen that the state constraint is imposed whenever the forward

difference approximation would result in negative savings s1,j,F ≤ 0. Otherwise if s1,j,F > 0

the forward difference approximation v′1,j,F is used at the boundary, implying that the value

function “never sees the state constraint.” At the upper end of the state space, the upwind

method should make sure that a backward-difference approximation is used. In practice, it can

sometimes help stability of the algorithm to simply impose a state constraint a ≤ amax where

amax is the upper end of the bounded state space used for computations (this can be achieved

by setting v′I,j,F = u′(zj + raI)).

Initial Guess. A natural initial guess is the value function of “staying put”

v0i,j =

u(zj + rai)

ρ.

Summary of Algorithm. Summarizing, the algorithm for finding a solution to the HJB

equation (1) and (2) is as follows. Guess v0i,j, i = 1, ..., I, j = 1, 2 and for n = 0, 1, 2, ... follow

1. Compute (vni,j)′ using (8) and (11).

2. Compute cn from cni,j = (u′)−1[(vni,j)′]

3. Find vn+1 from (10).

4. If vn+1 is close enough to vn: stop. Otherwise, go to step 1.

4

Page 5: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

One can show that, for a small enough ∆, this algorithm satisfies the three conditions of Barles

and Souganidis (1991) (monotonicity, consistency, stability). See the discussion in Section 5 of

Achdou et al. (2017).

1.2 Implicit Method

See matlab program HJB_stateconstraint_implicit.m. Relative to the explicit scheme in

(10), an implicit differs in how vn is updated. In particular, vn+1 is now implicitly defined by

the equation

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + (vn+1i,j )′(zj + rai − cni,j) + λj(v

n+1i,−j − vn+1

i,j )

Note the n + 1 superscripts on the right-hand side of the equation.3 The main advantage of

the implicit scheme is that the step size ∆ can be arbitrarily large.

Upwind Scheme. As was the case for the explicit method, we need to use an “upwind

scheme.” As above, the idea is still to use the forward difference approximation whenever the

drift of the state variable is positive and the backward difference approximation whenever it is

negative. We use the following finite difference approximation to (1) and (2).

vn+1i,j − vni,j

∆+ ρvn+1

i,j =u(cni,j) + (vn+1i,j,F )′[zj + rai − cni,j,F ]+ + (vn+1

i,j,B)′[zj + rai − cni,j,B]−

+ λj[vn+1i,−j − vn+1

i,j ]

(13)

where cni,j = (u′)−1[(vni,j)′] and (vni,j)

′ is given by (11).4 For any number x, the notation x+

means “the positive part of x”, i.e. x+ = max{x, 0} and analogously x− = min{x, 0}, i.e.

[zj + rai − cni,j,F ]+ = max{zj + rai − cni,j,F , 0} and [zj + rai − cni,j,B]− = min{zj + rai − cni,j,B, 0}.Equation (13) constitutes a system of 2 × I linear equations, and it can be written in

matrix notation using the following steps. Substituting the definition of the derivatives (8),

and defining sni,j,F = zj + rai − cni,j,F and similarly for sni,j,B, (13) is

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) +vn+1i+1,j − vn+1

i,j

∆a(sni,j,F )+ +

vn+1i,j − vn+1

i−1,j

∆a(sni,j,B)− + λj[v

n+1i,−j − vn+1

i,j ]

3Strictly speaking, the present method is a “semi-implicit method.” A fully implicit method would featuren+ 1 superscripts also on ci,j . Such a fully implicit scheme can be solved using a Newton method, which endsup looking very similar to the iterative scheme outlined here.

4As noted in the discussion of the explicit scheme in Section 1.1, this works well when the value function isconcave. When the value function is not concave, we can again use the scheme (12) instead of (11).

5

Page 6: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Collecting terms with the same subscripts on the right-hand side:

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + vn+1i−1,jxi,j + vn+1

i,j yi,j + vn+1i+1,jzi,j + vn+1

i,−jλj where

xi,j = −(sni,j,B)−

∆a,

yi,j = −(sni,j,F )+

∆a+

(sni,j,B)−

∆a− λj,

zi,j =(sni,j,F )+

∆a

(14)

Note that importantly x1,j = zI,j = 0, j = 1, 2 so vn+10,j and vn+1

I+1,j are never used. Equation (14)

is a system of 2× I linear equations which can be written in matrix notation as:

1

∆(vn+1 − vn) + ρvn+1 = un + Anvn+1 (15)

where

An =

y1,1 z1,1 0 · · · 0 λ1 0 0 · · · 0

x2,1 y2,1 z2,1 0 · · · 0 λ1 0 0 · · ·0 x3,1 y3,1 z3,1 0 · · · 0 λ1 0 0...

. . . . . . . . . . . . . . . . . . . . . . . ....

0. . . . . . xI,1 yI,1 0 0 0 0 λ1

λ2 0 0 0 0 y1,2 z1,2 0 0 0

0 λ2 0 0 0 x2,2 y2,2 z2,2 0 0

0 0 λ2 0 0 0 x3,2 y3,2 z3,2 0

0 0. . . . . . . . . . . . . . . . . . . . . . . .

0 · · · · · · 0 λ2 0 · · · 0 xI,2 yI,2

, un =

u(cn1,1)......

u(cnI,1)

u(cn1,2)......

u(cnI,2)

This system can in turn be written as

Bnvn+1 = bn, Bn =

(1

∆+ ρ

)I−An, bn = un +

1

∆vn (16)

Equation (16) can be solved very efficiently in matlab using sparse matrix routines. To check

that one has constructed the intensity matrix A correctly, the matlab function spy is a conve-

nient tool. Figure 1 plots an example of spy’s output with 30 × 2 grid points (we usually use

many more).

Finally, it is instructive to consider the case with an infinite updating step size 1∆

= 0 and

to write the linear system (15) as

ρvn+1 = un + Anvn+1 (17)

6

Page 7: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

0 10 20 30 40 50 60

0

10

20

30

40

50

60

nz = 177

Figure 1: Visualization of the matrix A using Matlab’s spy function

It can be seen that (17) is just a way of writing the discretized version of the HJB equations

(1) and (2) in matrix form. In particular the matrix An encodes the evolution of the stochastic

process (at, zt). The finite difference method basically approximates this process with a discrete

Poisson process with a transition matrix An summarizing the corresponding Poisson intensi-

ties. Note that An satisfies all the properties a Poisson transition matrix needs to satisfy. In

particular, all rows sum to zero, diagonal elements are non-positive and off-diagonal elements

are non-negative (all entries in a row being zero would mean that the state remains fixed over

time). We will therefore sometimes refer to An as “Poisson transition matrix” or “intensity

matrix.” All this will be useful in section 2 below when we solve the Kolmogorov Forward

equation (3) and (4).

Summary of Algorithm. The algorithm is exactly the same as above, except that the

updating step uses (13) or equivalently (16). Guess v0i,j, i = 1, ..., I, j = 1, 2 and for n = 0, 1, 2, ...

follow

1. Compute (vni,j)′ using (8) and (11).

2. Compute cn from cni,j = (u′)−1[(vni,j)′]

3. Find vn+1 from (16).

4. If vn+1 is close enough to vn: stop. Otherwise, go to step 1.

7

Page 8: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

One can show that this algorithm satisfies the three conditions of Barles and Souganidis (1991)

(monotonicity, consistency, stability) regardless of the size of ∆. See the discussion in Section

5 of Achdou et al. (2017). This is the big advantage of an implicit scheme over an explicit

scheme.

2 Kolmogorov Forward (Fokker-Planck) Equation

See matlab code huggett_partialeq.m We now turn to the solution of (3) and (4), which also

have to satisfy (5). The rough idea is to discretize these as

0 = −[si,jgi,j]′ − λjgi,j + λ−jgi,−j (18)

1 =I∑i=1

gi,1∆a+I∑i=1

gi,2∆a (19)

(Instead of (19), one could also use a slightly more accurate trapezoidal rule, but results are

virtually identical given the fine grid size.) Because (3) and (4) are linear in g1 and g2 so is the

finite difference approximation. As a result, no iterative procedure like the one for the HJB

equation is needed and the equation can be solved in one step.

Upwind Scheme. There is again a question when to use a forward and a backward approxi-

mation for the derivative [si,jgi,j]′. It turns out that the most convenient/correct approximation

is as follows:

−(sni,j,F )+gi,j − gi−1,j(s

ni−1,j,F )+

∆a−gi+1,j(s

ni+1,j,B)− − gi,j(sni,j,B)−

∆a− gi,jλj + gi,−jλ−j = 0

Note that because g0,j and gI+1,j are outside the state space, the density at these points is zero

and so (s0,j,F )+ and (sI+1,j,B)− are never used. The reason why the approximation above is

desirable is as follows. Collecting terms, we can write

gi−1,jzi−1,j + gi,jyi,j + gi+1,jxi+1,j + gi,−jλ−j = 0

xi+1,j = −(sni,j+1,B)−

∆a

yi,j = −(sni,j,F )+

∆a+

(sni,j,B)−

∆a− λj

zi−1,j =(sni,j−1,F )+

∆a

8

Page 9: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

The reason this is the preferred approximation is that it can be written in matrix form in a

way that is closely related to the approximation used for the HJB equation

ATg = 0 (20)

where AT is the transpose of the intensity matrix A from the HJB equation (17) (An from

the final HJB iteration). This makes sense: the operation is exactly the same as that used

for finding the stationary distribution of a discrete Poisson process (continuous-time Markov

chain). The matrix A captures the evolution of the stochastic process and to find the stationary

distribution, one solves the eigenvalue problem ATg = 0. There is therefore a deep reason why

one wants to use the transpose of the intensity matrix A. For interested readers, this can be

made more precise using some tools from the theory of differential operators: one can write

the HJB equations (1) and (2) in terms of a differential operator A, the so-called “infinitesimal

generator” of the process. Similarly, the Kolmogorov Forward equations (3) and (4) can be

written in terms of an operatorA∗. An operator is the infinite-dimensional analogue of a matrix.

And the analogue of a matrix transpose is the so-called “adjoint” of an operator. It turns out

that the operator in the Kolmogorov Forward equation A∗ is the “adjoint” of the operator in the

HJB equation A. Putting things together, A is simply the discretized infinitesimal generator

whereas AT is the discretized version of its adjoint, the “Kolmogorov Forward operator.”

Besides making sense, this approximation is also convenient: once one has constructed the

matrix A for solving the HJB equation using an implicit method, almost no extra work is

needed.

To solve the eigenvalue problem (20) while imposing (19), the simplest procedure is as

follows. Fix gi,j = 0.1 (any other number will do as well) for an arbitrary (i, j), to then solve

the system for some g and then to renormalize gi,j = gi,j/(∑I

i=1 gi,1∆a +∑I

i=1 gi,2∆a). Fixing

gi,j = 0.1 is achieved by replacing the corresponding entry of the zero vector in (20) by 0.1,

and the corresponding row of AT by a row of zeros everywhere except for one on the diagonal.

Without this “dirty fix,” the matrix AT is singular and so cannot be inverted.

Alternatively, the eigenvalue problem (20) can be solved using a pre-built routine for nu-

merical eigenvalue problems. For example, MATLAB’s eigs function is well suited.5

As shown in Achdou et al. (2017), the wealth distribution of the low-income type g1 features

a Dirac mass at the borrowing constraint a (the left boundary of the state space). When

discretizing the distribution using a finite difference method, there is technically a Dirac mass

at every point in the state space. The algorithm therefore simply ignores the Dirac mass at the

boundary and treats it like any other point. Nevertheless, as we will see below, the numerical

solution has a clearly visible spike at a.

5The particular command [g,val]=eigs(A’,1,’lr’) seems to work well. It returns the eigenvalue v andeigenvector val of AT with the largest real part (which is v= 0). Finally, for very large problems (e.g. withthree or four state variables) iterative methods such as bicgstab may be preferable.

9

Page 10: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

2.1 Results

Figure 2 (a) plots the functions s1(a) (solid blue line), s2(a) (solid green line). Note that

s′1(a)→ −∞ as a→ a as expected. Figure 2 (b) plots the associated densities g1(a) and g2(a).

Wealth, a

Saving,sj(a)

a

s1(a)s2(a)

Wealth, aDensities,gj(a)

a

g1(a)g2(a)

Figure 2: Savings Policy Function and Implied Wealth Distribution

3 Equilibrium

3.1 Asset Supply

See matlab code huggett_asset_supply.m. After having solved (1) to (5), the asset supply

function S(r) defined in (6) can be easily computed. We approximate it as

S(r) ≈I∑i=1

aigi,1∆a+I∑i=1

aigi,2∆a

Figure 3 plots asset supply as a function of the interest rate. It looks as expected: in particular,

supply is bounded below by the borrowing constraint and S(r)→∞ as r → ρ.

3.2 Finding the Equilibrium Interest Rate

See matlab code huggett_equilibrium_iterate.m. The equilibrium interest rate can easily

be found using a bisection method: the obvious idea is to increase r whenever S(r) < 0 and

decrease r whenever S(r) > 0. See the code for details.

10

Page 11: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

r

r = ρ

S(r)

B

a = a

Figure 3: Asset Supply S(r)

4 Transition Dynamics and “MIT Shocks”

See matlab codes huggett_transition.m which needs input from huggett_initial.m and

huggett_terminal.m (the initial and terminal conditions). Besides solving transition dynamics

from an arbitrary initial condition, the same algorithm can be used to study the economy’s

impulse response after an “MIT shock,” i.e. an unanticipated (zero probability) shock followed

by a deterministic transition. We provide an example in Section 6.3.

The system to be solved is:

0 =S(r(t)) =

∫ ∞a

ag1(a, t)da+

∫ ∞a

ag2(a, t)da (21)

ρvj(a, t) = maxc

u(c) + ∂avj(a, t)[zj + r(t)a− c] + λj[v−j(a, t)− vj(a, t)] + ∂tvj(a, t), (22)

∂tgj(a, t) =− ∂a[sj(a, t)gj(a, t)]− λjgj(a, t) + λ−jg−j(a, t) (23)

sj(a, t) =zj + r(t)a− cj(a, t), cj(a, t) = (u′)−1(∂avj(a, t)) (24)

The bond market clearing condition can be written as

0 =

∫ ∞a

s1(a, t)g1(a, t)da+

∫ ∞a

s2(a, t)g2(a, t)da

We solve this system using the following algorithm. Guess a function r0(t) and then for ` =

0, 1, 2, ... follow

1. Given r`(t), solve the HJB equation (22) with terminal condition v`j(a, T ) = vj(a) back-

ward in time to compute the time path of v`j(a, t). Also compute the implied saving policy

function s`j(a, t)

2. Given s`j(a, t), solve the Kolmogorov Forward equation (23) with initial condition gj(a, 0) =

11

Page 12: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

gj0(a) forward in time to calculate the time path for g`j(a, t).

3. Given s`j(a, t) and g`j(a, t) calculate

S`(t) =

∫ ∞a

ag`1(a, t)da+

∫ ∞a

ag`2(a, t)da

4. Update r`+1(t) = r`(t)− ξ dS`(t)dt

, where ξ > 0.

5. Stop when r`+1 is sufficiently close to r`(t).

4.1 Solving the Time-Dependent HJB Equation (Step 1)

Approximate the value function at I discrete points in the wealth dimension and N discrete

points in the time dimension, and use the shorthand notation vni,j = vj(ai, tn). The discrete

approximation to the time-dependent HJB (22) is

ρvni,j = u(cn+1i,j ) + (vni,j)

′[zj + rn+1ai − cn+1i,j ] + λj[v

ni,−j − vni,j] +

vn+1i,j − vni,j

∆t(25)

with terminal condition vNi,j = vj(ai). Given vn+1, this system can be solved for vn exactly as

in Section 1.2. In particular, one can write this in matrix notation as

ρvn = un+1 + An+1vn +1

∆t(vn+1 − vn) (26)

where An+1 is defined in an analogous fashion to Section 1.2 and still has the interpretation

of the transition matrix of the discretized stochastic process for (at, zt). Now each n has the

interpretation of a time step instead of an iteration on the stationary value function. The reason

for this similarity in the algorithm is that intuitively a stationary value function can be found

by solving a time-dependent problem and going far enough back in time, i.e. as t→ −∞.

4.2 Solving the Time-Dependent Kolmogorov Forward Eq. (Step 2)

Analogously to the value function, we approximate the density at J discrete points in the

wealth dimension and N discrete points in the time dimension, and use the shorthand notation

gni,j = gj(ai, tn). Similarly to Section 2 one can directly make use of the transition matrix An

defined when solving the time-dependent HJB equation (Section 4.1). Given an initial condition

g0i,j = gj,0(ai), the Kolmogorov Forward equation (23) is then easily solved. One here has the

option of using either an explicit method

gn+1 − gn

∆t= (An)Tgn ⇒ gn+1 = ∆t(An)Tgn + gn (27)

12

Page 13: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

or an implicit method

gn+1 − gn

∆t= (An)Tgn+1 ⇒ gn+1 = (I−∆t(An)T)−1gn.

Note that these schemes preserve mass: starting from any initial distribution g0 that sums to

one, all future gn’s also sum to one. This follows from the fact that the rows of the intensity

matrices An sum to zero. The implicit scheme is also guaranteed to preserve the positivity of

g for arbitrary time steps ∆t.

4.3 Results: Transition Dynamics

Figure 4 plots the time path for the equilibrium interest rate in response to a permanent increase

in “unemployment risk”, λ2. Figure 5 plots the densities g1(a, t) and g2(a, t) at various points in

time during the transition. For comparison, the Figure also plots densities in the initial steady

state (dashed lines).

0 2 4 6 8 10−0.02

−0.01

0

0.01

0.02

0.03

Year

Equilibrium Interest Rate, r(t)

Figure 4: Time Path of Equilibrium Interest Rate

13

Page 14: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

−0.1 0 0.1 0.2 0.3 0.4 0.50

0.5

1

1.5

2

2.5

3

Wealth, a

Densities,

gi(a)

t = 0.1

g1(a)

g2(a)

−0.1 0 0.1 0.2 0.3 0.4 0.50

0.5

1

1.5

2

2.5

3

Wealth, a

Densities,

gi(a)

t = 2

g1(a)

g2(a)

−0.1 0 0.1 0.2 0.3 0.4 0.50

0.5

1

1.5

2

2.5

3

Wealth, a

Densities,

gi(a)

t = 5

g1(a)

g2(a)

−0.1 0 0.1 0.2 0.3 0.4 0.50

0.5

1

1.5

2

2.5

3

Wealth, a

Densities,

gi(a)

t = ∞

g1(a)

g2(a)

Figure 5: Dynamics of Wealth Distribution

5 Generalization to Diffusion Process

The system to be solved is:

ρv(a, z) = maxc

u(c) + ∂av(a, z)[z + ra− c] + µ(z)∂zv(a, z) +σ2(z)

2∂zzv(a, z) (28)

0 = −∂a[s(a, z)g(a, z)]− ∂z[µ(z)g(a, z)] +1

2∂zz[σ

2(z)g(a, z)] (29)

1 =

∫ ∞0

∫ ∞a

g(a, z)dadz (30)

0 =

∫ ∞0

∫ ∞a

ag(a, z)dadz ≡ S(r) (31)

14

Page 15: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

We assume that the z-process gets reflected at some z and z. One can show that this gives rise

to the following boundary conditions for v:6

0 = ∂zv(a, z) = ∂zv(a, z)

Finally, we have the state constraint boundary condition:

∂av(a, z) ≥ u′(z + ra), all z. (32)

We again use a finite difference method and use the short-hand notation v(ai, zj) = vi,j. Note

that we changed notation slightly and i now indexes wealth and j indexes productivity.

5.1 HJB Equation

See matlab program HJB_diffusion_implicit.m. With a diffusion process, an explicit method

becomes extremely inefficient so we here only explain the solution of the HJB with an implicit

method. The derivative in the a dimension is again approximated using an upwind method,

i.e. using either a forward or a backward difference approximation depending on the sign of

the drift:

∂a,Bvi,j =vi,j − vi−1,j

∆a

∂a,Fvi,j =vi+1,j − vi,j

∆a

(33)

Similarly, we also use an upwind method in the z-direction. For the second-order derivative,

we use a central difference approximation. Hence:

∂z,Bvi,j =vi,j − vi,j−1

∆z

∂z,Fvi,j =vi,j+1 − vi,j

∆z

∂zzvi,j =vi,j+1 − 2vi,j + vi,j−1

(∆z)2

Analogously to the model with Poisson shocks, vn+1 is now implicitly defined by the equation

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + ∂avn+1i,j [zj + rai − cni,j] + µj∂zv

n+1i,j +

σ2j

2∂zzv

n+1i,j

Note the n+ 1 superscripts on the right-hand side of the equation. The main advantage of the

implicit scheme is that the step size ∆ can be arbitrarily large.

6See e.g. Section 3.5 in Dixit (1993).

15

Page 16: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Upwind Scheme. We again need to use an “upwind scheme.” As above, the idea is still to

use the forward difference approximation whenever the drift of the state variable is positive

and the backward difference approximation whenever it is negative. We use the following finite

difference approximation to (28).

vn+1i,j − vni,j

∆+ ρvn+1

i,j =u(cni,j) + ∂a,Fvn+1i,j [zj + rai − cni,j,F ]+ + ∂a,Bv

n+1i,j [zj + rai − cni,j,B]−

+ ∂z,Fvn+1i,j µ+

j + ∂z,Bvn+1i,j µ−j +

σ2j

2∂zzv

n+1i,j

(34)

Equation (34) constitutes a system of I × J linear equations, and it can be written in matrix

notation using the following steps. Substituting the definition of the derivatives (33), and

defining sni,j,F = zj + rai − cni,j,F and similarly for sni,j,B, (34) is

vn+1i,j − vni,j

∆+ ρvn+1

i,j =u(cni,j) +vn+1i+1,j − vn+1

i,j

∆a(sni,j,F )+ +

vn+1i,j − vn+1

i−1,j

∆a(sni,j,B)−

+vn+1i,j+1 − vn+1

i,j

∆zµ+j +

vn+1i,j − vn+1

i,j−1

∆zµ−j +

σ2j

2

vn+1i,j+1 − 2vn+1

i,j + vn+1i,j−1

(∆z)2

Collecting terms with the same subscripts on the right-hand side

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + vn+1i−1,jxi,j + vn+1

i,j (yi,j + υj) + vn+1i+1,jzi,j + vn+1

i,j−1χj + vn+1i,j+1ζj

xi,j = −(sni,j,B)−

∆a,

yi,j = −(sni,j,F )+

∆a+

(sni,j,B)−

∆a,

zi,j =(sni,j,F )+

∆a

χj = −µ−j∆z

+σ2j

2(∆z)2

υj =µ−j∆z−µ+j

∆z−

σ2j

(∆z)2

ζj =µ+j

∆z+

σ2j

2(∆z)2

(35)

Note that importantly x1,j = zI,j = 0 for all j so vn+10,j and vn+1

I+1,j are never used. At the

boundaries in the j dimension, the equations become

vn+1i,1 − vni,1

∆+ ρvn+1

i,1 = u(cni,1) + vn+1i−1,1xi,1 + vn+1

i,1 (yi,1 + υ1 + χ1) + vn+1i+1,1zi,1 + vn+1

i,2 ζ1

vn+1i,J − vni,J

∆+ ρvn+1

i,J = u(cni,J) + vn+1i−1,Jxi,J + vn+1

i,J (yi,J + υJ + ζJ) + vn+1i+1,Jzi,J + vn+1

i,J−1χJ

16

Page 17: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

where, in the first equation, we have used that ∂z,Bvi,1 =vi,1−vi,0

∆z= 0 and hence vi,0 = vi,1.

Similarly, in the second equation, ∂z,Fvi,J =vi,J+1−vi,J

∆z= 0 and hence vi,J+1 = vi,J . Note that

we here defined the boundary conditions relative to the points j = 0 and j = J + 1 and used

the values vi,0 and vi,J+1. These points are sometimes called “ghost nodes”. Equation (35) is a

system of I × J linear equations which can be written in matrix notation as:

1

∆(vn+1 − vn) + ρvn+1 = un + Anvn+1 (36)

where vn is a vector of length I × J with entries (v1,1, ..., vI,1, v1,2, ..., vI,2, ..., vI,J) and An =

An + C where the (I × J)× (I × J) matrices An and C are

An =

y1,1 z1,1 0 · · · · · · · · · · · · · · · · · · · · · · · · · · · 0

x2,1 y2,1 z2,1 0. . . . . . . . . . . . . . . . . . . . . . . .

...

0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

......

. . . xI,1 yI,1 0 0. . . . . . . . . . . . . . . . . .

......

. . . 0 0 y1,2 z1,2 0. . . . . . . . . . . . . . .

......

. . . . . . 0 x2,2 y2,2 z2,2 0. . . . . . . . . . . .

......

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

.... . . . . . . . . . . . 0 xI,2 yI,2 0

. . . . . . . . ....

.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

......

. . . . . . . . . . . . . . . . . . . . . 0 y1,J z1,J 0...

.... . . . . . . . . . . . . . . . . . . . . 0 x2,J y2,J z2,J 0

.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

0 . . . . . . . . . . . . . . . . . . . . . . . . . . . 0 xI,J yI,J

17

Page 18: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

C =

υ1 + χ1 0 · · · · · · 0 ζ1 0 · · · · · · · · · · · · · · · 0

0 υ1 + χ1 0. . . . . . 0 ζ1 0

. . . . . . . . . . . ....

.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

...

0. . . 0 υ1 + χ1 0

. . . . . . 0 ζ1 0. . . . . .

...

χ2 0. . . 0 υ2 0

. . . . . . 0 ζ2 0. . .

...

0 χ2 0. . . 0 υ2 0

. . . . . . 0 ζ2 0...

... 0. . . . . . . . . . . . . . . . . . . . . . . . 0

. . ....

.... . . 0 χ2 0

. . . 0 υ2 0. . . . . . 0

......

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

.... . . . . . . . . 0 χJ 0

. . . . . . υJ + ζJ 0. . .

......

. . . . . . . . . . . . 0 χJ 0. . . 0 υJ + ζJ 0

......

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0

0 . . . . . . . . . . . . . . . . . . 0 χJ 0 . . . 0 υJ + ζJ

Equation (36) can again be solved very efficiently in matlab. Figure 6 again plots a visualization

of the intensity matrix A in practice (using Matlab’s spy function).

0 500 1000 1500 2000 2500 3000 3500 4000

0

500

1000

1500

2000

2500

3000

3500

4000

nz = 15749

Figure 6: Visualization of the matrix A using spy in model with diffusion process

18

Page 19: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

5.2 Kolmogorov Forward Equation and Equilibrium

See matlab program huggett_diffusion_partialeq.m. The Kolmogorov Forward equation is

solved exactly as in section 2, and the equilibrium is found as in section 3.

5.3 Results

Figures 7 and 8 plot the functions s(a, z) and g(a, z).

1.5

Productivity, z

1

0.525

20

Wealth, a

15

10

5

0

-0.4

-0.2

-0.8

0

0.6

0.4

0.2

-0.6

Sa

vin

gs s

(a,z

)

Figure 7: Savings Policy Function in Huggett Model with Diffusion

6 Aiyagari Model

We now briefly explain how to solve the Aiyagari model in Section 6 of Achdou et al. (2017).

As in the paper we focus on the case where productivity follows a diffusion process so as

to explain how to handle that case. Of course, it is also straightforward to solve an Aiya-

gari model in which income follows a two-state Poisson process. Codes for this case are

also available: aiyagari_poisson_steadystate.m, aiyagari_poisson_asset_supply.m and

aiyagari_poisson_MITshock.m.

19

Page 20: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

1.5

Productivity, z

1

0.58

6

Wealth, a

4

2

0

0.05

0.1

0

0.15

0.2

0.25D

ensity g

(a,z

)

Figure 8: Wealth Distribution in Huggett Model with Diffusion

6.1 Steady State

See matlab program aiyagari_diffusion_equilibrium.m, Julia program aiyagari_diffusion_

equilibrium.jl and C++ program aiyagari_diffusion_equilibrium.cpp. A steady state

or stationary equilibrium can be represented by the following system of equations which we

aim to solve numerically:

ρv(a, z) = maxc

u(c) + ∂av(a, z)(wz + ra− c) + ∂zv(a, z)µ(z) +1

2∂zzv(a, z)σ2(z) (37)

0 =∂a(s(a, z)g(a, z))− ∂z(µ(z)g(a, z)) +1

2∂zz(σ

2(z)g(a, z)) (38)

r = ∂KF (K, 1)− δ, w = ∂LF (K, 1), (39)

K =

∫ z

z

∫ ∞a

ag(a, z)dadz (40)

on (a,∞)×(z, z), where s(a, z) = wz+ra−c(a, z), c(a, z) = (u′)−1(∂av(a, z)) and with boundary

conditions

u′(wz + ra) ≥ ∂av(a, z), all z

∂zv(a, z) = 0, ∂zv(a, z) = 0, all a

20

Page 21: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

The algorithm for solving the HJB and KF equations is the same as in Sections 5.1 and 5.2.

To find the equilibrium wage and interest rate w and r, we use a fixed point algorithm on the

scalar K. Alternatively, one can express w as a function of r using (39) and (40) and use a

bisection method to solve for the equilibrium r.

6.2 Transition Dynamics

See matlab program aiyagari_diffusion_transition.m. The algorithm for solving the HJB

and KF equations is the natural generalization to the time-dependent case of that outlined in

Sections 5.1 and 5.2. To solve for the equilibrium time paths of the wage and interest rate, we

use a fixed point algorithm on the function K(t).

6.3 “MIT Shocks”

See matlab program aiyagari_poisson_MITshock.m. We consider the version of the Aiyagari

model with Poisson income shocks and compute the impulse response to a negative aggregate

productivity shock that mean reverts over time. This shock is modeled as an “MIT shock,” i.e.

an unexpected (zero probability) shock followed by a deterministic transition. More precisely,

we assume that the aggregate production function is Yt = Ft(K,L) = AtKαL1−α and aggregate

productivity follows a deterministic version of an Ornstein-Uhlenbeck process (the continuous-

time analogue of an AR(1) process):

dAt = ν(A− At)dt.

The parameter ν governs the speed of mean reversion (one can show that Corr(At, At+s) =

e−νs). Figure 9 plots the impulse response to this productivity MIT-shock. In particular note

the measures of income and wealth inequality in the last two panels.

6.4 Visualizing Evolution of Wealth Distribution as Movie

After running aiyagari_diffusion_transition.m or aiyagari_poisson_MITshock.m, you

can run make_movie.m to make the movie of the transition of the distribution.

After you run make_movie.m, you will have distribution.avi file. One can use ffmpeg

(available at https://www.ffmpeg.org/) to convert from avi file to mp4 file. In particular

type

ffmpeg -i distribution.avi -c:v libx264 -g 30 -pix_fmt yuv420p distribution.mp4

and this will create a mp4 file that is accepted by pretty much anything (and it works on

website with html5).7

7”-pix_fmtyuv420p” is only necessary while browsers and media players are being updated. It is using oldformat because ffmpeg was updated recently, and most media players have not been.

21

Page 22: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Year

0 20 40 60 80 100

0.097

0.0975

0.098

0.0985

0.099

0.0995

0.1

0.1005Aggregate Productivity

Year

0 20 40 60 80 100

0.293

0.294

0.295

0.296

0.297

0.298

0.299

0.3Aggregate Capital Stock

Year

0 20 40 60 80 100

0.0376

0.0378

0.038

0.0382

0.0384

0.0386

0.0388

0.039Wage

Year

0 20 40 60 80 100

0.0445

0.045

0.0455

0.046

0.0465

0.047

0.0475

0.048

0.0485

0.049Interest Rate

Year

0 20 40 60 80 100

0.882

0.884

0.886

0.888

0.89

0.892

0.894

0.896Wealth Gini

Year

0 20 40 60 80 100

0.236

0.237

0.238

0.239

0.24

0.241

0.242Income Gini

Figure 9: Impulse Response to Negative Productivity MIT-shock in Aiyagari Model

If you do not want to go through the pain of compiling your own ffmpeg binary and have a

Mac, you can download a pre-compiled binary at http://ffmpegmac.net/. Copy the binary

ffmpeg into the same directory as your avi file and run (don’t forget the “./” in the beginning)

./ffmpeg -i distribution.avi -c:v libx264 -g 30 -pix_fmt yuv420p distribution.mp4

The resulting movie is at http://www.princeton.edu/~moll/HACTproject/distribution.

mp4.

7 Non-uniform Grids

In all previous sections, we worked with uniformly spaced grids. But for many applications, we

may want to economize on grid points. This can be achieved by working with non-uniform grids

and “putting grid points” at points in the state space where the value and density functions

have the most curvature.

22

Page 23: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

7.1 HJB equation with non-uniform grid

Extending our algorithm for the HJB equation to the case of non-uniform grids is straightfor-

ward. Denoting by ∆ai,+ = ai+1−ai and ∆ai,− = ai−ai−1, the forward and backward distance

between two grid points, we simply change (8) to

v′j(ai) ≈vi+1,j − vi,jai+1 − ai

=vi+1,j − vi,j

∆ai,+≡ v′i,j,F

v′j(ai) ≈vi,j − vi−1,j

ai − ai−1

=vi,j − vi−1,j

∆ai,−≡ v′i,j,B

(41)

Following the same steps as above, we end up with

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + vn+1i−1,jxi,j + vn+1

i,j yi,j + vn+1i+1,jzi,j + vn+1

i,−jλj where

xi,j = −(sni,j,B)−

∆ai,−, yi,j = −

(sni,j,F )+

∆ai,++

(sni,j,B)−

∆ai,−− λj, zi,j =

(sni,j,F )+

∆ai,+

(42)

This can again be written in matrix form (15) where the intensity matrix An has the same

structure as above but with the entries in (42). The rest of the algorithm is unchanged.

If an approximation to the second derivative of vj is needed as in Section 8, a good candidate

approximation is

v′′j (ai) ≈∆ai,−vi+1,j − (∆ai,− + ∆ai,+)vi,j + ∆ai,+vi−1,j

12

(∆ai,+ + ∆ai,−) ∆ai,−∆ai,+(43)

This approximation can be derived from a Taylor approximation to vj8 and it can be seen that

with ∆ai,− = ∆ai,+ = ∆a, it reduces to the standard second-derivative approximation in the

case with uniform grids:

v′′j (ai) ≈vi+1,j − 2vi,j + vi−1,j

(∆a)2

8Consider a second-order Taylor approximation to vj around ai:

vi+1,j ≈ vi,j + ∆ai,+v′j(ai) +

1

2(∆ai,+)2v′′j (ai)

vi−1,j ≈ vi,j −∆ai,−v′j(ai) +

1

2(∆ai,−)2v′′j (ai)

Multiply the first equation by ∆ai,− and the second equation by ∆ai,+ and add the two equations

∆ai,−vi+1,j + ∆ai,+vi−1,j ≈ (∆ai,− + ∆ai,+)vi,j + (∆ai,+ + ∆ai,−)1

2∆ai,−∆ai,+v

′′j (ai)

Rearranging yields (43).

23

Page 24: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

7.2 Kolmogorov Forward equation with non-uniform grid

Extending the Kolmogorov Forward equation to the case of a non-uniform grid requires more

work. Section 2 suggests working with the transpose of the intensity matrix A and to solve

gn+1 − gn

∆t= ATgn

or the implicit analogue. The problem with this scheme is that it is not guaranteed to preserve

mass: starting with an initial distribution g that integrates/sums to one, the total mass may

converge to a different number over time (including zero or infinity).

To see this consider a simplified example with one income type only z1 = z2 in which case

the intensity matrix is of size I × I with entries Ai,i′ given by

Ai,i−1 = xi = −(sni,B)−

∆ai,−, Ai,i = yi = −

(sni,F )+

∆ai,++

(sni,B)−

∆ai,−, Ai,i+1 = zi =

(sni,F )+

∆ai,+(44)

From (44), we can see that the rows of A still sum to zero,∑

i′ Ai,i′ = 0. Therefore

I∑i=1

gn+1i − gni

∆t=

I∑i′=1

(I∑i=1

Ai′,i

)gi′ = 0

In the case of a uniform grid, this also implies mass preservation∑I

i=1gn+1i −gni

∆t∆a = 0. However,

in the case of a non-uniform grid what we want instead is that an appropriate approximation

of the integral equals zero, i.e. something like∑I

i=1gn+1i −gni

∆t∆ai = 0 (where the ∆ai’s depend

on the precise integral approximation method).

The following solution to this problem seems to work well in practice, in particular it

preserves mass and the positivity of g.9 First, approximate the integral of g with the trapezoidal

rule 10

∫ amax

a

g(a, tn)da ≈ 1

2

I−1∑i=1

(ai+1 − ai)(gi+1 + gi) =I∑i=1

gi∆ai

∆ai =

12∆ai,+, i = 1

12(∆ai,+ + ∆ai,−), i = 2, ..., I − 1

12∆ai,−, i = I

The key idea is now that, rather than working with the vector g with elements gi, we work

directly with a vector g whose elements are gi = gi∆ai and which must therefore satisfy

9We have not yet been able to derive a rigorous theoretical justification for the proposed scheme.10See http://en.wikipedia.org/wiki/Trapezoidal_rule#Non-uniform_grid

24

Page 25: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

∑Ii=1 gi = 1. Given an initial condition g1 we simply solve the following analogue of (27):

gn+1 − gn

∆t= ATgn.

Following the same logic as above, the condition∑I

i′=1Ai,i′ = 0 guarantees mass preservation∑Ii=1 gi =

∑Ii=1 gi∆ai = 1. We can then always back out the true distribution gn from gi =

gi/(∆ai). In matrix form, we have g = Dg where D is a diagonal matrix with elements

∆ai, i = 1, ..., I. Therefore underlying distribution g can also be found from g = D−1g.11

It is also straightforward to show that the same approach – working with the rescaled density

g = Dg – also applies to the case with multiple income states (or a continuum).

8 Aiyagari Model with Fat-tailed Wealth Distribution

See matlab code fat_tail_partialeq.m. This section shows how to extend our computational

methods to the model with two assets and a fat-tailed wealth distribution from Section 4 of

Achdou et al. (2017).

8.1 Model Setup

The system of equations to be solved is

ρvj(a) = maxc,k≤a+φ

u(c) + v′j(a)(zj + ra+ (R− r)k − c) +1

2v′′j (a)σ2k2 + λj(v−j(a)− vj(a)) (45)

0 =− d

da[sj(a)gj(a)] +

1

2

d2

da2[σ2kj(a)2gj(a)]− λjgj(a) + λ−jg−j(a) (46)∫ ∞

a

k1(a)g1(a)da+

∫ ∞a

k2(a)g2(a)da =

∫ ∞a

ag1(a)da+

∫ ∞a

ag2(a)da (47)

11A previous version of this Appendix advocated working with a rescaled version of the intensity matrix Arather than a rescaled version of the density g: replace the intensity matrix A in (20) or (27) with an alternative

intensity matrix A that satisfies∑I

i′=1 Ai,i′∆ai′ = 0 and then solve

gn+1 − gn

∆t= ATgn.

The rescaled intensity matrix was given by A = DAD−1 where again D is a diagonal matrix with elements∆ai, i = 1, ..., I. The two approaches are equivalent given that g = Dg. However, we prefer the approach inthe text because it is much more transparent and easier to explain. We are grateful to Matthieu Gomez forsuggesting the approach in the current version.

25

Page 26: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Optimal consumption and choice of risky assets are

cj(a) = v′j(a)−1/γ (48)

kj(a) = min

{v′j(a)

−v′′j (a)

R− rσ2

, a+ φ

}. (49)

Boundary Conditions In theory, the HJB equation (45) is defined on (a,∞) but in practice

it has to be solved on a bounded interval (a, amax). A non-trivial issue concerns the question

what boundary condition to impose at amax. We use the asymptotic behavior of the value

function in Lemma 2 in the paper to motivate boundary conditions as follows.12 For large a,

we have

vj(a) = v0,j + v1,ja1−γ

for unknown constants v0,j and v1,j. Hence, we impose the following boundary condition

v′′j (amax) = −γv′j(amax)/amax. (50)

To solve (45), what we really need is a boundary condition for the term σ2

2v′′j (a)k(a)2. From

(49) and (50)

kj(amax) =R− rγσ2

amax (51)

σ2

2kj(amax)2v′′j (amax) = −σ

2

2kj(amax)2γv′j(amax)/amax

= v′j(amax)ξ, ξ = −(R− r)2

2γσ2amax

(52)

We will condition (52) below when solving (45) using a finite difference method. Finally, it

sometimes helps numerical stability to impose a state constraint a ≤ amax. This is equivalent

to cj(amax) ≥ zj + ramax + (R− r)kj(amax) or using (51)

v′j(amax) ≤(zj + ramax +

(R− r)2

γσ2amax

)−γ.

8.2 Finite Difference Method for HJB Equation

The steps follow closely the solution method of the one-asset model. We therefore only outline

the main differences. As before we use an implicit upwind method. In contrast to before,

the HJB equation (45) now involves the second derivative of the value function. For sake of

transparency, we here explain our finite difference method for a uniform grid. The code in

fat_tail_partialeq.m instead uses a non-uniform grid as explained in Section 7 so as to be

12We thank Matthieu Gomez for suggesting this boundary condition.

26

Page 27: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

able to put more points in the region of the state space where policy functions have a lot of

curvature, i.e. close to the borrowing constraint.

Defining sni,j,F = zj + (R − r)ki,j + rai − cni,j,F and similarly for sni,j,B, the discretization of

(45) is

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) +vn+1i+1,j − vn+1

i,j

∆a(sni,j,F )+ +

vn+1i,j − vn+1

i−1,j

∆a(sni,j,B)−

+ λj[vn+1i,−j − vn+1

i,j ] +σ2

2k2i,j

vn+1i+1,j − 2vn+1

i,j + vn+1i−1,j

(∆a)2

Collecting terms with the same subscripts on the right-hand side:

vn+1i,j − vni,j

∆+ ρvn+1

i,j = u(cni,j) + vn+1i−1,jxi,j + vn+1

i,j yi,j + vn+1i+1,jzi,j + vn+1

i,−jλj where

xi,j = −(sni,j,B)−

∆a+σ2

2

k2i,j

(∆a)2,

yi,j = −(sni,j,F )+

∆a+

(sni,j,B)−

∆a− σ2

k2i,j

(∆a)2− λj,

zi,j =(sni,j,F )+

∆a+σ2

2

k2i,j

(∆a)2

(53)

At the upper boundary a = amax = aI , we make use of (52) and write the approximation as

vn+1I,j − vnI,j

∆+ ρvn+1

I,j = u(cnI,j) +vn+1I+1,j − v

n+1I,j

∆a(snI,j,F )+ +

vn+1I,j − v

n+1I−1,j

∆a(snI,j,B)−

+ λj[vn+1I,−j − v

n+1I,j ] +

vn+1I,j − v

n+1I−1,j

∆aξ

so that the corresponding entries of (53) become.

xI,j = −(snI,j,B)−

∆a− ξ

∆a,

yI,j = −(snI,j,F )+

∆a+

(snI,j,B)−

∆a+

ξ

∆a− λj,

zI,j =(snI,j,F )+

∆a

(54)

Equations (53) and (54) is a system of 2 × I linear equations which can be written in matrix

notation like equation (15)

1

∆(vn+1 − vn) + ρvn+1 = un + Anvn+1

and that can be solved efficiently.

27

Page 28: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

8.3 Finite Difference for Kolmogorov Forward Equation

The solution of the Kolmogorov Forward equation (46) is exactly as in Section 2, with one

difference: because of the second-order term one has to decide what to do at the upper end

of the state space amax. The cleanest solution is to impose an artificial reflecting barrier. To

this end, consider the “intensity matrix” A. Rather than using (54) at the upper end of

the state space, construct the transition matrix according to (53). But then move all entries

corresponding to the (non-existent) grid point I + 1 to the entry corresponding to I:

xI,j = xI,j = −(snI,j,B)−

∆a+σ2

2

k2I,j

(∆a)2,

yI,j = yI,j + zI,j =(snI,j,B)−

∆a− σ2

2

k2I,j

(∆a)2− λj,

zI,j = 0.

(55)

The interpretation is that whenever the process would leave the state space according to the

discretized law of motion (if it would go to point I + 1), it is “reflected” back in (back down to

point I).13

8.4 Results

See Figure 10.

9 Accuracy of Finite Difference Method

See Appendix D of Achdou et al. (2017) available at http://www.princeton.edu/~moll/HACT_

appendix.pdf for various accuracy checks.

13This condition can be derived more rigorously as follows. For simplicity consider the case without produc-tivity shocks z1 = z2 so that the process for wealth is dat = s(at)dt+ σk(at)dWt. Impose a reflecting barrier ata = amax. Then the infinitesimal generator corresponding to this process is given by

(Af)(a) = s(a)f ′(a) +σ2

2k(a)2f ′′(a)

with the boundary condition corresponding to a reflecting barrier: f ′(amax) = 0. Its discrete version is:

(Af)i = s+ifi+1 − fi

∆a+ s−i

fi − fi−1∆a

+σ2

2k2ifi+1 − 2fi + fi−1

(∆a)2

= xifi−1 + yifi + zifi−1

where xi, yi, zi are analogous to (53). The discretized boundary condition is f ′(aI) ≈ (fI+1 − fI)/(∆a) = 0 orfI+1 = fI . Therefore (Af)I = xIfI−1 + (yI + zI)fI = xIfI + yIfI with yI = yI + zI i.e. just like in (55).

28

Page 29: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Wealth, a

Savings,si(a)

a

s1(a)

s2(a)

(a) Saving Policy Function

Wealth, a

RiskyAssets,ki(a)

a

k1(a)

k2(a)

(b) Investment in Risky Assets

Wealth, a

Densities,gi(a)

a

g1(a)

g2(a)

(c) Stationary Densities

Log Wealth, x = log (a)

log(f

i(x))

log(f1(x))

log(f2(x))

Slope = -ζ

(d) Pareto Tail of Stationary Distribution

Figure 10: Optimal Choices and Pareto Tail of Wealth Distribution in Two-Asset Model

References

Achdou, Yves, Jiequn Han, Jean-Michel Lasry, Pierre-Louis Lions, and Benjamin

Moll. 2017. “Income and Wealth Distribution in Macroeconomics: A Continuous-Time Ap-

proach.” National Bureau of Economic Research, Inc NBER Working Papers 23732.

Barles, G., and P. E. Souganidis. 1991. “Convergence of approximation schemes for fully

nonlinear second order equations.” Asymptotic Analysis, 4: 271–283.

Candler, Graham V. 1999. “Finite-Difference Methods for Dynamic Programming Prob-

lems.” In Computational Methods for the Study of Dynamic Economies. Cambridge, Eng-

land:Cambridge University Press.

29

Page 30: Income and Wealth Distribution in Macroeconomicsmoll/HACTproject/HACT_Numerical_Appendix.pdfAll algorithms are available as matlab codes from ... use matlab’s sparse matrix routines

Dixit, Avinash. 1993. The Art of Smooth Pasting. Fundamentals of Pure and Applied Eco-

nomics 55, The Routledge.

Huggett, Mark. 1993. “The risk-free rate in heterogeneous-agent incomplete-insurance

economies.” Journal of Economic Dynamics and Control, 17(5-6): 953–969.

30