Skip to content

Commit 3fd1ecd

Browse files
committed
minor updates
1 parent 8691c65 commit 3fd1ecd

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

lectures/aiyagari.md

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ We begin by discussing an example of a Bewley model due to Rao Aiyagari {cite}`A
4242

4343
The model features
4444

45-
* Heterogeneous agents
46-
* A single exogenous vehicle for borrowing and lending
47-
* Limits on amounts individual agents may borrow
45+
* heterogeneous agents
46+
* a single exogenous vehicle for borrowing and lending
47+
* limits on amounts individual agents may borrow
4848

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

@@ -133,7 +133,7 @@ The wage and interest rate are fixed over time.
133133

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

136-
## Firms
136+
### Firms
137137

138138
Firms produce output by hiring capital and labor.
139139

@@ -217,7 +217,7 @@ def r_to_w(r, firm):
217217

218218
### Equilibrium
219219

220-
We construct a *stationary rational expectations equilibrium* (SREE).
220+
We construct a **stationary rational expectations equilibrium (SREE)**.
221221

222222
In such an equilibrium
223223

@@ -231,16 +231,7 @@ In more detail, an SREE lists a set of prices, savings and production policies s
231231
* the resulting aggregate quantities are consistent with the prices; in particular, the demand for capital equals the supply
232232
* aggregate quantities (defined as cross-sectional averages) are constant
233233

234-
In practice, once parameter values are set, we can check for an SREE by the following steps
235-
236-
1. pick a proposed quantity $K$ for aggregate capital
237-
1. determine corresponding prices, with interest rate $r$ determined by {eq}`aiy_rgk` and a wage rate $w(r)$ as given in {eq}`aiy_wgr`
238-
1. determine the common optimal savings policy of households given these prices
239-
1. compute aggregate capital as the mean of steady-state capital given this savings policy
240-
241-
If this final quantity agrees with $K$ then we have a SREE.
242-
243-
## Code
234+
## Implementation
244235

245236
Let's look at how we might compute such an equilibrium in practice.
246237

@@ -250,7 +241,7 @@ Below we provide code to solve the household problem, taking $r$ and $w$ as fixe
250241

251242
We will solve the household problem using Howard policy iteration (see Ch 5 of [Dynamic Programming](https://dp.quantecon.org/)).
252243

253-
First we set up a namedtuple to store the parameters that define a household asset accumulation problem, as well as the grids used to solve it.
244+
First we set up a `NamedTuple` to store the parameters that define a household asset accumulation problem, as well as the grids used to solve it
254245

255246
```{code-cell} ipython3
256247
class Household(NamedTuple):
@@ -272,15 +263,13 @@ def create_household(β=0.96, # Discount factor
272263
return Household(β=β, a_grid=a_grid, z_grid=z_grid, Π=Π)
273264
```
274265

275-
For now we assume that $u(c) = \log(c)$.
276-
277-
(CRRA utility is treated in the exercises.)
266+
For now we assume that $u(c) = \log(c)$
278267

279268
```{code-cell} ipython3
280269
u = jnp.log
281270
```
282271

283-
Here's a namedtuple that stores the wage rate and interest rate, as well as a function that creates a price namedtuple with default values.
272+
Here's a namedtuple that stores the wage rate and interest rate, as well as a function that creates a price namedtuple with default values
284273

285274
```{code-cell} ipython3
286275
class Prices(NamedTuple):
@@ -317,15 +306,15 @@ def B(v, household, prices):
317306
c = w * z + (1 + r) * a - ap
318307
319308
# Calculate continuation rewards at all combinations of (a, z, ap)
320-
v = jnp.reshape(v, (1, 1, a_size, z_size)) # v[ip, jp] -> v[i, j, ip, jp]
321-
Π = jnp.reshape(Π, (1, z_size, 1, z_size)) # Π[j, jp] -> Π[i, j, ip, jp]
322-
EV = jnp.sum(v * Π, axis=-1) # sum over last index jp
309+
v = jnp.reshape(v, (1, 1, a_size, z_size)) # v[ip, jp] -> v[i, j, ip, jp]
310+
Π = jnp.reshape(Π, (1, z_size, 1, z_size)) # Π[j, jp] -> Π[i, j, ip, jp]
311+
EV = jnp.sum(v * Π, axis=-1) # sum over last index jp
323312
324313
# Compute the right-hand side of the Bellman equation
325314
return jnp.where(c > 0, u(c) + β * EV, -jnp.inf)
326315
```
327316

328-
The next function computes greedy policies.
317+
The next function computes greedy policies
329318

330319
```{code-cell} ipython3
331320
@jax.jit
@@ -334,10 +323,11 @@ def get_greedy(v, household, prices):
334323
Computes a v-greedy policy σ, returned as a set of indices. If
335324
σ[i, j] equals ip, then a_grid[ip] is the maximizer at i, j.
336325
"""
337-
return jnp.argmax(B(v, household, prices), axis=-1) # argmax over ap
326+
# argmax over ap
327+
return jnp.argmax(B(v, household, prices), axis=-1)
338328
```
339329

340-
The following function computes the array $r_{\sigma}$ which gives current rewards given policy $\sigma$.
330+
The following function computes the array $r_{\sigma}$ which gives current rewards given policy $\sigma$
341331

342332
```{code-cell} ipython3
343333
@jax.jit
@@ -402,7 +392,7 @@ def R_σ(v, σ, household):
402392
return v - β * jnp.sum(V * Π, axis=-1)
403393
```
404394

405-
The next function computes the lifetime value of a given policy.
395+
The next function computes the lifetime value of a given policy
406396

407397
```{code-cell} ipython3
408398
@jax.jit
@@ -413,13 +403,15 @@ def get_value(σ, household, prices):
413403
v_σ = R_σ^{-1} r_σ
414404
"""
415405
r_σ = compute_r_σ(σ, household, prices)
406+
416407
# Reduce R_σ to a function in v
417408
_R_σ = lambda v: R_σ(v, σ, household)
418-
# Compute v_σ = R_σ^{-1} r_σ using an iterative routing.
409+
410+
# Compute v_σ = R_σ^{-1} r_σ using an iterative routine.
419411
return jax.scipy.sparse.linalg.bicgstab(_R_σ, r_σ)[0]
420412
```
421413

422-
Here's the Howard policy iteration.
414+
Here's the Howard policy iteration
423415

424416
```{code-cell} ipython3
425417
def howard_policy_iteration(household, prices,
@@ -442,11 +434,11 @@ def howard_policy_iteration(household, prices,
442434
v_σ = v_σ_new
443435
i = i + 1
444436
if verbose:
445-
print(f"Concluded loop {i} with error {error}.")
437+
print(f"iteration {i} with error {error}.")
446438
return σ
447439
```
448440

449-
As a first example of what we can do, let's compute and plot an optimal accumulation policy at fixed prices.
441+
As a first example of what we can do, let's compute and plot an optimal accumulation policy at fixed prices
450442

451443
```{code-cell} ipython3
452444
# Create an instance of Household
@@ -461,7 +453,7 @@ print(f"Interest rate: {r}, Wage: {w}")
461453
%time σ_star = howard_policy_iteration(household, prices, verbose=True)
462454
```
463455

464-
The next plot shows asset accumulation policies at different values of the exogenous state.
456+
The next plot shows asset accumulation policies at different values of the exogenous state
465457

466458
```{code-cell} ipython3
467459
β, a_grid, z_grid, Π = household
@@ -537,7 +529,7 @@ The distribution should sum to one:
537529
ψ_a.sum()
538530
```
539531

540-
The next function computes aggregate capital supply by households under policy $\sigma$, given wages and interest rates.
532+
The next function computes aggregate capital supply by households under policy $\sigma$, given wages and interest rates
541533

542534
```{code-cell} ipython3
543535
def capital_supply(σ, household):
@@ -549,9 +541,9 @@ def capital_supply(σ, household):
549541
return float(jnp.sum(ψ_a * a_grid))
550542
```
551543

552-
## Equilibrium
544+
### Equilibrium
553545

554-
We compute a stationary rational expectations equilibrium (SREE) as follows:
546+
We compute a SREE as follows:
555547

556548
1. Set $n=0$ and start with an initial guess $K_0$ for aggregate capital.
557549
1. Determine prices $r, w$ from the firm decision problem, given $K_n$.
@@ -569,23 +561,22 @@ If $K_{n+1}$ agrees with $K_n$ then we have a SREE.
569561

570562
In other words, our problem is to find the fixed point of the one-dimensional map $G$.
571563

572-
Here's $G$ expressed as a Python function:
564+
Here's $G$ expressed as a Python function
573565

574566
```{code-cell} ipython3
575567
def G(K, firm, household):
576568
# Get prices r, w associated with K
577569
r = r_given_k(K, firm)
578570
w = r_to_w(r, firm)
571+
579572
# Generate a household object with these prices, compute
580573
# aggregate capital.
581574
prices = create_prices(r=r, w=w)
582575
σ_star = howard_policy_iteration(household, prices)
583576
return capital_supply(σ_star, household)
584577
```
585578

586-
### Visual inspection
587-
588-
Let's inspect visually as a first pass.
579+
Let's inspect visually as a first pass
589580

590581
```{code-cell} ipython3
591582
num_points = 50
@@ -602,8 +593,6 @@ ax.legend()
602593
plt.show()
603594
```
604595

605-
### Computing the equilibrium
606-
607596
Now let's compute the equilibrium.
608597

609598
Looking at the figure above, we see that a simple iteration scheme $K_{n+1} = G(K_n)$ will cycle from high to low values, leading to slow convergence.
@@ -647,14 +636,13 @@ You can try varying $\alpha$, but usually this parameter is hard to set a priori
647636

648637
In the exercises below you will be asked to use bisection instead, which generally performs better.
649638

650-
651639
### Supply and demand curves
652640

653641
We can visualize the equilibrium using supply and demand curves.
654642

655643
The following code draws the aggregate supply and demand curves.
656644

657-
The intersection gives the equilibrium interest rate and capital.
645+
The intersection gives the equilibrium interest rate and capital
658646

659647
```{code-cell} ipython3
660648
def prices_to_capital_stock(household, r, firm):
@@ -717,7 +705,7 @@ In `bisect`,
717705
:class: dropdown
718706
```
719707

720-
We use bisection to find the zero of the function $h(k) = k - G(k)$.
708+
We use bisection to find the zero of the function $h(k) = k - G(k)$
721709

722710
```{code-cell} ipython3
723711
def compute_equilibrium_bisect(firm, household, a=1.0, b=20.0):

0 commit comments

Comments
 (0)