@@ -4,7 +4,7 @@ jupytext:
44 extension : .md
55 format_name : myst
66 format_version : 0.13
7- jupytext_version : 1.16.7
7+ jupytext_version : 1.17.1
88kernelspec :
99 display_name : Python 3 (ipykernel)
1010 language : python
@@ -60,7 +60,6 @@ We use the following imports:
6060``` {code-cell} ipython3
6161import quantecon as qe
6262import matplotlib.pyplot as plt
63- import numpy as np
6463import jax
6564import jax.numpy as jnp
6665from typing import NamedTuple
@@ -85,7 +84,6 @@ def compute_stationary(P):
8584 return jnp.linalg.solve(A, jnp.ones(n))
8685```
8786
88-
8987### References
9088
9189The primary reference for this lecture is {cite}` Aiyagari1994 ` .
@@ -269,18 +267,12 @@ For now we assume that $u(c) = \log(c)$
269267u = jnp.log
270268```
271269
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
270+ Here's a namedtuple that stores the wage rate and interest rate with default values
273271
274272``` {code-cell} ipython3
275273class Prices(NamedTuple):
276- r: float # Interest rate
277- w: float # Wages
278-
279- def create_prices(r=0.01, w=1.0):
280- """
281- Create a Prices namedtuple that stores wage and interest rate.
282- """
283- return Prices(r=r, w=w)
274+ r: float = 0.01 # Interest rate
275+ w: float = 1.0 # Wages
284276```
285277
286278Now we set up a vectorized version of the right-hand side of the Bellman equation (before maximization), which is a 3D array representing
@@ -443,7 +435,7 @@ As a first example of what we can do, let's compute and plot an optimal accumula
443435``` {code-cell} ipython3
444436# Create an instance of Household
445437household = create_household()
446- prices = create_prices ()
438+ prices = Prices ()
447439
448440r, w = prices
449441print(f"Interest rate: {r}, Wage: {w}")
@@ -573,7 +565,7 @@ def G(K, firm, household):
573565
574566 # Generate a household object with these prices, compute
575567 # aggregate capital.
576- prices = create_prices (r=r, w=w)
568+ prices = Prices (r=r, w=w)
577569 σ_star = howard_policy_iteration(household, prices)
578570 return capital_supply(σ_star, household)
579571```
@@ -584,7 +576,8 @@ Let's inspect visually as a first pass
584576num_points = 50
585577firm = create_firm()
586578household = create_household()
587- k_vals = np.linspace(4, 12, num_points)
579+ k_min, k_max = 4, 12
580+ k_vals = [k_min + i * (k_max - k_min) / (num_points - 1) for i in range(num_points)]
588581out = [G(k, firm, household) for k in k_vals]
589582
590583fig, ax = plt.subplots(figsize=(11, 8))
@@ -651,7 +644,7 @@ def prices_to_capital_stock(household, r, firm):
651644 Map prices to the induced level of capital stock.
652645 """
653646 w = r_to_w(r, firm)
654- prices = create_prices (r=r, w=w)
647+ prices = Prices (r=r, w=w)
655648
656649 # Compute the optimal policy
657650 σ_star = howard_policy_iteration(household, prices)
@@ -661,18 +654,20 @@ def prices_to_capital_stock(household, r, firm):
661654
662655# Create a grid of r values to compute demand and supply of capital
663656num_points = 20
664- r_vals = np.linspace(0.005, 0.04, num_points)
657+ r_min, r_max = 0.005, 0.04
658+ r_vals = [r_min + i * (r_max - r_min) / (num_points - 1) for i in range(num_points)]
665659
666660# Compute supply of capital
667- k_vals = np.empty(num_points)
668- for i, r in enumerate( r_vals) :
669- k_vals[i] = prices_to_capital_stock(household, r, firm)
661+ k_vals = []
662+ for r in r_vals:
663+ k_vals.append( prices_to_capital_stock(household, r, firm) )
670664
671665# Plot against demand for capital by firms
672666fig, ax = plt.subplots(figsize=(11, 8))
673667ax.plot(k_vals, r_vals, lw=2, alpha=0.6,
674668 label='supply of capital')
675- ax.plot(k_vals, r_given_k(k_vals, firm), lw=2, alpha=0.6,
669+ ax.plot(k_vals, r_given_k(
670+ jnp.array(k_vals), firm), lw=2, alpha=0.6,
676671 label='demand for capital')
677672
678673# Add marker at equilibrium
@@ -686,7 +681,6 @@ ax.legend(loc='upper right')
686681plt.show()
687682```
688683
689-
690684## Exercises
691685
692686``` {exercise}
@@ -737,8 +731,10 @@ Use the following values of $\beta$ and plot the relationship you find.
737731``` {code-cell} ipython3
738732:tags: [hide-output]
739733
740- β_vals = np.linspace(0.94, 0.98, 20)
734+ β_min, β_max, num_β = 0.94, 0.98, 20
735+ β_vals = [β_min + i * (β_max - β_min) / (num_β - 1) for i in range(num_β)]
741736```
737+
742738``` {exercise-end}
743739```
744740
@@ -747,14 +743,14 @@ Use the following values of $\beta$ and plot the relationship you find.
747743```
748744
749745``` {code-cell} ipython3
750- K_vals = np.empty_like(β_vals)
746+ K_vals = []
751747K = 6.0 # initial guess
752748
753- for i, β in enumerate( β_vals) :
749+ for β in β_vals:
754750 household = create_household(β=β)
755751 K = compute_equilibrium_bisect(firm, household, 0.5 * K, 1.5 * K)
756752 print(f"Computed equilibrium {K:.4} at β = {β}")
757- K_vals[i] = K
753+ K_vals.append(K)
758754
759755fig, ax = plt.subplots()
760756ax.plot(β_vals, K_vals, ms=2)
0 commit comments