\[v_\pi(s)=\mathbb E_\pi[G_t\mid S_t=s].\]
One step
Many steps · Start in \(s\), follow \(\pi\), and discount future rewards by \(\gamma=0.9\).
Split each trajectory’s return into its first reward and the return that follows: \[G_t=R_{t+1}+\gamma G_{t+1}.\]
Average over the first action and the outcome it produces: \[v_\pi(s)=\sum_a \underbrace{\pi(a\mid s)}_{\text{choose a move}} \sum_{s',r}\underbrace{p(s',r\mid s,a)}_{\text{observe an outcome}} \big[r+\gamma v_\pi(s')\big].\]
Once the next state is known, its value summarizes the rest of the trajectory.
\[\pi'\succeq\pi\quad\Longleftrightarrow\quad v_{\pi'}(s)\ge v_\pi(s)\quad\text{for every state }s.\]
Strict improvement also requires a higher value in at least one state. Here, always north wins at A (\(24.4>8.8\)) and loses at the upper-left corner (\(-10<3.3\)). Neither policy dominates.
\(\textcolor{#00776e}{\pi(a\mid C)}\) action probability · \(\textcolor{#a44316}{r_a}\) reward · \(\textcolor{#733c95}{\gamma}\) discount · \(\textcolor{#245395}{v_\pi(s'_a)}\) next-state value
\[\textcolor{#245395}{v_\pi(C)}=\sum_a \textcolor{#00776e}{\pi(a\mid C)}\big[\textcolor{#a44316}{r_a}+\textcolor{#733c95}{\gamma}\,\textcolor{#245395}{v_\pi(s'_a)}\big].\]
\[\begin{aligned} v_\pi(C)\approx{}&\textcolor{#00776e}{\tfrac24}(\textcolor{#a44316}{-1}+\textcolor{#733c95}{0.9}\cdot\textcolor{#245395}{3.3}) &&\text{north or west → C}\\ +{}&\textcolor{#00776e}{\tfrac14}(\textcolor{#a44316}{0}+\textcolor{#733c95}{0.9}\cdot\textcolor{#245395}{8.8}) &&\text{east → E}\\ +{}&\textcolor{#00776e}{\tfrac14}(\textcolor{#a44316}{0}+\textcolor{#733c95}{0.9}\cdot\textcolor{#245395}{1.5}) &&\text{south → S}\\ ={}&3.3025. \end{aligned}\]
Rounded to one decimal · \(3.3\), the value shown at C.
Both quantities measure expected discounted return. What do we fix before averaging?
| Quantity | Inputs | First move | How to use it |
|---|---|---|---|
| \(v_\pi(s)\) | State | Draw from \(\pi\) | Evaluate a starting square. To compare moves, look ahead with known or estimated dynamics. |
| \(q_\pi(s,a)\) | State and action | Choose \(a\), then follow \(\pi\) | Compare moves directly at the same square, even with unknown dynamics. |
In Gridworld, \(q_\pi(C,\mathrm{east})\) values east from the corner and the whole path afterward.
Fix the first action and average the remaining trajectory: \[q_\pi(s,a)=\mathbb E[G_t\mid S_t=s,\ \text{take }a\text{ now, then follow }\pi].\]
Look one step ahead to compute an action value: \[q_\pi(s,a)=\sum_{s',r}p(s',r\mid s,a)\,[r+\gamma v_\pi(s')].\]
Let the policy choose to recover the state value: \[v_\pi(s)=\sum_a\pi(a\mid s)q_\pi(s,a).\]
Choose a deterministic greedy move \(\pi'(s)\in\arg\max_a q_\pi(s,a)\) in every state.
1. Change one move. Take the new move, then follow the old policy: \[q_\pi(s,\pi'(s))=\max_a q_\pi(s,a)\ge v_\pi(s).\]
2. Keep using the improved policy. The same one-step improvement holds at every successor. Applying it there, and then at each later step, gives \[v_{\pi'}(s)=q_{\pi'}(s,\pi'(s))\ \ge\ q_\pi(s,\pi'(s)).\]
The two \(q\) terms fix the same first move. Their subscripts tell us which policy takes over afterward.
In each square: arrows give the policy (“all” means four equally likely moves). The number is the value of that policy.
Read left to right. Evaluate each map → choose greedy moves everywhere → evaluate again. At the lower-right corner the value rises from \(-1.98\) to \(10.89\) to \(11.67\).
\[v_*(s)=\max_a q_*(s,a).\]
From optimal action values \(q_*\)
\[\pi_*(s)\in\arg\max_a q_*(s,a).\]
At the upper-left corner, look up each move’s value. East scores 21.98, north and west 18.78, and south 17.80. Choose east.
From optimal state values \(v_*\) and the model
\[\begin{aligned} h(s,a)&=\sum_{s',r}p(s',r\mid s,a)\,[r+\gamma v_*(s')],\\ \pi_*(s)&\in\arg\max_a h(s,a). \end{aligned}\]
East reaches A. Its score is \(0+0.9(24.42)\approx21.98\), the largest of the four.
\[v_\pi(s)=\mathbb E_\pi[G_t\mid S_t=s]\ \approx\ \frac{1}{N(s)}\sum_{i=1}^{N(s)}g^{(i)}(s).\]
First-visit Monte Carlo
Toy grid · two episodes · \(\gamma=0.9\)
\(N(C)=2,\quad\hat v(C)=(9+1.8)/2=5.4\).
Tradeoff. Full returns use observed rewards. Updates wait for the episode to finish.
Evaluate the policy generating the data
\[\underbrace{y=r+\gamma\hat v(s')}_{\text{one-step target}}\] \[\hat v(s)\leftarrow\hat v(s)+\alpha[y-\hat v(s)].\]
Same first move · \(C\xrightarrow{0}B\)
Suppose \(\hat v(C)=2\), \(\hat v(B)=8\).
\[y=0+0.9(8)=7.2.\] \[\hat v(C)\leftarrow 2+0.25(7.2-2)=3.3.\]
Only the entry for \(C\) changes.
Experience only. Sample under \(\pi\); its probabilities and the transition model are unnecessary.
Tradeoff. Update immediately, but error in \(\hat v(s')\) enters the target.
Learn to choose a move
\[y=r+\gamma\max_{a'}\hat q(s',a')\] \[\hat q(s,a)\leftarrow\hat q(s,a)+\alpha[y-\hat q(s,a)].\]
Same first move · east from \(C\) to \(B\)
At \(B\), suppose the two moves score \(6\) and \(10\). At \(C\), east scores \(2\).
\[y=0+0.9\max(6,10)=9.\] \[\hat q(C,\mathrm{east})\leftarrow 2+0.25(9-2)=3.75.\]
The target uses \(10\) even if exploration next tries the move valued at \(6\).
From experience to control. Explore to collect transitions. Each update uses the sample and the Q table alone. Choose greedily from the learned values.
Value-based control. Estimate values, then choose a policy \(\pi\). With state values \(v(s)\), look one move ahead using the model. With action values \(q(s,a)\), choose a highest-valued move directly.
Direct policy optimization. Learn the policy \(\pi_\theta(a\mid s)\) directly. Sampled returns guide changes to \(\theta\) without requiring a separate value estimate.
Actor–critic. Learn both \(\pi_\theta\) (the actor) and \(v_\phi\) (the critic). The critic estimates the return expected from each state, giving the actor a baseline that can reduce noise in its updates.
For a finite episode \(\tau=(s_0,a_0,r_1,\ldots,s_T)\), let \(G_0(\tau)\) be its discounted return. Policy objective. Maximize expected return from starting states drawn from \(\rho_0\): \[J(\theta)=\mathbb E_{\tau\sim p_\theta}[G_0(\tau)]=\sum_\tau p_\theta(\tau)G_0(\tau).\]
\[p_\theta(\tau)=\underbrace{\rho_0(s_0)}_{\text{start}}\prod_{t=0}^{T-1}\underbrace{\pi_\theta(a_t\mid s_t)}_{\text{policy}}\,\underbrace{p(s_{t+1},r_{t+1}\mid s_t,a_t)}_{\text{environment}}.\]
A recorded trajectory has a fixed return. Its probability of occurring changes with \(\theta\).
For a fixed trajectory, \(G_0(\tau)\) is fixed. Differentiating only its return would miss the change in trajectory probability.
The log-derivative trick · On trajectories with positive probability, \[\nabla_\theta\log p_\theta(\tau)=\frac{\nabla_\theta p_\theta(\tau)}{p_\theta(\tau)} \quad\Longrightarrow\quad \nabla_\theta p_\theta(\tau)=p_\theta(\tau)\nabla_\theta\log p_\theta(\tau).\]
\[\begin{aligned} \nabla_\theta J(\theta) &=\nabla_\theta\sum_\tau p_\theta(\tau)G_0(\tau)\\ &=\sum_\tau [\nabla_\theta p_\theta(\tau)]G_0(\tau)\\ &=\sum_\tau p_\theta(\tau)[\nabla_\theta\log p_\theta(\tau)]G_0(\tau). \end{aligned}\]
The leading \(p_\theta(\tau)\) gives the sampling distribution for the gradient.
Taking the log turns the trajectory product into a sum: \[\log p_\theta(\tau)=\underbrace{\log\rho_0(s_0)}_{\text{fixed}} +\sum_t\Big[\log\pi_\theta(a_t\mid s_t)+\underbrace{\log p(s_{t+1},r_{t+1}\mid s_t,a_t)}_{\text{fixed environment}}\Big].\]
For this recorded path, only the action probabilities depend on \(\theta\): \[\nabla_\theta\log p_\theta(\tau)=\sum_t\nabla_\theta\log\pi_\theta(a_t\mid s_t).\]
\[\nabla_\theta J(\theta)=\mathbb E_{\tau\sim p_\theta}\!\left[G_0(\tau)\sum_t\nabla_\theta\log\pi_\theta(A_t\mid S_t)\right].\]
For ascent: sample a return and differentiate the policy. The update needs a gradient estimate and can proceed without evaluating \(J(\theta)\) exactly.
The batch mean of the returns estimates \(J\). The weighted gradients estimate how \(J\) changes.
Let \(z_t=\nabla_\theta\log\pi_\theta(A_t\mid S_t)\) be the sampled action’s gradient. Center its return using a baseline \(b(S_t)\): \[\mathbb E[(G_t-b(S_t))z_t\mid S_t=s]=\mathbb E[G_t z_t\mid S_t=s].\]