# 12.3 Temporal Differences

To understand how reinforcement learning works, consider how to average values that arrive to an agent sequentially.

Suppose there is a sequence of numerical values, $v_{1},v_{2},v_{3},\dots$, and the goal is to predict the next value, given the previous values. One way to do this is to have a running approximation of the expected value of $v_{i}$. For example, given a sequence of students’ grades and the aim of predicting the next grade, a reasonable prediction may be to predict the average grade. This can be implemented by maintaining a running average, as follows:

Let $A_{k}$ be an estimate of the expected value based on the first $k$ data points $v_{1},\dots,v_{k}$. A reasonable estimate is the sample average:

 $A_{k}=\frac{v_{1}+\cdots+v_{k}}{k}~{}.$

Thus,

 $\displaystyle k*A_{k}$ $\displaystyle=v_{1}+\cdots+v_{k-1}+v_{k}$ $\displaystyle=(k-1)A_{k-1}+v_{k}.$

Dividing by $k$ gives

 $A_{k}=\left(1-\frac{1}{k}\right)*A_{k-1}+\frac{v_{k}}{k}~{}.$

Let $\alpha_{k}=\frac{1}{k}$; then

 $\displaystyle A_{k}=$ $\displaystyle(1-\alpha_{k})*A_{k-1}+\alpha_{k}*v_{k}$ $\displaystyle=$ $\displaystyle A_{k-1}+\alpha_{k}*(v_{k}-A_{k-1}).$ (12.1)

The difference, $v_{k}-A_{k-1}$, is called the temporal difference error or TD error; it specifies how different the new value, $v_{k}$, is from the old prediction, $A_{k-1}$. The old estimate, $A_{k-1}$, is updated by $\alpha_{k}$ times the TD error to get the new estimate, $A_{k}$. The qualitative interpretation of the temporal difference formula is that if the new value is higher than the old prediction, increase the predicted value; if the new value is less than the old prediction, decrease the predicted value. The change is proportional to the difference between the new value and the old prediction. Note that this equation is still valid for the first value, $k=1$, in which case $A_{1}=v_{1}$.

This analysis assumes that all of the values have an equal weight. However, suppose you are keeping an estimate of the expected price of some item in the grocery store. Prices go up and down in the short term, but tend to increase slowly; the newer prices are more useful for the estimate of the current price than older prices, and so they should be weighted more in predicting new prices.

In reinforcement learning, the values are estimates of the effects of actions; more recent values are more accurate than earlier values because the agent is learning, and so they should be weighted more. One way to weight later examples more is to use Equation 12.1, but with $\alpha$ as a constant ($0<\alpha\leq 1$) that does not depend on $k$. Unfortunately, this does not converge to the average value when there is variability in the values in the sequence, but it can track changes when the underlying process generating the values changes.

You could reduce $\alpha$ more slowly and potentially have the benefits of both approaches: weighting recent observations more and still converging to the average. You can guarantee convergence if

 $\sum_{k=1}^{\infty}\alpha_{k}=\infty\mbox{ ~{}~{} and ~{}~{} }\sum_{k=1}^{% \infty}\alpha_{k}^{2}<\infty.$

The first condition is to ensure that random fluctuations and initial conditions get averaged out, and the second condition guarantees convergence.

One way to give more weight to more recent experiences, but also converge to the average, is to set $\alpha_{k}=(r+1)/(r+k)$ for some $r>0$. For the first experience $\alpha_{1}=1$, so it ignores the prior $A_{0}$. If $r=9$, after 11 experiences, $\alpha_{11}=0.5$ so it weights that experience as equal to all of its prior experiences. The parameter $r$ should be set to be appropriate for the domain.

Note that guaranteeing convergence to the average is not compatible with being able to adapt to make better predictions when the underlying process generating the values keeps changing.

For the rest of this chapter, $\alpha$ without a subscript is assumed to be a constant. With a subscript it is a function of the number of cases used for the particular estimate.