Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Deep reinforcement learning and MDPs

Marks the fundamentals of Markov decision processes (MDPs), Bellman equations, classical RL algorithms (value iteration, policy iteration, Q-learning) and the basic ideas behind deep RL (DQN) with proofs, formulas and worked numerical examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: Deep reinforcement learning (deep RL) is how agents learn to make sequences of decisions from interaction with environments. Engineers use MDPs and RL when building systems that must plan or control over time (robotics, resource allocation, recommendation, game playing); deep RL scales these methods to high-dimensional inputs (images, sensors). Formal definition: An MDP is a tuple (S, A, P, R, γ) with state set S, action set A, transition kernel P(s'|s,a), expected immediate reward R(s,a,s') = E[R_{t+1}|S_t=s,A_t=a,S_{t+1}=s'], and discount γ∈[0,1]; it satisfies the Markov property (next-state and reward distributions depend only on (s,a)).

Key ideas

  • Return and objective — definition: The return from time t is G_t = ∑{k=0}^∞ γ^k R{t+k+1}. The objective is to find a policy π that maximises expected return from start states.

  • State-value function (v_π) — definition: v_π(s) = E_π[G_t | S_t = s]. Assumes expectation over both actions chosen by π and environment stochasticity.

  • Action-value function (q_π) — definition: q_π(s,a) = E_π[G_t | S_t = s, A_t = a].

  • Bellman expectation equation (exact identity for v_π): v_π(s) = E_{a∼π(·|s), s'∼P(·|s,a)}[R(s,a,s') + γ v_π(s')]. Show derivation: start from definition of return, split G_t into immediate reward plus γ G_{t+1}, apply expectation and law of total expectation.

  • Bellman optimality equation — definition: The optimal value function v_* satisfies v_*(s) = max_a E_{s'∼P(·|s,a)}[R(s,a,s') + γ v_*(s')]. Assumption: finite MDP or bounded rewards so max and expectations are finite. Similarly q_*(s,a) = E_{s'}[R + γ max_{a'} q_*(s',a')].

  • Bellman operator (T_π and T_*) — definitions: (T_π v)(s) = E_{a∼π(·|s), s'∼P(·|s,a)}[R+γ v(s')] and (T_* v)(s) = max_a E_{s'∼P(·|s,a)}[R+γ v(s')]. Important property: T_π and T_* are contraction mappings under max-norm when γ∈[0,1). Show contraction proof step-by-step: for any v and u, ||T_π v - T_π u||∞ ≤ γ ||v - u||∞, because the expectation is linear and γ scales the difference; with γ<1 this ensures unique fixed point.

  • Consequence: Existence and uniqueness of solutions to Bellman equations and convergence of iterative algorithms (value iteration, policy evaluation) follow from contraction mapping theorem.

  • Dynamic programming algorithms

    • Policy evaluation: solve v_π = T_π v_π; for finite MDPs we can do iterative policy evaluation (value backup) v_{k+1} = T_π v_k; converges to v_π at rate γ.
    • Policy improvement theorem: If for all s, q_π(s, π'(s)) ≥ v_π(s), then policy π' is at least as good as π. Use to build policy iteration: alternate evaluation and greedy improvement until stable.
    • Value iteration: repeatedly apply T_*: v_{k+1} = T_* v_k until convergence; then extract greedy policy π(s) = argmax_a E[R+γ v(s')]. Show error bound: after k iterations ||v_k - v_*||∞ ≤ γ^k / (1-γ) ||v_1 - v_0||∞ (derive from contraction).
  • Model-free RL: when P and R unknown we use sampled updates.

    • Monte Carlo: estimate returns by averaging episode returns.
    • Temporal-Difference (TD) learning: bootstrap from current estimate; update v(s) ← v(s) + α (R + γ v(s') - v(s)).
    • Q-learning (off-policy TD control) — update: Q(s,a) ← Q(s,a) + α [r + γ max_{a'} Q(s',a') - Q(s,a)]. Conditions for convergence: learning rates α_t(s,a) satisfying ∑ α_t = ∞ and ∑ α_t^2 < ∞ (Robbins-Monro), sufficient exploration (every state-action visited infinitely often), bounded rewards.
  • SARSA (on-policy TD control) — update: Q(s,a) ← Q(s,a) + α [r + γ Q(s', a') - Q(s,a)], where a' is the next action actually taken under the behavior policy (e.g., ε-greedy). Converges in the tabular case under similar Robbins–Monro step-size conditions with GLIE exploration.

  • Deep Q-networks (DQN) — idea: approximate Q(s,a; θ) using a neural network. Practical stabilising tricks: experience replay (sample mini-batches from replay memory), target network θ^- updated slowly to compute target y = r + γ max_{a'} Q(s',a'; θ^-). These are empirical fixes; convergence guarantees no longer straightforward but work well in practice. Typical loss: minimize L(θ) = E[(y − Q(s,a; θ))^2] over sampled transitions.

  • Diagram to draw when asked: Draw 4 boxes labelled S_t, A_t, R_{t+1}, S_{t+1} arranged left to right. Draw arrows S_t -> A_t (policy π), arrow (S_t,A_t) -> S_{t+1} labelled P(s'|s,a), arrow (S_t,A_t,S_{t+1}) -> R_{t+1} labelled R(s,a,s'). For MDP sketch a small graph with 3 states and arrows showing actions and probabilities; label probabilities on arrows and immediate rewards.

Formulas

G_t = Σ_{k=0}^∞ γ^k R_{t+k+1} where G_t is return (units: reward units), γ is unitless and 0 ≤ γ < 1. Applies always when discounting infinite horizon returns. For episodic finite-horizon tasks, γ may be 1 (undiscounted), and the sum remains finite.

v_π(s) = E_π[G_t | S_t = s] where v_π(s) is expected return from state s under policy π (reward units).

v_π(s) = E_{a∼π(·|s), s'∼P(·|s,a)}[R(s,a,s') + γ v_π(s')] Bellman expectation equation; symbols: s state, a action, s' next state, R(s,a,s') denotes expected immediate reward (reward units), γ discount factor (unitless).

v_*(s) = max_a E_{s'∼P(·|s,a)}[R(s,a,s') + γ v_*(s')] Bellman optimality equation; v_* is optimal value function (reward units).

q_π(s,a) = E[R + γ v_π(s') | s,a] action-value under π.

Q(s,a) ← Q(s,a) + α [r + γ max_{a'} Q(s',a') - Q(s,a)] Q-learning update; α is learning rate (unitless), r reward (units), γ discount (unitless). Applies in model-free RL with sufficient exploration and suitable α schedule for convergence in tabular case.

v_{k+1}(s) = max_a E_{s'∼P(·|s,a)}[R(s,a,s') + γ v_k(s')] value-iteration backup; iterate until convergence.

TD error δ = r + γ v(s') - v(s) used in TD updates; units: reward.

Worked examples

Example 1 — Value iteration on a small MDP (numerical) Problem: Finite deterministic MDP with states S = {s1, s2, s3(terminal)}. Actions A = {a1,a2} available in s1 and s2. Transitions and rewards:

  • From s1: a1 -> s2 with r=2, a2 -> s3 with r=0.
  • From s2: a1 -> s1 with r=1, a2 -> s3 with r=3.
  • s3 is terminal with v(s3)=0. Take γ = 0.9. Start v0(s) = 0 for all s. Step 1: compute v1(s) = max_a [r + γ v0(next)]. Since v0=0, v1(s1) = max(2+0, 0+0)=2. v1(s2)=max(1+0,3+0)=3. Step 2: v2(s1)=max(2 + 0.9·v1(s2), 0 + 0.9·v1(s3)) = max(2 + 0.9·3, 0) = max(2 + 2.7,0)=4.7. v2(s2)=max(1 + 0.9·v1(s1), 3 + 0.9·0) = max(1 + 0.9·2, 3) = max(2.8, 3)=3.0 (no tie here; a2 is preferred). Step 3: v3(s1)=max(2 + 0.9·3.0, 0)=2 + 2.7=4.7. v3(s2)=max(1 + 0.9·4.7, 3) = max(1+4.23,3)=5.23. Note: v(s1) will increase further in later iterations as v(s2) increases. Continue until change < ε. The true optimal values are v_*(s1) ≈ 15.2632 and v_*(s2) ≈ 14.7368; optimal greedy actions are: from s1 take a1 (to s2), from s2 take a1 (to s1) — no tie. You can compute the exact fixed point by solving v_*(s1)=2+0.9·v_*(s2), v_*(s2)=max(1+0.9·v_*(s1), 3), which yields v_*(s2)=2.8/0.19 and v_*(s1)=2+0.9·v_*(s2). Final answer (rounded): v_*(s1) ≈ 15.2632, v_*(s2) ≈ 14.7368. (Bold these if writing by hand.)

Example 2 — One-step Q-learning numeric update Suppose current Q-table Q(s,a) all zero. Observe transition: s = s0, a = a0, r = 1.0, s' = s1. Suppose max_{a'} Q(s1,a') = 2.0 (from previous knowledge). Choose α = 0.5, γ = 0.9. Compute TD target: r + γ max Q(s1,·) = 1.0 + 0.9·2.0 = 2.8. Q update: Q(s0,a0) ← 0 + 0.5·(2.8 - 0) = 1.4. Final Q(s0,a0) = 1.4 (reward units).

Example 3 — Short model answer: derive Bellman expectation equation Start from definition v_π(s) = E_π[Σ_{k=0}^∞ γ^k R_{t+k+1} | S_t = s]. Split first term: = E_π[R_{t+1} + Σ_{k=1}^∞ γ^k R_{t+k+1} | S_t = s] = E_π[R_{t+1} + γ Σ_{k=0}^∞ γ^k R_{t+k+2} | S_t = s]. Recognise inner sum as G_{t+1}. Then v_π(s) = E_π[R_{t+1} + γ G_{t+1} | S_t = s] = E_{a∼π(·|s), s'∼P}[R(s,a,s') + γ v_π(s')]. QED.

Common mistakes

  • Using off-policy algorithms (Q-learning) but not ensuring sufficient exploration (e.g., greedy forever) — leads to bad estimates.
  • In value iteration, stopping too early without understanding the ε → policy suboptimality relation. Not converting v to greedy policy correctly.
  • In DQN descriptions, forgetting to mention both experience replay and target network as stabilisers or claiming DQN has convergence proofs like tabular Q-learning (it does not in general).

For your exam

Question types: definitions (MDP, policy, Bellman eq.), short notes (TD vs MC), derivations (derive Bellman equations, show contraction), numerical problems (value iteration, single Q-learning update), small program or pseudocode for value iteration or Q-learning, diagram of MDP. What full-marks answers include: precise definitions with symbols, clear assumptions (finite MDP, bounded rewards, γ<1), step-by-step derivations (show splitting of return), explicit algorithm steps with stopping criteria and complexity comments, numerical work with units and rounded final values.

Quick check

  1. State the Bellman optimality equation for q_* (action-values).
  2. What condition on γ ensures the Bellman operator is a contraction in max-norm?
  3. Give the one-step Q-learning update rule and name two conditions needed for its convergence in the tabular case. Answers: (1) q_*(s,a) = E_{s'}[R(s,a,s') + γ max_{a'} q_*(s',a')]. (2) γ ∈ [0,1) and in practice γ < 1 (strictly less than 1 ensures contraction). (3) Q(s,a) ← Q(s,a) + α [r + γ max_{a'} Q(s',a') - Q(s,a)]; conditions: α_t satisfy Robbins–Monro (∑ α_t = ∞, ∑ α_t^2 < ∞) and all state-action pairs visited infinitely often (sufficient exploration).

Notes for the reviewer: I tried to give precise theorem statements and derivations and included worked numeric steps. I have omitted long DQN convergence proofs because standard proofs do not apply; I stated stabilising heuristics instead.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?