Skip to content

[aiyagari] MAINT: transfer np.sum(a * b) to a @ b #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 94 additions & 9 deletions lectures/aiyagari.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ jupytext:
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.16.7
kernelspec:
display_name: Python 3
display_name: Python 3 (ipykernel)
language: python
name: python3
---
Expand All @@ -26,10 +28,9 @@ kernelspec:

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

```{code-cell} ipython
---
tags: [hide-output]
---
```{code-cell} ipython3
:tags: [hide-output]

!pip install quantecon
```

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

Let's start with some imports:

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

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

```{code-cell} python3
```{code-cell} ipython3
class Household:
"""
This class takes the parameters that define a household asset accumulation
Expand Down Expand Up @@ -318,7 +319,7 @@ def asset_marginal(s_probs, a_size, z_size):

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

```{code-cell} python3
```{code-cell} ipython3
# Example prices
r = 0.03
w = 0.956
Expand Down Expand Up @@ -366,7 +367,9 @@ The following code draws aggregate supply and demand curves.

The intersection gives equilibrium interest rates and capital.

```{code-cell} python3
```{code-cell} ipython3
:tags: [hide-input]

A = 1.0
N = 1.0
α = 0.33
Expand Down Expand Up @@ -439,3 +442,85 @@ ax.legend(loc='upper right')

plt.show()
```

```{code-cell} ipython3
k_vals_orig = k_vals
```

```{code-cell} ipython3
A = 1.0
N = 1.0
α = 0.33
β = 0.96
δ = 0.05


def r_to_w(r):
"""
Equilibrium wages associated with a given interest rate r.
"""
return A * (1 - α) * (A * α / (r + δ))**(α / (1 - α))

def rd(K):
"""
Inverse demand curve for capital. The interest rate associated with a
given demand for capital K.
"""
return A * α * (N / K)**(1 - α) - δ


def prices_to_capital_stock(am, r):
"""
Map prices to the induced level of capital stock.

Parameters:
----------

am : Household
An instance of an aiyagari_household.Household
r : float
The interest rate
"""
w = r_to_w(r)
am.set_prices(r, w)
aiyagari_ddp = DiscreteDP(am.R, am.Q, β)
# Compute the optimal policy
results = aiyagari_ddp.solve(method='policy_iteration')
# Compute the stationary distribution
stationary_probs = results.mc.stationary_distributions[0]
# Extract the marginal distribution for assets
asset_probs = asset_marginal(stationary_probs, am.a_size, am.z_size)
# Return K
return asset_probs @ am.a_vals


# Create an instance of Household
am = Household(a_max=20)

# Use the instance to build a discrete dynamic program
am_ddp = DiscreteDP(am.R, am.Q, am.β)

Copy link
Preview

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable am_ddp is assigned but never used; it can be removed to reduce clutter.

Suggested change
am_ddp = DiscreteDP(am.R, am.Q, am.β)

Copilot uses AI. Check for mistakes.

# Create a grid of r values at which to compute demand and supply of capital
num_points = 20
r_vals = np.linspace(0.005, 0.04, num_points)

# Compute supply of capital
k_vals = np.empty(num_points)
for i, r in enumerate(r_vals):
k_vals[i] = prices_to_capital_stock(am, r)

# Plot against demand for capital by firms
fig, ax = plt.subplots(figsize=(11, 8))
ax.plot(k_vals, r_vals, lw=2, alpha=0.6, label='supply of capital')
ax.plot(k_vals, rd(k_vals), lw=2, alpha=0.6, label='demand for capital')
ax.grid()
ax.set_xlabel('capital')
ax.set_ylabel('interest rate')
ax.legend(loc='upper right')

plt.show()
```

```{code-cell} ipython3
np.allclose(k_vals_orig, k_vals)
```
Loading