Daisuke Oyama
Faculty of Economics, University of Tokyo
This notebook describes the implementation details of the DiscreteDP
class.
For the theoretical background and notation, see the lecture Discrete Dynamic Programming.
The DiscreteDP
class currently implements the following solution algorithms:
Policy iteration computes an exact optimal policy in finitely many iterations, while value iteration and modified policy iteration return an $\varepsilon$-optimal policy for a prespecified value of $\varepsilon$.
Value iteration relies on (only) the fact that the Bellman operator $T$ is a contraction mapping and thus iterative application of $T$ to any initial function $v^0$ converges to its unique fixed point $v^*$.
Policy iteration more closely exploits the particular structure of the problem, where each iteration consists of a policy evaluation step, which computes the value $v_{\sigma}$ of a policy $\sigma$ by solving the linear equation $v = T_{\sigma} v$, and a policy improvement step, which computes a $v_{\sigma}$-greedy policy.
Modified policy iteration replaces the policy evaluation step in policy iteration with "partial policy evaluation", which computes an approximation of the value of a policy $\sigma$ by iterating $T_{\sigma}$ for a specified number of times.
Below we describe our implementation of these algorithms more in detail.
(While not explicit, in the actual implementation each algorithm is terminated
when the number of iterations reaches iter_max
.)
DiscreteDP.value_iteration(v_init, epsilon, iter_max)
Given $\varepsilon > 0$,
the value iteration algorithm terminates in a finite number of iterations,
and returns an $\varepsilon/2$-approximation of the optimal value funciton and
an $\varepsilon$-optimal policy function
(unless iter_max
is reached).
DiscreteDP.policy_iteration(v_init, iter_max)
The policy iteration algorithm terminates in a finite number of iterations, and
returns an optimal value function and an optimal policy function
(unless iter_max
is reached).
DiscreteDP.modified_policy_iteration(v_init, epsilon, iter_max, k)
Given $\varepsilon > 0$,
provided that $v^0$ is such that $T v^0 \geq v^0$,
the modified policy iteration algorithm terminates in a finite number of iterations,
and returns an $\varepsilon/2$-approximation of the optimal value funciton and
an $\varepsilon$-optimal policy function
(unless iter_max
is reached).
Remarks
v_init
is not specified, it is set to the latter, $\min_{(s', a)} r(s', a))$.We illustrate the algorithms above by the simple example from Puterman (2005), Section 3.1, pp.33-35.
In [1]:
import numpy as np
import pandas as pd
from quantecon.markov import DiscreteDP
In [2]:
n = 2 # Number of states
m = 2 # Number of actions
# Reward array
R = [[5, 10],
[-1, -float('inf')]]
# Transition probability array
Q = [[(0.5, 0.5), (0, 1)],
[(0, 1), (0.5, 0.5)]] # Probabilities in Q[1, 1] are arbitrary
# Discount rate
beta = 0.95
ddp = DiscreteDP(R, Q, beta)
Analytical solution:
In [3]:
def sigma_star(beta):
sigma = np.empty(2, dtype=int)
sigma[1] = 0
if beta > 10/11:
sigma[0] = 0
else:
sigma[0] = 1
return sigma
def v_star(beta):
v = np.empty(2)
v[1] = -1 / (1 - beta)
if beta > 10/11:
v[0] = (5 - 5.5*beta) / ((1 - 0.5*beta) * (1 - beta))
else:
v[0] = (10 - 11*beta) / (1 - beta)
return v
In [4]:
sigma_star(beta=beta)
Out[4]:
In [5]:
v_star(beta=beta)
Out[5]:
Solve the problem by value iteration; see Example 6.3.1, p.164 in Puterman (2005).
In [6]:
epsilon = 1e-2
v_init = [0, 0]
res_vi = ddp.solve(method='value_iteration', v_init=v_init, epsilon=epsilon)
The number of iterations required to satisfy the termination criterion:
In [7]:
res_vi.num_iter
Out[7]:
The returned value function:
In [8]:
res_vi.v
Out[8]:
It is indeed an $\varepsilon/2$-approximation of $v^*$:
In [9]:
np.abs(res_vi.v - v_star(beta=beta)).max() < epsilon/2
Out[9]:
The returned policy function:
In [10]:
res_vi.sigma
Out[10]:
Value iteration converges very slowly. Let us replicate Table 6.3.1 on p.165:
In [11]:
num_reps = 164
values = np.empty((num_reps, n))
diffs = np.empty(num_reps)
spans = np.empty(num_reps)
v = np.array([0, 0])
values[0] = v
diffs[0] = np.nan
spans[0] = np.nan
for i in range(1, num_reps):
v_new = ddp.bellman_operator(v)
values[i] = v_new
diffs[i] = np.abs(v_new - v).max()
spans[i] = (v_new - v).max() - (v_new - v).min()
v = v_new
In [12]:
df = pd.DataFrame()
df[0], df[1], df[2], df[3] = values[:, 0], values[:, 1], diffs, spans
df.columns = '$v^i(0)$', '$v^i(1)$', \
'$\\lVert v^i - v^{i-1}\\rVert$', '$\\mathrm{span}(v^i - v^{i-1})$'
iter_nums = pd.Series(list(range(num_reps)), name='$i$')
df.index = iter_nums
display_nums = \
list(range(10)) + [10*i for i in range(1, 16)] + [160+i for i in range(4)]
df.iloc[display_nums, [0, 1, 2]]
Out[12]:
On the other hand, the span decreases faster than the norm; the following replicates Table 6.6.1, page 205:
In [13]:
df.iloc[list(range(1, 13)) + [10*i for i in range(2, 7)], [2, 3]]
Out[13]:
The span-based termination criterion is satisfied when $i = 11$:
In [14]:
epsilon * (1-beta) / beta
Out[14]:
In [15]:
spans[11] < epsilon * (1-beta) / beta
Out[15]:
In fact, modified policy iteration with $k = 0$ terminates with $11$ iterations:
In [16]:
epsilon = 1e-2
v_init = [0, 0]
k = 0
res_mpi_1 = ddp.solve(method='modified_policy_iteration',
v_init=v_init, epsilon=epsilon, k=k)
In [17]:
res_mpi_1.num_iter
Out[17]:
In [18]:
res_mpi_1.v
Out[18]:
If $\{\sigma^i\}$ is the sequence of policies obtained by policy iteration with an initial policy $\sigma^0$, one can show that $T^i v_{\sigma^0} \leq v_{\sigma^i}$ ($\leq v^*$), so that the number of iterations required for policy iteration is smaller than that for value iteration at least weakly, and indeed in many cases, the former is significantly smaller than the latter.
In [19]:
v_init = [0, 0]
res_pi = ddp.solve(method='policy_iteration', v_init=v_init)
In [20]:
res_pi.num_iter
Out[20]:
Policy iteration returns the exact optimal value function (up to rounding errors):
In [21]:
res_pi.v
Out[21]:
In [22]:
np.abs(res_pi.v - v_star(beta=beta)).max()
Out[22]:
To look into the iterations:
In [23]:
v = np.array([0, 0])
sigma = np.array([-1, -1]) # Dummy
sigma_new = ddp.compute_greedy(v)
i = 0
while True:
print('Iterate {0}'.format(i))
print(' value: {0}'.format(v))
print(' policy: {0}'.format(sigma_new))
if np.array_equal(sigma_new, sigma):
break
sigma[:] = sigma_new
v = ddp.evaluate_policy(sigma)
sigma_new = ddp.compute_greedy(v)
i += 1
print('Terminated')
See Example 6.4.1, pp.176-177.
The evaluation step in policy iteration which solves the linear equation $v = T_{\sigma} v$ to obtain the policy value $v_{\sigma}$ can be expensive for problems with a large number of states. Modified policy iteration is to reduce the cost of this step by using an approximation of $v_{\sigma}$ obtained by iteration of $T_{\sigma}$. The tradeoff is that this approach only computes an $\varepsilon$-optimal policy, and for small $\varepsilon$, takes a larger number of iterations than policy iteration (but much smaller than value iteration).
In [24]:
epsilon = 1e-2
v_init = [0, 0]
k = 6
res_mpi = ddp.solve(method='modified_policy_iteration',
v_init=v_init, epsilon=epsilon, k=k)
In [25]:
res_mpi.num_iter
Out[25]:
The returned value function:
In [26]:
res_mpi.v
Out[26]:
It is indeed an $\varepsilon/2$-approximation of $v^*$:
In [27]:
np.abs(res_mpi.v - v_star(beta=beta)).max() < epsilon/2
Out[27]:
To look into the iterations:
In [28]:
epsilon = 1e-2
v = np.array([0, 0])
k = 6
i = 0
print('Iterate {0}'.format(i))
print(' v: {0}'.format(v))
sigma = np.empty(n, dtype=int) # Store the policy function
while True:
i += 1
u = ddp.bellman_operator(v, sigma=sigma)
diff = u - v
span = diff.max() - diff.min()
print('Iterate {0}'.format(i))
print(' sigma: {0}'.format(sigma))
print(' T_sigma(v): {0}'.format(u))
print(' span: {0}'.format(span))
if span < epsilon * (1-ddp.beta) / ddp.beta:
v = u + ((diff.max() + diff.min()) / 2) * \
(ddp.beta / (1 - ddp.beta))
break
ddp.operator_iteration(ddp.T_sigma(sigma), v=u, max_iter=k)
v = u
print(' T_sigma^k+1(v): {0}'.format(v))
print('Terminated')
print(' sigma: {0}'.format(sigma))
print(' v: {0}'.format(v))
Compare this with the implementation with the norm-based termination rule as described in Example 6.5.1, pp.187-188.
In [ ]: