Notes on Chapters 1–2 of Shiyu Zhao's Mathematical Foundations of Reinforcement Learning [1].

Reinforcement learning studies an agent that learns how to act by interacting with an environment. This post sets up the language (Markov decision processes), defines how good a policy is (state values), and derives the equation that computes it (the Bellman equation).

1. Basic concepts

The running example is a robot (the agent) in a grid. Orange cells are forbidden (the agent may enter them but is punished) and the blue cell is the target.

s1s2s3s4s5s6s7s8s9(a) statesa1a2a3a4a5(b) actions
Figure 1. (a) A \(3\times 3\) grid world with states \(s_1, \dots, s_9\); \(s_6, s_7\) are forbidden and \(s_9\) is the target. (b) The five actions: up, right, down, left, stay.

Everything is packed into a Markov decision process (MDP):

  • Sets: states \(\mathcal{S}\) (here the cells), actions \(\mathcal{A}(s)\) (here up, right, down, left, stay) and rewards \(\mathcal{R}(s,a)\).
  • Model: the transition probabilities \(p(s'\,|\,s,a)\) and reward probabilities \(p(r\,|\,s,a)\). In the grid, moving into the boundary bounces the agent back, and the rewards are \(r_{\text{boundary}} = r_{\text{forbidden}} = -1\), \(r_{\text{target}} = 1\) (including staying at the target), and \(0\) otherwise.
  • Policy: \(\pi(a\,|\,s)\), the probability of taking \(a\) in \(s\). We draw policies as arrows (a circle means "stay").
  • Markov property: the next state and reward depend only on the current state and action, \(p(s_{t+1}\,|\,s_t, a_t, s_{t-1}, \dots, s_0, a_0) = p(s_{t+1}\,|\,s_t, a_t)\), and likewise for \(r_{t+1}\).

Following a policy produces a trajectory of states, actions and rewards. Its quality is not the immediate reward but the rewards collected in the long run. Since an agent that stays at the target collects \(+1\) forever, we sum the rewards with a discount rate \(\gamma \in (0,1)\):

(a) policy 1: return = 1s1s2s3s4s5s6s7s8s9000+1(b) policy 2: return = 0s1s2s3s4s5s6s7s8s90−10+1
Figure 2. Two policies started from \(s_1\). The first walks around the forbidden cells; the second walks through \(s_7\).
\[ \begin{aligned} \text{policy 1:}\quad & 0 + \gamma\cdot 0 + \gamma^2\cdot 0 + \gamma^3\cdot 1 + \gamma^4\cdot 1 + \cdots = \frac{\gamma^3}{1-\gamma}, \\ \text{policy 2:}\quad & 0 + \gamma\cdot(-1) + \gamma^2\cdot 0 + \gamma^3\cdot 1 + \gamma^4\cdot 1 + \cdots = -\gamma + \frac{\gamma^3}{1-\gamma}. \end{aligned} \]

The discounted return is always finite (at most \(r_{\max}/(1-\gamma)\)), and \(\gamma\) sets how far the agent looks ahead: a small \(\gamma\) makes it short-sighted, a \(\gamma\) close to \(1\) far-sighted. Policy 1 has the larger return, which agrees with the intuition that it is the better policy.

A trajectory that stops at a terminal state is an episode. We never really stop: the target is treated as an ordinary state with all five actions, and the agent learns by itself that staying there is best. This way episodic and continuing tasks are handled in the same framework.

2. State values

With time steps \(t = 0, 1, 2, \dots\), the agent in state \(S_t\) takes \(A_t\), then receives \(R_{t+1}\) and moves to \(S_{t+1}\). The discounted return from time \(t\) is the random variable

\[ \begin{equation} G_t \doteq R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots = R_{t+1} + \gamma\, G_{t+1}. \label{eq:Gt} \end{equation} \]

When the policy or the model is random, a state leads to many trajectories, so we judge a policy by the expected return. This is the state value:

\[ \begin{equation} v_\pi(s) \doteq \mathbb{E}\,[\,G_t \,|\, S_t = s\,]. \label{eq:v} \end{equation} \]

It depends on the starting state and on the policy, but not on \(t\), because the model and the policy do not change over time. If the policy and the model are deterministic, there is one trajectory per state and the state value is its return; otherwise it is the mean of the returns. A policy with larger state values is a better policy.

3. The Bellman equation

3.1 Derivation

Insert \(\eqref{eq:Gt}\) into \(\eqref{eq:v}\):

\[ v_\pi(s) = \underbrace{\mathbb{E}[R_{t+1}\,|\,S_t = s]}_{\text{immediate reward}} + \gamma\,\underbrace{\mathbb{E}[G_{t+1}\,|\,S_t = s]}_{\text{future rewards}} . \]

The first term is computed by conditioning on the action:

\[ \mathbb{E}[R_{t+1}\,|\,S_t = s] = \sum_a \pi(a\,|\,s)\,\mathbb{E}[R_{t+1}\,|\,S_t = s, A_t = a] = \sum_a \pi(a\,|\,s) \sum_r p(r\,|\,s,a)\, r . \]

The second by conditioning on the next state:

\[ \begin{aligned} \mathbb{E}[G_{t+1}\,|\,S_t = s] &= \sum_{s'} \mathbb{E}[G_{t+1}\,|\,S_t = s, S_{t+1} = s']\; p(s'\,|\,s) = \sum_{s'} \mathbb{E}[G_{t+1}\,|\,S_{t+1} = s']\; p(s'\,|\,s) && \text{(Markov property)} \\ &= \sum_{s'} v_\pi(s')\, p(s'\,|\,s) = \sum_{s'} v_\pi(s') \sum_a \pi(a\,|\,s)\, p(s'\,|\,s, a). \end{aligned} \]

Together they give the Bellman equation:

\[ \begin{equation} v_\pi(s) = \sum_a \pi(a\,|\,s) \Big[ \sum_r p(r\,|\,s,a)\, r + \gamma \sum_{s'} p(s'\,|\,s,a)\, v_\pi(s') \Big] = \mathop{\mathbb{E}}_{a \sim \pi(\cdot|s)} \mathop{\mathbb{E}}_{r, s'}\big[\, r + \gamma\, v_\pi(s') \,\big], \qquad s \in \mathcal{S}. \label{eq:bellman} \end{equation} \]

The first form uses the marginals \(p(r|s,a)\) and \(p(s'|s,a)\), the second the joint \(p(r, s'|s,a)\). They agree by linearity of expectation, whether or not \(r\) and \(s'\) are independent.

It is a system of \(|\mathcal{S}|\) equations in which the value of a state is expressed through the values of other states (bootstrapping). Solving it for a given \(\pi\) is called policy evaluation.

Example. On the \(2\times 2\) grid of Figure 3 (\(s_2\) forbidden, \(s_4\) target), take the policy that at \(s_1\) goes right or down with probability \(0.5\) each:

(a) policy 1s1s2s3s4(b) policy 2s1s2s3s40.50.5(c) policy 3s1s2s3s4
Figure 3. Three policies on a \(2\times 2\) grid that differ only at \(s_1\). The example uses policy 3.
\[ v_\pi(s_1) = 0.5\,[\,0 + \gamma v_\pi(s_3)\,] + 0.5\,[\,{-1} + \gamma v_\pi(s_2)\,], \qquad v_\pi(s_2) = v_\pi(s_3) = 1 + \gamma v_\pi(s_4), \qquad v_\pi(s_4) = 1 + \gamma v_\pi(s_4). \]

Solving from the bottom up gives \(v_\pi(s_2) = v_\pi(s_3) = v_\pi(s_4) = \frac{1}{1-\gamma}\) and \(v_\pi(s_1) = -0.5 + \frac{\gamma}{1-\gamma}\); with \(\gamma = 0.9\), \(v_\pi = (8.5, 10, 10, 10)\).

3.2 Matrix–vector form

Define the policy-averaged reward and transition probability

\[ r_\pi(s) \doteq \sum_a \pi(a\,|\,s) \sum_r p(r\,|\,s,a)\, r, \qquad p_\pi(s'\,|\,s) \doteq \sum_a \pi(a\,|\,s)\, p(s'\,|\,s,a). \]

Stacking the equations of all \(n = |\mathcal{S}|\) states, with \([P_\pi]_{ij} = p_\pi(s_j\,|\,s_i)\),

\[ \begin{equation} v_\pi = r_\pi + \gamma P_\pi v_\pi \qquad\Longleftrightarrow\qquad (I - \gamma P_\pi)\, v_\pi = r_\pi . \label{eq:bellman-matrix} \end{equation} \]

For the example above,

\[ \begin{bmatrix} v_\pi(s_1) \\ v_\pi(s_2) \\ v_\pi(s_3) \\ v_\pi(s_4) \end{bmatrix} = \begin{bmatrix} -0.5 \\ 1 \\ 1 \\ 1 \end{bmatrix} + \gamma \begin{bmatrix} 0 & 0.5 & 0.5 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} v_\pi(s_1) \\ v_\pi(s_2) \\ v_\pi(s_3) \\ v_\pi(s_4) \end{bmatrix}. \]

\(P_\pi\) is nonnegative and each row sums to one (\(P_\pi \mathbf{1} = \mathbf{1}\)). Throughout the series, \(\ge\) between vectors or matrices is elementwise.

4. Solving the Bellman equation

4.1 \(I - \gamma P_\pi\) is invertible

A matrix is invertible if and only if \(0\) is not an eigenvalue, since \(\det A = \lambda_1 \lambda_2 \cdots \lambda_n\). To locate the eigenvalues we use the following theorem.

Gershgorin circle theorem [3]

Every eigenvalue of \(A \in \mathbb{C}^{n\times n}\) lies in at least one disc \(D_i = \big\{ z : |z - a_{ii}| \le \sum_{j\neq i} |a_{ij}| \big\}\).

Proof. Let \(A\mathbf{x} = \lambda\mathbf{x}\), \(\mathbf{x} \neq \mathbf{0}\), and pick \(i\) with \(|x_i| = \max_j |x_j|\). Row \(i\) gives \((\lambda - a_{ii})\, x_i = \sum_{j \neq i} a_{ij}\, x_j\), so \(|\lambda - a_{ii}|\,|x_i| \le \sum_{j\neq i} |a_{ij}|\,|x_i|\). Divide by \(|x_i| > 0\). \(\square\)

For \(I - \gamma P_\pi\), row \(i\) has diagonal entry \(1 - \gamma p_{ii}\), and its off-diagonal entries sum in absolute value to \(\gamma(1 - p_{ii})\). If \(0\) were an eigenvalue, then for some \(i\),

\[ 1 - \gamma p_{ii} \le \gamma (1 - p_{ii}) \quad\Longrightarrow\quad 1 \le \gamma , \]

a contradiction. In fact, every disc lies to the right of \(\operatorname{Re} z = 1 - \gamma > 0\) (Figure 4).

ReIm01Re = 1 − γ
Figure 4. Gershgorin discs of \(I - \gamma P\) for a \(4\times 4\) stochastic \(P\) and \(\gamma = 0.9\), with the eigenvalues in black. No disc reaches \(0\).

4.2 The closed-form solution

Hence \(v_\pi = (I - \gamma P_\pi)^{-1} r_\pi\), and the inverse expands as a Neumann series:

\[ \begin{equation} (I - \gamma P_\pi)^{-1} = I + \gamma P_\pi + \gamma^2 P_\pi^2 + \cdots \label{eq:neumann} \end{equation} \]

Proof. With \(A = \gamma P_\pi\) and \(S_n = I + A + \cdots + A^n\), we have \((I - A)S_n = I - A^{n+1}\). Diagonalizing \(A\) to show \(A^n \to 0\) does not work in general, since \(P_\pi\) need not be diagonalizable. Use the max-row-sum norm \(\|A\|_\infty = \max_i \sum_j |a_{ij}|\) instead: it is submultiplicative and \(\|P_\pi\|_\infty = 1\), so \(\|A^{n}\|_\infty \le \gamma^{n} \to 0\). \(\square\)

Every term of \(\eqref{eq:neumann}\) is nonnegative, so \((I - \gamma P_\pi)^{-1} \ge I \ge 0\). In particular, it preserves order: if \(r_1 \ge r_2\) then \((I - \gamma P_\pi)^{-1} r_1 \ge (I - \gamma P_\pi)^{-1} r_2\). This is used again and again in the next posts.

The series also has a direct meaning. \([P_\pi^k]_{ij}\) is the probability of being in \(s_j\) exactly \(k\) steps after \(s_i\), so \([P_\pi^k r_\pi]_i = \mathbb{E}[R_{t+k+1}\,|\,S_t = s_i]\), and \(v_\pi = \sum_k \gamma^k P_\pi^k r_\pi\) is just \(\mathbb{E}[R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots]\), term by term.

4.3 The iterative solution

Inverting an \(n\times n\) matrix costs \(O(n^3)\). In practice we iterate from any \(v_0\):

\[ v_{k+1} = r_\pi + \gamma P_\pi v_k, \qquad k = 0, 1, 2, \dots \]

The error \(\delta_k = v_k - v_\pi\) satisfies \(\delta_{k+1} = \gamma P_\pi \delta_k\), so \(\delta_k = (\gamma P_\pi)^k \delta_0\) and \(\|v_k - v_\pi\|_\infty \le \gamma^k \|v_0 - v_\pi\|_\infty \to 0\). The error shrinks by a factor \(\gamma\) per step from any start. From \(v_0 = 0\), \(v_k\) is the expected discounted sum of the first \(k\) rewards, a truncation of the Neumann series. This pattern, iterating a map that shrinks distances, returns in the next post as the contraction mapping theorem.

Figure 5 evaluates two policies on a \(5\times 5\) grid world (\(\gamma = 0.9\)). Under the good policy, the target is worth \(1/(1-\gamma) = 10\), and a state \(d \ge 1\) steps away is worth \(10\gamma^{d-1}\). Under "always move right", every value is negative.

12345123453.53.94.34.85.33.13.54.85.35.92.82.510.05.96.62.510.010.010.07.32.39.010.09.08.1(a) a good policy1234512345−6.6−7.3−8.1−9.0−10.0−8.5−8.3−8.1−9.0−10.0−7.5−8.3−8.1−9.0−10.0−7.5−7.2−9.1−9.0−10.0−7.6−7.3−8.1−9.0−10.0(b) “always move right”
Figure 5. Two policies and their state values (number under each arrow). (a) A sensible policy. (b) "Always move right", which keeps bumping into the right wall.

5. Action values

To improve a policy we need to know how good each action is. The action value is the expected return after taking \(a\) in \(s\) and following \(\pi\) afterwards:

\[ q_\pi(s, a) \doteq \mathbb{E}\,[\,G_t \,|\, S_t = s, A_t = a\,]. \]

Conditioning on the action, and comparing with the bracket in \(\eqref{eq:bellman}\), gives the two relations

\[ \begin{equation} v_\pi(s) = \sum_a \pi(a\,|\,s)\, q_\pi(s, a), \qquad q_\pi(s, a) = \sum_r p(r\,|\,s,a)\, r + \gamma \sum_{s'} p(s'\,|\,s,a)\, v_\pi(s'). \label{eq:q-v} \end{equation} \]

For the example above (\(v_\pi = (8.5, 10, 10, 10)\)), the action values at \(s_1\) are \(6.65, 8, 9, 6.65, 7.65\) for up, right, down, left, stay, e.g. \(q_\pi(s_1, \text{down}) = 0 + \gamma v_\pi(s_3) = 9\), and indeed \(v_\pi(s_1) = 0.5 \cdot 8 + 0.5 \cdot 9 = 8.5\). Actions that the policy never takes still have values: take the action once, then follow \(\pi\). It is tempting to skip them or set them to zero, but that is wrong, because not being selected does not mean being bad; maybe the policy is the bad one. Here "down" is the best action at \(s_1\), which suggests changing the policy. This is the idea of the next post.

Substituting the first relation (at \(s'\)) into the second gives the Bellman equation for action values:

\[ q_\pi(s,a) = \sum_r p(r\,|\,s,a)\,r + \gamma \sum_{s'} p(s'\,|\,s,a) \sum_{a'} \pi(a'\,|\,s')\, q_\pi(s', a'). \]

In matrix form, stacking the \(nm\) pairs \((s_1,a_1), \dots, (s_n, a_m)\),

\[ q_\pi = \tilde r + \gamma\, P\, \Pi\, q_\pi, \qquad [\tilde r]_{(s,a)} = \sum_r p(r|s,a)\,r, \quad [P]_{(s,a),s'} = p(s'|s,a), \quad [\Pi]_{s,(s,a)} = \pi(a|s), \]

where \(\Pi \in \mathbb{R}^{n\times nm}\) is block diagonal. \(P\Pi\) is the transition matrix between state–action pairs, again nonnegative with unit row sums, so \(q_\pi\) is unique by the same argument. The model sits in \(\tilde r\) and \(P\), the policy only in \(\Pi\), and \(v_\pi = \Pi q_\pi\), \(q_\pi = \tilde r + \gamma P v_\pi\).

References

[1] Shiyu Zhao. Mathematical Foundations of Reinforcement Learning. Springer Nature Press and Tsinghua University Press, 2025. Chapters 1–2.

[2] Richard S. Sutton and Andrew G. Barto. Reinforcement Learning: An Introduction, 2nd edition. MIT Press, 2018. Chapter 3.

[3] Roger A. Horn and Charles R. Johnson. Matrix Analysis, 2nd edition. Cambridge University Press, 2013. Section 6.1.