Study Notes

Reinforcement Learning

Reinforcement learning is the problem of acting to maximize long-run reward when four difficulties hit at once: you optimize a utility, consequences are delayed (credit assignment), you must explore to discover what’s good, and you must generalize across states you’ve never seen. Everything in the subject hangs off one object — the value function Vπ(s)V^\pi(s), the expected discounted return from ss under policy π\pi — and one recursion, the Bellman equation, which says today’s value is the immediate reward plus the discounted value of where you land. From there the field forks along two axes. Known vs unknown model: with the model you plan (dynamic programming — iterate a Bellman operator to its fixed point); without it you learn from samples (Monte Carlo vs temporal difference). Value-based vs policy-based: value methods estimate V/QV/Q then act greedily; policy methods ascend the policy’s value directly. Convergence is earned, not assumed, and a small set of proof strategies recur — contraction + fixed point, monotone improvement, telescoping advantages, Jensen, Robbins–Monro, the score-function trick, mean-zero baselines, concentration + union bound, importance reweighting. Naming the strategy at each proof is the point: it turns a wall of derivations into a handful of reusable moves. Prerequisites cashed in: expectation, variance, conditional expectation, Markov chains (track 1); MLE and bias/variance (tracks 3, 6); contraction/fixed-point and norms (track 2); SGD, function approximation, the score function (tracks 11a, 11b); concentration and Jensen (track 1); conjugate priors (track 7); importance sampling intuition (track 9).

1. The problem: MDPs, returns, and value functions

  • The agent–environment loop. At each step tt the agent takes action ata_t, the world transitions and emits observation oto_t and reward rtr_t. The agent picks actions from a policy π\pi to maximize expected cumulative reward. We seek states of high value (long-run reward), not high immediate reward — that’s the whole delayed-consequence problem.
  • The Markov assumption. p(st+1st,at)=p(st+1ht,at)p(s_{t+1}\mid s_t, a_t) = p(s_{t+1}\mid h_t, a_t) — the state is a sufficient statistic of history; given the present, the future is independent of the past. (You can always make the state the full history, but that’s unwieldy; we assume st=ots_t = o_t, full observability.)
  • The MDP is the tuple (S,A,P,R,γ)(S, A, P, R, \gamma): states, actions, transition dynamics P(ss,a)P(s'\mid s,a), reward R(s,a)=E[rtst=s,at=a]R(s,a) = \mathbb E[r_t\mid s_t{=}s, a_t{=}a], discount γ[0,1]\gamma\in[0,1]. A policy π(as)\pi(a\mid s) is a distribution over actions. (MDP + fixed policy collapses to a Markov reward process with averaged dynamics Pπ(ss)=aπ(as)P(ss,a)P^\pi(s'\mid s) = \sum_a \pi(a\mid s)P(s'\mid s,a) — useful: prediction problems reduce to MRPs.)
  • Return and value. The return is the discounted reward sum Gt=rt+γrt+1+γ2rt+2+G_t = r_t + \gamma r_{t+1} + \gamma^2 r_{t+2} + \cdots (γ\gamma toward 1 weights the future more; γ<1\gamma<1 keeps infinite-horizon returns finite). The state-value and action-value functions are

Vπ(s)=Eπ[Gtst=s],Qπ(s,a)=Eπ[Gtst=s,at=a].V^\pi(s) = \mathbb E_\pi[G_t\mid s_t{=}s], \qquad Q^\pi(s,a) = \mathbb E_\pi[G_t\mid s_t{=}s, a_t{=}a].

VV measures how good a state is; QQ how good an action is from a state. They link by Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a\mid s)Q^\pi(s,a) and Qπ(s,a)=R(s,a)+γsP(ss,a)Vπ(s)Q^\pi(s,a) = R(s,a) + \gamma\sum_{s'}P(s'\mid s,a)V^\pi(s').

  • Two tasks: prediction (evaluate a given π\pi) vs control (find the best π\pi). Model-based (you have/estimate P,RP, R) vs model-free (you only sample).

Core competency set

  • State the Markov assumption, the MDP tuple, return, and Vπ/QπV^\pi/Q^\pi with their linking equations.
  • Distinguish prediction vs control and model-based vs model-free.

2. The Bellman equations — the recursion everything rests on

  • Where it comes from — split the return after one step, Gt=rt+γGt+1G_t = r_t + \gamma G_{t+1}, and take expectations:

Vπ(s)=Eπ[rt+γGt+1st=s].V^\pi(s) = \mathbb E_\pi[r_t + \gamma G_{t+1}\mid s_t{=}s].

The expected next-step return is itself a value, Eπ[Gt+1st+1]=Vπ(st+1)\mathbb E_\pi[G_{t+1}\mid s_{t+1}] = V^\pi(s_{t+1}):

Vπ(s)=Eπ[rt+γVπ(st+1)st=s].V^\pi(s) = \mathbb E_\pi[r_t + \gamma V^\pi(s_{t+1})\mid s_t{=}s].

Reading the expectation out over π\pi and PP gives the Bellman expectation equation

Vπ(s)=Rπ(s)+γsPπ(ss)Vπ(s).V^\pi(s) = R^\pi(s) + \gamma\sum_{s'}P^\pi(s'\mid s)\,V^\pi(s').

Today’s value = immediate reward + discounted value of the next state. A one-step lookahead, in other words.

  • Matrix form (finite states): V=Rπ+γPπVV = R^\pi + \gamma P^\pi V, solved directly by V=(IγPπ)1RπV = (I - \gamma P^\pi)^{-1}R^\pi — exact but O(S3)O(|S|^3) and needs the inverse to exist (it does for γ<1\gamma<1).
  • Bellman optimality for the best policy maxes over actions instead of averaging:

V(s)=maxa[R(s,a)+γsP(ss,a)V(s)].V^*(s) = \max_a\Big[R(s,a) + \gamma\sum_{s'}P(s'\mid s,a)V^*(s')\Big].

  • The Bellman operators turn these equations into maps on value functions — the objects whose fixed points are the value functions:

(BπV)(s)=Rπ(s)+γsPπ(ss)V(s),(BV)(s)=maxa[R(s,a)+γsP(ss,a)V(s)].(B^\pi V)(s) = R^\pi(s) + \gamma\sum_{s'}P^\pi(s'\mid s)V(s'), \qquad (B^*V)(s) = \max_a\Big[R(s,a) + \gamma\sum_{s'}P(s'\mid s,a)V(s')\Big].

VπV^\pi is the fixed point of BπB^\pi; VV^* the fixed point of BB^*. The next section’s algorithms are just iterating these operators.

  • Picture (draw it): the backup diagram — state ss at the root branches down to action nodes (the agent’s choice), each action branches to next-state nodes (the environment’s chance), one step deep. The Bellman expectation equation averages values back up this tree; optimality takes a max at the action layer instead of an average. Every value-based algorithm is some version of backing values up this diagram.

Core competency set

  • Derive the Bellman expectation equation from Gt=rt+γGt+1G_t = r_t + \gamma G_{t+1}; write the optimality equation and the matrix solve.
  • Define BπB^\pi and BB^* and state that Vπ,VV^\pi, V^* are their fixed points.

3. Planning by dynamic programming (known model)

  • Policy evaluation = compute VπV^\pi by iterating Vk=BπVk1V_{k} = B^\pi V_{k-1} from any V0V_0 until convergence (a “Bellman backup,” a full sweep of states each iteration). It converges because BπB^\pi is a contraction (proved below alongside BB^*).
  • Policy iteration = alternate evaluate (VπV^\pi) and greedily improve (π(s)=argmaxaQπ(s,a)\pi'(s) = \arg\max_a Q^\pi(s,a)) until the policy stops changing. Two guarantees make it work:
    • Policy improvement theoremstrategy: telescoping the one-step improvement inequality. Let π\pi' be greedy w.r.t. QπQ^\pi, so Qπ(s,π(s))=maxaQπ(s,a)Qπ(s,π(s))=Vπ(s)Q^\pi(s,\pi'(s)) = \max_a Q^\pi(s,a) \ge Q^\pi(s,\pi(s)) = V^\pi(s). Then unroll:

Vπ(s)Qπ(s,π(s))=Eπ[rt+γVπ(st+1)st=s]V^\pi(s) \le Q^\pi(s,\pi'(s)) = \mathbb E_{\pi'}[r_t + \gamma V^\pi(s_{t+1})\mid s_t{=}s]

Eπ[rt+γQπ(st+1,π(st+1))st=s]\le \mathbb E_{\pi'}[r_t + \gamma Q^\pi(s_{t+1},\pi'(s_{t+1}))\mid s_t{=}s]

Eπ[rt+γrt+1+γ2Vπ(st+2)st=s]Vπ(s).\le \mathbb E_{\pi'}[r_t + \gamma r_{t+1} + \gamma^2 V^\pi(s_{t+2})\mid s_t{=}s] \le \cdots \le V^{\pi'}(s).

Each step replaces a $V^\pi$ by the at-least-as-large $Q^\pi(\cdot,\pi')$ and expands one more reward; in the limit, $V^{\pi'} \ge V^\pi$.
  • Terminationstrategy: monotone improvement over a finite set. Each iteration strictly improves a deterministic policy, and there are only AS|A|^{|S|} of them, so no policy repeats → it halts. At the halt, greedy gives no change, so Vπ(s)=maxaQπ(s,a)V^\pi(s) = \max_a Q^\pi(s,a) — the Bellman optimality equation — hence π\pi is optimal.
  • Value iteration = iterate the optimality operator Vk=BVk1V_{k} = B^* V_{k-1} directly (no separate evaluation phase), reading off π\pi at the end via one greedy step. Intuition: VkV_k is the optimal value with kk steps left; grow the horizon. (Unlike policy iteration, the iterates don’t increase monotonically — they converge from wherever they start.)
  • The convergence proof (the centerpiece)strategy: contraction mapping + Banach fixed-point theorem. Claim: BB^* is a γ\gamma-contraction in the sup-norm, BVBUγVU\lVert B^*V - B^*U\rVert_\infty \le \gamma\lVert V - U\rVert_\infty. For any state ss,

BV(s)BU(s)=maxaQV(s,a)maxaQU(s,a)maxaQV(s,a)QU(s,a)|B^*V(s) - B^*U(s)| = \Big|\max_a Q_V(s,a) - \max_a Q_U(s,a)\Big| \le \max_a |Q_V(s,a) - Q_U(s,a)|

(using maxafmaxagmaxafg|\max_a f - \max_a g| \le \max_a |f-g|), and each term is

maxaγsP(ss,a)(V(s)U(s))γmaxasP(ss,a)VU=γVU\max_a\,\gamma\Big|\sum_{s'}P(s'\mid s,a)\big(V(s') - U(s')\big)\Big| \le \gamma\max_a\sum_{s'}P(s'\mid s,a)\,\lVert V-U\rVert_\infty = \gamma\lVert V-U\rVert_\infty

(probabilities sum to 1). Take the max over ss: BVBUγVU\lVert B^*V - B^*U\rVert_\infty \le \gamma\lVert V-U\rVert_\infty. Since γ<1\gamma<1, Banach gives a unique fixed point VV^* and geometric convergence VkVγkV0V\lVert V_k - V^*\rVert_\infty \le \gamma^k\lVert V_0 - V^*\rVert_\infty. The identical argument for BπB^\pi proves policy evaluation converges to the unique VπV^\pi.

  • Finite horizon: just iterate value iteration exactly HH times (V0=0V_0 = 0); now the optimal policy is non-stationary — with 1 step left vs 10, the best action differs.
ProblemBellman equationAlgorithm
Predictionexpectationiterative policy evaluation
Controlexpectation + greedy improvementpolicy iteration
Controloptimalityvalue iteration

Core competency set

  • Run policy evaluation/iteration/value iteration as operator iterations; state which Bellman equation each uses.
  • Prove the policy improvement theorem (telescoping) and policy-iteration termination (monotone + finite).
  • Prove BB^* (and BπB^\pi) is a γ\gamma-contraction and invoke Banach for unique fixed point + geometric convergence.

4. Model-free prediction — learning VπV^\pi from samples

The move to learning: we no longer know P,RP, R, only sampled experience. Two estimators of VπV^\pi, trading off bias and variance.

  • Monte Carlo (MC). Replace the expectation E[Gt]\mathbb E[G_t] with an empirical mean of complete-episode returns. Maintain a count N(s)N(s) and update incrementally (the running-mean identity μk=μk1+1k(xkμk1)\mu_k = \mu_{k-1} + \tfrac1k(x_k - \mu_{k-1})):

Vπ(s)Vπ(s)+1N(s)(GtVπ(s)).V^\pi(s) \leftarrow V^\pi(s) + \tfrac{1}{N(s)}\big(G_{t} - V^\pi(s)\big).

First-visit MC (count only the first visit per episode) is unbiased; every-visit is biased (within-episode dependence) but consistent. Properties: no Markov assumption, but episodic only (you must wait for the episode to end), and high variance (a full return accumulates all the randomness of a trajectory).

  • Temporal difference (TD(0)). Don’t wait for the episode — bootstrap: replace the unknown rest-of-return with the current estimate of the next state’s value. With the TD target rt+γVπ(st+1)r_t + \gamma V^\pi(s_{t+1}) and TD error δt=rt+γVπ(st+1)Vπ(st)\delta_t = r_t + \gamma V^\pi(s_{t+1}) - V^\pi(s_t),

Vπ(st)Vπ(st)+αδt.V^\pi(s_t) \leftarrow V^\pi(s_t) + \alpha\,\delta_t.

This is the Bellman equation with the expectation over st+1s_{t+1} replaced by a single sample. Properties: online (updates every step), works in continuing settings, uses the Markov property, lower variance (only one step’s randomness) but biased (the bootstrap target uses an estimate).

  • DP with certainty equivalence: estimate the MLE model from data (count transitions, average rewards), then plan on it. Data-efficient and consistent, but expensive (re-solve the MDP each update).
  • The comparison. MC is unbiased, high-variance, assumption-light, insensitive to initialization; TD is biased, low-variance, more data-efficient, Markov-exploiting, but converges only guaranteed under tabular representations. The classic AB example: MC converges to the minimum-MSE fit on the observed returns; TD converges to the value of the maximum-likelihood Markov model — so TD effectively builds and exploits a model of the world.
  • Numeric anchor (Mars rover, γ=0.9\gamma = 0.9, observed trajectory s3 ⁣ ⁣s2 ⁣ ⁣s2 ⁣ ⁣s1 ⁣s_3\!\to\!s_2\!\to\!s_2\!\to\!s_1\!\to terminal with rewards 0,0,0,10,0,0,1). Returns per visit: G(s3)=γ3=0.729G(s_3) = \gamma^3 = 0.729, first G(s2)=γ2=0.81G(s_2) = \gamma^2 = 0.81, second G(s2)=γ=0.9G(s_2) = \gamma = 0.9, G(s1)=1G(s_1) = 1. First-visit MC counts only the first s2s_2: V(s2)=0.81V(s_2) = 0.81. Every-visit MC averages both visits: V(s2)=12(0.81+0.9)=0.855V(s_2) = \tfrac12(0.81 + 0.9) = 0.855. TD(0) on the first tuple (s3,r=0,s2)(s_3, r{=}0, s_2) with α=0.5\alpha = 0.5 and all values init 0: target 0+γV(s2)=00 + \gamma V(s_2) = 0, so V(s3)V(s_3) stays 0 — TD propagates the terminal reward back one step per sweep, where MC moved it in a single episode (variance-for-immediacy, made concrete).

Core competency set

  • Write MC (incremental mean) and TD(0) (TD error/target) updates; identify TD as “Bellman with a sampled next state.”
  • Lay out the MC-vs-TD tradeoff (bias/variance, episodic/online, Markov) and the AB / min-MSE-vs-max-likelihood distinction.

5. Model-free control — learning to act

  • Generalized policy iteration (GPI) without a model. Greedy improvement on VV needs the model (argmaxaR+γPV\arg\max_a R + \gamma\sum P V), so switch to QQ: argmaxaQ(s,a)\arg\max_a Q(s,a) needs no model. But pure greed never explores untried actions — hence the explore/exploit tradeoff. Picture (draw it): GPI as two interacting processes pulling on (V,π)(V,\pi)evaluation drags VV toward VπV^\pi, improvement drags π\pi toward greedy(V)(V); each step partly undoes the other, and they come to rest only at (V,π)(V^*,\pi^*), the one point consistent with both (the Bellman optimality equation). Almost every control algorithm here is GPI with a different evaluation step (MC/TD/nn-step) and a softened greedy (ε\varepsilon-greedy).
  • ε\varepsilon-greedy balances them: with prob 1ε1-\varepsilon take the greedy action, else a uniform random one.
    • Monotone ε\varepsilon-greedy improvementstrategy: the policy improvement theorem again, checking the one-step inequality survives. For the ε\varepsilon-greedy π\pi' w.r.t. QπQ^\pi,

aπ(as)Qπ(s,a)=εAaQπ(s,a)+(1ε)maxaQπ(s,a)Vπ(s),\sum_a \pi'(a\mid s)Q^\pi(s,a) = \tfrac{\varepsilon}{|A|}\sum_a Q^\pi(s,a) + (1-\varepsilon)\max_a Q^\pi(s,a) \ge V^\pi(s),

because shifting weight onto the max action (relative to $\pi$) can only raise the average. The improvement theorem then gives $V^{\pi'}\ge V^\pi$.
  • GLIE (Greedy in the Limit with Infinite Exploration): every (s,a)(s,a) visited infinitely often and the policy converges to greedy (e.g. εi=1/i\varepsilon_i = 1/i). GLIE Monte-Carlo control converges to QQ^*.
  • SARSAon-policy TD control on QQ, updating from the tuple (s,a,r,s,a)(s,a,r,s',a') where aa' is drawn from the current (ε\varepsilon-greedy) policy:

Q(st,at)Q(st,at)+α(rt+γQ(st+1,at+1)Q(st,at)).Q(s_t,a_t) \leftarrow Q(s_t,a_t) + \alpha\big(r_t + \gamma Q(s_{t+1},a_{t+1}) - Q(s_t,a_t)\big).

Convergence to QQ^* requires (1) a GLIE policy sequence and (2) Robbins–Monro step sizes tαt=, tαt2<\sum_t \alpha_t = \infty,\ \sum_t \alpha_t^2 < \inftystrategy: stochastic approximation (steps big enough to reach anywhere, shrinking enough to settle).

  • Q-learningoff-policy control: bootstrap off the best next action rather than the one taken, so it learns QQ^* while behaving with an exploratory policy:

Q(st,at)Q(st,at)+α(rt+γmaxaQ(st+1,a)Q(st,at)).Q(s_t,a_t) \leftarrow Q(s_t,a_t) + \alpha\big(r_t + \gamma\max_{a'}Q(s_{t+1},a') - Q(s_t,a_t)\big).

Needs only (s,a,r,s)(s,a,r,s'). Converges to QQ^* with all (s,a)(s,a) visited infinitely often and R–M step sizes — not requiring GLIE (that’s only needed to make the behavior policy optimal).

  • Maximization biasstrategy: Jensen’s inequality (max is convex). Even with unbiased per-action estimates Q^\hat Q, the greedy value over-estimates:

V^π^(s)=E[maxaQ^(s,a)]maxaE[Q^(s,a)]=maxaQ(s,a).\hat V^{\hat\pi}(s) = \mathbb E\big[\max_a \hat Q(s,a)\big] \ge \max_a \mathbb E[\hat Q(s,a)] = \max_a Q(s,a).

The fix — double Q-learning — splits the data into two independent estimates, using Q1Q_1 to select a=argmaxaQ1(s,a)a^* = \arg\max_a Q_1(s,a) and Q2Q_2 to evaluate Q2(s,a)Q_2(s,a^*), decoupling selection from evaluation so the estimate is unbiased.

Core competency set

  • Explain why control needs QQ (model-free greedy) and ε\varepsilon-greedy; prove monotone ε\varepsilon-greedy improvement.
  • Write SARSA (on-policy) vs Q-learning (off-policy, the max) and their convergence conditions (GLIE + R–M; R–M alone for QQ^*).
  • Derive maximization bias via Jensen and explain double-Q’s selection/evaluation split.

6. Value-function approximation and deep RL

  • Why. Tabular V/QV/Q can’t handle large or continuous state spaces. Parameterize V^(s;w)\hat V(s;w), Q^(s,a;w)\hat Q(s,a;w) (tabular is the special case where features are state indicators). Fit by SGD on the mean-squared value error MSVE(w)=sμ(s)(Vπ(s)V^(s;w))2\text{MSVE}(w) = \sum_s \mu(s)\big(V^\pi(s) - \hat V(s;w)\big)^2, μ\mu the state-visitation distribution.
  • The oracle/supervised view. If an oracle gave Vπ(s)V^\pi(s), this is regression on (s,Vπ(s))(s, V^\pi(s)) pairs: Δw=α(Vπ(s)V^(s;w))wV^\Delta w = \alpha\big(V^\pi(s) - \hat V(s;w)\big)\nabla_w\hat V. No oracle exists, so plug in a target:
    • MC-VFA: target = return GtG_t (unbiased). ww+α(Gtx(s)Tw)x(s)w \leftarrow w + \alpha(G_t - x(s)^Tw)x(s); converges to the min-MSVE weights wMCw_{MC} (the best linear approximation, which may not be VπV^\pi).
    • TD-VFA: target = bootstrapped r+γV^(s;w)r + \gamma\hat V(s';w) (a semi-gradient — we don’t differentiate through the target); biased, but lower variance, and converges to a different fixed point.
  • The deadly triad. Combining bootstrapping + function approximation + off-policy learning can diverge. (Each pair is fine; all three together is the danger — the central caution of deep RL.)
  • DQN makes Q-learning + neural nets stable with two tricks that attack the triad’s instabilities: experience replay (sample past transitions in random minibatches — breaks the temporal correlation that violates i.i.d.) and a target network (a periodically-frozen copy supplies the bootstrap target — stops the target from chasing the parameters). Extensions: double DQN (maximization-bias fix, online net selects / target net evaluates), prioritized replay (sample high-TD-error transitions more often), dueling DQN (split the head into V(s)V(s) + advantage A(s,a)A(s,a)).

Core competency set

  • Set up MSVE and the SGD update; contrast MC-VFA (unbiased target) vs TD-VFA (semi-gradient, biased) and their fixed points.
  • State the deadly triad and how DQN’s replay + target network address it; name double/prioritized/dueling.

7. Policy gradient methods — optimize the policy directly

  • Why policy-based. Parameterize the policy πθ(as)\pi_\theta(a\mid s) and ascend its value. Advantages: smoother convergence (no value-function discontinuities flipping the greedy action), works in continuous/high-dimensional action spaces (no global maxa\max_a), and can represent stochastic optimal policies (needed under partial observability / state aliasing — e.g. rock-paper-scissors). Disadvantage: typically local optima, high variance.
  • The objective J(θ)=Vπθ(s0)=τP(τ;θ)R(τ)J(\theta) = V^{\pi_\theta}(s_0) = \sum_\tau P(\tau;\theta)R(\tau) over trajectories τ\tau.
  • The score-function / likelihood-ratio trickstrategy: p=plogp\nabla p = p\,\nabla\log p to turn a gradient of a probability into an expectation. Differentiate the objective:

θJ=τθP(τ;θ)R(τ).\nabla_\theta J = \sum_\tau \nabla_\theta P(\tau;\theta)\,R(\tau).

Multiply and divide by PP (the log-derivative identity):

=τP(τ;θ)θlogP(τ;θ)R(τ).= \sum_\tau P(\tau;\theta)\,\nabla_\theta\log P(\tau;\theta)\,R(\tau).

Recognize the weighted sum as an expectation:

=Eτ[R(τ)θlogP(τ;θ)].= \mathbb E_\tau\big[R(\tau)\,\nabla_\theta\log P(\tau;\theta)\big].

Now expand the trajectory log-prob into its factors:

θlogP(τ;θ)=θ[logμ(s0)+tlogp(st+1st,at)+tlogπθ(atst)].\nabla_\theta\log P(\tau;\theta) = \nabla_\theta\Big[\log\mu(s_0) + \sum_t \log p(s_{t+1}\mid s_t,a_t) + \sum_t\log\pi_\theta(a_t\mid s_t)\Big].

Drop every term free of θ\thetaμ\mu and the dynamics pp vanish, the key payoff: the model cancels:

θlogP(τ;θ)=tθlogπθ(atst).\nabla_\theta\log P(\tau;\theta) = \sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t).

Estimate by sampling mm trajectories: g^=1miR(τi)tθlogπθ(atisti)\hat g = \tfrac1m\sum_i R(\tau_i)\sum_t\nabla_\theta\log\pi_\theta(a_t^i\mid s_t^i).

  • Policy gradient theorem (general, beyond episodic): for any of the standard objectives,

θJ(θ)=Eπθ[θlogπθ(as)Qπθ(s,a)].\nabla_\theta J(\theta) = \mathbb E_{\pi_\theta}\big[\nabla_\theta\log\pi_\theta(a\mid s)\,Q^{\pi_\theta}(s,a)\big].

The intuition: push up the log-probability of actions in proportion to how good they are. (Softmax policy score: θlogπ=ϕ(s,a)Eπ[ϕ(s,)]\nabla_\theta\log\pi = \phi(s,a) - \mathbb E_{\pi}[\phi(s,\cdot)] — feature of the action taken minus its average.)

  • REINFORCE: use the sampled return GtG_t as an unbiased estimate of QπQ^{\pi}: θθ+αθlogπθ(st,at)Gt\theta \leftarrow \theta + \alpha\,\nabla_\theta\log\pi_\theta(s_t,a_t)\,G_t. Correct but high variance (it’s Monte-Carlo).
  • Baselines and the advantagestrategy: subtract a mean-zero term to cut variance without touching the mean. Claim: for any b(s)b(s) that depends only on the state, Eπ[θlogπθ(as)b(s)]=0\mathbb E_\pi[\nabla_\theta\log\pi_\theta(a\mid s)\,b(s)] = 0. Pull b(s)b(s) out and write the inner sum over actions:

Eπ[θlogπθ(as)b(s)]=sd(s)b(s)aπθ(as)θlogπθ(as).\mathbb E_{\pi}\big[\nabla_\theta\log\pi_\theta(a\mid s)\,b(s)\big] = \sum_s d(s)\,b(s)\sum_a \pi_\theta(a\mid s)\,\nabla_\theta\log\pi_\theta(a\mid s).

Undo the log-derivative (πlogπ=π\pi\,\nabla\log\pi = \nabla\pi):

=sd(s)b(s)aθπθ(as).= \sum_s d(s)\,b(s)\sum_a \nabla_\theta\pi_\theta(a\mid s).

Swap the sum and gradient; the action probabilities sum to 1, so its gradient is 0:

=sd(s)b(s)θ ⁣(aπθ(as))=sd(s)b(s)θ1=0.= \sum_s d(s)\,b(s)\,\nabla_\theta\!\Big(\sum_a\pi_\theta(a\mid s)\Big) = \sum_s d(s)\,b(s)\,\nabla_\theta 1 = 0.

So subtracting b(s)b(s) leaves the gradient unbiased but can shrink its variance. The near-optimal choice is b(s)=Vπ(s)b(s) = V^\pi(s), giving the advantage Aπ(s,a)=Qπ(s,a)Vπ(s)A^\pi(s,a) = Q^\pi(s,a) - V^\pi(s) (“how much better than average is this action”):

θJ=Eπ[θlogπθ(as)Aπ(s,a)].\nabla_\theta J = \mathbb E_{\pi}\big[\nabla_\theta\log\pi_\theta(a\mid s)\,A^\pi(s,a)\big].

  • Actor–critic. Maintain an actor (policy θ\theta) and a critic (value estimate ww). The critic supplies the advantage. The clean trick: the TD error is an unbiased sample of the advantage

Eπ[δπs,a]=Eπ[r+γVπ(s)s,a]Vπ(s).\mathbb E_\pi\big[\delta^\pi\mid s,a\big] = \mathbb E_\pi\big[r + \gamma V^\pi(s')\mid s,a\big] - V^\pi(s).

The first term is exactly Qπ(s,a)Q^\pi(s,a) (Bellman), so the difference is the advantage:

Eπ[δπs,a]=Qπ(s,a)Vπ(s)=Aπ(s,a),\mathbb E_\pi\big[\delta^\pi\mid s,a\big] = Q^\pi(s,a) - V^\pi(s) = A^\pi(s,a),

so θJ=Eπ[θlogπθ(as)δπ]\nabla_\theta J = \mathbb E_\pi[\nabla_\theta\log\pi_\theta(a\mid s)\,\delta^\pi], using the approximate δv=r+γVw(s)Vw(s)\delta_v = r + \gamma V_w(s') - V_w(s) in practice. Variance reduction also comes from reward-to-go (only future rewards multiply each logπt\nabla\log\pi_t) and nn-step / blended targets.

  • Monotonic improvement → TRPO. Stepping too far in RL is unrecoverable (a bad policy collects bad data). The performance-difference lemmastrategy: express one policy’s value via the other’s advantages

V(θ~)=V(θ)+Eπθ~[tγtAπθ(st,at)].V(\tilde\theta) = V(\theta) + \mathbb E_{\pi_{\tilde\theta}}\Big[\sum_{t}\gamma^t A_{\pi_\theta}(s_t,a_t)\Big].

Why (telescoping): along a π~\tilde\pi-trajectory, expand the advantage Aπθ(st,at)=rt+γVπθ(st+1)Vπθ(st)A_{\pi_\theta}(s_t,a_t) = r_t + \gamma V_{\pi_\theta}(s_{t+1}) - V_{\pi_\theta}(s_t), so

tγtAπθ(st,at)=tγtrtVπθ(s0)\sum_t \gamma^t A_{\pi_\theta}(s_t,a_t) = \sum_t \gamma^t r_t - V_{\pi_\theta}(s_0)

(the discounted VV terms telescope) =GV(θ)= G - V(\theta); take Eπ~\mathbb E_{\tilde\pi} to get V(θ~)V(θ)V(\tilde\theta) - V(\theta). Exact, but it needs the new policy’s state distribution μπ~\mu_{\tilde\pi}. Local approximation swaps in the old μπ\mu_\pi to get a surrogate Lπ(π~)L_\pi(\tilde\pi) that matches VV and its gradient at θ\theta. Conservative policy iteration bounds the gap by a term in the policy difference; generalizing to a KL-divergence penalty gives a guaranteed-improvement lower bound. TRPO maximizes the surrogate subject to a trust-region KL constraint (the principled, larger step size), using importance sampling and a linear-objective/quadratic-constraint solve.

Core competency set

  • Derive J=E[logπR]\nabla J = \mathbb E[\nabla\log\pi\,R] via the score-function trick and show the dynamics cancel; state the policy gradient theorem.
  • Prove the baseline leaves the gradient unbiased (E[logπb(s)]=0\mathbb E[\nabla\log\pi\,b(s)] = 0) and define the advantage; show δ\delta is an unbiased advantage sample.
  • Explain REINFORCE → actor-critic (variance reduction) and the performance-difference-lemma → TRPO trust-region story.

8. Exploration and bandits

  • Multi-armed bandit (MAB) = a single-state MDP: actions (“arms”) with unknown reward distributions; maximize cumulative reward. The pure-greedy estimator Q^t(a)=1Nt(a)r1[aτ=a]\hat Q_t(a) = \tfrac{1}{N_t(a)}\sum r\mathbb 1[a_\tau{=}a] can lock onto a suboptimal arm forever.
  • Regret is the currency: Lt=E[τVQ(aτ)]L_t = \mathbb E\big[\sum_\tau V^* - Q(a_\tau)\big]. Decompose it by gaps and countsstrategy: rewrite the sum over time as a sum over arms — with gap Δa=VQ(a)\Delta_a = V^* - Q(a):

Lt=aE[Nt(a)]Δa.L_t = \sum_a \mathbb E[N_t(a)]\,\Delta_a.

A good algorithm keeps small counts on large-gap arms. Greedy and fixed-ε\varepsilon-greedy have linear regret (a constant fraction of pulls are suboptimal); the goal is sub-linear, and the lower bound is logarithmic, LtlogtaΔa/DKL(RaRa)L_t \gtrsim \log t\sum_a \Delta_a / D_{KL}(\mathcal R^a\Vert\mathcal R^{a^*}).

  • Optimism under uncertainty → UCBstrategy: a concentration inequality + invert for a bonus + union bound over time. Hoeffding: P(E[X]>Xˉn+u)e2nu2P\big(\mathbb E[X] > \bar X_n + u\big) \le e^{-2nu^2}. Set the RHS to a target δ\delta and solve for the bonus:

e2Nt(a)u2=δ  u=log(1/δ)2Nt(a),e^{-2N_t(a)\,u^2} = \delta \ \Rightarrow\ u = \sqrt{\frac{\log(1/\delta)}{2N_t(a)}},

so Q(a)Q^t(a)+uQ(a) \le \hat Q_t(a) + u with probability 1δ\ge 1-\delta. Choosing δ=t4\delta = t^{-4} (so it holds across all steps, via a union bound) gives UCB1:

at=argmaxa[Q^t(a)+2logtNt(a)].a_t = \arg\max_a\Big[\hat Q_t(a) + \sqrt{\tfrac{2\log t}{N_t(a)}}\Big].

Less-tried arms (small NtN_t) get a bigger bonus → exploration. Regret boundstrategy: a pulled suboptimal arm had a high UCB, so the gap is bounded by the bonus. When UCB pulls suboptimal aa, its index beats the optimal’s, which (under the concentration event) exceeds Q(a)Q(a^*); chaining gives 2Clogt/Nt(a)Δa2\sqrt{C\log t/N_t(a)} \ge \Delta_a, hence Nt(a)4Clogt/Δa2N_t(a) \le 4C\log t/\Delta_a^2, and Lt=aΔaNt(a)=O(alogT/Δa)L_t = \sum_a \Delta_a N_t(a) = O\big(\sum_a \log T/\Delta_a\big)logarithmic, matching the lower bound’s rate. Picture: draw each arm’s value as a distribution; optimism compares the upper ends, so a wide (uncertain) arm can outrank a higher-mean narrow one — and its band shrinks as you sample it, which is the explore→exploit transition made visible.

  • PAC (an alternative criterion): act ε\varepsilon-optimally with probability 1δ\ge 1-\delta on all but a polynomial number of steps. Optimistic initialization (init QQ high) is a simpler route to sub-linear regret if tuned right — but optimism may not survive function approximation.
  • Bayesian bandits & Thompson sampling. Keep a posterior over each arm’s reward parameter (e.g. Beta–Bernoulli conjugacy, posterior Beta(α+#1,β+#0)\text{Beta}(\alpha+\#1, \beta+\#0)). Thompson sampling: sample a parameter from each arm’s posterior, act greedily w.r.t. the sampled values, update the chosen arm’s posterior. It implements probability matchingselect arm aa with probability that it is optimal, π(aht)=P(Q(a)>Q(a) aht)\pi(a\mid h_t) = P(Q(a) > Q(a')\ \forall a'\mid h_t) — which is automatically optimistic (more-uncertain arms have more upside mass). Achieves the Lai–Robbins lower bound; sensitive to prior misspecification (no mass on the truth → can’t converge). Contextual bandits add an i.i.d. context per step (between bandits and MDPs).
  • Numeric anchor. UCB, two arms at t=10t = 10: arm 1 pulled N1=8N_1 = 8, mean Q^1=0.5\hat Q_1 = 0.5; arm 2 pulled N2=2N_2 = 2, mean Q^2=0.4\hat Q_2 = 0.4. Bonuses 2ln10/80.76\sqrt{2\ln 10 / 8} \approx 0.76 and 2ln10/21.52\sqrt{2\ln 10 / 2} \approx 1.52; indices 0.5+0.76=1.260.5 + 0.76 = 1.26 vs 0.4+1.52=1.920.4 + 1.52 = 1.92 → pull the less-tried arm 2 despite its lower mean (optimism in action). Thompson: arm with prior Beta(1,1)\text{Beta}(1,1) (mean 0.50.5), observe reward 0 → posterior Beta(1,2)\text{Beta}(1,2) (mean 13\tfrac13); a later reward 1 → Beta(2,2)\text{Beta}(2,2) (mean back to 0.50.5, but tighter).

Core competency set

  • Define regret and decompose it into gaps × counts; explain why greedy/ε\varepsilon-greedy are linear and the log-tt lower bound.
  • Derive the UCB bonus from Hoeffding (+ union bound) and sketch the Nt(a)=O(logt/Δa2)N_t(a) = O(\log t/\Delta_a^2) regret argument.
  • Explain Thompson sampling and that it implements probability matching.

9. Fast MDP learning and offline (batch) RL

  • Optimism in MDPs: MBIE-EB adds an exploration bonus to rewards in a tabular MDP (the UCB idea lifted to states/actions); Bayesian MDPs maintain a posterior over dynamics.
  • The batch/offline setting: you’re handed a fixed dataset of trajectories from a behavior policy πb\pi_b and must evaluate/optimize a new policy πe\pi_e without collecting more data — the counterfactual, censored-data problem (you only see outcomes of decisions actually taken). Assumptions to make it tractable: stationarity, Markov, sequential ignorability (no unrecorded confounders — the potential-outcomes link to track 9), and overlap (πb(as)>0\pi_b(a\mid s) > 0 wherever πe(as)>0\pi_e(a\mid s) > 0 — you can’t evaluate actions the data never tried). The deadly triad bites again for off-policy value methods.
  • Off-policy evaluation by importance samplingstrategy: importance reweighting (multiply and divide by the sampling density). To estimate Ep[r]\mathbb E_p[r] from samples drawn from qq:

Ep[r]=xp(x)r(x).\mathbb E_p[r] = \sum_x p(x)\,r(x).

Multiply and divide by the sampling density qq:

=xq(x)p(x)q(x)r(x)=Eq[p(x)q(x)r(x)].= \sum_x q(x)\,\frac{p(x)}{q(x)}\,r(x) = \mathbb E_q\Big[\tfrac{p(x)}{q(x)}\,r(x)\Big].

Estimate from samples xiqx_i \sim q:

1Nip(xi)q(xi)r(xi).\approx \tfrac1N\sum_i \frac{p(x_i)}{q(x_i)}\,r(x_i).

Sample under qq (behavior), reweight by p/qp/q — unbiased given overlap. Lifted to trajectories, Vπ(s)=Eτπb[p(τπ)p(τπb)R(τ)]V^\pi(s) = \mathbb E_{\tau\sim\pi_b}\big[\tfrac{p(\tau\mid\pi)}{p(\tau\mid\pi_b)}R(\tau)\big], and expanding the trajectory probabilities, the dynamics p(ss,a)p(s'\mid s,a) cancel — only the product of policy ratios tπ(atst)πb(atst)\prod_t \tfrac{\pi(a_t\mid s_t)}{\pi_b(a_t\mid s_t)} remains. So importance sampling is unbiased with no Markov assumption and no model — but high variance: the product of ratios makes variance scale exponentially in the horizon. Per-decision IS trims it (a reward only needs the ratios up to its own time, since later actions can’t affect it). (Effective sample size flags how little data a recommendation truly rests on.)

Core competency set

  • List the offline-RL assumptions (stationarity, Markov, ignorability, overlap) and why overlap is required.
  • Derive the importance-sampling estimator and the trajectory version where dynamics cancel; state its unbiasedness, the no-Markov property, and the exponential-in-horizon variance.
  • Model-based RL: learn p^(ss,a)\hat p(s'\mid s,a) and r^(s,a)\hat r(s,a) from experience (supervised regression / density estimation), then plan on the learned model. Upside: data-efficient, and the model’s uncertainty can guide exploration. Downside: two error sources — model error and planning error on top of it.
  • Sample-based planning: use the model only as a simulator of (s,r)(s', r), then run any model-free method on the simulated experience.
  • Simulation-based search computes a policy only for the current state (no full state-space sweep — efficient when you visit only a small subset). Simple MC search: from the current state, simulate KK episodes per action under a fixed policy and pick the highest mean return (one step of policy improvement). The expectimax tree does better — full lookahead taking max\max at action nodes and expectation at state nodes (value iteration on the current sub-MDP) — but is (SA)H(|S||A|)^H. Picture (draw it): the expectimax tree — square max nodes (actions) alternating with round expectation nodes (next states), rooted at the current state; MCTS grows only the promising branches (UCB-selecting at each max node) and rolls out a default policy past the frontier, instead of expanding the whole tree.
  • MCTS avoids building the whole tree: run KK simulations from the root, each with a tree policy (pick actions to maximize QQ inside the explored tree) and a rollout policy (random/default once you leave it); a node’s value is the average return of simulations passing through it. Converges to the optimal action with enough simulations; best for large state space, small action space, long horizon.
  • UCT = MCTS where each tree node is treated as its own bandit and actions are chosen by UCB: Q(s,a,i)+clnn(s)n(s,a)Q(s,a,i) + c\sqrt{\tfrac{\ln n(s)}{n(s,a)}} — so search effort concentrates on promising, under-explored branches. The simulating policy changes as estimates evolve.
  • AlphaGo ties it together: an adversarial (minimax) tree, a value network for position evaluation and a policy network (bootstrapped by imitation learning of human moves, then self-play) to prune the huge action branching, bootstrapping instead of rolling to the leaf (MC → TD), and self-play to densify the win/loss reward signal.

Core competency set

  • Contrast model-based (learn model, plan; two error sources) with model-free; explain sample-based planning.
  • Explain MCTS (tree vs rollout policy, node value) and UCT (each node a UCB bandit); summarize AlphaGo’s value net + policy net + self-play.

11. Memorize cold

  • Return Gt=kγkrt+kG_t = \sum_k \gamma^k r_{t+k}; value Vπ(s)=Eπ[Gts]V^\pi(s) = \mathbb E_\pi[G_t\mid s], Qπ(s,a)=Eπ[Gts,a]Q^\pi(s,a) = \mathbb E_\pi[G_t\mid s,a]; Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a\pi(a\mid s)Q^\pi(s,a).
  • Bellman expectation Vπ=Rπ+γPπVπV^\pi = R^\pi + \gamma P^\pi V^\pi (matrix solve (IγPπ)1Rπ(I-\gamma P^\pi)^{-1}R^\pi); optimality V(s)=maxa[R(s,a)+γsP(ss,a)V(s)]V^*(s) = \max_a[R(s,a) + \gamma\sum_{s'}P(s'\mid s,a)V^*(s')]. Operators Bπ,BB^\pi, B^*; Vπ,VV^\pi, V^* are their fixed points.
  • BB^* is a γ\gamma-contraction in \lVert\cdot\rVert_\infty → Banach → unique VV^*, VkVγkV0V\lVert V_k - V^*\rVert_\infty \le \gamma^k\lVert V_0 - V^*\rVert_\infty. (proof strategy: contraction + fixed point.)
  • Policy iteration: evaluate + greedy; improvement theorem (telescoping) + finite policies → terminates at optimal. Value iteration: iterate BB^*.
  • MC update VV+1N(GtV)V\leftarrow V + \tfrac1N(G_t - V) (unbiased FV, high variance, episodic). TD(0) V(st)V(st)+αδtV(s_t)\leftarrow V(s_t) + \alpha\delta_t, δt=rt+γV(st+1)V(st)\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t) (biased, low variance, online, Markov).
  • SARSA (on-policy) target r+γQ(s,a)r + \gamma Q(s',a'); Q-learning (off-policy) target r+γmaxaQ(s,a)r + \gamma\max_{a'}Q(s',a'). Converge with R–M steps (α=,α2<\sum\alpha=\infty, \sum\alpha^2<\infty); SARSA→QQ^* needs GLIE.
  • Maximization bias: E[maxQ^]maxE[Q^]\mathbb E[\max\hat Q]\ge\max\mathbb E[\hat Q] (Jensen) → double-Q (select with Q1Q_1, evaluate with Q2Q_2).
  • Deadly triad = bootstrapping + function approximation + off-policy → can diverge. DQN = replay + target network.
  • Score-function trick: θJ=E[θlogπθ(as)Qπ(s,a)]\nabla_\theta J = \mathbb E[\nabla_\theta\log\pi_\theta(a\mid s)\,Q^\pi(s,a)]; dynamics cancel in logP(τ)=tlogπθ\nabla\log P(\tau)=\sum_t\nabla\log\pi_\theta. REINFORCE uses GtG_t. Baseline b(s)b(s) keeps it unbiased (E[logπb]=0\mathbb E[\nabla\log\pi\,b]=0); advantage A=QVA = Q - V; TD error is an unbiased AA sample.
  • Regret Lt=aE[Nt(a)]ΔaL_t = \sum_a \mathbb E[N_t(a)]\Delta_a. UCB1 Q^(a)+2logt/Nt(a)\hat Q(a)+\sqrt{2\log t/N_t(a)} (Hoeffding + union bound) → O(alogT/Δa)O(\sum_a\log T/\Delta_a) regret. Thompson sampling = sample posterior, act greedy = probability matching.
  • Importance sampling Ep[r]=Eq[pqr]\mathbb E_p[r]=\mathbb E_q[\tfrac pq r]; trajectory IS keeps only tπ/πb\prod_t\pi/\pi_b (dynamics cancel) — unbiased, no Markov, variance exponential in HH. Needs overlap.
  • UCT = MCTS with a UCB bandit at each node.

Named moves (cross-track glossary): value-is-the-object (everything hangs off V/QV/Q); one-step-lookahead (the Bellman recursion); contraction-then-fixed-point (VI/PE convergence — Banach, track 2); monotone-improvement-over-finite-set (PI terminates); telescope-the-advantage (policy improvement & performance-difference lemmas); bootstrap-vs-sample-the-return (TD vs MC, the bias/variance dial); Robbins–Monro (stochastic-approximation step sizes); Jensen-overestimates-the-max (maximization bias → decouple select/evaluate); score-function-trick (p=plogp\nabla p = p\nabla\log p, model-free policy gradient — tracks 11a/11b); subtract-a-mean-zero-baseline (variance reduction without bias); TD-error-is-the-advantage; optimism-under-uncertainty (UCB / MBIE / Thompson); concentration-plus-union-bound (UCB bonus & regret — track 1); probability-matching (Thompson); importance-reweighting (off-policy evaluation, dynamics cancel — track 9); bandit-at-every-node (UCT); simulate-don’t-enumerate (sample-based planning breaks the curse of dimensionality).