> For the complete documentation index, see [llms.txt](https://drdh.gitbook.io/rl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://drdh.gitbook.io/rl/deep-rl-course/actor-critic-algorithms/policy-evaluation.md).

# Policy evaluation

## Fit what ?

![Policy evaluation](/files/-LlW-d17CYTygjyhOvt8)

So now we need to fit one of $$Q^\pi,V^\pi,A^\pi$$. The question is fit what to what? We knew:

$$
\begin{aligned}
Q^\pi(s\_t,a\_t)&=\sum\_{t'=t}^T \mathbb{E}*{\pi*\theta}\[r(s\_{t'},a\_{t'})|s\_t,a\_t] \\
V^\pi(s\_t)&=\mathbb{E}*{a\_t\sim \pi*\theta(a\_t|s\_t)}\[Q^\pi(s\_t,a\_t)]    \\
A^\pi(s\_t,a\_t)&=Q^\pi(s\_t,a\_t)-V^\pi(s\_t)
\end{aligned}
$$

Firstly, both $$Q^\pi$$ and $$A^\pi$$ can be calculated or approximated by $$V^\pi$$:

$$
\begin{aligned}
Q^\pi(s\_t,a\_t) &=  \sum\_{t'=t}^T \mathbb{E}*{\pi*\theta}\[r(s\_{t'},a\_{t'})|s\_t,a\_t] \\
&\approx r(s\_t,a\_t)+\sum\_{t'=t+1}^T \mathbb{E}*{\pi*\theta}\[r(s\_{t'},a\_{t'})|s\_t,a\_t] \\
&\approx r(s\_t,a\_t)+V^\pi(s\_{t+1})\\

A^\pi(s\_t,a\_t)&\approx r(s\_t,a\_t)+V^\pi(s\_{t+1})-V^\pi(s\_t)
\end{aligned}
$$

Apart from this, both $$Q^\pi$$ and $$A^\pi$$ need 2 inputs $$s\_t, a\_t$$, but $$V^\pi$$ only need $$s\_t$$, which may be easier.

So let's just fit $$V^\pi$$.

## Fitted to what?

$$
V^\pi(s\_t)=\sum\_{t'=t}^T \mathbb{E}*{\pi*\theta}\[r(s\_{t'},a\_{t'})|s\_t]
$$

Fit $$V^\pi(s\_t)$$, evaluate how good is the policy, which is called policy evaluation. As what policy gradient does, we can use Monte Carlo policy evaluation.

$$
V^\pi(s\_t)\approx\sum\_{t'=t}^T r(s\_{t'},a\_{t'})
$$

if we are able to reset the simulator, we can do more than one samples:

$$
V^\pi(s\_t)\approx\frac{1}{N}\sum\_{i=1}^N\sum\_{t'=t}^T r(s\_{t'},a\_{t'})
$$

But the former is still pretty good, so the supervised learning condition is:

training data: $$\left{\left (s\_{i,t},\sum\_{t'=t}^T r(s\_{i,t'},a\_{i,t'})\right) \right}$$. Define target $$y\_{i,t}=\sum\_{t'=t}^T r(s\_{i,t'},a\_{i,t'})$$.

supervised regression: $$\mathcal{L}(\phi)=\frac{1}{2}\sum\_i ||\hat{V}\_{\phi}^\pi(s\_i)-y\_i||^2$$

## Bootstrapped estimate

The Monte Carlo target $$y\_i$$ is not perfect. Can we do better? The ideal target is:

$$
\begin{aligned}
y\_{i,t}&=\sum\_{t'=t}^T \mathbb{E}*{\pi*\theta}\[r(s\_{i,t'},a\_{i,t'})|s\_{i,t}] \\
&\approx r(s\_{i,t},a\_{i,t})+\sum\_{t'=t+1}^T \mathbb{E}*{\pi*\theta}\[r(s\_{i,t'},a\_{i,t'})|s\_{i,t}] \\
&\approx r(s\_{i,t},a\_{i,t})+V^\pi(s\_{t+1})\\
&\approx  r(s\_{i,t},a\_{i,t})+\hat{V}^\pi\_{\phi}(s\_{t+1})\\
\end{aligned}
$$

Sample one step and the directly use previous fitted value function.

This estimate will cause more bias but lower variance.

## Algorithm

> Batch actor-critic algorithm:
>
> repeat until converge:
>
> \====1: sample $${s\_i,a\_i }$$ from $$\pi\_\theta(a|s)$$ (run it on the robot)
>
> \====2: fit $$\hat{V}^\pi\_\phi(s)$$ to sampled reward sums (use bootstrapped estimate target)
>
> \====3: evaluate $$\hat{A}^\pi(s\_i,a\_i)=r(s\_i,a\_i)+\hat{V}^\pi\_\phi(s\_i')-\hat{V}^\pi\_\phi(s\_i)$$
>
> \====4: $$\nabla\_\theta J(\theta)\approx \sum\_i \nabla\_\theta \log \pi\_\theta (a\_i|s\_i)\hat{A}^\pi(s\_i,a\_i)$$
>
> \====5: $$\theta \leftarrow \theta+ \alpha\nabla\_\theta J(\theta)$$

![Batch actor-critic algorithm](/files/-LlW-d1C2_gXHmXkBuBZ)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://drdh.gitbook.io/rl/deep-rl-course/actor-critic-algorithms/policy-evaluation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
