# Policies for which the policy improvement theorem holds

According to Reinforcement Learning (2nd Edition) by Sutton and Barto, the policy improvement theorem states that for any pair of deterministic policies $$\pi'$$ and $$\pi$$, if $$q_\pi(s,\pi'(s)) \geq v_\pi(s)$$ $$\forall s \in \mathcal{S}$$, then $$v_{\pi'}(s) \geq v_\pi(s)$$ $$\forall s \in \mathcal{S}$$.

The proof of this theorem seems to rely on $$\pi$$ and $$\pi'$$ being identical for all states except $$s$$. To my best understanding, this is what allows us to write the expectation $$\mathbb{E}[R_{t+1} + \gamma v_\pi(S_{t+1})|S_t = a, A_t = \pi'(s)]$$ as $$\mathbb{E}_{\pi'}[R_{t+1} + \gamma v_\pi(S_{t+1})|S_t = a]$$ in line 2, which is central to the proof (re-produced from the book below).

\begin{aligned} v_{\pi}(s) & \leq q_{\pi}\left(s, \pi^{\prime}(s)\right) \\ &=\mathbb{E}\left[R_{t+1}+\gamma v_{\pi}\left(S_{t+1}\right) \mid S_{t}=s, A_{t}=\pi^{\prime}(s)\right] \\ &=\mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma v_{\pi}\left(S_{t+1}\right) \mid S_{t}=s\right] \\ & \leq \mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma q_{\pi}\left(S_{t+1}, \pi^{\prime}\left(S_{t+1}\right)\right) \mid S_{t}=s\right] \\ &=\mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma \mathbb{E}_{\pi^{\prime}}\left[R_{t+2}+\gamma v_{\pi}\left(S_{t+2}\right) \mid S_{t+1}, A_{t+1}=\pi^{\prime}\left(S_{t+1}\right)\right] \mid S_{t}=s\right] \\ &=\mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma R_{t+2}+\gamma^{2} v_{\pi}\left(S_{t+2}\right) \mid S_{t}=s\right] \\ & \leq \mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma R_{t+2}+\gamma^{2} R_{t+3}+\gamma^{3} v_{\pi}\left(S_{t+3}\right) \mid S_{t}=s\right] \\ & \vdots \\ & \leq \mathbb{E}_{\pi^{\prime}}\left[R_{t+1}+\gamma R_{t+2}+\gamma^{2} R_{t+3}+\gamma^{3} R_{t+4}+\cdots \mid S_{t}=s\right] \\ &=v_{\pi^{\prime}}(s) \end{aligned}

Does this mean that the proof is merely proving the special case of the policy improvement theorem for when the policies are identical except at $$s$$? I am having trouble seeing why the proof holds for the more general case of the two policies being potentially different for all states. In that case, line 2 would not hold and the theorem would not hold for all states as it claims to do.

There are two policies in the derivation, $$\pi$$ and $$\pi'$$. When you have an expectation in the form

$$\mathbb{E}_{\pi_A}[R_{t+1} + \gamma v_{\pi_B}(S_{t+1}) | S_t = s]$$

, it means that from your current state $$s$$, you take an action according to $$\pi_A$$. Let's say this action is $$a$$, then the reward function from the environment will transition to the next state, represented by the random variable $$S_{t+1}$$ and also gives you a reward $$R_{t+1}$$ which can depend on the current state $$s$$, the action taken $$a$$ and the next state $$S_{t+1}$$. That's where your reliance on $$\pi_A$$ ends because $$v_{\pi_B}$$ is an expectation itself:

$$v_{\pi_B}(s) = \mathbb{E}_{\pi_B}[G_{t+1}|S_{t} = s]$$

So going from line 2 to line 3 in your derivation just means that you are taking the next action according to $$\pi'$$, but nothing inside the expectation changes, it still uses the same policy $$\pi$$. The next state, $$S_{t+1}$$, comes from the environment and depends on $$\pi'$$. The same is true for the observed reward $$R_{t+1}$$, but nothing inside $$v_\pi$$ needs the policy $$\pi'$$.

The proof then replaces $$v_\pi(S_{t+1})$$ with $$q_\pi(S_{t+1}, \pi'(S_{t+1}))$$ but the equality changes to a less than or equal to sign because the whole discussion in the book starts with the condition $$q_\pi(s, \pi'(s)) \ge v_\pi(s)$$.

It does not matter in how many states the policies differ. If the two policies are the same in states $$s_i$$, then $$q_\pi(s_i, \pi'(s_i)) = v_\pi(s_i)$$. If policy $$\pi'$$ is better than policy $$\pi$$ in states $$s_j$$, then $$q_\pi(s_j, \pi'(s_j)) > v_\pi(s_j)$$. The initial condition $$q_\pi(s, \pi'(s)) \ge v_\pi(s)$$ combines these two statements. If you know in which states the policy $$\pi'$$ is better, then you could think of replacing the $$\le$$ signs with $$<$$ signs in the derivation, and the rest of the states with $$=$$. You can't really do that because these are random variables, so even if you knew in which states your new policy performs better, you often don't know the environment dynamics that determine the next state, $$S_{t+1}$$.
{"ft_lang_label":"__label__en","ft_lang_prob":0.67622954,"math_prob":1.0000024,"size":2136,"snap":"2021-43-2021-49","text_gpt3_token_len":832,"char_repetition_ratio":0.18996248,"word_repetition_ratio":0.04255319,"special_character_ratio":0.40121722,"punctuation_ratio":0.039473683,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":1.000009,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-30T00:45:04Z\",\"WARC-Record-ID\":\"<urn:uuid:d01dc2dd-3393-42aa-acee-b7262eebb05a>\",\"Content-Length\":\"139463\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6ec30d23-11f0-4a6a-abb7-ae092fd13037>\",\"WARC-Concurrent-To\":\"<urn:uuid:fe371a6c-e903-4a99-b6fb-05a0b8d50a6a>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://ai.stackexchange.com/questions/30328/policies-for-which-the-policy-improvement-theorem-holds\",\"WARC-Payload-Digest\":\"sha1:VIHXNT6XTQZZW7NGHUD3T736BXXWI2PM\",\"WARC-Block-Digest\":\"sha1:HANEPO44NW2S6EKN42GVETNMO3HBSLJD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358847.80_warc_CC-MAIN-20211129225145-20211130015145-00130.warc.gz\"}"}