5.1.5Reinforcement Learning Foundations

Bellman equations

2,606 words12 min readdifficulty · medium6 backlinks

WHY they matter: In RL, an agent needs to know "how good is this state?" or "how good is this action?". The Bellman equations give us a way to compute these values by bootstrapping—using estimates of future values to improve current estimates. This is dynamic programming at its core.

WHAT they describe: The expected return (cumulative discounted reward) from any state or action pair, expressed in terms of immediate reward and the value of successor states.

HOW they work: By decomposing the infinite-horizon value into one-step reward + discounted future value, we turn an intractable sum into a recursive formula that can be solved iteratively.


Derivation from First Principles

Vπ(s)=Eπ[t=0γtRt+1S0=s]V^\pi(s) = \mathbb{E}_\pi\left[\sum_{t=0}^\infty \gamma^t R_{t+1} \mid S_0 = s\right]

where γ[0,1)\gamma \in [0,1) is the discount factor, Rt+1R_{t+1} is the reward at time t+1t+1, and the expectation is over trajectories generated by policy π\pi.

WHY this definition? We care about long-term cumulative reward, but distant rewards matter less (discount factor). The expectation accounts for stochasticity in both the environment and policy.


Bellman Equation for VπV^\pi (State Value)

Goal: Express Vπ(s)V^\pi(s) recursively.

Step 1: Expand the return sum: Vπ(s)=Eπ[R1+γR2+γ2R3+S0=s]V^\pi(s) = \mathbb{E}_\pi\left[R_1 + \gamma R_2 + \gamma^2 R_3 + \cdots \mid S_0 = s\right]

Step 2: Factor out the first reward: Vπ(s)=Eπ[R1S0=s]+Eπ[γ(R2+γR3+)S0=s]V^\pi(s) = \mathbb{E}_\pi\left[R_1 \mid S_0 = s\right] + \mathbb{E}_\pi\left[\gamma(R_2 + \gamma R_3 + \cdots) \mid S_0 = s\right]

Why this step? We're separating the immediate reward (which happens now) from the future rewards (which depend on where we go next).

Step 3: Recognize that the second term is the discounted value of the next state S1S_1: Vπ(s)=Eπ[R1S0=s]+γEπ[Vπ(S1)S0=s]V^\pi(s) = \mathbb{E}_\pi[R_1 \mid S_0 = s] + \gamma \mathbb{E}_\pi[V^\pi(S_1) \mid S_0 = s]

Step 4: Expand the expectation over actions and transitions: The policy gives us π(as)\pi(a|s), the dynamics give us p(s,rs,a)p(s', r | s, a):

Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s', r} p(s', r | s, a) \left[r + \gamma V^\pi(s')\right]

Interpretation: The value of state ss under policy π\pi equals the weighted average (over actions chosen by π\pi) of the expected immediate reward plus discounted next-state value.


Bellman Equation for QπQ^\pi (Action Value)

Qπ(s,a)=Eπ[t=0γtRt+1S0=s,A0=a]Q^\pi(s, a) = \mathbb{E}_\pi\left[\sum_{t=0}^\infty \gamma^t R_{t+1} \mid S_0 = s, A_0 = a\right]

WHY? Sometimes we need to evaluate specific actions (e.g., in Q-learning). QQ tells us "how good is action aa in state ss?".

Derivation:

Step 1: Condition on taking action aa first: Qπ(s,a)=E[R1+γVπ(S1)S0=s,A0=a]Q^\pi(s, a) = \mathbb{E}\left[R_1 + \gamma V^\pi(S_1) \mid S_0 = s, A_0 = a\right]

Why? After taking aa, we transition to S1S_1 and then follow policy π\pi, which gives us Vπ(S1)V^\pi(S_1).

Step 2: Expand over environment dynamics: Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]Q^\pi(s, a) = \sum_{s', r} p(s', r | s, a) \left[r + \gamma V^\pi(s')\right]

Step 3: Express Vπ(s)V^\pi(s') in terms of QπQ^\pi: Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s') = \sum_{a'} \pi(a'|s') Q^\pi(s', a')

Step 4: Substitute back:


Bellman Optimality Equations

GOAL: Find the best possible policy π\pi^* and its value functions V(s)V^*(s) and Q(s,a)Q^*(s, a).

KEY INSIGHT: The optimal policy is greedy with respect to VV^* or QQ^*: π(s)=argmaxaQ(s,a)\pi^*(s) = \arg\max_a Q^*(s, a)

Derivation of Bellman Optimality for VV^*:

Step 1: The optimal policy chooses the best action: V(s)=maxaQ(s,a)V^*(s) = \max_a Q^*(s, a)

Step 2: Substitute the QQ^* definition: V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s', r} p(s', r | s, a) \left[r + \gamma V^*(s')\right]

Why the max? We're no longer following a fixed policy—we're choosing the action that maximizes value at each step.

Derivation of Bellman Optimality for QQ^*:

Q(s,a)=s,rp(s,rs,a)[r+γmaxaQ(s,a)]Q^*(s, a) = \sum_{s', r} p(s', r | s, a) \left[r + \gamma \max_{a'} Q^*(s', a')\right]


Worked Examples

Compute Vπ(s1)V^\pi(s_1) using the Bellman equation.

Step 1: Write the Bellman equation for s1s_1: Vπ(s1)=aπ(as1)s,rp(s,rs1,a)[r+γVπ(s)]V^\pi(s_1) = \sum_a \pi(a|s_1) \sum_{s', r} p(s', r | s_1, a) \left[r + \gamma V^\pi(s')\right]

Step 2: Assume s1s_1 can move right to s2s_2 or down to s3s_3, and hitting walls keeps you in place:

  • Action "right": goes to s2s_2, reward 1-1
  • Action "down": goes to s3s_3, reward 1-1
  • Actions "left", "up": stay at s1s_1, reward 1-1

Step 3: Substitute: Vπ(s1)=0.25[(1)+0.9Vπ(s2)]+0.25[(1)+0.9Vπ(s3)]+0.25[(1)+0.9Vπ(s1)]+0.25[(1)+0.9Vπ(s1)]V^\pi(s_1) = 0.25[(-1) + 0.9 V^\pi(s_2)] + 0.25[(-1) + 0.9 V^\pi(s_3)] + 0.25[(-1) + 0.9 V^\pi(s_1)] + 0.25[(-1) + 0.9 V^\pi(s_1)]

Why this step? Each action has probability 0.25 under the uniform policy. We sum over all actions and their outcomes.

Step 4: Simplify (assuming symmetry Vπ(s2)=Vπ(s3)=VV^\pi(s_2) = V^\pi(s_3) = V): Vπ(s1)=1+0.9[0.25V+0.25V+0.25Vπ(s1)+0.25Vπ(s1)]V^\pi(s_1) = -1 + 0.9[0.25V + 0.25V + 0.25V^\pi(s_1) + 0.25V^\pi(s_1)] Vπ(s1)=1+0.9[0.5V+0.5Vπ(s1)]V^\pi(s_1) = -1 + 0.9[0.5V + 0.5V^\pi(s_1)] Vπ(s1)=1+0.45V+0.45Vπ(s1)V^\pi(s_1) = -1 + 0.45V + 0.45V^\pi(s_1) 0.55Vπ(s1)=1+0.45V0.55V^\pi(s_1) = -1 + 0.45V

Result: This is a system of linear equations. Solving iteratively or exactly gives the value. The Bellman equation turned a sequential problem into an algebraic one.


Apply the Bellman optimality equation as an update:

Step 1: Bellman optimality says: Q(s,a)=E[r+γmaxaQ(s,a)]Q^*(s, a) = \mathbb{E}[r + \gamma \max_{a'} Q^*(s', a')]

Step 2: We got a sample (s,a,r,s)(s, a, r, s') so the temporal difference target is: target=r+γmaxaQ(s,a)=5+0.9×12=5+10.8=15.8\text{target} = r + \gamma \max_{a'} Q(s', a') = 5 + 0.9 \times 12 = 5 + 10.8 = 15.8

Why? This is our bootstrapped estimate of the "true" Q(s,a)Q^*(s, a) based on this experience.

Step 3: Update Q(s,a)Q(s, a) toward the target (with learning rate α=0.1\alpha = 0.1): Q(s,a)Q(s,a)+α[targetQ(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha[\text{target} - Q(s, a)] Q(s,a)10+0.1[15.810]=10+0.58=10.58Q(s, a) \leftarrow 10 + 0.1[15.8 - 10] = 10 + 0.58 = 10.58

Interpretation: Our estimate of Q(s,a)Q(s, a) improved by moving toward the value predicted by the Bellman optimality equation.


Common Mistakes

Why it feels right: Seems simpler—just add rewards.

The problem: Without discounting, infinite-horizon returns diverge. If every state gives r=1r=1, then V(s)=V(s) = \infty. The discount γ\gamma ensures convergence and prefers near-term rewards.

Fix: Always include γ\gamma in the recursive term: V(s)=r+γV(s)V(s) = r + \gamma V(s').


Why it feels right: Both involve actions and values—easy to mix up.

The problem:

  • VπV^\pi is tied to a fixed policy π\pi, so we take the expectation over actions chosen by π\pi.
  • VV^* is the optimal value, so we take the max over all actions (choosing the best).

Fix:

  • Bellman expectation (for π\pi): aπ(as)\sum_a \pi(a|s) \ldots
  • Bellman optimality (for π\pi^*): maxa\max_a \ldots

Why it feels right: In deterministic environments, there's only one ss' per (s,a)(s, a).

The problem: Most environments are stochastic. Multiple next states are possible with different probabilities p(ss,a)p(s' | s, a).

Fix: Always sum/integrate over possible next states: V(s)=sp(ss,a)[r(s,a,s)+γV(s)]V(s) = \sum_{s'} p(s' | s, a) [r(s, a, s') + \gamma V(s')]


Active Recall Prompts

Recall Explain Bellman Equations to a 12-Year-Old

Imagine you're playing a video game and trying to figure out: "How many points will I get from this level?"

The Bellman equation says: Your total points = points you get right now + (slightly less valuable) points from the next level.

Why "slightly less valuable"? Because points later matter a bit less than points now (like getting ₹100 today is better than ₹100 next year—that's the discount factor).

So instead of adding up points from the entire game all at once (which is hard), you just think: "What do I get now, and what's the next level worth?" Then you use your guess of the next level's value to figure out this level's value. Keep doing this backward, and you figure out every level's value!

The "expectation" part means: if the game is random (sometimes you get a power-up, sometimes you don't), you average over all the possibilities.



Connections

  • Dynamic Programming — Bellman equations enable DP methods (policy iteration, value iteration)
  • Temporal Difference Learning — TD learning uses Bellman equations as update rules
  • Q-Learning — Off-policy algorithm built directly on Bellman optimality for QQ^*
  • Policy Gradient Methods — Contrast: optimize policy directly without explicit value functions
  • Markov Decision Process — Bellman equations formalize the value structure of MDPs
  • Discount Factor — Controls the tradeoff between immediate and future rewards
  • Bootstrapping — Key RL concept: using value estimates to update value estimates

#flashcards/ai-ml

What is the Bellman expectation equation for Vπ(s)V^\pi(s)? :: Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s', r} p(s', r | s, a) [r + \gamma V^\pi(s')] — the value of state ss under policy π\pi equals expected immediate reward plus discounted next-state value.

What is the Bellman optimality equation for V(s)V^*(s)?
V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s', r} p(s', r | s, a) [r + \gamma V^*(s')] — the optimal value is obtained by choosing the action that maximizes expected reward plus discounted optimal next-state value.
What is the difference between Bellman expectation and Bellman optimality equations?
Expectation equations use aπ(as)\sum_a \pi(a|s) (average over policy π\pi's action distribution) for a fixed policy; optimality equations use maxa\max_a (choose best action) for the optimal policy.
Why do we need the discount factor γ\gamma in Bellman equations?
To ensure infinite-horizon returns converge (sum doesn't blow up), and to express preference for near-term rewards over distant ones. Without it, t=0rt\sum_{t=0}^\infty r_t would diverge.

What is the recursive structure of the Bellman equation? :: V(s)=immediate reward+γ×value of next stateV(s) = \text{immediate reward} + \gamma \times \text{value of next state} — breaks infinite-horizon value into one-step reward + discounted future, enabling iterative solution.

What is the action-value function Qπ(s,a)Q^\pi(s, a)?
Expected return starting from state ss, taking action aa, then following policy π\pi: Qπ(s,a)=Eπ[t=0γtRt+1S0=s,A0=a]Q^\pi(s, a) = \mathbb{E}_\pi[\sum_{t=0}^\infty \gamma^t R_{t+1} | S_0=s, A_0=a].
How is Vπ(s)V^\pi(s) related to Qπ(s,a)Q^\pi(s, a)?
Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s, a) — the state value is the expected action value under the policy's action distribution.
What is bootstrapping in the context of Bellman equations?
Using current value estimates to update value estimates (e.g., using V(s)V(s') to compute V(s)V(s)) rather than waiting for full Monte Carlo returns.

Concept Map

defines

weights future in

decomposed via

yields

averages actions in

weights transitions in

relates

relates

enables

foundation of

Expected return sum of discounted rewards

Discount factor gamma

State value function V pi s

Action value function Q pi s a

Bellman equations recursive

Immediate reward plus discounted future value

Policy pi a given s

Dynamics p s prime r given s a

Dynamic programming bootstrapping

RL algorithms

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo yaar, ise simple tarike se samajhte hain. RL me hamare paas ek agent hota hai jo alag-alag states me hota hai aur usko decide karna hota hai ki kaunsa action best hai. Ab problem ye hai ki "kitna achha hai ye state?" ka answer nikalna mushkil hai, kyunki future me infinite rewards aate rehte hain. Bellman equation ka core intuition yahi hai — poore future ke total value ko do hisso me tod do: ek immediate reward (jo abhi milta hai) aur baaki ka discounted future value (jo aage aane wale states se milega). Matlab "iss state ki value = abhi ka reward + thoda kam kiya hua next state ka value". Ye recursive relationship hi saara magic hai.

Ab ye matter kyun karta hai? Socho, agar tum poore infinite future ka sum ek baar me calculate karne jao to wo intractable hai — kabhi solve nahi hoga. Lekin Bellman equation isko chhote-chhote solvable pieces me tod deta hai. Yahi cheez ko hum bootstrapping bolte hain — future values ke estimates use karke current state ki value ko improve karte jaate hain. Yehi dynamic programming ka dil hai, aur isi wajah se almost saare RL algorithms (jaise Q-learning, value iteration) isi foundation pe khade hain. Discount factor gamma isliye hai kyunki door ke rewards abhi ke rewards se kam important hote hain, aur expectation isliye kyunki environment aur policy dono me randomness hoti hai.

Do versions yaad rakhna: V(s) batata hai ki ek state kitni achhi hai policy follow karte hue, aur Q(s,a) batata hai ki ek particular action kitna achha hai us state me. Dono ek dusre se related hain — V basically saare possible actions ka policy-weighted average hai Q values ka. Jab tum ye samajh jaoge ki value ko immediate + discounted-future me todna hi sab kuch hai, to aage ke RL topics bahut aasani se click karenge. Ye equation seekhna matlab RL ki nींव pakadna, isliye ise theek se dimaag me bitha lo.

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections