Skip to content

Commit 0358f5f

Browse files
committed
MAINT: transfer np.sum(a * b) to a @ b
1 parent e024f92 commit 0358f5f

File tree

1 file changed

+94
-9
lines changed

1 file changed

+94
-9
lines changed

lectures/aiyagari.md

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.16.7
68
kernelspec:
7-
display_name: Python 3
9+
display_name: Python 3 (ipykernel)
810
language: python
911
name: python3
1012
---
@@ -26,10 +28,9 @@ kernelspec:
2628

2729
In addition to what's in Anaconda, this lecture will need the following libraries:
2830

29-
```{code-cell} ipython
30-
---
31-
tags: [hide-output]
32-
---
31+
```{code-cell} ipython3
32+
:tags: [hide-output]
33+
3334
!pip install quantecon
3435
```
3536

@@ -54,7 +55,7 @@ The Aiyagari model has been used to investigate many topics, including
5455

5556
Let's start with some imports:
5657

57-
```{code-cell} ipython
58+
```{code-cell} ipython3
5859
import matplotlib.pyplot as plt
5960
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
6061
import numpy as np
@@ -208,7 +209,7 @@ when the parameters change.
208209

209210
The class also includes a default set of parameters that we'll adopt unless otherwise specified.
210211

211-
```{code-cell} python3
212+
```{code-cell} ipython3
212213
class Household:
213214
"""
214215
This class takes the parameters that define a household asset accumulation
@@ -318,7 +319,7 @@ def asset_marginal(s_probs, a_size, z_size):
318319

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

321-
```{code-cell} python3
322+
```{code-cell} ipython3
322323
# Example prices
323324
r = 0.03
324325
w = 0.956
@@ -366,7 +367,9 @@ The following code draws aggregate supply and demand curves.
366367

367368
The intersection gives equilibrium interest rates and capital.
368369

369-
```{code-cell} python3
370+
```{code-cell} ipython3
371+
:tags: hide-input
372+
370373
A = 1.0
371374
N = 1.0
372375
α = 0.33
@@ -439,3 +442,85 @@ ax.legend(loc='upper right')
439442
440443
plt.show()
441444
```
445+
446+
```{code-cell} ipython3
447+
k_vals_orig = k_vals
448+
```
449+
450+
```{code-cell} ipython3
451+
A = 1.0
452+
N = 1.0
453+
α = 0.33
454+
β = 0.96
455+
δ = 0.05
456+
457+
458+
def r_to_w(r):
459+
"""
460+
Equilibrium wages associated with a given interest rate r.
461+
"""
462+
return A * (1 - α) * (A * α / (r + δ))**(α / (1 - α))
463+
464+
def rd(K):
465+
"""
466+
Inverse demand curve for capital. The interest rate associated with a
467+
given demand for capital K.
468+
"""
469+
return A * α * (N / K)**(1 - α) - δ
470+
471+
472+
def prices_to_capital_stock(am, r):
473+
"""
474+
Map prices to the induced level of capital stock.
475+
476+
Parameters:
477+
----------
478+
479+
am : Household
480+
An instance of an aiyagari_household.Household
481+
r : float
482+
The interest rate
483+
"""
484+
w = r_to_w(r)
485+
am.set_prices(r, w)
486+
aiyagari_ddp = DiscreteDP(am.R, am.Q, β)
487+
# Compute the optimal policy
488+
results = aiyagari_ddp.solve(method='policy_iteration')
489+
# Compute the stationary distribution
490+
stationary_probs = results.mc.stationary_distributions[0]
491+
# Extract the marginal distribution for assets
492+
asset_probs = asset_marginal(stationary_probs, am.a_size, am.z_size)
493+
# Return K
494+
return asset_probs @ am.a_vals
495+
496+
497+
# Create an instance of Household
498+
am = Household(a_max=20)
499+
500+
# Use the instance to build a discrete dynamic program
501+
am_ddp = DiscreteDP(am.R, am.Q, am.β)
502+
503+
# Create a grid of r values at which to compute demand and supply of capital
504+
num_points = 20
505+
r_vals = np.linspace(0.005, 0.04, num_points)
506+
507+
# Compute supply of capital
508+
k_vals = np.empty(num_points)
509+
for i, r in enumerate(r_vals):
510+
k_vals[i] = prices_to_capital_stock(am, r)
511+
512+
# Plot against demand for capital by firms
513+
fig, ax = plt.subplots(figsize=(11, 8))
514+
ax.plot(k_vals, r_vals, lw=2, alpha=0.6, label='supply of capital')
515+
ax.plot(k_vals, rd(k_vals), lw=2, alpha=0.6, label='demand for capital')
516+
ax.grid()
517+
ax.set_xlabel('capital')
518+
ax.set_ylabel('interest rate')
519+
ax.legend(loc='upper right')
520+
521+
plt.show()
522+
```
523+
524+
```{code-cell} ipython3
525+
np.allclose(k_vals_orig, k_vals)
526+
```

0 commit comments

Comments
 (0)