Bellman equation derivation: Difference between revisions

From Machinelearning
No edit summary
No edit summary
Line 67: Line 67:
:<math>\sum_a \pi(a \mid s) \sum_{s',r} p(s',r\mid s,a) \sum_g g \Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>
:<math>\sum_a \pi(a \mid s) \sum_{s',r} p(s',r\mid s,a) \sum_g g \Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>


This is basically what we want, except we want to say that <math>\sum_g g \Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \mathbb E_\pi[G_{t+1} \mid S_{t+1}=s']</math>, since the latter expression equals <math>v_\pi(s')</math>. Thankfully, we can say this, because of the Markov assumption. Actually proving this is a little complicated; see [https://math.stackexchange.com/questions/3143290/writing-action-value-function-in-terms-of-state-value-function-for-a-markov-deci this question] for some details. I think the following works: It suffices to show <math>\Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \Pr(G_{t+1} = g \mid S_{t+1}=s')</math>. Conditioning over actions in the <math>t+1</math> timestep, we have <math>\Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) =\sum_{a'} \Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)\Pr(A_{t+1}=a' \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>. But the action is chosen by the policy, which depends only on the state, so <math>\Pr(A_{t+1}=a' \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \pi(a' \mid s')</math>. As for <math>\Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>, recall that <math>G_{t+1} = R_{t+2} + R_{t+3} + \cdots</math> so has only rewards starting on timestep <math>t+2</math>. Since we are working with an MDP, the environment's dynamics are completely determined by the state and action on timestep <math>t+1</math>, i.e. on <math>s'</math> and <math>a'</math>. So this is equal to <math>\Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s')</math>. Thus we have <math>\sum_{a'} \Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s')\pi(a'\mid s')</math>. Now we can reverse the conditionalization, i.e. we marginalize to obtain <math>\Pr(G_{t+1} = g \mid S_{t+1}=s')</math>.
This is basically what we want, except we want to say that <math>\sum_g g \Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \mathbb E_\pi[G_{t+1} \mid S_{t+1}=s']</math>, since the latter expression equals <math>v_\pi(s')</math>. Thankfully, we can say this, because of the Markov assumption. Actually proving this is a little complicated; see [https://math.stackexchange.com/questions/3143290/writing-action-value-function-in-terms-of-state-value-function-for-a-markov-deci this question] for some details. I think the following works: It suffices to show <math>\Pr(G_{t+1} = g \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \Pr(G_{t+1} = g \mid S_{t+1}=s')</math>. Conditioning over actions in the <math>t+1</math> timestep, we have <math>\sum_{a'} \Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)\Pr(A_{t+1}=a' \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>. But the action is chosen by the policy, which depends only on the state, so <math>\Pr(A_{t+1}=a' \mid S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s) = \pi(a' \mid s')</math>. As for <math>\Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s', R_{t+1}=r, A_t=a, S_t=s)</math>, recall that <math>G_{t+1} = R_{t+2} + R_{t+3} + \cdots</math> so has only rewards starting on timestep <math>t+2</math>. Since we are working with an MDP, the environment's dynamics are completely determined by the state and action on timestep <math>t+1</math>, i.e. on <math>s'</math> and <math>a'</math>. So this is equal to <math>\Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s')</math>. Thus we have <math>\sum_{a'} \Pr(G_{t+1} = g \mid A_{t+1}=a', S_{t+1}=s')\pi(a'\mid s')</math>. Now we can reverse the conditionalization, i.e. we marginalize to obtain <math>\Pr(G_{t+1} = g \mid S_{t+1}=s')</math>.


So we end up with
So we end up with

Revision as of 03:38, 1 September 2019

Bellman equation for vπ.

We want to show vπ(s)=aπ(as)s,rp(s,rs,a)[r+γvπ(s)] for all states s.

The core idea of the proof is to use the law of total probability to go from marginal to conditional probabilities, and then invoke the Markov assumption.

The law of total probability states that if B is an event, and C1,,Cn are events that partition the sample space, then Pr(B)=j=1nPr(BCj)=j=1nPr(BCj)Pr(Cj).

For fixed event A with non-zero probability, the mapping BPr(BA) is another valid probability measure. In other words, define PrA by PrA(B):=Pr(BA) for all events B. Now the law of total probability for PA states that PrA(B)=j=1nPrA(BCj)PrA(Cj). We also have

PrA(BCj)=PrA(BCj)PrA(Cj)=Pr(BCjA)Pr(CjA)=Pr(BCjA)/Pr(A)Pr(CjA)/Pr(A)=Pr(B(CjA))Pr(CjA)=Pr(BCj,A)

So the law of total probability states that Pr(BA)=j=1nPr(BCj,A)Pr(CjA).

Now we see how the law of total probability interacts with conditional expectation. Let X be a random variable. Then

E[XA]=xxPr(X=xA)=xxjPr(X=xCj,A)Pr(CjA)

Here the event X=x is playing the role of B in the statement of the conditional law of total probability.

This is the basic trick of the proof; we keep conditioning on different things (actions, next states, rewards) and using the law of total probability.

By definition, vπ(s)=Eπ[GtSt=s]. Now rewrite Gt=Rt+1+γGt+1 and use the linearity of expectation to get Eπ[Rt+1St=s]+γEπ[Gt+1St=s]. From here, we can work separately with Eπ[Rt+1St=s] and Eπ[Gt+1St=s] for a while.

Using the law of total probability while conditioning over actions, we have

Eπ[Rt+1St=s]=rrPr(Rt+1=rSt=s)=rraPr(Rt+1=rAt=a,St=s)Pr(At=aSt=s)

Using the convention that Pr(At=aSt=s)=π(as), this becomes

Eπ[Rt+1St=s]=rraPr(Rt+1=rAt=a,St=s)π(as)

Now we can reorder the sums to get

aπ(as)rrPr(Rt+1=rAt=a,St=s)

Again, using the law of total probability (in its conjunction form) while conditioning this time over states, we have

aπ(as)rrsPr(Rt+1=r,St+1=sAt=a,St=s)

Reordering the sums, this becomes

aπ(as)srPr(Rt+1=r,St+1=sAt=a,St=s)r

Sutton and Barto abbreviate Pr(Rt+1=r,St+1=sAt=a,St=s) as p(s,rs,a) (strictly speaking, we should track the timestep parameter t, but we will omit this detail here). We can also combine the nested sums sr into a single sum that iterates over pairs (s,r). So we obtain

aπ(as)s,rp(s,rs,a)r

This completes the part for Eπ[Rt+1St=s]. In other words, we have shown that

Eπ[Rt+1St=s]=aπ(as)s,rp(s,rs,a)r

Now we do a similar series of steps for Eπ[Gt+1St=s]. Conditioning over actions,

Eπ[Gt+1St=s]=ggPr(Gt+1=gSt=s)=ggaPr(Gt+1=gAt=a,St=s)π(as)

Rearranging sums, this is

aπ(as)ggPr(Gt+1=gAt=a,St=s)

Conditioning over states and rewards, we have

aπ(as)ggs,rPr(Gt+1=g,St+1=s,Rt+1=rAt=a,St=s)

Now write Pr(Gt+1=g,St+1=s,Rt+1=rAt=a,St=s) as Pr(Gt+1=gSt+1=s,Rt+1=r,At=a,St=s)Pr(St+1=s,Rt+1=rAt=a,St=s). Since Pr(St+1=s,Rt+1=rAt=a,St=s)=p(s,rs,a) we have Pr(Gt+1=g,St+1=s,Rt+1=rAt=a,St=s)=Pr(Gt+1=gSt+1=s,Rt+1=r,At=a,St=s)p(s,rs,a). Thus, substituting this expression and rearranging sums, we have

aπ(as)s,rp(s,rs,a)ggPr(Gt+1=gSt+1=s,Rt+1=r,At=a,St=s)

This is basically what we want, except we want to say that ggPr(Gt+1=gSt+1=s,Rt+1=r,At=a,St=s)=Eπ[Gt+1St+1=s], since the latter expression equals vπ(s). Thankfully, we can say this, because of the Markov assumption. Actually proving this is a little complicated; see this question for some details. I think the following works: It suffices to show Pr(Gt+1=gSt+1=s,Rt+1=r,At=a,St=s)=Pr(Gt+1=gSt+1=s). Conditioning over actions in the t+1 timestep, we have aPr(Gt+1=gAt+1=a,St+1=s,Rt+1=r,At=a,St=s)Pr(At+1=aSt+1=s,Rt+1=r,At=a,St=s). But the action is chosen by the policy, which depends only on the state, so Pr(At+1=aSt+1=s,Rt+1=r,At=a,St=s)=π(as). As for Pr(Gt+1=gAt+1=a,St+1=s,Rt+1=r,At=a,St=s), recall that Gt+1=Rt+2+Rt+3+ so has only rewards starting on timestep t+2. Since we are working with an MDP, the environment's dynamics are completely determined by the state and action on timestep t+1, i.e. on s and a. So this is equal to Pr(Gt+1=gAt+1=a,St+1=s). Thus we have aPr(Gt+1=gAt+1=a,St+1=s)π(as). Now we can reverse the conditionalization, i.e. we marginalize to obtain Pr(Gt+1=gSt+1=s).

So we end up with

aπ(as)s,rp(s,rs,a)vπ(s)

So now we have

vπ(s)=aπ(as)s,rp(s',rs,a)r+γaπ(as)s,rp(s',rs,a)vπ(s')=aπ(as)s,rp(s',rs,a)[r+γvπ(s')]

This completes the proof. I'm pretty confused why Sutton and Barto seem to think abbreviating all these steps without comment is a good idea.