Solutions for http://quant-econ.net/dp_intro.html
In [1]:
%matplotlib inline
In [2]:
import numpy as np
import matplotlib.pyplot as plt
from quantecon import GrowthModel, compute_fixed_point
In [3]:
alpha, beta = 0.65, 0.95
gm = GrowthModel()
true_sigma = (1 - alpha * beta) * gm.grid**alpha
w = 5 * gm.u(gm.grid) - 25 # Initial condition
fig, ax = plt.subplots(3, 1, figsize=(8, 10))
for i, n in enumerate((2, 4, 6)):
ax[i].set_ylim(0, 1)
ax[i].set_xlim(0, 2)
ax[i].set_yticks((0, 1))
ax[i].set_xticks((0, 2))
v_star = compute_fixed_point(gm.bellman_operator, w, max_iter=n)
sigma = gm.compute_greedy(v_star)
ax[i].plot(gm.grid, sigma, 'b-', lw=2, alpha=0.8, label='approximate optimal policy')
ax[i].plot(gm.grid, true_sigma, 'k-', lw=2, alpha=0.8, label='true optimal policy')
ax[i].legend(loc='upper left')
ax[i].set_title('{} value function iterations'.format(n))
In [4]:
from scipy import interp
gm = GrowthModel()
w = 5 * gm.u(gm.grid) - 25 # To be used as an initial condition
discount_factors = (0.9, 0.94, 0.98)
series_length = 25
fig, ax = plt.subplots(figsize=(8,5))
ax.set_xlabel("time")
ax.set_ylabel("capital")
for beta in discount_factors:
# Compute the optimal policy given the discount factor
gm.beta = beta
v_star = compute_fixed_point(gm.bellman_operator, w, max_iter=20)
sigma = gm.compute_greedy(v_star)
# Compute the corresponding time series for capital
k = np.empty(series_length)
k[0] = 0.1
sigma_function = lambda x: interp(x, gm.grid, sigma)
for t in range(1, series_length):
k[t] = gm.f(k[t-1]) - sigma_function(k[t-1])
ax.plot(k, 'o-', lw=2, alpha=0.75, label=r'$\beta = {}$'.format(beta))
ax.legend(loc='lower right')
Out[4]:
In [ ]: