---
abstract: 'This paper investigates estimating the variance of a temporal-difference learning agent’s update target. Most reinforcement learning methods use an estimate of the value function, which captures how good it is for the agent to be in a particular state and is mathematically expressed as the expected sum of discounted future rewards (called the return). These values can be straightforwardly estimated by averaging batches of returns using Monte Carlo methods. However, if we wish to update the agent’s value estimates during learning–before terminal outcomes are observed–we must use a different estimation target called the $\lambda$-return, which truncates the return with the agent’s own estimate of the value function. Temporal difference learning methods estimate the expected $\lambda$-return for each state, allowing these methods to update online and incrementally, and in most cases achieve better generalization error and faster learning than Monte Carlo methods. Naturally one could attempt to estimate higher-order moments of the $\lambda$-return. This paper is about estimating the variance of the $\lambda$-return. Prior work has shown that given estimates of the variance of the $\lambda$-return, learning systems can be constructed to (1) mitigate risk in action selection, and (2) automatically adapt the parameters of the learning process itself to improve performance. Unfortunately, existing methods for estimating the variance of the $\lambda$-return are complex and not well understood empirically. We contribute a method for estimating the variance of the $\lambda$-return directly using policy evaluation methods from reinforcement learning. Our approach is significantly simpler than prior methods that independently estimate the second moment of the $\lambda$-return. Empirically our new approach behaves at least as well as existing approaches, but is generally more robust.'
bibliography:
- 'main.bib'
title: 'Directly Estimating the Variance of the $\lambda$-Return Using Temporal-Difference Methods'
---

Introduction
============

![Each TD node takes as input a reward ${R}$, a discounting function $\gamma$, and features $\phi$. For the direct method (**top**) the squared TD error of the first-stage value estimator is used as the [meta-reward]{} for the second-stage [$V$]{} estimator. For VTD (**bottom**), a more complex computation is used for the [meta-reward]{} and an extra stage of computation is required. []{data-label="fig:networks"}](combined_graphic.pdf){width="\linewidth"}

Conventionally in reinforcement learning, the agent estimates the expected value of the return—the discounted sum of future rewards, as an intermediate step to find an optimal policy. Given a trajectory of experience, the agent can average the returns observed from each state. To estimate the value function online—while the trajectory unfolds—we update the agent’s value estimates towards the expected $\lambda$-return. The $\lambda$-return has the same expected value as the return, but can be estimated online using a memory trace. Algorithms that estimate the expected value of the $\lambda$-return are called temporal-difference learning methods. The first moment, however, is not the only statistic that can be estimated. In addition to the expected value, we could estimate the variance of the $\lambda$-return.

An estimate of the variance of the $\lambda$-return can be used in several ways to improve estimation and decision-making. @Sato2002 [@Ghavamzadeh; @Tamar2012; @Tamar2013b] use an estimate of the variance of the $\lambda$-return to design algorithms that account for risk in decision making. Specifically they formulate the agent’s objective as maximizing reward, while minimizing the variance of the $\lambda$-return. @White2016b estimated the variance of the $\lambda$-return, [$V$]{}, to automatically adapt the trace-decay parameter, $\lambda$, used in learning updates. This resulted in faster learning for the agent, but more importantly removed the need to tune $\lambda$ by hand.

The variance [$V$]{} can be estimated directly or indirectly. Indirect estimation involves estimating the first moment (the value ${J}$) and second moment ($M$) of the return and taking their difference as: ${V}(s)=M(s)-{J}(s)^2$. @Sobel1982 were the first to formulate a Bellman operators for $M$. Later @Tamar2016 [@Tamar2013b; @Ghavamzadeh], extended @Sobel1982’s approach to estimating the variance for $\lambda = 0$ to $\lambda =1$. Finally, @White2016b introduced an estimation method called VTD, that supports off-policy learning [@Sutton2009; @Maei2011], state-dependent discounts and state-dependent trace-decay parameters. An alternative approach is to estimate the variance of the $\lambda$-return [$V$]{} directly. This has been considered by @Tamar2012, but they were unable to derive a Bellman operator—instead giving a Bellman-like operator—and considered only cost-to-go problems.

In this paper, we show that one can use temporal-difference learning, a online method for estimating value functions [@Sutton1988], to estimate [$V$]{} directly. Our new method supports off-policy learning, state-dependent discounts, and state-dependent trace-decay parameters. We introduce a new Bellman operator for the variance of the $\lambda$-return, and further prove that even for a value function that does not satisfy the Bellman operator for the expected $\lambda$-return, the error in this recursive formulation is proportional to the error in the value function approximation. Interestingly, the Bellman operator for the second moment requires an unbiased estimate of the $\lambda$-return [@White2016b]; our Bellman operator for the variance avoids this term, and so has a simpler update. Both our direct method and VTD can be seen as a network of two TD estimators running sequentially (Figure \[fig:networks\]). Our goal is to understand the empirical properties of the direct and indirect approaches for estimating variance, as neither have yet been thoroughly studied. In general, we found that direct estimation is just as good as VTD, and in many cases better. Specifically, we observe that the direct approach is better behaved in the early stages of learning before the value function has converged. Further, we observe that the variance of the [$V$]{} estimates can be higher for VTD under several circumstances: (1) when there is a mismatch in step-size between the value estimator and the [$V$]{} estimator, (2) when traces are used with the value estimator, (3) when estimating [$V$]{} of the off-policy return, and (4) when there is error in the value estimate. Overall, we conclude that the direct approach to estimating [$V$]{} is both simpler and better behaved than VTD.

The MDP Setting
===============

We model the agent’s interaction with the environment as a finite Markov decision process (MDP) consisting of a finite set of states ${\mathcal{S}}$, a finite set of actions, ${\mathcal{A}}$, and a transition model $p: {\mathcal{S}}\times {\mathcal{S}}\times {\mathcal{A}}\rightarrow [0,1]$ defining the probability $p(s'|s, a)$ of transition from state $s$ to $s'$ when taking action $a$. In the policy evaluation setting considered in this paper, the agent follows a fixed policy $\pi(a|s)\in [0,1]$ that provides the probability of taking action $a$ in state $s$. At each timestep the agent receives a random reward $R_{t+1}$, dependent only on $S_t, A_t, S_{t+1}$. The return is the discounted sum of future rewards $$\begin{aligned}
\label{eq:MCreturn}
G_{t}&={R}_{t+1} + \gamma_{t+1}{R}_{t+2} + \gamma_{t+1}\gamma_{t+2}{R}_{t+3} + \ldots \\
&={R}_{t+1} + \gamma_{t+1}G_{t+1}.
\end{aligned}$$ The discount function $\gamma: {\mathcal{S}}\rightarrow [0,1]$, with $\gamma_{t}\equiv\gamma(S_t)$, provides a variable level of discounting depending on the state [@Sutton2011]. The value of a state, ${j}(s)$, is defined as the expected return from state $s$ under a particular policy $\pi$ $$\begin{aligned}
{j}(s)=&{\mathbb{E}}_{\pi}[G_t|S_t=s].
\label{eq:value}\end{aligned}$$ We use ${j}$ to indicate the true value function and ${J}$ the estimate. The TD-error is the difference between the one-step approximation and the current estimate: $$\begin{aligned}
\delta_t={R}_{t+1}+\gamma_{t+1}{J}_t(S_{t+1})-{J}_t(S_t).
\label{eq:TDerr}\end{aligned}$$ The $\lambda$-return $$G_t^{\lambda}={R}_{t+1}+\gamma_{t+1}(1-\lambda_{t+1}){J}_t(S_{t+1}) +\gamma_{t+1}\lambda_{t+1} G^{\lambda}_{t+1}
$$ provides a bias-variance trade-off by incorporating ${J}$, which is a potentially lower-variance but biased estimate of the return. This trade-off is determined by a state-dependent trace-decay parameter, $\lambda_t\equiv\lambda(S_t) \in [0,1]$. When ${J}_t(S_{t+1})$ is equal to the expected return from $S_{t+1}=s$, then ${\mathbb{E}}_{\pi}[(1-\lambda_{t+1}){J}_t(S_{t+1})
 +\gamma_{t+1}\lambda_{t+1} G^{\lambda}_{t+1}|S_{t+1}=s] = {\mathbb{E}}_{\pi}[G^{\lambda}_{t+1}|S_{t+1}=s]$, and so the $\lambda$-return is unbiased. Beneficially, however, the expected value ${J}_t(S_{t+1})$ is lower-variance than the sample $G^{\lambda}_{t+1}$. If ${J}_t$ is inaccurate, however, some bias is introduced. Therefore, when $\lambda=0$, the $\lambda$-return is lower-variance but can be biased. When $\lambda=1$, the $\lambda$-return equals the Monte Carlo return (Equation ); in this case, the update target exhibits more variance, but no bias. In the tabular setting evaluated in this paper, $\lambda$ does not affect the fixed point solution of the value estimate, only the rate at which learning occurs. It does, however, affect the observed variance of the return, which we estimate. The $\lambda$-return is implemented using traces as in the following [TD($\lambda$) ]{}algorithm, shown with accumulating traces: $$\begin{aligned}
    {E}_t(s)&\leftarrow\begin{cases}
      \gamma_t\lambda_t {E}_{t-1}(s) + 1 & s=S_t \\
      \gamma_t\lambda_t {E}_{t-1}(s) & \forall s \in {\mathcal{S}}, s \ne S_t
   \end{cases}\\
{J}_{t+1}(S_t)&\leftarrow {J}_t(S_t) + \alpha\delta_t {E}_t(S_t)
\end{aligned}
\label{eq:td_replacing_traces}$$

Estimating the Variance of the Return {#sec:derivation}
=====================================

When estimating [$V$]{}, we have both a value estimator and a variance estimator. The *value estimator* provides an estimate of the expected $\lambda$-return, known as the policy evaluation problem. The *variance estimator* provides an estimate of the variance of the $\lambda$-return. We show below how we can similarly use any TD method to learn the variance estimator, such as TD with accumulating traces (Equation \[eq:td\_replacing\_traces\]).

Because we have two separate TD estimators—one each for [$J$]{} and [$V$]{} — they can select different trace-decay parameters for learning. In fact, as done by @White2016b, the value estimator can use a different trace-decay parameter than the $\lambda$-return for which we are estimating [$V$]{}. This is because the $\lambda$-return is defined for any given value function, regardless of how that value function is estimated. There are three possible trace-decay parameters: 1) the $\lambda$ of the $\lambda$-return for which variance is being estimated, 2) that used by the traces of the value estimator (${\kappa}$), 3) that used by the traces of the variance estimator (${\bar{\kappa}}$).

We summarize the notation here for easy reference. Variables without the bar refer to the value estimator and variables with bars refer to the variance estimator. $$\begin{aligned}
{j}-&\text{true value function of the target policy $\pi$.}\\
{J}-&\text{estimate of ${j}$.}\\
{R}-&\text{reward used in the value function estimate.}\\
\bar{{R}}-&\text{{meta-reward}{} used in the variance estimate.}\\
{\lambda}-&\text{bias-variance parameter of the target $\lambda$-return.}\\
{\kappa}-&\text{trace-decay parameter of the value estimator.}\\
{\bar{\kappa}}-&\text{trace-decay parameter of the secondary estimator.}\\
\gamma -&\text{discounting function used by the value estimator.}\\
\bar{\gamma} -&\text{discounting function used by the variance estimator.}\\
\delta_t -&\text{TD error of the value function at time $t$.}\\
\bar{\delta_t} -&\text{TD error of the variance estimator at time $t$.}\\
{M}-&\text{estimate of the second moment.}\\
{v}-&\text{true variance of the return.}\\
{V}-&\text{estimate of ${v}$.}\end{aligned}$$ Our direct algorithm, shown here, uses TD(0) to estimate variance. For an expanded implementation with traces in the off-policy setting see Appendix \[appendix:general\].\
\
**Direct Variance Algorithm** $$\begin{aligned}
\bar{\gamma}_{t+1}& \leftarrow \gamma_{t+1}^2 {\lambda}_{t+1}^2\\
\bar{{R}}_{t+1}& \leftarrow \delta_t^2\\
\bar{\delta}_t& \leftarrow \bar{{R}}_{t+1} + \bar{\gamma}_{t+1}{V}_t(s') - {V}_t(s)\\
{V}_{t+1}(s)&\leftarrow {V}_t(s)+\bar{\alpha} \bar{\delta}_t
\end{aligned}
\label{alg:direct}$$ An alternative to this direct method is to instead estimate the second moment. The variant shown here is equivalent to on-policy VTD with no traces, ${\bar{\kappa}}=0$, and the step-size for the second set of weights set to 0. Further, the Tamar TD(0) algorithm ([@Tamar2016]) can be recovered from Equation \[alg:comparison\] by using ${\kappa}=0, {\lambda}=1, {\bar{\kappa}}=0$. This algorithm does not impose that the variance be non-negative.\
\
**Second Moment Algorithm (VTD)** $$\begin{aligned}
\bar{\gamma}_{t+1}\leftarrow&\gamma_{t+1}^2 {\lambda}_{t+1}^2 \nonumber\\
\bar{{R}}_{t+1}\leftarrow&({R}_{t+1}+\gamma_{t+1}{J}_{t+1}(s'))^2- \bar{\gamma}_{t+1}{J}_{t+1}(s')^2 \nonumber\\
\bar{\delta}_t\leftarrow&\bar{{R}}_{t+1} + \bar{\gamma}_{t+1}{M}_t(s') - {M}_t(s) \label{alg:comparison}
\\
{M}_{t+1}(s)\leftarrow&{M}_t(s)+\bar{\alpha} \bar{\delta}_t \nonumber\\
{V}_{t+1}(s) =& {M}_{t+1}(s)-{J}_{t+1}(s)^2 \nonumber\end{aligned}$$

Derivation of the Direct Method
===============================

The derivation of the direct method follows from characterizing the Bellman operator for the variance of the $\lambda$-return. Theorem \[recurse\_var\] gives a Bellman equation for the variance ${v}$. It has precisely the form of a TD target with [meta-reward]{} $\bar{\delta}_t=\delta_t^2$ and discounting function $\bar{\gamma}_{t+1}=\gamma_{t+1}^2\lambda_{t+1}^2$. Therefore, we can conveniently estimate [$V$]{} using TD methods. Further, we show that even when the value function does not satisfy the Bellman equation, this results only in a proportional error in the variance estimator. We first show the result for the on-policy setting, for simplicity; the more general off-policy algorithm is provided in Appendix \[appendix:general\]

This result provides the first general Bellman operator directly for the variance. The Bellman operators for the variance are general, in that they allow for either the episodic or continuing setting, by using variable $\gamma$. Interestingly, by directly estimating variance, we avoid a second term in the cumulant, that is present in approaches that estimate the second moment [@Tamar2013b; @Tamar2016; @White2016b]. While @Tamar2012 also developed an approach to directly estimate the variance, their method defined a non-linear Bellman operator and is restricted to cost-to-go problems. Follow-up work moved to estimating the second-moment instead [@Tamar2013b; @Tamar2016], but with simplifying assumptions that only considered expected reward from a state and assuming $\lambda = 1$. The work developing VTD generalizes to any $\lambda$, but does not characterize error when using an inaccurate value function. To have a well-defined solution to the fixed point, we need the discount to be less than one for some transition [@White2017; @Yu2015]. This corresponds to assuming that the policy is proper, for the cost-to-go setting [@Tamar2016].

The policy reaches a state $s$ where $\gamma(s) < 1$ in a finite number of steps.

For any $s \in {\mathcal{S}}$, \[recurse\_var\] $$\begin{aligned}
{j}(s)&={\mathbb{E}}\Big[ R_{t+1} + \gamma_{t+1} {J}(S_{t+1})\ | \ S_t=s\Big] \nonumber\\
{v}(s) 
&={\mathbb{E}}\Big[\delta_t^2+\gamma_{t+1}^2\lambda_{t+1}^2{v}(S_{t+1})\ | \ S_t=s\Big] \label{eq_bellman_var}
$$ Further, for approximate value function ${J}$, if there is an $\epsilon: {\mathcal{S}}\rightarrow [0,\infty)$ bounding value estimates $({J}(s) - {j}(s))^2 \le \epsilon(s)$ and covariance terms $|{{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} ({j}(S_{t+1}) - {J}(S_{t+1})) |S_t = s \right]} | \le \epsilon(s)$, then $$\begin{aligned}
\left|{V}(s) - {\mathbb{E}}\Big[\delta_t^2+\gamma_{t+1}^2\lambda_{t+1}^2{V}(S_{t+1})\ | \ S_t=s\Big] \right|\le 3 \epsilon(s)\end{aligned}$$

First we expand $G_{t}^{\lambda} - {j}(S_{t})$, from which we recover a series with the form of a return. $$\begin{aligned}
G_{t}^{\lambda} - {j}(S_{t})
&= R_{t+1} + \gamma_{t+1} ( 1- \lambda_{t+1} ) {j}(S_{t+1}) - {j}(S_{t}) 
+ \gamma_{t+1} \lambda_{t+1} (G_{t+1}^{\lambda} - {j}(S_{t+1}))
\nonumber\\
&= R_{t+1} + \gamma_{t+1} {j}(S_{t+1}) - {j}(S_{t}) + \gamma_{t+1} \lambda_{t+1} (G_{t+1}^{\lambda} - {j}(S_{t+1})) \label{eq:delta-expansion}
$$

The variance of $G_{t}^{\lambda}$ is therefore $$\begin{aligned}
    {v}(s) &=
    {{\mathbb{E}}\left[ \left(G_{t}^{\lambda}-{{\mathbb{E}}\left[ G_{t}^{\lambda}|S_t=s \right]}\right)^2|S_t=s \right]} \nonumber\\
    &= {{\mathbb{E}}\left[ (G_{t}^{\lambda} - {j}(s))^2|S_t=s \right]} \label{eq_recursive_var}\\
            &= {{\mathbb{E}}\left[ 
        \Big(\delta_{t} + \gamma_{t+1}\lambda_{t+1}(G_{t+1}^{\lambda} - {j}(S_{t+1}))\Big)^2
    |S_t=s \right]}
   \nonumber\\
    &= {{\mathbb{E}}\left[ \delta_{t}^{2} | S_t = s \right]}
        + {{\mathbb{E}}\left[  \gamma_{t+1}^{2} \lambda_{t+1}^{2} (G_{t+1}^{\lambda} - {j}(S_{t+1}))^2 | S_t=s \right]}
        + 2 {{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} (G_{t+1}^{\lambda} - {j}(S_{t+1})) |S_t = s \right]}\nonumber\end{aligned}$$

Equation follows from Lemma \[delta\_var\] in the appendix, showing ${{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} (G_{t+1}^{\lambda} - {j}(S_{t+1})) |S_t = s \right]} = 0$.\
Now consider the case where we estimate the variance of the $\lambda$-return ${V}$ of an approximate value function ${J}$. $$\begin{aligned}
    {V}(s)
    &= {{\mathbb{E}}\left[ (G_{t}^{\lambda} - {j}(s) + {J}(s) - {J}(s) )^2|S_t=s \right]}\\
    &= {{\mathbb{E}}\left[ (G_{t}^{\lambda} - {J}(s))^2 | S_t=s \right]} + ({J}(s) - {j}(s))^2
    +2{{\mathbb{E}}\left[ G_{t}^{\lambda} - {J}(s)|S_t=s \right]} ({J}(s) - {j}(s))
    .
    \end{aligned}$$ This last term simplifies to $$\begin{aligned}
\!\!\!{{\mathbb{E}}\left[ G_{t}^{\lambda} \!-\! {J}(s)|S_t\!=\!s \right]} 
\!&= \! {{\mathbb{E}}\left[ G_{t}^{\lambda} \!-\! {j}(s)|S_t\!=\!s \right]} \!+\! {j}(s) - {J}(s)\\
&= {j}(s) - {J}(s)\end{aligned}$$ giving $ ({J}(s) - {j}(s))^2 + 2 ({j}(s) - {J}(s))({J}(s) - {j}(s)) = -({J}(s) - {j}(s))^2$. We can use the same recursive form, therefore, as , giving $$\begin{aligned}
    {V}(s) &= {{\mathbb{E}}\left[ \delta_t^2 + \gamma_{t+1}^{2} \lambda_{t+1}^{2}{V}(S_{t+1}) | S_t = s \right]}
        + 2 {{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} (G_{t+1}^{\lambda} - {J}(S_{t+1})) |S_t = s \right]}
        - ({J}(s) - {j}(s))^2\end{aligned}$$ For the second term, $$\begin{aligned}
\bigg|{{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} (G_{t+1}^{\lambda} - {J}(S_{t+1})) |S_t = s \right]}\bigg|
= &\bigg|{{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} (G_{t+1}^{\lambda} - {j}(S_{t+1})) |S_t = s \right]} \\
&+ {{\mathbb{E}}\left[ \gamma_{t+1} \lambda_{t+1} \delta_{t} ({j}(S_{t+1}) - {J}(S_{t+1})) |S_t = s \right]}\bigg|\\
= &\bigg|{{\mathbb{E}}\left[  \gamma_{t+1} \lambda_{t+1} \delta_{t} ({j}(S_{t+1}) - {J}(S_{t+1})) |S_t = s \right]} \bigg|\\
\le &\epsilon(s)
.\end{aligned}$$ where the second equality follows from Lemma \[delta\_var\] and the last step from the assumption about bounded covariance terms. Therefore, $$\begin{aligned}
\bigg|{V}(s) &- {{\mathbb{E}}\left[ \delta_t^2 + \gamma_{t+1}^{2} \lambda_{t+1}^{2}{V}(S_{t+1}) | S_t = s \right]}\bigg|
\le 2 \epsilon(s) + ({J}(s) - {j}(s))^2 \le 3 \epsilon(s)\end{aligned}$$

Experiments
===========

The primary purpose of these experiments is to demonstrate that both the [direct]{} method and VTD can approximate the true expected [$V$]{} under various conditions in the tabular setting. We consider two domains. The first is a deterministic chain, in Figure \[fig:chain\], which is useful for basic evaluation and gives results which are easy to interpret. The second is a more complex MDP, in Figure \[fig:complex\_mdp\], with different discount and trace-decay parameters in each state. For all experiments Algorithm \[eq:td\_replacing\_traces\] is used as the value estimator. Unless otherwise stated, traces are not used (${\kappa}={\bar{\kappa}}=0$). For each experimental setting 30 separate experiments were run and the estimates averaged, with standard deviation shown as shaded regions in the plots. The true values were determined by Monte Carlo estimation and are shown as dashed lines in the figures. Unless otherwise stated, the estimates are all initialized to zero.

We look at the effects of relative step-size between the value estimator and the variance estimators in Section \[sec:exp:alpha\]. In Section \[sec:exp:state-dependent\] we use the complex domain to show that both algorithms can estimate the variance with state-dependent $\gamma$ and ${\lambda}$. In Section \[sec:exp:err\] we evaluate the two algorithms’ responses to errors in the value estimate. Section \[sec:exp:traces\] looks at the effect of using traces in the estimation method. Finally, in Section \[sec:exp:off-policy\] we examine the off-policy setting.

![Chain MDP with 4 non-terminal states and 1 terminal state. From each non-terminal state there is only a single action with a deterministic transition to the next state to the right. On each transition rewards are drawn from a normal distribution with mean and variance of 1.0. Evaluation was performed for ${\lambda}=0.9$, which was chosen because it is not at either extreme and because 0.9 is a commonly used value for many RL experimental domains.[]{data-label="fig:chain"}](experiments/chain_state_diagram.pdf){width="\linewidth"}

![Complex MDP, with state-based values for $\gamma$ and ${\lambda}$ and a stochastic policy. The state-dependent values of $\gamma$ and ${\lambda}$ are chosen to provide a range of values, with at least one state acting as a terminal state where $\gamma=0$. On-policy action probabilities are indicated by $\mu$ and off-policy ones by $\pi$.[]{data-label="fig:complex_mdp"}](complex_offpolicy_1/variance-complex-offpolicy.pdf){height="3in"}

The Effect of Step-size {#sec:exp:alpha}
-----------------------

We use the chain MDP to investigate the impact of step-size choice. In Figure \[fig:chain\_l1.0\_same\] all step-sizes are the same $(\alpha=\bar{\alpha}=0.001)$. Both algorithms behave similarly. For Figure \[fig:chain\_l1.0\_less\] the step-size of the value estimate, $(\alpha=0.01)$, is greater than that of the variance estimators, $(\bar{\alpha}=0.001)$. The [direct]{} algorithm smoothly approaches the correct value, while VTD first dips well below zero. This is to be expected as the estimates are initialized to zero and the variance is calculated as ${V}(s)={M}(s)-{J}(s)^2$. If the second moment lags behind the value estimate then the variance will be negative. In Figure \[fig:chain\_l1.0\_more\] the step-size for the variance estimators is larger than for the value estimator $(0.001=\alpha<\bar{\alpha}=0.01)$. While both methods overshoot the target, VTD has greater overshoot. For both cases of unequal step-size we see higher variance in the variance estimates for VTD.

Figure \[fig:val\_init\_true\_alpha\_0\] explores this further. Here the value estimator is initialized to the true values and updates are turned off ($\alpha=0$). The variance estimators are initialized to zero and learn with $\bar{\alpha}=0.001$, chosen simply to match the step-sizes used in the previous experiments. Despite being given the true values the VTD algorithm produces higher variance in its estimates, suggesting that VTD is dependent on the value estimator tracking.

This sensitivity to step-size is shown in Figure \[fig:alphas\]. All estimates are initialized to their true values. For each ratio we computed the average variance of the 30 runs of 2000 episodes. We can see that the [direct]{} method is largely insensitive to step-size ratio, but that VTD has higher mean squared error (MSE) except when the step-sizes are equal. This result holds for the other experimental settings of this paper, including the complex MDP, but further results are omitted for brevity.

![**Chain MDP** (${\lambda}=0.9$). Value estimate held fixed at the true values ($\alpha=0,\bar{\alpha}=0.001$). Notice the increased variance in the estimates for VTD, particularly in State 0.[]{data-label="fig:val_init_true_alpha_0"}](chain/val_init_true_alpha_0.png){width="\linewidth"}

-0.5em

![**Chain MDP** (${\lambda}=0.9$). The MSE summed over all states as a function of ratios between the value step-size $\alpha$ (shown along the x-axis) and the variance step-size $\bar{\alpha}$ (shown as the 5 series). The [direct]{} algorithm is indicated by the solid lines and VTD is indicated by the dashed. The MSE of the VTD algorithm is higher than the [direct]{} algorithm, except when the step-size is the same for all estimators, $\alpha=\bar{\alpha}$ or for very small $\bar{\alpha}$.[]{data-label="fig:alphas"}](chain/alphas.png){height="2.0in"}

-2em

These results beg the question, would there ever be a situation where different step-sizes between value and variance estimators is justified? Methods which automatically set the step-sizes may produce different values which are specific to the performance of each estimator. One such algorithm is ADADELTA, which adapts the step-size based on the TD error of the estimator [@Zeiler2012]. Figure \[fig:adadelta\] shows that using a separate ADADELTA step-size calculation for each estimator results in higher variance for VTD as expected (ADADELTA: $\rho=0.99, \epsilon=1e-6$), given that the value estimator and VTD produce different TD errors.

![**Chain MDP** (${\lambda}=0.9$). Results using ADADELTA algorithm to automatically and independently set the step-sizes $\alpha$ and $\bar{\alpha}$. The step-sizes produced are given in Appendix \[sec:adadelta\_step\_size\].[]{data-label="fig:adadelta"}](chain/adadelta.png){width="\linewidth"}

Estimating for State-dependent Gamma and Lambda. {#sec:exp:state-dependent}
------------------------------------------------

One of the contributions of VTD was the generalization to support state-based $\gamma$ and ${\lambda}$. Here we evaluate the complex MDP from Figure \[fig:complex\_mdp\] (in the on-policy setting, using $\mu$), which was designed for this scenario and which has a stochastic policy, is continuing, and has multiple possible actions from each state. Figure \[fig:complex\_mdp\_onpolicy\] shows that both algorithms estimate [$V$]{} with similar results. This experiment was run with all step-sizes equal ($\alpha=\bar{\alpha}=0.01$).

![**Complex MDP** evaluated on-policy with all step-sizes equal ($\alpha=\bar{\alpha}=0.01$).[]{data-label="fig:complex_mdp_onpolicy"}](complex_lambda/same_30_0_01_compare.png){width="\linewidth"}

Variable Error in the Value Estimates {#sec:exp:err}
-------------------------------------

The derivation of our [direct]{} algorithm assumes access to the true value function. The experiments of the previous sections demonstrate that both methods are robust under this assumption, in the sense that the value function was estimated from data and used to estimate [$V$]{}. It remains unclear, however, how well these methods perform when the value estimates converge to biased solutions.

To examine this we again use the complex MDP shown by Figure \[fig:complex\_mdp\]. True values for the value functions and variance estimates are calculated from Monte Carlo simulation of 10,000,000 timesteps. For each run of the experiment each state of the value estimator was initialized to the true value plus an error (${J}(s)_0={j}(s)+\epsilon(s)$) drawn from a uniform distribution: $\epsilon(s)\in[-\zeta,\zeta]$, where $ \zeta=\max_s(|v(s)|)*\text{err ratio}$ (the maximum value in this domain is 1.55082409). The value estimate was held constant throughout the run $(\alpha=0.0)$. The experiment consisted of 120 runs of 80,000 timesteps. To look at the steady-state response of the algorithms we use only the last 10,000 timesteps in our calculations. Figure \[fig:complex\_error\_rand\_std\_dev\] plots the average variance estimate for each state. Additionally we show the average standard deviation of the estimates in the shaded regions. Sweeps over step-size were conducted, $\bar{\alpha}\in[0.05, 0.04, 0.03, 0.02, 0.01, 0.007, 0.005, 0.003, 0.001]$, and the MSE evaluated for each state. Each data point is for the step-size with the lowest MSE for that error ratio and state. While the average estimate is closer to the true values for VTD, the variance of the estimates is much larger. Further, the average estimates for VTD are either unchanged or move negative, while those of the [direct]{} algorithm tend toward positive bias.

![**Complex MDP**. For each run the value estimate of each state is offset by a random amount from uniform distribution whose size is a function of the Err Ratio and the maximum true value in the MDP. Standard deviation of the estimates is shown by shading.[]{data-label="fig:complex_error_rand_std_dev"}](complex_error/err_rand_std_dev.png){height="2in"}

For Figure \[fig:best\_alpha\] the MSE is summed over all states. Again, for each error ratio the MSE was compared over the same step-sizes as before and for each point the smallest MSE is plotted.

![**Complex MDP**. The MSE computed for the last 10,000 timesteps of 120 runs summed over all states for the step-size with the lowest overall MSE at each error ratio. For each point the step-size used ($\alpha=\bar{\alpha}$) is displayed.[]{data-label="fig:best_alpha"}](complex_error/best_alpha.png){height="2in"}

These results suggest the [direct]{} algorithm is less affected by error in ${J}$.

Experiments with Traces {#sec:exp:traces}
-----------------------

In this section we briefly look at the behavior of the complex domain when traces are used. For Figure \[fig:complex\_traces\_l0\_lb\_1\] traces are used for the variance estimators, but not for policy evaluation (${\kappa}=0.0,{\bar{\kappa}}=1.0$) and the step-sizes are all equal (0.01). Here we see no significant difference between VTD and the [direct]{} algorithm. For Figure \[fig:complex\_traces\_l1\_lb\_0\] we look at the opposite scenario, where traces are used for policy evaluation, but not in the variance estimators (${\kappa}=1.0, {\bar{\kappa}}=0.0$). Here we do see a difference, particularly the VTD method shows more variance in its estimates for State 0 and 3.

Experiments in an Off-policy Setting {#sec:exp:off-policy}
------------------------------------

In the off-policy setting the agent follows a behavior policy $\mu$, but is estimating the value of a target policy $\pi$. The ratio between these two policies is called the importance sampling ratio, $\rho=\frac{\pi(s,a)}{\mu(s,a)}$, and is used to modify the value function update. We evaluate two different off-policy scenarios on the complex MDP. In the first scenario we estimate [$V$]{} under the target policy from off-policy samples. That is, we estimate [$V$]{} that would be observed if we were following the target policy. In this scenario ${\eta}=1,\bar{\rho}=\rho$. Figure \[fig:complex\_offpolicy\_same\] shows that both methods are able achieve the same results in this setting.

![**Complex MDP** estimating [$V$]{} from off-policy samples ($\alpha=\bar{\alpha}=0.01, {\eta}=1,\bar{\rho}=\rho$).[]{data-label="fig:complex_offpolicy_same"}](complex_offpolicy_1/same_30_0_01_0_01_0_01_compare_20000.png){width="\columnwidth"}

In the second off-policy setting we estimate the variance of the off-policy return, which is the return being used to update the value estimator and is simply the multiplication of the $\lambda$-return by $\rho$. In this scenario $\bar{\rho}=1$ and ${\eta}=\rho$. Figure \[fig:complex\_offpolicy\_2\_same\] shows that both algorithms successfully estimate the return in this setting. However, despite having the same step-size as the value estimator, VTD produces higher variance in its estimates, as is most clearly seen in State 3.

![**Complex MDP** estimating the variance of the off-policy return ($\alpha=\bar{\alpha}=0.01,\bar{\rho}=1,{\eta}=\rho$).[]{data-label="fig:complex_offpolicy_2_same"}](complex_offpolicy_2/same_30_0_01_0_01_0_01_compare_20000.png){width="\linewidth"}

Discussion
==========

Both the [direct]{} method and VTD effectively estimate the variance across a range of settings, but the [direct]{} method is simpler and more robust. This simplicity alone makes the [direct]{} method preferable. The higher variance in estimates produced by VTD is likely due to the inherently larger target which VTD uses in its learning updates: ${\mathbb{E}}[X^2] \geq {\mathbb{E}}[(X - {\mathbb{E}}[X])^2]$; we show more explicitly how this affects the updates of VTD in Appendix \[sec:updates\]. One would expect the differences between the two approaches to be most pronounced for domains with larger returns than those demonstrated here. Our focus was simple MDPs. In such settings we can define clear experiments where the properties of these variance estimation algorithms can be carefully evaluated isolated from additional effects like state-aliasing due to function approximation. Consider the task of helicopter hovering formalized as a reinforcement learning task [@Ng2004]. In the most well-known variants of this problem the agent receives massive negative reward for crashing the helicopter (e.g., minus one million). In such problems the magnitude and variance of the return is large. In such cases, estimating the second moment may not be feasible from a statistical point of view, whereas the target of our direct variance estimate should be better behaved.

We focused on the tabular case, where each state is represented uniquely. Future work will investigate extending our theoretical characterization and experiments to the function approximation case. Our algorithm extends naturally with little modification. To extend the theory, there have been some promising results characterizing fixed points under the projected Bellman operator for the second moment [@Tamar2016]. An extension to projected Bellman operators could also further help bound errors incurred from inaccuracies in the value function.

Conclusion
==========

In this paper we introduced a simple method for estimating the variance of the $\lambda$-return using temporal difference learning. Our approach is simpler than existing approaches, and appears to work better in practice. We performed an extensive empirical study. Our findings suggest that our new method outperforms VTD when: (1) there is a mismatch in step-size between the value estimator and the variance estimator, (2) traces are used with the value estimator, (3) estimating variances of the off-policy return, and (4) there is error in the value estimate.

Acknowledgements {#acknowledgements .unnumbered}
================

Funding for this work was provided by the Natural Sciences and Engineering Research Council of Canada, Alberta Innovates, and Google DeepMind.

### References {#references .unnumbered}

Variance Estimation in the Off-Policy Setting {#appendix:general}
=============================================

Value estimates are made with respect to a target policy, $\pi$. If the behavior policy, $\mu$, is the same as the target policy then we say that samples are collected on-policy and when they are not the same, the samples are collected off-policy. A common approach for off-policy learning algorithms is to weight each update by the importance sampling ratio: $\rho_t=\frac{\pi(S_t,A_t)}{\mu(S_t,A_t)}$. Off-policy estimates are then implemented by multiplying the trace updates by $\rho_t$: $$\begin{aligned}
{E}_t(s)&\leftarrow\begin{cases}
      \rho_t(\gamma_t\lambda_t {E}_{t-1}(s) + 1) & s=S_t\\
      \rho_t\gamma_t\lambda_t {E}_{t-1}(s) & \forall s \in {\mathcal{S}}, s \ne S_t
   \end{cases}.\end{aligned}$$

There are two different scenarios to be considered in the off-policy setting. The first scenario is estimating the variance of the (on-policy) $\lambda$-return of the target policy, while following a different behavior policy. In the second setting, the goal is to estimate the variance of the off-policy $\lambda$-return. The off-policy $\lambda$-return is $$\!\!G_t^{\lambda}
\!=\!\rho_{t} \Big(
{R}_{t+1}+\gamma_{t+1}(1\!-\!\lambda_{t+1}){j}_t(S_{t+1}) +\gamma_{t+1}\lambda_{t+1} G^{\lambda}_{t+1} \Big).
$$ where the multiplication by the potentially large importance sampling ratios can significantly increase variance. It is important to note that you would only ever estimate one or the other off-policy variance with a given estimator. Let ${\eta}$ be the weighting for the value estimator, and $\bar{\rho}$ the weighting for the variance estimator. If estimating the variance of the target return from off-policy samples, the first scenario, ${\eta}_t=1\ \forall t$ and $\bar{\rho}_t=\rho_t$. If estimating the variance of the off-policy return $\bar{\rho}_t=1\ \forall t$ and ${\eta}_t=\rho_t$.

Here we present the resulting algorithms which use TD($\lambda$) estimators with accumulating traces.

**Direct Variance Algorithm** $$\begin{aligned}
\bar{{R}}_{t+1}& \leftarrow ({\eta}_t\delta_t + ({\eta}_t-1){J}_{t+1}(s))^2\\
\bar{\gamma}_{t+1}& \leftarrow \gamma_{t+1}^2 {\lambda}_{t+1}^2{\eta}_t^2\\
\bar{\delta}_t& \leftarrow \bar{{R}}_{t+1} + \bar{\gamma}_{t+1}{V}_t(s') - {V}_t(s)\\
\bar{{E}}_t(s)&\leftarrow\begin{cases}
     \bar{\rho}_t(\bar{\gamma}_t{\bar{\kappa}}_t \bar{{E}}_{t-1}(s) + 1) & s=S_t \\
      \bar{\rho}_t(\bar{\gamma}_t{\bar{\kappa}}_t \bar{{E}}_{t-1}(s)) & \forall s \in {\mathcal{S}}, s \ne S_t
   \end{cases}\\
{V}_{t+1}(s)&\leftarrow {V}_t(s)+\bar{\alpha} \bar{\delta}_t \bar{{E}}_t(s)
\end{aligned}
\label{alg:direct_off_policy}$$

Variance is computed directly as ${V}_{t+1}(s)$.

**Second Moment Algorithm** $$\begin{aligned}
\bar{G}_t&\leftarrow {R}_{t+1} + \gamma_{t+1}(1-{\lambda}_{t+1}){J}_{t+1}(s')\\
\bar{{R}}_{t+1}& \leftarrow {\eta}_t^2 \bar{G}_t^2 + 2{\eta}_t^2 \gamma_{t+1} {\lambda}_{t+1} \bar{G}_t {J}_{t+1}(s')\\
\bar{\gamma}_{t+1}& \leftarrow {\eta}_t^2 \gamma_{t+1}^2 {\lambda}_{t+1}^2\\
\bar{\delta}_t& \leftarrow \bar{{R}}_{t+1} + \bar{\gamma}_{t+1}{M}_t(s') - {M}_t(s)\\
\bar{{E}}_t(s)&\leftarrow\begin{cases}
     \bar{\rho}_t(\bar{\gamma}_t{\bar{\kappa}}_t \bar{{E}}_{t-1}(s) + 1) & s=S_t \\
     \bar{\rho}_t(\bar{\gamma}_t{\bar{\kappa}}_t \bar{{E}}_{t-1}(s)) & \forall s \in {\mathcal{S}}, s \ne S_t
   \end{cases}\\
{M}_{t+1}(s)&\leftarrow {M}_t(s)+\bar{\alpha} \bar{\delta}_t\bar{{E}}_t(s)
\end{aligned}
\label{alg:comparison_off_policy}$$

Variance is computed as ${V}_{t+1}(s)=M_{t+1}(s)-{J}_{t+1}(s)^2$. For convenience we summarize the variables used: $$\begin{aligned}
{J}-&\text{estimated value function of the target policy $\pi$.}\\
{R}-&\text{reward used in the value function estimate.}\\
\bar{{R}}-&\text{{meta-reward}{} used in the variance estimate.}\\
{\lambda}-&\text{bias-variance parameter of the target $\lambda$-return.}\\
{\kappa}-&\text{trace-decay parameter of the value estimator.}\\
{\bar{\kappa}}-&\text{trace-decay parameter of the secondary estimator.}\\
\gamma -&\text{discounting function used by the {$J$}{} estimator.}\\
\bar{\gamma} -&\text{discounting function used by the {$V$}{} estimator.}\\
\delta_t -&\text{TD error of the value function at time $t$.}\\
\bar{\delta_t} -&\text{TD error of the variance estimator at time $t$.}\\
{M}-&\text{estimate of the second moment.}\\
{V}-&\text{estimate of the variance.}\\
\bar{\rho}-&\text{importance sampling ratio for estimating the }\\
&\text{variance of the target return from off-policy samples.}\\
{\eta}-&\text{importance sampling ratio used to estimate the} \\
&\text{variance of the off-policy return.}\end{aligned}$$

Bellman Operators for the Variance in the Off-Policy Setting
============================================================

\[delta\_var\] For ${j}(s) = {{\mathbb{E}}\left[  G_{t+1}^{\lambda}|S_t=s \right]}$, i.e., satisfying the Bellman equation, for any bounded function $b: {\mathcal{S}}\times {\mathcal{A}}\times \mathbb{R} \times {\mathcal{S}}\rightarrow \mathbb{R}$, $${\mathbb{E}}[b(S_t, A_t, R_{t+1}, S_{t+1})(G_{t+1}^\lambda-{j}(S_{t+1}))|S_t=s]=0$$

Let $b_t = b(S_t, A_t, R_{t+1}, S_{t+1})$. By the law of total expectation: $$\begin{aligned}
  {{\mathbb{E}}\left[  b_t (G_{t+1}^{\lambda}- {j}(S_{t+1})) |S_t=s \right]}
  = {\mathbb{E}}\left[
    {\mathbb{E}}[b_{t} (G_{t+1}^{\lambda} \!- {j}(S_{t+1})) |S_t, A_{t}, S_{t+1}]|S_t=s\right]\end{aligned}$$ Given $S_{t}$, $A_t$, $R_{t+1}$ and $S_{t+1}$, $b_{t}$ is constant and can be moved outside of the expectation. Therefore, $$\begin{aligned}
{\mathbb{E}}&[b_{t} (G_{t+1}^{\lambda} - {j}(S_{t+1})) \Big|S_{t}, A_{t}, R_{t+1}, S_{t+1}]
={{\mathbb{E}}\left[  b_{t} \big|S_{t}, A_{t}, R_{t+1}, S_{t+1}  \right]}
\times {{\mathbb{E}}\left[  G_{t+1}^{\lambda} - {j}(S_{t+1}) \big|S_{t}, A_{t}, R_{t+1}, S_{t+1} \right]}\end{aligned}$$ Because $${{\mathbb{E}}\left[  G_{t+1}^{\lambda} - {j}(S_{t+1}) \big|S_{t}, A_{t}, R_{t+1}, S_{t+1} \right]}=0$$ the result follows.

\[recurse\_var\_off\_policy\] $$\begin{aligned}
{v}(s)
={\mathbb{E}}[({\eta}_t\delta_t +({\eta}_t-1){j}(s))^2+\lambda_{t+1}^2\gamma_{t+1}^2{\eta}_t^2{v}(S_{t+1})|S_t=s]\end{aligned}$$

The proof is similar to the proof of Theorem \[recurse\_var\]. $$\begin{aligned}
{v}(s)
=&{\mathbb{E}}[\{G_t^\lambda - {j}(S_t)\}^2|S_t=s]\\
=& {\mathbb{E}}[\{{\eta}_t{R}_{t+1}+{\eta}_t\gamma_{t+1}(1-\lambda_{t+1}){j}(S_{t+1}) 
+ {\eta}_t\gamma_{t+1}\lambda_{t+1} G_{t+1}^\lambda - v(s)\}^2|S_t=s]\\
=& {\mathbb{E}}[\{{\eta}_t{R}_{t+1}+{\eta}_t\gamma_{t+1}{j}(S_{t+1})-{\eta}_t {j}(s) + {\eta}_t {j}(s) 
- {\eta}_t\gamma_{t+1}\lambda_{t+1} {j}(S_{t+1})\\
\ &+ {\eta}_t\gamma_{t+1}\lambda_{t+1} G_{t+1}^\lambda-{j}(s)\}^2|S_t=s]\\
=& {\mathbb{E}}[\{({\eta}_t\delta_t\ + ({\eta}_t - 1){j}(s)) + {\eta}_t\gamma_{t+1}\lambda_{t+1}(G_{t+1}^\lambda-{j}(S_{t+1}))\}^2|S_t=s]\\
=& {\mathbb{E}}[({\eta}_t\delta_t + ({\eta}_t -1){j}(s))^2
+{\eta}_t^2\gamma_{t+1}^2\lambda_{t+1}^2(G_{t+1}^\lambda- {j}(S_{t+1}))^2\\
\ &+2{\eta}_t \gamma_{t+1}\lambda_{t+1} ({\eta}_t\delta_t + ({\eta}_t - 1){j}(s))(G_{t+1}^\lambda- {j}(S_{t+1}))|S_t=s]\\
=& {\mathbb{E}}[({\eta}_t\delta_t + ({\eta}_t - 1){j}(s))^2
+{\eta}_t^2\gamma_{t+1}^2\lambda_{t+1}^2(G_{t+1}^\lambda- {j}(S_{t+1}))^2\\
\ &+2{\eta}_t^2 \gamma_{t+1}\lambda_{t+1} \delta_t (G_{t+1}^\lambda- {j}(S_{t+1}))
+ 2{\eta}_t \gamma_{t+1}\lambda_{t+1}({\eta}_t-1){j}(s)(G_{t+1}^\lambda-{j}(S_{t+1}))|S_t=s]\end{aligned}$$ Using Lemma \[delta\_var\], with different fixed functions $b$, we can conclude that the last two terms are zero, giving $$\begin{aligned}
{v}(s) =&{\mathbb{E}}[({\eta}_t\delta_t + ({\eta}_t-1){j}(s))^2
+{\eta}_t^2\gamma_{t+1}^2\lambda_{t+1}^2(G_{t+1}^\lambda-{j}(S_{t+1}))^2|S_t=s]\\
\intertext{By the law of total expectation} {v}(s)
=&{\mathbb{E}}[({\eta}_t\delta_t + ({\eta}_t-1){j}(s))^2
+{\mathbb{E}}[{\eta}_t^2\gamma_{t+1}^2\lambda_{t+1}^2(G_{t+1}^\lambda-{j}(s'))^2|S_{t+1}=s']|S_t=s]\\
=&{\mathbb{E}}[({\eta}_t\delta_t+({\eta}_t-1){j}(s))^2
+ {\eta}_t^2\gamma_{t+1}^2\lambda_{t+1}^2{v}(S_{t+1})|S_t=s].\end{aligned}$$ completing the proof.

Theorem \[recurse\_var\_off\_policy\] gives a Bellman equation for ${V}(s)$ in the more general off-policy setting. The resulting TD algorithm uses [meta-reward]{} $({\eta}_t\delta_t +({\eta}_t-1){j}(s))^2$ and discounting function ${\eta}_t^2\gamma_{t+1}^2\lambda^2$.

ADADELTA Step-Sizes {#sec:adadelta_step_size}
===================

The step-sizes generated by the ADADELTA algorithm in Figure \[fig:adadelta\] are shown in Figure \[fig:adadelta\_stepsizes\]. As we evaluate in the tabular case at each timestep only the step-size for the current state has any impact. Thus, the values shown here are the average step-size used over each episode.

![**Chain MDP**. The average step-sizes computed for ADADELTA in Figure \[fig:adadelta\].[]{data-label="fig:adadelta_stepsizes"}](chain/adadelta_alphas.png){height="2in"}

Variability in Updates {#sec:updates}
======================

In this section, we show the effective update to ${V}_t(s)$ on each timestep for each of the two algorithms in the on-policy setting. For notational clarity let ${r}={r}_{t+1},\alpha=\alpha_t, \gamma=\gamma_{t+1}, {\lambda}={\lambda}_{t+1},s=s_{t},s'=s_{t+1},\delta_t=\delta$.

For the direct algorithm the change is just:

$$\begin{aligned}
\Delta {V}_t(s)=\bar{\alpha}(\delta^2 + \bar{\gamma}{V}_t(s')-{V}_t(s)).
\label{eq:direct_update}
\end{aligned}$$

The updates for the VTD algorithm are much more complicated to compute and we will make some assumptions about the domain in order to simplify the derivation. First we compute the change in the second moment and value estimators separately.

We first expand the term $\delta^2$: $$\begin{aligned}
\delta=&{r}+\gamma {J}_t(s')-{J}_t(s)\\
\delta^2=&({r}+\gamma {J}_t(s'))^2 
- 2({r}+\gamma {J}_t(s')){J}_t(s)+ {J}_t(s)^2.
$$

Now we expand the change in the second moment estimate, $M$. To simplify the expansion we make the assumption that at each transition the agent moves to a new state, i.e. $s_t\ne s_{t+1}\forall t$ (this is not required for our algorithm, but simplifies the expansions below). This assumption holds for both of the domains examined in this paper. This allows us to substitute ${J}_{t+1}(s')={J}_{t}(s)$, which greatly simplifies the updates.

$$\begin{aligned}
\Delta M(s)=&\bar{\alpha}[({r}+\gamma {J}_{t+1}(s'))^2
- \bar{\gamma}^2 {J}_{t+1}(s')^2 + \bar{\gamma}M_t(s')-M_t(s)]\\
=&\bar{\alpha}[({r}+\gamma {J}_{t}(s'))^2 
- \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}M_t(s')-M_t(s)]\\
=&\bar{\alpha}[({r}+\gamma {J}_{t}(s'))^2 -2({r}+\gamma {J}_t(s')){J}_t(s)
+ {J}_t(s)^2 + 2({r}+\gamma {J}_t(s')){J}_t(s) - {J}_t(s)^2 \\
&- \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}M_t(s')-M_t(s)]\\
=&\bar{\alpha}[\delta^2 + 2({r}+\gamma {J}_t(s')){J}_t(s) - {J}_t(s)^2 
- \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}M_t(s')-M_t(s)]\\
\intertext{Notice that from the definition of the TD error: $R+\gamma {J}_t(s')=\delta+{J}_t(s)$.}
=&\bar{\alpha}[\delta^2 + 2(\delta+{J}_t(s)){J}_t(s) - {J}_t(s)^2 
- \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}M_t(s')-M_t(s)]\\
=&\bar{\alpha}[\delta^2 + 2\delta {J}_t(s) + {J}_t(s)^2 - \bar{\gamma}^2 {J}_{t}(s')^2 
+ \bar{\gamma}M_t(s')-M_t(s)]\\
=&\bar{\alpha}[\delta^2 + (\bar{\gamma}M_t(s')-\bar{\gamma}{J}_t(s')^2) 
-(M_t(s)-{J}_{t}(s)^2) + 2\delta {J}_t(s) - \bar{\gamma}^2 {J}_{t}(s')^2
+\bar{\gamma}{J}_t(s')^2]\\
=&\bar{\alpha}[\delta^2 + \bar{\gamma}{V}_t(s') - {V}_t(s) + 2\delta {J}_t(s)
- \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}{J}_t(s')^2]\\
=&\bar{\alpha}[\delta^2 + \bar{\gamma}{V}_t(s') - {V}_t(s)]
+ \bar{\alpha}[2\delta {J}_t(s) - \bar{\gamma}^2 {J}_{t}(s')^2 + \bar{\gamma}{J}_t(s')^2]\\\end{aligned}$$

The first half of this equation is the same as the update for the direct algorithm . Now we expand the change in the variance update for VTD:

$$\begin{aligned}
\Delta {V}_t(s) =&(M_{t+1}(s) - {J}_{t+1}(s)^2) - (M_{t}(s) - {J}_{t}(s)^2)\\
=&\Delta M(s) + {J}_{t}(s)^2 - {J}_{t+1}(s)^2\\
=&\Delta M(s) + {J}_{t}(s)^2 - (\alpha\delta + {J}_{t}(s))^2\\
=&\Delta M(s) + {J}_t(s)^2 
- ((\alpha\delta)^2 + 2\alpha\delta {J}_t(s) + {J}_t(s)^2)\\
=&\Delta M(s) -(\alpha\delta)^2 - 2\alpha\delta {J}_t(s).\end{aligned}$$

Note that in the case that $\alpha=\bar{\alpha}$ this last term cancels out and we’re left with:

$$\begin{aligned}
\Delta {V}_t(s)=&\alpha[\delta^2 + \bar{\gamma}{V}_t(s') - {V}_t(s)] +
\alpha {J}_t(s')^2(\bar{\gamma}- \bar{\gamma}^2) -(\alpha\delta)^2.\end{aligned}$$

This suggests that VTD will deviate from the direct method more when: $\alpha$ is larger, ${J}_t(s')$ is larger, $\bar{\gamma}=0.5$ and for large values of $\delta$. In general, we expect from this equation that the updates for the VTD will be larger than those of the direct method, suggesting a cause for the higher variance of variance estimates across runs as observed for VTD under a number of scenarios.

We also empirically tested this hypothesis, with Table \[table:updates\] showing the updates for the two algorithms across the various experiments. For episodic tasks (chain MDP, Figures \[fig:chain\_l1.0\_same\]-\[fig:adadelta\]) the results show the average total absolute change over all states for a given episode averaged across runs and then averaged across all episodes. For the continuing case (complex MDP, Figures \[fig:complex\_mdp\_onpolicy\]-\[fig:complex\_offpolicy\_2\_same\]) the results are the average absolute change for a timestep averaged over all runs and then averaged across the entire run length. The experiments shaded in gray are those where the two algorithms behaved nearly identically. In this case we see that the average magnitude of updates is nearly identical. For the other experiments the VTD algorithm showed higher variance in its variance estimates across runs. For these experiments we see that the average magnitude of the VTD updates is much larger than for the direct algorithm.

  Fig.                                    Value    Snd Mmnt     VTD     Direct
  ------------------------------------- --------- ---------- --------- ---------
  \[fig:chain\_l1.0\_same\]              0.00332    0.0157    0.00415   0.00415
  \[fig:chain\_l1.0\_less\]              0.0322     0.0165     0.143    0.00387
  \[fig:chain\_l1.0\_more\]              0.00332    0.156      0.142    0.0419
  \[fig:val\_init\_true\_alpha\_0\]        0.0      0.0166    0.0166    0.00381
  \[fig:adadelta\]                       0.0212     0.0306    0.0752    0.00884
  \[fig:complex\_mdp\_onpolicy\]         0.00362   0.00675    0.00381   0.00385
  \[fig:complex\_offpolicy\_same\]       0.00362   0.00461    0.00303   0.00307
  \[fig:complex\_offpolicy\_2\_same\]    0.00362    0.0110    0.0116    0.00838

  : Average updates for various experiments.[]{data-label="table:updates"}
