Skip to content

Commit

Permalink
auto sync from book-dp1
Browse files Browse the repository at this point in the history
  • Loading branch information
quantecon-services committed Oct 8, 2024
1 parent ce54d93 commit 771b8bc
Showing 58 changed files with 1,723 additions and 146 deletions.
33 changes: 2 additions & 31 deletions code/jl/expo_curve.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.6
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from scipy.linalg import expm, eigvals

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

# %matplotlib inline
fontsize = 16

# +
A = ((-2, -0.4, 0),
(-1.4, -1, 2.2),
(0, -2, -0.6))

A = np.array(A)
# -

ev = eigvals(A)

np.imag(ev)

np.max(np.real(ev))

h = 0.01
s0 = 0.01 * np.array((1, 1, 1))

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

# +
ax = plt.figure().add_subplot(projection='3d')

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

plt.savefig("../figures/expo_curve_1.pdf")
plt.show()
# -




2 changes: 1 addition & 1 deletion code/jl/markov_js_with_sep.jl
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ function plot_w_stars(; α_vals=LinRange(0.0, 1.0, 10),
ax.plot(α_vals, w_star_vec, lw=2, alpha=0.6, label="reservation wage")
ax.legend(frameon=false, fontsize=fontsize)
ax.set_xlabel(L"\alpha", fontsize=fontsize)
ax.set_xlabel(L"w", fontsize=fontsize)
ax.set_ylabel(L"w", fontsize=fontsize)
plt.show()
if savefig
fig.savefig(figname)
2 changes: 1 addition & 1 deletion code/jl/stoch_dom_finite.jl
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ PyPlot.matplotlib[:rc]("text", usetex=true) # allow tex rendering
p, q = 0.75, 0.25
fig, axes = plt.subplots(1, 2, figsize=(10, 5.2))
ax = axes[1]
ax.bar(1:2, (p, 1-p), label=L"\phi")
ax.bar(1:2, (p, 1-p), label=L"\varphi")

ax = axes[2]
ax.bar(1:2, (q, 1-q), label=L"\psi")
11 changes: 4 additions & 7 deletions code/py/american_option.py
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
from quantecon import compute_fixed_point

import numpy as np
import matplotlib.pyplot as plt
from collections import namedtuple
from numba import njit, prange

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


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

# Plots


import matplotlib.pyplot as plt


def plot_contours(savefig=False,
figname="./figures/american_option_1.pdf"):
figname="../figures_py/american_option_1.png"):

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

def plot_strike(savefig=False,
fontsize=9,
figname="./figures/american_option_2.pdf"):
figname="../figures_py/american_option_2.png"):
model = create_american_option_model()
t_vals, z_vals, w_vals, Q, φ, T, β, K = model
h_star = compute_cvf(model)
8 changes: 2 additions & 6 deletions code/py/ar1_spec_rad.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
"""

import numpy as np
import matplotlib.pyplot as plt

from quantecon.markov import tauchen

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


import matplotlib.pyplot as plt


def plot_beta_sim(T=80,
savefig=True,
figname="./figures/ar1_spec_rad.png"):
figname="../figures_py/ar1_spec_rad.png"):
β_vals = np.zeros(T)
Z = 1
for t in range(T):
@@ -92,5 +90,3 @@ def plot_beta_sim(T=80,
if savefig:
fig.savefig(figname)
plt.show()

plot_beta_sim()
56 changes: 56 additions & 0 deletions code/py/bellman_envelope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import numpy as np
import matplotlib.pyplot as plt


xmin = -0.5
xmax = 2.0

xgrid = np.linspace(xmin, xmax, 1000)

a1, b1 = 0.15, 0.5 # first T_σ
a2, b2 = 0.5, 0.4 # second T_σ
a3, b3 = 0.75, 0.2 # third T_σ

v1 = b1/(1-a1)
v2 = b2/(1-a2)
v3 = b3/(1-a3)

T1 = a1 * xgrid + b1
T2 = a2 * xgrid + b2
T3 = a3 * xgrid + b3
T = np.maximum.reduce([T1, T2, T3])

fig, ax = plt.subplots(figsize=(6, 5))
for spine in ["left", "bottom"]:
ax.spines[spine].set_position("zero")

for spine in ["right", "top"]:
ax.spines[spine].set_color("none")

ax.plot(xgrid, T1, "k-", lw=1)
ax.plot(xgrid, T2, "k-", lw=1)
ax.plot(xgrid, T3, "k-", lw=1)

ax.plot(xgrid, T, lw=6, alpha=0.3, color="blue",
label=r"$T = \bigvee_{\sigma \in \Sigma} T_\sigma$")


ax.text(2.1, 0.6, r"$T_{\sigma'}$")
ax.text(2.1, 1.4, r"$T_{\sigma''}$")
ax.text(2.1, 1.9, r"$T_{\sigma'''}$")

ax.legend(frameon=False, loc="upper center")


ax.set_xlim(xmin, xmax+0.5)
ax.set_ylim(-0.2, 2.5)
ax.text(2.4, -0.15, r"$v$")

ax.set_xticks([])
ax.set_yticks([])

plt.show()

file_name = "../figures_py/bellman_envelope.png"
fig.savefig(file_name)

25 changes: 25 additions & 0 deletions code/py/binom_stoch_dom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom


n, m, p = 10, 18, 0.5

phi = binom(n, p)
psi = binom(m, p)

x = np.arange(0, m+1)

fig, ax = plt.subplots(figsize=(9, 5.2))
lb = r"$\phi = B({}, {})$".format(n, p)
ax.plot(x, np.concatenate((phi.pmf(np.arange(0, n+1)),
np.zeros(m-n))), "-o", alpha=0.6, label=lb)
lb = r"$\psi = B({}, {})$".format(m, p)
ax.plot(x, psi.pmf(x), "-o", alpha=0.6, label=lb)

ax.legend(fontsize=16, frameon=False)

plt.show()

file_name = "../figures_py/binom_stoch_dom.png"
fig.savefig(file_name)
42 changes: 42 additions & 0 deletions code/py/concave_map_fp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import matplotlib.pyplot as plt

x0 = 0.25
xmin, xmax = 0, 3
fs = 18

x_grid = np.linspace(xmin, xmax, 1200)

def g(x):
return 1 + 0.5 * x**0.5

xstar = 1.64

fig, ax = plt.subplots(figsize=(10, 5.5))

# Plot the functions
lb = r"$g$"
ax.plot(x_grid, g(x_grid), lw=2, alpha=0.6, label=lb)
ax.plot(x_grid, x_grid, "k--", lw=1, alpha=0.7, label=r"$45$")

# Show and annotate the fixed point
fps = (xstar,)
ax.plot(fps, fps, "go", ms=10, alpha=0.6)
ax.set_xlabel(r"$x$", fontsize=fs)
ax.annotate(r"$x^*$",
xy=(xstar, xstar),
xycoords="data",
xytext=(-20, 20),
textcoords="offset points",
fontsize=fs)

ax.legend(loc="upper left", frameon=False, fontsize=fs)
ax.set_xticks((0, 1, 2, 3))
ax.set_yticks((0, 1, 2, 3))
ax.set_ylim(0, 3)
ax.set_xlim(0, 3)

plt.show()

file_name = "../figures_py/concave_map_fp.png"
fig.savefig(file_name)
Loading

1 comment on commit 771b8bc

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.