Leo's log

强化学习

贝尔曼方程:形式化、求解与扩展

The Bellman Equation: Formulation, Solution Methods, and Extensions

贝尔曼方程的核心概念

方程本身

贝尔曼方程做的事,是把“处在某个状态(state)值多少”拆成两半:

即时回报(Reward):现在待在这个状态、或者做某个动作,马上能拿到(或失去)多少。

未来回报(折扣后的未来价值):到了新状态之后,往后预期还能拿到多少。这部分要乘一个折扣因子,代表你有多在乎未来的奖励。

一句话概括:

一个状态的价值,等于眼下的即时奖励,加上之后所有收益打过折的总和。

写成公式,设 V(s)V(s) 是状态 ss 的价值:

V(s)=R(s)+γsP(ss)V(s)V(s) = R(s) + \gamma \sum_{s'} P(s'|s) V(s')

其中:

  • R(s)R(s):处在状态 ss 的即时奖励;
  • γ\gamma:折扣因子(discount factor),0γ<10 \le \gamma < 1。奖励离现在越远,折扣打得越狠;
  • P(ss)P(s'|s):从状态 ss 转移到 ss' 的概率。

这个递归把所有状态的价值串在了一起。只要模型信息齐全,既能迭代求解,也能直接解出闭式解。

即时回报和未来回报怎么权衡

权衡说到底就一件事:眼前的奖励和以后的奖励,各算多重。γ\gamma 取得大,未来回报权重高,策略更愿意做长期打算;取得小,就更看重眼前,容易短视。

状态 ss 的奖励

状态 ss 的奖励记作 R(s)R(s),指进入或停留在这个状态时马上结算的收益或损失,由环境定义,因问题而异。

奖励通常是个标量。也可以是多维的——比如用向量(或更高阶的张量)同时表示几个目标——但这不改变奖励的本质:给智能体一个明确的信号,把它的行为往目标上引。

奖励定义明确时

奖励定义得清楚,智能体就有一个直接的优化目标,策略好设计,效果也好评估。

奖励难以定义时
  • 奖励建模 / 模仿学习(reward modeling / imitation learning):从示范数据或人类反馈里学一个近似的奖励函数——RLHF 就是用人类的偏好比较拟合出一个 reward model;也可以干脆不要奖励,直接照着专家的动作学(行为克隆,behavioral cloning)。
  • 逆强化学习(Inverse RL):换个方向问——专家这么做,到底在优化什么?从可观测的行为里把奖励函数反推出来,再拿它去训练策略。

这个权衡怎么做?

最直接的办法当然是选 γ\gammaγ\gamma 靠近 1,看重长期收益;靠近 0,看重眼前。选多少取决于问题本身:未来很不确定、或者根本不重要,就取小一点;需要长线规划的问题,就取大一点。

麻烦在于,很难一上来就定死一个 γ\gamma 当超参数。有几条路:

元学习(Meta-learning):不手工挑,把 γ\gamma 当成训练中待优化的参数,用**贝叶斯优化(Bayesian optimization)**或进化策略在取值空间里搜,找到在验证任务上表现最好的那个。

自适应折扣(Adaptive discounting):让折扣因子随状态或上下文变。需要长线规划的状态,γ\gamma 大一点;眼前结果要紧的状态,γ\gamma 小一点。这个做法不算主流,但更灵活,实现上可以学一个函数 Γ(s)\Gamma(s),按当前状态给出合适的折扣。

多层折扣(Multi-stage discount):分层强化学习里,高层策略用长视野(大 γ\gamma),底层控制器用短视野(小 γ\gamma)。层一叠起来,就不用全局钉死一个折扣因子了。

几个生活里的类比

金融投资

手里有笔钱可以投。“状态”是你现在有多少钱,“即时奖励”是今天到账的利息或分红,“未来价值”是这笔投资往后预期的增值。贝尔曼方程算的就是继续持有的总价值:现在的收益,加上未来收益打折之后的部分。

职业选择

比如在纠结读研还是直接工作。“状态”是你现在的处境(刚毕业,还没收入)。即时奖励可能是一笔不多的补助,或者马上工作能拿的薪水。未来价值是学位带来的终身收入提升——它在未来,所以要打折扣。贝尔曼方程把短期成本和长期回报放在一起算,给每个选项一个总的“价值”。

公路旅行

长途开车,下一段是走风景线还是抄近路?即时“奖励”可能是一路风景,或者省下的油钱。未来价值要看接下来会到哪些地方:也许某条路往后住宿便宜、补给也方便。贝尔曼方程把这一步的即时收益和后面路段的期望收益合起来,告诉你哪条路从长远看更划算。

求解贝尔曼方程

状态价值函数(state-value function):

V(s)=R(s)+γsP(ss)V(s)V(s) = R(s) + \gamma \sum_{s'} P(s'|s)V(s')

动作价值函数(action-value function):

Q(s,a)=R(s,a)+γsP(ss,a)V(s)Q(s, a) = R(s, a) + \gamma \sum_{s'} P(s'|s, a)V(s')

两者是什么关系,要看说的是哪种价值函数:

  • 固定策略 π\pi 下:Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s)\, Q^\pi(s,a)——状态价值就是动作价值按策略概率的加权平均。
  • 只有在最优价值函数处:V(s)=maxaQ(s,a)V^*(s) = \max_a Q^*(s,a)。这个 max\max 正是贝尔曼最优性方程区别于期望方程的地方,对任意策略并不成立。

所谓求解贝尔曼方程,就是把 V(s)V(s)Q(s,a)Q(s,a) 迭代到收敛——评估固定策略时收敛到 VπV^\pi,求最优控制时收敛到 VV^*

求解方法

大体分四类,中间两类实践中经常搭配着用:

  • 线性代数直接解:模型已知、状态空间小的时候,能拿到精确解。
  • 动态规划(dynamic programming):价值迭代和策略迭代,前提同样是模型已知。
  • 无模型采样(model-free):蒙特卡洛和时序差分学习(TD learning,包括 SARSA、Q-learning),不需要模型,直接从采样经验里估价值。状态或动作空间一大,就得配上函数近似(线性模型或神经网络)来表示 VVQQ,DQN 是最典型的例子。
  • 基于模型(model-based):学一个(或直接给定)环境动态模型,用它模拟转移、做规划。

线性方程法

把贝尔曼方程写成向量-矩阵形式。设 V\mathbf{V} 是所有状态的价值向量R\mathbf{R}奖励向量P\mathbf{P}状态转移概率矩阵

V=R+γPV\mathbf{V} = \mathbf{R} + \gamma \mathbf{P} \mathbf{V}

其中 γ\gamma 仍是折扣因子。移项:

(IγP)V=R(\mathbf{I} - \gamma \mathbf{P})\mathbf{V} = \mathbf{R}

I\mathbf{I} 是单位矩阵。只要 (IγP)(\mathbf{I} - \gamma \mathbf{P}) 可逆,V\mathbf{V} 就能直接解出来。

于是:

V=(IγP)1R\mathbf{V} = (\mathbf{I} - \gamma \mathbf{P})^{-1}\mathbf{R}

只要 P\mathbf{P}R\mathbf{R} 已知,这就是精确解。代价是矩阵求逆要 O(S3)O(|\mathcal{S}|^3),状态空间稍大就撑不住——这也是后面这些迭代方法存在的理由。

动态规划方法

用 DP 解贝尔曼方程,目标有两个:

  • 策略评估(policy evaluation):给定策略 π\pi,算出它的价值函数 Vπ(s)V^\pi(s)。(评估
  • 策略迭代 / 价值迭代(policy iteration / value iteration):算出最优价值函数 V(s)V^*(s) 和最优策略 π\pi^*。(改进

区别在于:策略评估算的是一个固定策略值多少,策略迭代和价值迭代则不断改进策略,把最优的那个找出来

策略评估

算法 [1] · 策略评估

  • 对所有状态初始化 V(s)0V(s) \gets 0(或任意值)
  • 重复:
    • Δ0\Delta \gets 0
    • 对每个 sSs \in \mathcal{S}
      • vV(s)v \gets V(s)
      • V(s)aπ(as)sP(ss,a)[R(s,a,s)+γV(s)]V(s) \gets \sum_a \pi(a|s) \sum_{s'} P(s'|s, a)\,[R(s, a, s') + \gamma V(s')]
      • Δmax(Δ, vV(s))\Delta \gets \max(\Delta,\ |v - V(s)|)
  • 直到 Δ<ϵ\Delta < \epsilon

注意 Δ\Delta 比的是同一个状态更新前后的差,取一轮扫描里的最大值——不是 V(s)V(s)V(s)V(s') 的差。所有状态的最大变化量低于阈值 ϵ\epsilon,就认为价值函数稳定了。

策略迭代(带策略改进)

先用算法 [1]VπV^\pi 算出来,再对策略做贪心更新。

算法 [2] · 策略迭代

  • 任意初始化 π\pi
  • 重复:
    • 运行算法 [1] 至收敛,得到 VπV^\pi
    • 对每个状态 ss
      • π(s)argmaxasP(ss,a)[R(s,a)+γVπ(s)]\pi(s) \gets \arg\max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V^\pi(s')]
  • 直到策略不再变化
价值迭代

直接迭代贝尔曼最优性方程(Bellman optimality equation):

V(s)=maxasP(ss,a)[R(s,a)+γV(s)]V(s) = \max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]

算法 [3] · 价值迭代

  • 任意初始化 V(s)V(s),比如全 0
  • 重复:
    • Δ0\Delta \gets 0
    • 对每个 sSs \in \mathcal{S}
      • vV(s)v \gets V(s)
      • V(s)maxasP(ss,a)[R(s,a)+γV(s)]V(s) \gets \max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]
      • Δmax(Δ, vV(s))\Delta \gets \max(\Delta,\ |v - V(s)|)
  • 直到 Δ<ϵ\Delta < \epsilon
  • 最后提取最优策略:
    • π(s)=argmaxasP(ss,a)[R(s,a)+γV(s)]\pi^*(s) = \arg\max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]

近似方法

状态或动作空间大到没法精确求解时,就靠函数近似器——线性模型或神经网络——去估计价值函数或策略。

基于模型的方法

基于模型的方法靠环境模型来模拟转移和奖励,不用跟真实环境反复交互就能做规划和决策。比如 Dyna 那类把真实经验和模拟经验混着更新的做法,或者 AlphaZero/MuZero 式的树搜索:搜索树里的价值回传,本身就是在模拟转移上做贝尔曼备份(Bellman backup)。

衍生应用与扩展

下面这些,说到底都是同一个递归的不同用法。把贝尔曼备份当成基本操作来看,价值类强化学习的大半内容,连带不少经典算法,都是从它长出来的。

贝尔曼算子:迭代为什么收敛

定义贝尔曼算子 T\mathcal{T}(TV)(s)=R(s)+γsP(ss)V(s)(\mathcal{T}V)(s) = R(s) + \gamma \sum_{s'} P(s'|s)V(s')γ<1\gamma < 1 时,T\mathcal{T} 在上确界范数下是一个 γ\gamma-压缩映射(contraction mapping):

TV1TV2γV1V2\|\mathcal{T}V_1 - \mathcal{T}V_2\|_\infty \le \gamma \|V_1 - V_2\|_\infty

由巴拿赫不动点定理(Banach fixed-point theorem),它有唯一不动点,且反复作用会以几何速率收敛过去——策略评估和价值迭代的收敛保证就是从这来的。最优性算子 (TV)(s)=maxasP(ss,a)[R(s,a)+γV(s)](\mathcal{T}^*V)(s) = \max_a \sum_{s'} P(s'|s,a)[R(s,a) + \gamma V(s')] 同样是 γ\gamma-压缩映射,唯一不动点就是 VV^*

TD 学习、SARSA 和 Q-learning

模型 PP 未知时,把备份里的期望换成一条采出来的转移 (s,a,r,s)(s, a, r, s')TD 误差

δ=r+γV(s)V(s)\delta = r + \gamma V(s') - V(s)

就是采样版的贝尔曼残差。TD(0) 拿它把 V(s)V(s) 往自举目标(bootstrap target)挪一小步:V(s)V(s)+αδV(s) \gets V(s) + \alpha\,\delta

  • SARSA:对 QQ 做采样版的期望备份,同策略(on-policy):Q(s,a)Q(s,a)+α[r+γQ(s,a)Q(s,a)]Q(s,a) \gets Q(s,a) + \alpha\,[r + \gamma Q(s', a') - Q(s,a)],其中 aa' 是下一步实际执行的动作。
  • Q-learning:做采样版的最优性备份,异策略(off-policy):Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s,a) \gets Q(s,a) + \alpha\,[r + \gamma \max_{a'} Q(s', a') - Q(s,a)]

深度 Q 网络(DQN)

DQN 用神经网络表示 Qθ(s,a)Q_\theta(s,a),损失是对一个慢速更新的目标网络(target network)θ\theta^- 的贝尔曼残差平方:

L(θ)=E[(r+γmaxaQθ(s,a)Qθ(s,a))2]L(\theta) = \mathbb{E}\left[\left(r + \gamma \max_{a'} Q_{\theta^-}(s', a') - Q_\theta(s, a)\right)^2\right]

经验回放(experience replay)负责打散样本相关性,目标网络负责稳住不断移动的自举目标。这个损失,就是把贝尔曼最优性方程原样改写成了一个回归目标。

Actor–Critic 和优势估计

Actor–critic 里,critic 用贝尔曼备份学 VϕV_\phi;TD 误差顺便就是优势函数 A(s,a)=Q(s,a)V(s)A(s,a) = Q(s,a) - V(s) 的单样本估计,拿去驱动策略梯度。GAE(generalized advantage estimation)再进一步,用衰减参数 λ\lambda 把多步贝尔曼目标混合起来。这套机制直接搬进了大模型后训练:基于 PPO 的 RLHF 把逐 token 生成当作一个 MDP,训出来的 value head 服从的就是 token 序列上的同一条递归。

改备份本身

  • 软贝尔曼方程(最大熵强化学习):把硬 max\max 换成 log-sum-exp,V(s)=αlogaexp ⁣(Q(s,a)/α)V(s) = \alpha \log \sum_a \exp\!\left(Q(s,a)/\alpha\right),最优策略因此变成随机的,SAC 建立在这上面。
  • 值分布贝尔曼方程(distributional Bellman equation):不只建模回报的均值,而是建模整个回报分布,Z(s,a)=Dr+γZ(s,a)Z(s,a) \overset{D}{=} r + \gamma Z(s', a');C51 和 QR-DQN 都在这条线上。

连续时间:HJB 方程

对贝尔曼方程取连续时间极限,得到哈密顿–雅可比–贝尔曼(Hamilton–Jacobi–Bellman)偏微分方程,也就是最优控制理论的根基(LQR、机器人的轨迹优化)。离散时间的强化学习和连续时间的最优控制,本质是同一套思想,只是时间刻度不同。

强化学习之外:经典动态规划

  • Bellman–Ford 最短路就是价值迭代在确定性最短路问题上的特例:奖励换成代价,max\max 换成 min\minγ=1\gamma = 1
  • 库存控制和运筹学——贝尔曼方程 1950 年代就诞生在这个领域——补货策略就是在库存水平上做贝尔曼递归。
  • 期权定价:美式期权的二叉树估值,就是用风险中性概率(risk-neutral probability)做贝尔曼备份,“要不要提前行权”对应的正是那个 max\max

Reinforcement Learning

The Bellman Equation: Formulation, Solution Methods, and Extensions

贝尔曼方程:形式化、求解与扩展

Main Concept of the Bellman Equation

The Equation

The Bellman equation provides a way to break down the value of being in a certain situation (a “state”) into two parts:

Immediate Return (Reward): What you gain (or lose) right now by being in that state or taking a certain action.

Future Return (Discounted Future Value): The expected value of what you’ll gain in the future, starting from the new state you end up in, discounted by a factor that reflects how much you care about future rewards compared to immediate ones.

In essence, the Bellman equation says:

The value of a state is the immediate reward you get now plus the discounted value of what you’ll get afterward.

Formally, if V(s)V(s) represents the value of state ss:

V(s)=R(s)+γsP(ss)V(s)V(s) = R(s) + \gamma \sum_{s'} P(s'|s) V(s')

where

  • R(s)R(s) = immediate reward from being in state ss;
  • γ\gamma = discount factor, 0γ<10 \le \gamma < 1, which reduces the weight of future rewards as they move further into the future;
  • P(ss)P(s'|s) = probability of moving from state ss to state ss'.

This recursive relationship ties together the values of all states, allowing us to solve for them either iteratively or through direct methods, given the right information.

The Trade-off between Immediate and Future Return

The trade-off lies in deciding how much importance to place on immediate rewards versus future rewards. A higher discount factor (γ\gamma) places more weight on future returns, encouraging long-term planning, while a lower discount factor prioritizes immediate rewards, potentially leading to short-sighted decisions.

Reward of State ss

The reward in state ss, denoted R(s)R(s), represents the immediate gain or loss received upon entering or being in that state. It is typically defined by the environment and varies with the specific problem being solved.

Normally, the reward is a scalar signal. It can also be multidimensional — a vector or higher-order tensor representing several objectives or criteria simultaneously — but that doesn’t change the essence of a reward signal: return a clear signal to the agent and guide its behavior toward specific goals.

Clearly Defined Reward

When the reward is clearly defined, it provides a straightforward objective for the agent to optimize, making it easier to design policies and evaluate performance.

When the Reward Is Hard to Define
  • Reward modeling / imitation learning: learn an approximate reward function from demonstrations or human feedback — e.g., RLHF fits a reward model to human preference comparisons — or bypass the reward entirely by mimicking expert actions directly (behavioral cloning).
  • Inverse RL: infer the reward function an expert appears to be optimizing from their observed behavior, then train a policy against the recovered reward.

How to Make the Trade-off?

Clearly, the trade-off can be managed by carefully selecting the discount factor (γ\gamma). A higher γ\gamma (closer to 1) emphasizes long-term rewards, promoting strategies that prioritize future benefits, while a lower γ\gamma (closer to 0) focuses on immediate rewards, favoring short-term gains. The choice of γ\gamma depends on the specific problem and goals; for instance, in scenarios where future outcomes are uncertain or less relevant, a lower γ\gamma may be appropriate. Conversely, in problems requiring long-term planning, a higher γ\gamma is often preferred.

But it is hard to select a static γ\gamma as a hyper-parameter up front. There are several methods:

Meta-learning: Instead of hand-picking γ\gamma, you can treat it as a parameter to be optimized during training. Techniques like Bayesian optimization or evolutionary strategies can search over the space of γ\gamma values and find one that yields the best performance on a validation task.

Adaptive discounting: Some research explores a state-dependent or context-dependent discount factor. For instance, γ\gamma might be higher for states where long-term planning is more critical and lower for states where immediate outcomes are paramount. Though less common, this approach is more flexible and can be implemented by learning a function Γ(s)\Gamma(s) that predicts the appropriate discount based on the current state.

Multi-stage discount: In hierarchical RL, higher-level policies might operate with a longer horizon (larger γ\gamma), while lower-level controllers operate with a shorter horizon (smaller γ\gamma). By layering these, you avoid committing to a single global discount factor.

Some Real-Life Analogies of the Bellman Equation

Financial Investments

Imagine you have some money to invest. The “state” is having a certain amount of money right now. The “immediate reward” might be the interest or dividend you receive today. The “future value” is the expected growth of your investment over time. The Bellman equation tells you the total value of holding onto that investment — your current returns plus the discounted value of future returns.

Career Decisions

Say you’re choosing whether to go to graduate school or jump straight into a job. The “state” might represent your current position (just graduated, no income yet). The immediate reward could be a small stipend or the salary you’d earn if you start working. The future value is the benefit you’d gain over your lifetime from higher earning potential if you get the degree, discounted because those gains are in the future. The Bellman equation would factor in both the short-term costs and the long-term payoffs to give an overall “value” to each choice.

Planning a Road Trip

If you’re on a long journey and considering your next move — perhaps taking a scenic route or a shortcut — the immediate “reward” might be a beautiful view or saving on gas. The future value would consider which states you might reach next: maybe a route that leads to cheaper accommodations later or better opportunities to restock supplies. The Bellman equation combines the immediate payoff of your current move with the expected benefits in the upcoming stretches of the trip to help you decide which route is “valuable” in the long run.

Solving the Bellman Equation

The state-value function:

V(s)=R(s)+γsP(ss)V(s)V(s) = R(s) + \gamma \sum_{s'} P(s'|s)V(s')

The action-value function:

Q(s,a)=R(s,a)+γsP(ss,a)V(s)Q(s, a) = R(s, a) + \gamma \sum_{s'} P(s'|s, a)V(s')

The relationship between the two depends on which value function is meant:

  • For a fixed policy π\pi: Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s)\, Q^\pi(s,a) — the state value is the policy-weighted average of the action values.
  • For the optimal value functions only: V(s)=maxaQ(s,a)V^*(s) = \max_a Q^*(s,a) — the max\max relation is what defines the Bellman optimality equation; it does not hold for an arbitrary policy.

Solving the Bellman equation means iterating V(s)V(s) or Q(s,a)Q(s,a) until convergence — to VπV^\pi when evaluating a fixed policy, or to VV^* when solving for optimal control.

Solution Methods

Broadly, there are four families (the middle two are often combined in practice):

  • Direct solution via linear algebra — exact, when the model is known and the state space is small.
  • Dynamic programming — value iteration and policy iteration, when the model is known.
  • Model-free sampling methods — Monte Carlo and temporal-difference (TD) learning (SARSA, Q-learning), which estimate values from sampled experience without a model. For large state or action spaces they are combined with function approximation (linear models or neural networks) to represent VV or QQ; DQN is the canonical example.
  • Model-based methods — learn or assume a model of the environment’s dynamics and use it to simulate transitions and plan.

Linear Equation Method

Write the Bellman equation in vector-matrix form. Let V\mathbf{V} represent the vector of value functions for all states, R\mathbf{R} the reward vector, and P\mathbf{P} the state transition probability matrix. Then:

V=R+γPV\mathbf{V} = \mathbf{R} + \gamma \mathbf{P} \mathbf{V}

where γ\gamma is still the discount factor. Simple rearranging gives:

(IγP)V=R(\mathbf{I} - \gamma \mathbf{P})\mathbf{V} = \mathbf{R}

where I\mathbf{I} is the identity matrix. If (IγP)(\mathbf{I} - \gamma \mathbf{P}) is invertible, we can solve for V\mathbf{V} directly.

The linear method leads to solving

V=(IγP)1R\mathbf{V} = (\mathbf{I} - \gamma \mathbf{P})^{-1}\mathbf{R}

which provides an exact solution for V\mathbf{V} when P\mathbf{P} and R\mathbf{R} are known. The matrix inversion costs O(S3)O(|\mathcal{S}|^3), so this is practical only for small state spaces — which is exactly why the iterative methods below exist.

Dynamic Programming Methods

There are two primary goals when solving the Bellman equation via DP:

  • Policy Evaluation: Compute the value function Vπ(s)V^\pi(s) for a given policy π\pi. (Evaluation)
  • Policy Iteration and Value Iteration: Compute the optimal value function V(s)V^*(s) and optimal policy π\pi^*. (Improvement)

The difference is that policy evaluation computes the value of a fixed policy, while policy iteration and value iteration improve the policy to find the optimal one.

Policy Evaluation

Algorithm [1] · Policy evaluation

  • Initialize V(s)0V(s) \gets 0 (or arbitrarily) for all states ss
  • Repeat:
    • Δ0\Delta \gets 0
    • For each sSs \in \mathcal{S}:
      • vV(s)v \gets V(s)
      • V(s)aπ(as)sP(ss,a)[R(s,a,s)+γV(s)]V(s) \gets \sum_a \pi(a|s) \sum_{s'} P(s'|s, a)\,[R(s, a, s') + \gamma V(s')]
      • Δmax(Δ, vV(s))\Delta \gets \max(\Delta,\ |v - V(s)|)
  • Until Δ<ϵ\Delta < \epsilon

Note that Δ\Delta compares the new and old value of the same state — it is the maximum change across a full sweep, not a difference between V(s)V(s) and V(s)V(s'). When the maximum change across all states falls below the threshold ϵ\epsilon, the value function is considered stable.

Policy Iteration (with Policy Improvement)

Use Algorithm [1] to calculate VπV^\pi, then update the policy greedily.

Algorithm [2] · Policy iteration

  • Initialize π\pi arbitrarily for all states
  • Repeat:
    • Run Algorithm [1] until convergence, obtaining VπV^\pi
    • For each state ss:
      • π(s)argmaxasP(ss,a)[R(s,a)+γVπ(s)]\pi(s) \gets \arg\max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V^\pi(s')]
  • Until the policy no longer changes
Value Iteration

Directly compute the optimal value function by iterating the Bellman optimality equation:

V(s)=maxasP(ss,a)[R(s,a)+γV(s)]V(s) = \max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]

Algorithm [3] · Value iteration

  • Initialize V(s)V(s) arbitrarily for all states ss, e.g. V(s)=0V(s) = 0
  • Repeat:
    • Δ0\Delta \gets 0
    • For each sSs \in \mathcal{S}:
      • vV(s)v \gets V(s)
      • V(s)maxasP(ss,a)[R(s,a)+γV(s)]V(s) \gets \max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]
      • Δmax(Δ, vV(s))\Delta \gets \max(\Delta,\ |v - V(s)|)
  • Until Δ<ϵ\Delta < \epsilon
  • Extract the optimal policy:
    • π(s)=argmaxasP(ss,a)[R(s,a)+γV(s)]\pi^*(s) = \arg\max_a \sum_{s'} P(s'|s, a)\,[R(s, a) + \gamma V(s')]

Approximation Methods

Approximation methods are used when the state or action space is too large to compute exact solutions. They rely on function approximators — linear models or neural networks — to estimate value functions or policies efficiently.

Model-based Methods

Model-based methods use a model of the environment to simulate transitions and rewards, enabling planning and decision-making without direct interaction with the environment — e.g., Dyna-style updates that mix real and simulated experience, or tree-search planning as in AlphaZero/MuZero, where the value backup inside the search tree is itself a Bellman backup over simulated transitions.

Derived Applications and Extensions

Everything below is, at its core, a different way of using the same recursion. Once you see the Bellman backup as the primitive operation, most of value-based RL — and a surprising amount of classical algorithms — falls out of it.

The Bellman Operator: Why Iteration Converges

Define the Bellman operator T\mathcal{T} by (TV)(s)=R(s)+γsP(ss)V(s)(\mathcal{T}V)(s) = R(s) + \gamma \sum_{s'} P(s'|s)V(s'). For γ<1\gamma < 1, T\mathcal{T} is a γ\gamma-contraction in the sup-norm:

TV1TV2γV1V2\|\mathcal{T}V_1 - \mathcal{T}V_2\|_\infty \le \gamma \|V_1 - V_2\|_\infty

By the Banach fixed-point theorem, T\mathcal{T} has a unique fixed point, and repeated application converges to it geometrically — this is the convergence guarantee behind policy evaluation and value iteration. The optimality operator (TV)(s)=maxasP(ss,a)[R(s,a)+γV(s)](\mathcal{T}^*V)(s) = \max_a \sum_{s'} P(s'|s,a)[R(s,a) + \gamma V(s')] is also a γ\gamma-contraction, with VV^* as its unique fixed point.

TD Learning, SARSA, and Q-Learning

When the model PP is unknown, replace the expectation in the Bellman backup with a single sampled transition (s,a,r,s)(s, a, r, s'). The TD error

δ=r+γV(s)V(s)\delta = r + \gamma V(s') - V(s)

is a sampled Bellman residual, and TD(0) nudges V(s)V(s) toward the bootstrap target: V(s)V(s)+αδV(s) \gets V(s) + \alpha\,\delta.

  • SARSA applies the sampled Bellman expectation backup to QQ (on-policy): Q(s,a)Q(s,a)+α[r+γQ(s,a)Q(s,a)]Q(s,a) \gets Q(s,a) + \alpha\,[r + \gamma Q(s', a') - Q(s,a)], where aa' is the action actually taken next.
  • Q-learning applies the sampled Bellman optimality backup (off-policy): Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s,a) \gets Q(s,a) + \alpha\,[r + \gamma \max_{a'} Q(s', a') - Q(s,a)].

Deep Q-Networks (DQN)

DQN represents Qθ(s,a)Q_\theta(s,a) with a neural network and trains it by minimizing the squared Bellman residual against a slowly updated target network θ\theta^-:

L(θ)=E[(r+γmaxaQθ(s,a)Qθ(s,a))2]L(\theta) = \mathbb{E}\left[\left(r + \gamma \max_{a'} Q_{\theta^-}(s', a') - Q_\theta(s, a)\right)^2\right]

Experience replay decorrelates samples; the target network stabilizes the moving bootstrap target. The loss is literally the Bellman optimality equation turned into a regression objective.

Actor–Critic and Advantage Estimation

In actor–critic methods, the critic learns VϕV_\phi via Bellman backups, and the TD error doubles as a one-sample estimate of the advantage A(s,a)=Q(s,a)V(s)A(s,a) = Q(s,a) - V(s), which drives the policy gradient. GAE (generalized advantage estimation) blends multi-step Bellman targets with a decay parameter λ\lambda. This machinery carries directly into LLM post-training: PPO-based RLHF treats token generation as an MDP, and the value head trained there obeys the same recursion over token sequences.

Extending the Backup Itself

  • Soft Bellman equation (maximum-entropy RL): replace the hard max\max with a log-sum-exp, V(s)=αlogaexp ⁣(Q(s,a)/α)V(s) = \alpha \log \sum_a \exp\!\left(Q(s,a)/\alpha\right), which yields stochastic optimal policies and underlies SAC.
  • Distributional Bellman equation: model the full return distribution rather than its mean, Z(s,a)=Dr+γZ(s,a)Z(s,a) \overset{D}{=} r + \gamma Z(s', a'); C51 and QR-DQN are built on this.

Continuous Time: The HJB Equation

Taking the continuous-time limit of the Bellman equation gives the Hamilton–Jacobi–Bellman partial differential equation, the foundation of optimal control theory (LQR, trajectory optimization in robotics). Discrete-time RL and continuous-time control are the same idea on different clocks.

Beyond RL: Classical Dynamic Programming

  • Bellman–Ford shortest paths is value iteration specialized to a deterministic shortest-path problem: costs instead of rewards, min\min instead of max\max, and γ=1\gamma = 1.
  • Inventory control and operations research — where the equation originated in the 1950s — solve restocking policies as Bellman recursions over stock levels.
  • Option pricing: binomial-tree valuation of American options is a Bellman backup with risk-neutral probabilities, where the early-exercise decision plays the role of the max\max.