optgrowth_solutions-checkpoint


quant-econ Solutions: Infinite Horizon Dynamic Programming


In [1]:
%matplotlib inline

In [2]:
import numpy as np
import matplotlib.pyplot as plt
from quantecon import GrowthModel, compute_fixed_point

Exercise 1


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))


Computed iterate 1 with error 4.296600
Computed iterate 2 with error 4.080228
Computed iterate 1 with error 4.296600
Computed iterate 2 with error 4.080228
Computed iterate 3 with error 3.875034
Computed iterate 4 with error 3.680327
Computed iterate 1 with error 4.296600
Computed iterate 2 with error 4.080228
Computed iterate 3 with error 3.875034
Computed iterate 4 with error 3.680327
Computed iterate 5 with error 3.495502
Computed iterate 6 with error 3.327466

Exercise 2


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')


Computed iterate 1 with error 5.999396
Computed iterate 2 with error 3.791226
Computed iterate 3 with error 2.531863
Computed iterate 4 with error 1.767013
Computed iterate 5 with error 1.303066
Computed iterate 6 with error 1.011176
Computed iterate 7 with error 0.816201
Computed iterate 8 with error 0.689199
Computed iterate 9 with error 0.590297
Computed iterate 10 with error 0.514201
Computed iterate 11 with error 0.454587
Computed iterate 12 with error 0.402620
Computed iterate 13 with error 0.359115
Computed iterate 14 with error 0.321714
Computed iterate 15 with error 0.288297
Computed iterate 16 with error 0.258360
Computed iterate 17 with error 0.232150
Computed iterate 18 with error 0.208786
Computed iterate 19 with error 0.187838
Computed iterate 20 with error 0.169022
Computed iterate 1 with error 4.401010
Computed iterate 2 with error 3.153136
Computed iterate 3 with error 2.962891
Computed iterate 4 with error 2.784289
Computed iterate 5 with error 2.616550
Computed iterate 6 with error 2.458976
Computed iterate 7 with error 2.318565
Computed iterate 8 with error 2.171183
Computed iterate 9 with error 2.042677
Computed iterate 10 with error 1.921553
Computed iterate 11 with error 1.806962
Computed iterate 12 with error 1.698904
Computed iterate 13 with error 1.597220
Computed iterate 14 with error 1.494743
Computed iterate 15 with error 1.405415
Computed iterate 16 with error 1.321367
Computed iterate 17 with error 1.242295
Computed iterate 18 with error 1.167909
Computed iterate 19 with error 1.097938
Computed iterate 20 with error 1.032124
Computed iterate 1 with error 7.118648
Computed iterate 2 with error 6.974372
Computed iterate 3 with error 6.833300
Computed iterate 4 with error 6.702746
Computed iterate 5 with error 6.564332
Computed iterate 6 with error 6.427286
Computed iterate 7 with error 6.299795
Computed iterate 8 with error 6.173511
Computed iterate 9 with error 6.048887
Computed iterate 10 with error 5.924557
Computed iterate 11 with error 5.805184
Computed iterate 12 with error 5.687164
Computed iterate 13 with error 5.570272
Computed iterate 14 with error 5.453828
Computed iterate 15 with error 5.338835
Computed iterate 16 with error 5.225067
Computed iterate 17 with error 5.112733
Computed iterate 18 with error 5.001816
Computed iterate 19 with error 4.892653
Computed iterate 20 with error 4.784860
Out[4]:
<matplotlib.legend.Legend at 0x461f250>

In [ ]: