1  The Basic Idea of RL, and Its Link to Dynamic Programming

\[ \newcommand{\E}{\mathbb{E}} \newcommand{\R}{\mathbb{R}} \newcommand{\Prob}{\mathbb{P}} \newcommand{\BR}{\operatorname{BR}} \newcommand{\eps}{\varepsilon} \newcommand{\given}{\,\vert\,} \newcommand{\argmax}{\operatorname*{arg\,max}} \newcommand{\argmin}{\operatorname*{arg\,min}} \newcommand{\sm}{\setminus} \newcommand{\defeq}{\equiv} \]

This chapter follows Ben Moll’s Lecture 1. Reinforcement learning is Monte-Carlo dynamic programming: it uses Bellman recursions with expectations estimated from samples rather than computed from a known model.

NoteSource:

Ben Moll’s RL lecture slides, Lecture 1.

1.1 The basic idea of RL

For a random variable \(x\), compute \(\E[x]\) by integration or sampling:

  1. Exact. If we know the distribution \(p(x)\), integrate: \[ \E[x]=\int x\,p(x)\,dx . \]
  2. Monte Carlo. If we do not know \(p(x)\) but can sample \(\vec{x}=[x_{1},\dots,x_{N}]'\), average: \[ \E[x]\approx \bar{x}=\frac{1}{N}\sum_{i=1}^{N}x_{i} . \]

The Monte-Carlo mean can be built up incrementally. Writing \(\bar{x}_{k}=\tfrac{1}{k}\sum_{i=1}^{k}x_{i}\) for the running mean of the first \(k\) samples, the recursion is

\[ \bar{x}_{k}=\bar{x}_{k-1}+\frac{1}{k}\,(x_{k}-\bar{x}_{k-1}). \tag{1.1}\]

Tip

The step size \(\tfrac{1}{k}\) is the learning rate. A constant \(\alpha\) gives more weight to recent samples.

Computing a value function

Definition 1.1 (Markov reward process) A Markov reward process (MRP) assigns to each starting state \(s_{0}\) the discounted expected reward \[ v(s_{0})=\E\!\left[\sum_{t=0}^{\infty}\beta^{t}r(s_{t})\right], \] where the state \(s_{t}\) follows an exogenous Markov process and \(\beta\in(0,1)\) is the discount factor.

For \(v\), the corresponding methods are:

  1. Dynamic programming. If we know the transition density \(p(s'\given s)\), then \(v\) solves the fixed point \[ v(s)=r(s)+\beta\int v(s')\,p(s'\given s)\,ds' . \tag{1.2}\]
  2. Monte Carlo. If we cannot evaluate \(p\) but can sample trajectories \(\{s_{t}^{i}\}_{t=0}^{T}\), average the realized returns: \[ v(s_{0})\approx \hat{v}(s_{0})=\frac{1}{N}\sum_{i=1}^{N}\sum_{t=0}^{T}\beta^{t}r(s_{t}^{i}). \]

Averaging whole trajectories requires waiting for each trajectory to finish. We can instead update \(\hat{v}\) at every step \(t\) of every trajectory \(k\), using the incremental rule Equation 1.1 with a constant learning rate \(\alpha\):

\[ \hat{v}_{t+1}^{k}(s_{t}^{k})=\hat{v}_{t}^{k}(s_{t}^{k})+\alpha\big[\,r(s_{t}^{k})+\beta\,\hat{v}_{t}^{k}(s_{t+1}^{k})-\hat{v}_{t}^{k}(s_{t}^{k})\,\big]. \tag{1.3}\]

This is the TD(0) update.

NoteDerivation: from the incremental mean to TD(0)

Start from the dynamic-programming fixed point Equation 1.2 and read it as an expectation: \[ v(s)=r(s)+\beta\int v(s')\,p(s'\given s)\,ds' =\E_{s'\sim p(\cdot\given s)}\big[\,\underbrace{r(s)+\beta v(s')}_{Y\,\defeq\,r(s)+\beta v(s')}\,\big]. \] Apply Equation 1.1 to \(\E[Y]\), using a constant learning rate \(\alpha\).

At trajectory \(k\) the realized value of \(Y\) is \(r(s^{k})+\beta v(s'^{k})\). The true \(v(s'^{k})\) is unknown. We bootstrap by substituting the current estimate \(\hat{v}^{k}\) in its place: \[ x_{k}=r(s^{k})+\beta\,\hat{v}^{k}(s'^{k}). \] If \(\hat v^k\) converges uniformly to \(v\), the conditional bias of this target tends to zero. Feeding \(x_{k}\) into the incremental rule \(\bar{x}_{k}=\bar{x}_{k-1}+\alpha(x_{k}-\bar{x}_{k-1})\), with \(\bar{x}_{k}=\hat{v}'^{k}(s^{k})\) the updated estimate and \(\bar{x}_{k-1}=\hat{v}^{k}(s^{k})\) the old one (a prime marks the post-update value), gives \[ \begin{aligned} \hat{v}'^{k}(s^{k}) &=\hat{v}^{k}(s^{k})+\alpha\big[\,r(s^{k})+\beta\,\hat{v}^{k}(s'^{k})-\hat{v}^{k}(s^{k})\,\big] \\ &=(1-\alpha)\,\hat{v}^{k}(s^{k})+\alpha\big[\,r(s^{k})+\beta\,\hat{v}^{k}(s'^{k})\,\big], \end{aligned} \] which is Equation 1.3 written one step at a time.

Tip

The bracketed quantity \(r+\beta\hat{v}(s')-\hat{v}(s)\) is the temporal-difference error, the gap between the one-step target and the current estimate.

NoteWhy a constant step size?

The step \(\tfrac{1}{k}\) in Equation 1.1 weights samples equally; for iid observations with finite variance, its variance falls like \(1/k\). The TD target \(r+\beta\,\hat{v}(s')\) moves as \(\hat{v}\) and the policy change, so shrinking steps can track it slowly.

A constant \(\alpha\) keeps weight on new observations. For iid targets with variance \(\sigma^2\) and \(0<\alpha<1\), the recursion \(\hat{v}\leftarrow(1-\alpha)\hat{v}+\alpha\cdot(\text{target})\) has limiting variance \(\alpha\sigma^2/(2-\alpha)\). Persistent noise leaves fluctuations; a deterministic contraction can converge with a constant step. The Robbins-Monro conditions below give sufficient conditions for convergence with noisy updates.

The trajectory average and the TD(0) update estimate the same \(v\) with different bias, variance and timing:

Table 1.1: Monte Carlo vs. TD(0) as estimators of the value function.
Trajectory average (MC) TD(0) (bootstrap)
Target realized return \(\sum_{t}\beta^{t}r\) \(r+\beta\,\hat{v}(s')\)
Target bias unbiased for a complete terminating or infinite return; truncation omits the tail conditional target bias \(\beta\,\E[\hat{v}(s')-v(s')\mid s]\)
Sampling noise accumulated across rewards and transitions in the return one sampled transition and the current continuation estimate
Updates only once the trajectory ends every step, online

The two are endpoints of a spectrum: \(n\)-step returns and TD(\(\lambda\)) interpolate between them.

Figure 1.1: Both estimators use the same episodes from a five-state discounted Markov reward process; error is measured across all states. TD(0) has lower error at small budgets, then reaches a noise floor set by \(\alpha\). Monte Carlo eventually crosses below it.

Reinforcement learning and deep learning

Often we approximate the value function by a parametric family \[ \hat{V}(s,w), \] where \(w\) is a parameter vector, for instance the weights of a neural network. Using a neural network gives deep reinforcement learning.

Note

RL estimates values and policies from sampled experience. A neural network is one choice of function approximator.