Þ ¥bonds€¬cell_resultsÞ ¹Ù$77ca116d-675d-4db5-8a68-53d1085528f4Цqueued¤logs�§running¦output†¤body ¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ•ušùn°persist_js_state·has_pluto_hook_features§cell_idÙ$77ca116d-675d-4db5-8a68-53d1085528f4¹depends_on_disabled_cells§runtimeÎ )g"µpublished_object_keys�¸depends_on_skipped_cellsçerroredÂÙ$4b7b7bb6-8484-42ac-983f-ec33dbf2c73eЦqueued¤logs�§running¦output†¤bodyÚi ¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ•u‡†/°persist_js_state·has_pluto_hook_features§cell_idÙ$4b7b7bb6-8484-42ac-983f-ec33dbf2c73e¹depends_on_disabled_cells§runtimeÎ)錵published_object_keys‘Ù57cd57c4c-38f6-11f0-1841-758f50f1faa4/1672b64eae5eaa24¸depends_on_skipped_cellsçerroredÂÙ$c4916313-d4f0-443c-a81e-05d2b765acf0Цqueued¤logs�§running¦output†¤bodyÚi ¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ•y$ìN°persist_js_state·has_pluto_hook_features§cell_idÙ$c4916313-d4f0-443c-a81e-05d2b765acf0¹depends_on_disabled_cells§runtimeΠ˵published_object_keys‘Ù57cd57c4c-38f6-11f0-1841-758f50f1faa4/47c514b709eef6c0¸depends_on_skipped_cellsçerroredÂÙ$6e6b9d64-2d90-40a4-abde-2fd0d6ab7d7aЦqueued¤logs�§running¦output†¤bodyÚi ¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ•xñvd°persist_js_state·has_pluto_hook_features§cell_idÙ$6e6b9d64-2d90-40a4-abde-2fd0d6ab7d7a¹depends_on_disabled_cells§runtimeÎÐ`¢µpublished_object_keys‘Ù57cd57c4c-38f6-11f0-1841-758f50f1faa4/6889c420c5601634¸depends_on_skipped_cellsçerroredÂÙ$29364905-2458-426a-999c-210cd3c60263Цqueued¤logs�§running¦output†¤bodyÙ;
The importance sampling ratio weights samples from one distribution so sample statistics can match the target distribution.
$$\rho_t \doteq \rho_{t:t} = \frac{\pi(A_t \vert S_t)}{b(A_t \vert S_t)} \tag{11.1}$$
We can use the importance sampling ratio in the semi-gradient weight updates from before to try to implement an off-policy parameter update:
$$\mathbf{w}_{t+1} \doteq \mathbf{w}_t + \alpha \rho_t \delta_t \nabla \hat v(S_t \vert \mathbf{w}_t) \tag{11.2}$$
where $\delta_t$ is the error term depending on the target value such as the TD(0) discounted reward:
$$\delta_t \doteq R_{t+1} + \gamma \hat v (S_{t+1}, \mathbf{w}_t) - \hat v(S_t, \mathbf{w}_t) \tag{11.3}$$
or the average reward for a continuing task:
$$\delta_t \doteq R_{t+1} - \bar R_t + \hat v(S_{t+1}, \mathbf{w}_t) - \hat v (S_t, \mathbf{w}_t) \tag{11.4}$$
For action-values and expected updates, we do not need to use the importance sampling ratio (as we don't in Q-learning) since the bootstrap update does not depend on the actual action taken. However, in the tabular case each state estimation was independent so this method converged even in the off policy case. In the case of function approximation, the target value is actually the value-error which depends on the on-policy distribution. So if the samples appear according to the off-policy distriution this method may still not converge to the correct values as we will see in some later examples.
$$\mathbf{w}_{t+1} \doteq \mathbf{w}_t + \alpha \delta_t \nabla \hat q(S_t, A_t, \mathbf{w}_t) \tag{11.5}$$
$$\delta_t \doteq R_{t+1} + \gamma \sum_a \pi(a \vert S_{t + 1})\hat q(S_{t+1}, a, \mathbf{w}_t) - \hat q (S_t, A_t, \mathbf{w}_t) \tag{discounted expected sarsa}$$
$$\delta_t \doteq R_{t+1} - \bar R_t + \sum_a \pi(a \vert S_{t + 1})\hat q(S_{t+1}, a, \mathbf{w}_t) - \hat q (S_t, A_t, \mathbf{w}_t) \tag{average reward expected sarsa}$$
Using the true values, we can write down the value error objective and directly calculate the parameters which minimize it by taking the gradient:
$$\overline{\text{VE}} = \frac{1}{4} \left ( (w_A - 1)^2 + (w_A - 0)^2 + (w_B - 1)^2 + (w_C - 0)^2 \right ) = \frac{1}{4} \left ( 2(w_A^2 - w_A) + 1 + (w_B - 1)^2 + w_C^2 \right )$$
$$\begin{flalign} \frac{\partial \overline{\text{VE}}}{\partial w_A} &= \frac{1}{2} \left ( 2 w_A - 1 \right ) \\ \frac{\partial \overline{\text{VE}}}{\partial w_B} &= \frac{1}{2} \left (w_B - 1 \right )\\ \frac{\partial \overline{\text{VE}}}{\partial w_C} &= \frac{1}{2} w_C\\ \end{flalign}$$
Setting each of these to zero reveals the unique solution which minimizes the value error:
$$\begin{flalign} w_A &= \frac{1}{2} \\ w_B &= 1 \\ w_C &= 0 \\ \end{flalign}$$
which is the same solution as before, expect now the value error is not zero but rather $\frac{1}{4}(2(\frac{1}{4} - \frac{1}{2}) + 1) = \frac{1}{8}$. An exact solution is not possible since we have one too few parameters, but the whole purpose of approximation is to define what we mean by a good solution that cannot be exact. The data with respect to the parameters is the same as would be for the A-split example, and if this data did come from that problem then the value error would be 0. The fact that two distinct problems can appear the same but have different true value error might seem to be a problem, but the difference is value error is always a variance term which does not depend on the parameters but only on the stochastic nature of the problem. From that perspective, the A-split example has zero value error for these parameters because the true value from state A is always $\frac{1}{2}$ while the the A-presplit example the true value of A1 and A2 is not $\frac{1}{2}$ but that information is hidden from the approximation.
TDC Learned Policy
Total Reward: -173.0f0
Notice there is a point where the Bellman operator produces the true value function. At this point the Bellman Error equals the value error though neither of them are minimized. It is unclear if other problems have a point in general for which this occurs. The point approximation point is just something for which if the values were initialized there then one sweep of dynamic programming would produce the correct value. Note that the projected Bellman error vector also suggests a direction of change for the parameter which is used when we do semi-gradient TD(0). You could also project the value error and the point for which this is 0 is also the point of minimum VE. The problem with this objective is that we cannot observe the true value. The Bellman operator on the approximation however can always be evaluated from the environment or if not that then at least a sample of it. There is also a point for which the mean squared TD error is equal to the mean squared Bellman error. We can see the two vectors that make up the TD error together are equivalent to the Bellman Operator. With the TD error we minimize the sum of the lengths of these vectors whereas minimizing the Bellman error is minimizing the length of the sum of the vectors. When these equal, the Bellman operator points along the line of approximation functions. So at this particular w, the Bellman operator produces another value function which is in the approximation space.
Exercise 11.2
Convert the equations of $n\text{-step} \; Q(\sigma)$ (7.11 and 7.17) to semi-gradient form. Give definitions that cover both the episodic and continuing cases.
$$\begin{flalign} Q_{t+n} & \doteq Q_{t+n-1}(S_t, A_t) + \alpha \rho_{t+1:t+n}[G_{t:t+n} - Q_{t+n-1}(S_t, A_t)] \tag{7.11}\\ G_{t:h} & \doteq R_{t+1} + \gamma \left ( \sigma_{t+1}\rho_{t+1} + (1 - \sigma_{t+1} \pi(A_{t+1}|S_{t+1}) \right ) \left ( G_{t+1:h} - Q_{h-1}(S_{t+1}, A_{t+1}) \right )\\ & + \gamma \bar V_{h-1}(S_{t+1}) \tag{7.17}\\ \bar V_t(s) & \doteq \sum_a \pi(a|s)Q_t(s, a) \end{flalign}$$
To convert this to a semi-gradient method we need to provide update equations for the weight vector that defines the value function approximation.
$$\begin{flalign} \mathbf{w}_{t+n} &\doteq \mathbf{w}_{t+n-1} + \alpha \rho_{t+1} \cdots \rho_{t+n} [G_{t:t+n} - \hat q(S_{t}, A_t, \mathbf{w}_{t+n-1})]\nabla \hat q (S_t, A_t, \mathbf{w}_{t+n-1})\\ G_{t:h} &\doteq R_{t+1} + \gamma \left ( \sigma_{t+1}\rho_{t+1} + (1 - \sigma_{t+1} \pi(A_{t+1}|S_{t+1}) \right ) \left ( G_{t+1:h} - \hat q(S_{t+1}, A_{t+1}, \mathbf{w}_{h-1}) \right )\\ & + \gamma \overline{V}_{h-1}(S_{t+1}), \text{\; for } t < h \leq T \tag{episodic}\\ G_{t:h} &\doteq R_{t+1} - \bar R_{t+1} + \gamma \left ( \sigma_{t+1}\rho_{t+1} + (1 - \sigma_{t+1} \pi(A_{t+1}|S_{t+1}) \right ) \left ( G_{t+1:h} - \hat q(S_{t+1}, A_{t+1}, \mathbf{w}_{h-1}) \right )\\ & + \gamma \bar V_{h-1}(S_{t+1}), \text{\; for } t < h \leq T \tag{continuing}\\ \overline{V}_t(s) & \doteq \sum_a \pi(a|s)\hat q(s, a, \mathbf{w}_t) \end{flalign}$$
There are 7 states, 2 actions, and 8 parameters. We can analytically write down the different errors in terms of the 8 parameter values for the target policy which always takes the solid action. Also, the on-policy distribution for the target policy is $\mu(7) = 1$ so we only need to consider there error for that state.
The true values are 0 for every state, so the value error is simply:
$$\overline{\text{VE}} = (w_7 + 2w_8)^2$$
which is minimized when $w_7 = -2w_8$
$$(0 + \gamma (w_7 + 2 w_8) - w_7 - 2 w_8)^2 = ((w_7 + 2w_8)(\gamma - 1))^2$$
If we minimize this with respect to $w_7$ and $w_8$
$$0 = 2(\gamma - 1)(w_7 + 2w_8) \implies w_7 = -2w_8$$
$$0 = 4(\gamma - 1)(w_7 + 2w_8) \implies w_7 = -2w_8$$
Normally, there are fewer parameters than states, so there is a single projection point which minimizes the distance from any true state value function to the approximation function. Here we have one more parameter, so there is a whole family of projections that are equally good and reproduce the exact value function. Again, the only state value error that matters is for state 7 whose value is given as
$$\begin{flalign} v_7 &= w_7 + 2w_8 \\ \end{flalign}$$
This distance is 0 for any parameters that satisfy this equality. To calculate the mean squared projected Bellman Error, we first take state 7 and apply the Bellman operator to it:
$$B_\pi(\hat v_7) = \gamma(w_7 + 2 w_8)$$
Given this new value, which parameters satisfy the projection? We can pick any new value which satisfies $\gamma (w_7 + 2 w_8) = w_7^\prime + 2w_8^\prime$. One easy option that satisfies this relationship is just $w_7^\prime = \gamma w_7$ and $w_8^\prime = \gamma w_8$ so each parameter can just be multiplied by $\gamma$ to find the projection point. Now the projected Bellman error is the distance between these two values:
$$\overline{\text{PBE}} = ((w_7 + 2w_8) - \gamma (w_7 + 2 w_8))^2 = ((w_7 + 2 w_8)(1-\gamma))^2 = \overline{\text{VE}} (1-\gamma)^2$$
which is the same as the Bellman Error since the projection in this case can just match any value from the Bellman operator, and we already know this is minimized when $w_7 = -2w_8$ which is the same value that minimizes the value error.
Consider the value error under this approximation:
$$\begin{flalign} \overline{\text{VE}} &= \frac{1}{2} \left [ (\hat v_1 - v_1)^2 + (\hat v_2 - v_2)^2\right ] \\ &= \frac{1}{2}\left [\left ( w - \frac{\gamma}{1 - \gamma} \right )^2 + \left (cw - \frac{2-\gamma}{1-\gamma} \right )^2 \right ] \\ \end{flalign}$$
Our goal is to find the value of $w$ that minimizes the value error and we can use gradient updates to do this with the derivative:
$$\frac{\partial \overline{\text{VE}}}{\partial w} = w - \frac{\gamma}{1 - \gamma} + c^2w - c\frac{2-\gamma}{1-\gamma}$$
Setting this equal to 0 implies:
$$\begin{flalign} w(1 + c^2) &= \frac{\gamma + 2c - c \gamma}{1 - \gamma} \\ &\therefore \\ w &= \frac{\gamma + 2c - c \gamma}{(1 - \gamma)(1 + c^2)} \\ &= \frac{\gamma(1 - c) + 2c}{(1 - \gamma)(1 + c^2)} \\ \end{flalign}$$