skip to content
[Jimjimu Notes]

Chapter 2: Bellman Equation

/ 10 min read

Table of Contents

Calculating return

给定这么一个例子和策略 alt text

Method 1: by definition

Let viv_i denote the return obtained starting from sis_i (i=1,2,3,4i = 1, 2, 3, 4)

v1=r1+γr2+γ2r3+v2=r2+γr3+γ2r4+v3=r3+γr4+γ2r1+v4=r4+γr1+γ2r2+\begin{aligned} v_1 &= r_1 + \gamma r_2 + \gamma^2 r_3 + \dots \\ v_2 &= r_2 + \gamma r_3 + \gamma^2 r_4 + \dots \\ v_3 &= r_3 + \gamma r_4 + \gamma^2 r_1 + \dots \\ v_4 &= r_4 + \gamma r_1 + \gamma^2 r_2 + \dots \end{aligned}

Method 2:

v1=r1+γ(r2+γr3+)=r1+γv2v2=r2+γ(r3+γr4+)=r2+γv3v3=r3+γ(r4+γr1+)=r3+γv4v4=r4+γ(r1+γr2+)=r4+γv1\begin{aligned} v_1 &= r_1 + \gamma(r_2 + \gamma r_3 + \dots) = r_1 + \gamma v_2 \\ v_2 &= r_2 + \gamma(r_3 + \gamma r_4 + \dots) = r_2 + \gamma v_3 \\ v_3 &= r_3 + \gamma(r_4 + \gamma r_1 + \dots) = r_3 + \gamma v_4 \\ v_4 &= r_4 + \gamma(r_1 + \gamma r_2 + \dots) = r_4 + \gamma v_1 \end{aligned}
  • The returns rely on each other. Bootstrapping! 相互依赖的。

How to solve these equations? Write in the following matrix-vector form:

[v1v2v3v4]v=[r1r2r3r4]+[γv2γv3γv4γv1]=[r1r2r3r4]r+γ[0100001000011000]P[v1v2v3v4]v\underbrace{ \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ v_4 \end{bmatrix} }_{\mathbf{v}} = \begin{bmatrix} r_1 \\ r_2 \\ r_3 \\ r_4 \end{bmatrix} + \begin{bmatrix} \gamma v_2 \\ \gamma v_3 \\ \gamma v_4 \\ \gamma v_1 \end{bmatrix} = \underbrace{ \begin{bmatrix} r_1 \\ r_2 \\ r_3 \\ r_4 \end{bmatrix} }_{\mathbf{r}} + \gamma \underbrace{ \begin{bmatrix} 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 \end{bmatrix} }_{\mathbf{P}} \underbrace{ \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ v_4 \end{bmatrix} }_{\mathbf{v}}

which can be rewritten as

v=r+γPv\mathbf{v} = \mathbf{r} + \gamma \mathbf{P} \mathbf{v}

此处P\mathbf P叫做状态转移矩阵

接下去移项求逆: 因此:

v=(IγP)1r\boxed{ \mathbf v = (I-\gamma P)^{-1}\mathbf r }

这就是直接求解 state value 的方法。

State Value

Notations

Consider the following single-step process:

StAtRt+1,St+1S_t \xrightarrow{A_t} R_{t+1}, S_{t+1}
  • t,t+1t, t + 1: discrete time instances
  • StS_t: state at time tt
  • AtA_t: the action taken in state StS_t
  • Rt+1R_{t+1}: the reward obtained after taking AtA_t,有时候也写作RtR_t
  • St+1S_{t+1}: the state transited to after taking AtA_t

Note that St,At,Rt+1S_t, A_t, R_{t+1} are all random variables. 随机变量,意思就是可以进行求期望等操作

This step is governed by (由…决定)the following probability distributions:

  • StAtS_t \to A_t is governed by π(At=aSt=s)\pi(A_t = a \mid S_t = s)
  • St,AtRt+1S_t, A_t \to R_{t+1} is governed by p(Rt+1=rSt=s,At=a)p(R_{t+1} = r \mid S_t = s, A_t = a)
  • St,AtSt+1S_t, A_t \to S_{t+1} is governed by p(St+1=sSt=s,At=a)p(S_{t+1} = s' \mid S_t = s, A_t = a)

At this moment, we assume we know the model (i.e., the probability distributions)!

Discounted return

Consider the following multi-step trajectory:

StAtRt+1,St+1At+1Rt+2,St+2At+2Rt+3,S_t \xrightarrow{A_t} R_{t+1}, S_{t+1} \xrightarrow{A_{t+1}} R_{t+2}, S_{t+2} \xrightarrow{A_{t+2}} R_{t+3}, \dots

The discounted return is

Gt=Rt+1+γRt+2+γ2Rt+3+G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots
  • γ(0,1)\gamma \in (0, 1) is a discount rate.
  • GtG_t is also a random variable since Rt+1,Rt+2,R_{t+1}, R_{t+2}, \dots are random variables.

State Value

GtG_t的均值定义为state value或者state-value function

vπ(s)=E[GtSt=s]v_{\pi}(s) = \mathbb E [G_t \mid S_t = s]

它由s,πs,\pi决定,因此也可以写作v(π,s)v(\pi,s)

Bellman equation 贝尔曼公式

贝尔曼公式描述了不同state value之间的关系,接下去,我们来推导(derive)贝尔曼公式

还是考虑这么一个轨迹:

StAtRt+1,St+1At+1Rt+2,St+2At+2Rt+3,S_t \xrightarrow{A_t} R_{t+1}, S_{t+1} \xrightarrow{A_{t+1}} R_{t+2}, S_{t+2} \xrightarrow{A_{t+2}} R_{t+3}, \dots

根据discounted return的定义,我们可以将GtG_t写为:

Gt=Rt+1+γRt+2+γ2Rt+3+=Rt+1+γ(Rt+2+γRt+3+)=Rt+1+γGt+1\begin{aligned} G_t &= R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots \\ & = R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + \dots) \\ &= R_{t+1} + \gamma G_{t+1} \end{aligned}

根据state value的定义:

vπ(s)=E[GtSt=s]=E[Rt+1+γGt+1St=s]=E[Rt+1St=s]+γE[Gt+1St=s]v_{\pi}(s) = \mathbb E[G_t \mid S_t = s] = \mathbb E[R_{t+1} + \gamma G_{t+1} \mid S_t = s] \\ = \mathbb E[R_{t+1} \mid S_t = s] + \gamma \mathbb E[ G_{t+1} \mid S_t = s]

接下来分别计算这两个期望,

我们知道Rt+1R_{t+1}St+1S_{t+1}StS_tAtA_t决定,因此:

E[Rt+1St=s]=aπ(as)E[Rt+1St=s,At=a]=aπ(as)rrp(rs,a)\mathbb E[R_{t+1} \mid S_t = s] = \sum_{a} \pi(a|s) \mathbb E[R_{t+1} \mid S_t = s, A_t = a] \\ = \sum_{a} \pi(a|s) \sum_{r} r p(r | s, a)

This is the mean of immediate rewards

计算E[Gt+1St=s]\mathbb E[G_{t+1} \mid S_t = s]:

E[Gt+1St=s]=sE[Gt+1St=s,St+1=s]p(ss)全期望公式,设下一步状态s=sE[Gt+1St+1=s]p(ss)Markov process is memoryless =svπ(s)p(ss)definition of state value =svπ(s)ap(ss,a)π(as)\begin{aligned} \mathbb E[G_{t+1} \mid S_t = s] &= \sum_{s'} \mathbb E[G_{t+1} \mid S_t=s, S_{t+1} = s']p(s' \mid s) \quad 全期望公式,设下一步状态s' \\ &= \sum_{s'} \mathbb E[G_{t+1} \mid S_{t+1} = s'] p(s' \mid s) \quad \text{Markov process is memoryless }\\ &= \sum_{s'} v_{\pi}(s') p(s' \mid s) \quad \text{definition of state value }\\ &= \sum_{s'} v_{\pi}(s') \sum_{a} p(s' \mid s,a) \pi(a \mid s) \\ \end{aligned}

This is the mean of future rewards

Therefore, we have

vπ(s)=E[Rt+1St=s]+γE[Gt+1St=s],=aπ(as)rp(rs,a)rmean of immediate rewards+γaπ(as)sp(ss,a)vπ(s)mean of future rewards,=aπ(as)[rp(rs,a)r+γsp(ss,a)vπ(s)],sS.\begin{aligned} {\color{red}v_{\pi}(s)} &= \mathbb{E}[R_{t+1}|S_t = s] + \gamma \mathbb{E}[G_{t+1}|S_t = s], \\ &= {\color{blue}\underbrace{ {\color{blue}\sum_{a}\pi(a|s)\sum_{r}p(r|s,a)}{\color{black}r} }_{\text{mean of immediate rewards}}} + \gamma {\color{blue}\underbrace{ {\color{blue}\sum_{a}\pi(a|s)\sum_{s'}p(s'|s,a)}{\color{red}v_{\pi}(s')} }_{\text{mean of future rewards}}}, \\ &= {\color{blue}\sum_{a}\pi(a|s)} {\color{blue}\left[ {\color{blue}\sum_{r}p(r|s,a)}{\color{black}r} + \gamma {\color{blue}\sum_{s'}p(s'|s,a)}{\color{red}v_{\pi}(s')} \right]}, \quad \forall s \in \mathcal{S}. \end{aligned}

Highlights:

  • The above equation is called the Bellman equation, which characterizes the relationship among the state-value functions of different states.
  • It consists of two terms: the immediate reward term and the future reward term.
  • A set of equations: every state has an equation like this!!!

可以先把里面的变量分成三组:

vπ(s),vπ(s)要求的,π(as)给定的策略,p(rs,a),p(ss,a)环境模型\underbrace{v_\pi(s),v_\pi(s')}_{\text{要求的}} ,\qquad \underbrace{\pi(a|s)}_{\text{给定的策略}} ,\qquad \underbrace{p(r|s,a),p(s'|s,a)}_{\text{环境模型}}
  • vπ(s)v_\pi(s)vπ(s)v_\pi(s') 是要计算的,他们是相互依赖的
  • 这里π\pi是给定的策略,不是找最好的策略,而求解这个方程的过程就是给定一个π\pi,去求解vπv_{\pi} ,这个过程叫策略评估policy evaluation
  • p(rs,a)p(r|s,a)p(ss,a)p(s'|s,a) 是 Dynamic Model, 主要有两部分
    • reward model p(rs,a)p(r \mid s, a)
    • transition model p(ss,a)p(s' \mid s, a)

Matrix-vector form of the Bellman equation

上一节得出的贝尔曼公式是element-wise的,它对于每一个state sSs \in \mathcal S均成立,意味着一共有S|\mathcal S|个这样的公式,我们用向量和矩阵来表示他们。

Recall that:

vπ(s)=aπ(as)[rp(rs,a)r+γsp(ss,a)vπ(s)]v_{\pi}(s) = \sum_{a} \pi(a|s) \left[ \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) v_{\pi}(s') \right]

Rewrite the Bellman equation as

vπ(s)=rπ(s)+γspπ(ss)vπ(s)(1)v_{\pi}(s) = r_{\pi}(s) + \gamma \sum_{s'} p_{\pi}(s'|s) v_{\pi}(s') \tag{1}

where

rπ(s)aπ(as)rp(rs,a)r,pπ(ss)aπ(as)p(ss,a)r_{\pi}(s) \triangleq \sum_{a} \pi(a|s) \sum_{r} p(r|s, a) r, \qquad p_{\pi}(s'|s) \triangleq \sum_{a} \pi(a|s) p(s'|s, a)

其实rπ(s)r_{\pi}(s)就是E[Rt+1St=s] \mathbb E[R_{t+1} \mid S_t = s] 就是即时Reward的均值,pπ(ss)p_{\pi}(s'|s)为当前状态为ss转移到ss'的概率

设状态s1,s2,...sns_1,s_2,...s_n,对于状态sis_i :

vπ(si)=rπ(si)+γsjpπ(sjsi)vπ(sj)v_{\pi}(s_i) = r_{\pi}(s_i) + \gamma \sum_{s_j} p_{\pi}(s_j \mid s_i) v_{\pi}(s_j)

写成矩阵形式:

vπ=rπ+γPπvπv_{\pi} = r_{\pi} + \gamma P_{\pi} v_{\pi}

where

  • vπ=[vπ(s1),,vπ(sn)]TRnv_{\pi} = [v_{\pi}(s_1), \dots, v_{\pi}(s_n)]^T \in \mathbb{R}^n
  • rπ=[rπ(s1),,rπ(sn)]TRnr_{\pi} = [r_{\pi}(s_1), \dots, r_{\pi}(s_n)]^T \in \mathbb{R}^n
  • PπRn×nP_{\pi} \in \mathbb{R}^{n \times n}, where [Pπ]ij=pπ(sjsi)[P_{\pi}]_{ij} = p_{\pi}(s_j|s_i), is the state transition matrix

这个[Pπ]ij=pπ(sjsi)[P_{\pi}]_{ij} = p_{\pi}(s_j|s_i)这么定义也是因为贝尔曼方程左边sis_i是出发状态,右边sjs_j枚举到达状态

举个例子: alt text

Solve state value

Given a policy, finding out the corresponding state values is called policy evaluation!

贝尔曼方程:

vπ=rπ+γPπvπv_{\pi} = r_{\pi} + \gamma P_{\pi} v_{\pi}

移项求逆:

vπ=(IγPπ)1rπv_{\pi} = (I - \gamma P_{\pi})^{-1} r_{\pi}

但是矩阵求逆可能很贵, 下面给出迭代法:

定义

vk=第 k 次迭代时,对真实 vπ 的估计\mathbf v_k = \text{第 }k\text{ 次迭代时,对真实 }\mathbf v_\pi\text{ 的估计}

vk+1=rπ+γPπvkv_{k+1} = r_{\pi} + \gamma P_{\pi} v_{k}

先随便猜一个 value vector v0v_0, 然后按照上述递推式更新即可,可以证明:

limkvk=vπ=(IγPπ)1rπ\lim_{k\rightarrow\infty} v_k = v_{\pi} = (I - \gamma P_{\pi})^{-1} r_{\pi} alt text

Action Value

From state value to action value:

  • State value: the average return the agent can get starting from a state.
  • Action value: the average return the agent can get starting from a state and taking an action

定义Action Value:

qπ(s,a)=E[GtSt=s,At=a]q_{\pi} (s,a) = \mathbb E[G_t \mid S_t=s, A_t=a]
  • qπ(s,a)q_{\pi} (s,a) 是关于(s,a)(s,a)的函数,并且以来与π\pi

由全期望公式:

E[GtSt=s]vπ(s)=aE[GtSt=s,At=a]qπ(s,a)π(as)\underbrace{\mathbb{E}[G_t \mid S_t = s]}_{v_{\pi}(s)} = \sum_{a} \underbrace{\mathbb{E}[G_t \mid S_t = s, A_t = a]}_{q_{\pi}(s,a)} \pi(a|s)

Hence,

vπ(s)=aπ(as)qπ(s,a)(2){\color{red}v_{\pi}(s)} = \sum_{a} \pi(a|s) {\color{red}q_{\pi}(s,a)} \tag{2}

再看贝尔曼公式

vπ(s)=aπ(as)[rp(rs,a)r+γsp(ss,a)vπ(s)]qπ(s,a)(3)v_{\pi}(s) = \sum_{a} \pi(a|s) \underbrace{\left[ \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) v_{\pi}(s') \right]}_{{\color{red}q_{\pi}(s,a)}} \tag{3}

By comparing (2) and (3), we have the action-value function as

qπ(s,a)=rp(rs,a)r+γsp(ss,a)vπ(s)(4){\color{red}q_{\pi}(s,a)} = \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) {\color{red}v_{\pi}(s')} \tag{4}

(2) and (4) are the two sides of the same coin:

  • (2) shows how to obtain state values from action values.
  • (4) shows how to obtain action values from state values.