Skip to content

Commit 771b8bc

Browse files
auto sync from book-dp1
1 parent ce54d93 commit 771b8bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1723
-146
lines changed

code/jl/expo_curve.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,18 @@
1-
# ---
2-
# jupyter:
3-
# jupytext:
4-
# text_representation:
5-
# extension: .py
6-
# format_name: light
7-
# format_version: '1.5'
8-
# jupytext_version: 1.14.6
9-
# kernelspec:
10-
# display_name: Python 3
11-
# language: python
12-
# name: python3
13-
# ---
14-
151
import numpy as np
162
import matplotlib.pyplot as plt
173
import matplotlib
4+
from scipy.linalg import expm, eigvals
185

196
matplotlib.rc("text", usetex=True) # allow tex rendering
20-
fontsize=16
21-
from scipy.linalg import expm, eigvals
227

23-
# %matplotlib inline
8+
fontsize = 16
249

25-
# +
2610
A = ((-2, -0.4, 0),
2711
(-1.4, -1, 2.2),
2812
(0, -2, -0.6))
2913

3014
A = np.array(A)
31-
# -
32-
3315
ev = eigvals(A)
34-
35-
np.imag(ev)
36-
37-
np.max(np.real(ev))
38-
3916
h = 0.01
4017
s0 = 0.01 * np.array((1, 1, 1))
4118

@@ -48,7 +25,6 @@
4825
y.append(b)
4926
z.append(c)
5027

51-
# +
5228
ax = plt.figure().add_subplot(projection='3d')
5329

5430
ax.plot(x, y, z, label='$t \mapsto \mathrm{e}^{t A} u_0$')
@@ -67,8 +43,3 @@
6743

6844
plt.savefig("../figures/expo_curve_1.pdf")
6945
plt.show()
70-
# -
71-
72-
73-
74-

code/jl/markov_js_with_sep.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function plot_w_stars(; α_vals=LinRange(0.0, 1.0, 10),
117117
ax.plot(α_vals, w_star_vec, lw=2, alpha=0.6, label="reservation wage")
118118
ax.legend(frameon=false, fontsize=fontsize)
119119
ax.set_xlabel(L"\alpha", fontsize=fontsize)
120-
ax.set_xlabel(L"w", fontsize=fontsize)
120+
ax.set_ylabel(L"w", fontsize=fontsize)
121121
plt.show()
122122
if savefig
123123
fig.savefig(figname)

code/jl/stoch_dom_finite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PyPlot.matplotlib[:rc]("text", usetex=true) # allow tex rendering
66
p, q = 0.75, 0.25
77
fig, axes = plt.subplots(1, 2, figsize=(10, 5.2))
88
ax = axes[1]
9-
ax.bar(1:2, (p, 1-p), label=L"\phi")
9+
ax.bar(1:2, (p, 1-p), label=L"\varphi")
1010

1111
ax = axes[2]
1212
ax.bar(1:2, (q, 1-q), label=L"\psi")

code/py/american_option.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from quantecon import compute_fixed_point
88

99
import numpy as np
10+
import matplotlib.pyplot as plt
1011
from collections import namedtuple
1112
from numba import njit, prange
1213

@@ -36,7 +37,7 @@ def create_american_option_model(
3637
z_vals, Q = mc.state_values + μ, mc.P
3738
w_vals, φ, β = np.array([-s, s]), np.array([0.5, 0.5]), 1 / (1 + r)
3839
return Model(t_vals=t_vals, z_vals=z_vals, w_vals=w_vals, Q=Q,
39-
φ=φ, T=T, β=β, K=K)
40+
φ=φ, T=T, β=β, K=K)
4041

4142

4243
@njit(parallel=True)
@@ -72,12 +73,8 @@ def compute_cvf(model):
7273

7374
# Plots
7475

75-
76-
import matplotlib.pyplot as plt
77-
78-
7976
def plot_contours(savefig=False,
80-
figname="./figures/american_option_1.pdf"):
77+
figname="../figures_py/american_option_1.png"):
8178

8279
model = create_american_option_model()
8380
t_vals, z_vals, w_vals, Q, φ, T, β, K = model
@@ -110,7 +107,7 @@ def plot_contours(savefig=False,
110107

111108
def plot_strike(savefig=False,
112109
fontsize=9,
113-
figname="./figures/american_option_2.pdf"):
110+
figname="../figures_py/american_option_2.png"):
114111
model = create_american_option_model()
115112
t_vals, z_vals, w_vals, Q, φ, T, β, K = model
116113
h_star = compute_cvf(model)

code/py/ar1_spec_rad.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
import numpy as np
12+
import matplotlib.pyplot as plt
1213

1314
from quantecon.markov import tauchen
1415

@@ -69,12 +70,9 @@ def compute_mc_spec_rad(n, ρ, σ, μ, m, b):
6970
# Plots
7071

7172

72-
import matplotlib.pyplot as plt
73-
74-
7573
def plot_beta_sim(T=80,
7674
savefig=True,
77-
figname="./figures/ar1_spec_rad.png"):
75+
figname="../figures_py/ar1_spec_rad.png"):
7876
β_vals = np.zeros(T)
7977
Z = 1
8078
for t in range(T):
@@ -92,5 +90,3 @@ def plot_beta_sim(T=80,
9290
if savefig:
9391
fig.savefig(figname)
9492
plt.show()
95-
96-
plot_beta_sim()

code/py/bellman_envelope.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
xmin = -0.5
6+
xmax = 2.0
7+
8+
xgrid = np.linspace(xmin, xmax, 1000)
9+
10+
a1, b1 = 0.15, 0.5 # first T_σ
11+
a2, b2 = 0.5, 0.4 # second T_σ
12+
a3, b3 = 0.75, 0.2 # third T_σ
13+
14+
v1 = b1/(1-a1)
15+
v2 = b2/(1-a2)
16+
v3 = b3/(1-a3)
17+
18+
T1 = a1 * xgrid + b1
19+
T2 = a2 * xgrid + b2
20+
T3 = a3 * xgrid + b3
21+
T = np.maximum.reduce([T1, T2, T3])
22+
23+
fig, ax = plt.subplots(figsize=(6, 5))
24+
for spine in ["left", "bottom"]:
25+
ax.spines[spine].set_position("zero")
26+
27+
for spine in ["right", "top"]:
28+
ax.spines[spine].set_color("none")
29+
30+
ax.plot(xgrid, T1, "k-", lw=1)
31+
ax.plot(xgrid, T2, "k-", lw=1)
32+
ax.plot(xgrid, T3, "k-", lw=1)
33+
34+
ax.plot(xgrid, T, lw=6, alpha=0.3, color="blue",
35+
label=r"$T = \bigvee_{\sigma \in \Sigma} T_\sigma$")
36+
37+
38+
ax.text(2.1, 0.6, r"$T_{\sigma'}$")
39+
ax.text(2.1, 1.4, r"$T_{\sigma''}$")
40+
ax.text(2.1, 1.9, r"$T_{\sigma'''}$")
41+
42+
ax.legend(frameon=False, loc="upper center")
43+
44+
45+
ax.set_xlim(xmin, xmax+0.5)
46+
ax.set_ylim(-0.2, 2.5)
47+
ax.text(2.4, -0.15, r"$v$")
48+
49+
ax.set_xticks([])
50+
ax.set_yticks([])
51+
52+
plt.show()
53+
54+
file_name = "../figures_py/bellman_envelope.png"
55+
fig.savefig(file_name)
56+

code/py/binom_stoch_dom.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from scipy.stats import binom
4+
5+
6+
n, m, p = 10, 18, 0.5
7+
8+
phi = binom(n, p)
9+
psi = binom(m, p)
10+
11+
x = np.arange(0, m+1)
12+
13+
fig, ax = plt.subplots(figsize=(9, 5.2))
14+
lb = r"$\phi = B({}, {})$".format(n, p)
15+
ax.plot(x, np.concatenate((phi.pmf(np.arange(0, n+1)),
16+
np.zeros(m-n))), "-o", alpha=0.6, label=lb)
17+
lb = r"$\psi = B({}, {})$".format(m, p)
18+
ax.plot(x, psi.pmf(x), "-o", alpha=0.6, label=lb)
19+
20+
ax.legend(fontsize=16, frameon=False)
21+
22+
plt.show()
23+
24+
file_name = "../figures_py/binom_stoch_dom.png"
25+
fig.savefig(file_name)

code/py/concave_map_fp.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
x0 = 0.25
5+
xmin, xmax = 0, 3
6+
fs = 18
7+
8+
x_grid = np.linspace(xmin, xmax, 1200)
9+
10+
def g(x):
11+
return 1 + 0.5 * x**0.5
12+
13+
xstar = 1.64
14+
15+
fig, ax = plt.subplots(figsize=(10, 5.5))
16+
17+
# Plot the functions
18+
lb = r"$g$"
19+
ax.plot(x_grid, g(x_grid), lw=2, alpha=0.6, label=lb)
20+
ax.plot(x_grid, x_grid, "k--", lw=1, alpha=0.7, label=r"$45$")
21+
22+
# Show and annotate the fixed point
23+
fps = (xstar,)
24+
ax.plot(fps, fps, "go", ms=10, alpha=0.6)
25+
ax.set_xlabel(r"$x$", fontsize=fs)
26+
ax.annotate(r"$x^*$",
27+
xy=(xstar, xstar),
28+
xycoords="data",
29+
xytext=(-20, 20),
30+
textcoords="offset points",
31+
fontsize=fs)
32+
33+
ax.legend(loc="upper left", frameon=False, fontsize=fs)
34+
ax.set_xticks((0, 1, 2, 3))
35+
ax.set_yticks((0, 1, 2, 3))
36+
ax.set_ylim(0, 3)
37+
ax.set_xlim(0, 3)
38+
39+
plt.show()
40+
41+
file_name = "../figures_py/concave_map_fp.png"
42+
fig.savefig(file_name)

0 commit comments

Comments
 (0)