# Value functions and Q-functions

## Definition: Q-function

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

Total reward from taking $$a\_t$$ in $$s\_t$$

## Definition: Value function

Total reward from $$s\_t$$

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

and can rewrite with Q-function. (The relation between Q-function and Value function)

$$
V^\pi(s\_t)=E\_{a\_t\sim \pi\_\theta (a\_t|s\_t)}\[Q^\pi (s\_t,a\_t)]
$$

Besides, the RL objective can rewrite with value function

$$
J(\theta)=E\_{s\_1\sim p(s\_1)}\[V^\pi(s\_1)]
$$

## Using Q-functions and Value function

### Idea 1

$$Q^\pi (s\_t,a\_t) \Rightarrow \text{improve policy }\pi$$

set $$\pi'(a|s)=1$$ if $$a=\arg\max\_a Q^\pi (s,a)$$. This new policy $$\pi'$$ is at least as good as $$\pi$$ (and probably better) and it doesn't matter what $$\pi$$ is.

### Idea 2

Compute gradient to increase probability of good actions $$a$$

if $$Q^\pi(s,a)> V^\pi (s)$$, the $$a$$ is better than average, recall that $$V^\pi(s)=E\[Q^\pi(s,a)]$$ under $$\pi(a|s)$$

So we can modify $$\pi(a|s)$$ to increase probability of $$a$$, like compute gradient.


---

# Agent Instructions: 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:

```
GET https://drdh.gitbook.io/rl/deep-rl-course/intro-to-rl/value-functions-and-q-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
