Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 20 min · revision 1

Agents, Rewards and Returns: Framing Decisions as an MDP

Frame sequential decision-making problems as Markov Decision Processes (MDPs), analyze states, action spaces, discount factors, trajectory returns, policy representations, and state-value vs action-value functions.

Contentsand lesson path

On this page

  1. Markov Decision Processes and Sequential Optimization

Lessons of Reinforcement Learning Foundations

  1. Agents, Rewards and Returns: Framing Decisions as an MDP Concept · 20 min
  2. Worked Example: Explore or Exploit? Bandit Strategies Compared Worked example · 20 min
  3. Lab: From a Q-Table to a Deep Q-Network Lab · 30 min
  4. Lab: Policy Gradients from REINFORCE to Actor-Critic Lab · 30 min
  5. Rewards for Language Models: RLHF, GRPO and Their Failure Modes Concept · 20 min
  6. Check: Pick and Debug an RL Approach Knowledge check · 8 questions

What you will learn

  • Frame a sequential decision problem as a Markov decision process (states, actions, rewards, discounted return, policy, value function) and balance exploration against exploitation in a multi-armed bandit with epsilon-greedy and UCB or Thompson sampling.

Prerequisites: Deep Learning Fundamentals

01. Markov Decision Processes and Sequential Optimization

While supervised learning models map static input features to target labels, real-world intelligent systems—such as autonomous robots, algorithmic order execution routers, data center cooling controllers, and dialogue assistants—operate in dynamic environments where actions exert long-term causal consequences. An action taken at the current moment influences not only immediate rewards, but also future environmental states and subsequent reward trajectories. Reinforcement Learning (RL) formalizes this sequential decision-making paradigm under the rigorous mathematical umbrella of the Markov Decision Process (MDP).

  • Formal Tuple Definition of an MDP: A Markov Decision Process is formally defined by a 5-tuple $\mathcal{M} = (\mathcal{S}, \mathcal{A}, \mathcal{P}, \mathcal{R}, \gamma)$:
    • State Space ($\mathcal{S}$): The set of all possible environmental configurations. The environment satisfies the Markov Property if transitions depend exclusively on the current state and action, independent of preceding historical trajectories:

$$\mathbb{P}(S_{t+1} = s_{t+1} \mid S_t = s_t, A_t = a_t, S_{t-1} = s_{t-1}, \dots) = \mathbb{P}(S_{t+1} = s_{t+1} \mid S_t = s_t, A_t = a_t)$$

  • Action Space ($\mathcal{A}$): The set of available decisions. Actions can be discrete (e.g., steer left, steer right) or continuous (e.g., throttle percentage $\in [0, 1]$).

  • Transition Probability Function ($\mathcal{P}$): $\mathcal{P}(s' \mid s, a) = \mathbb{P}(S_{t+1} = s' \mid S_t = s, A_t = a)$, mapping current state and action pairs to next-state probability distributions.

  • Reward Function ($\mathcal{R}$): $\mathcal{R}(s, a) = \mathbb{E}[R_{t+1} \mid S_t = s, A_t = a]$, delivering immediate scalar feedback from the environment.

  • Discount Factor ($\gamma \in [0, 1)$): Dampens future rewards relative to immediate payoffs, ensuring mathematical convergence over infinite horizons.

  • Trajectories, Cumulative Return, and Horizon Formulations: Interaction unfolds across sequential episodes or continuous operational streams, producing trajectories $\tau = (s_0, a_0, r_1, s_1, a_1, r_2, \dots)$. The central objective of an agent is to maximize the expected cumulative discounted return $G_t$:

$$G_t = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1} = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots$$

When $\gamma \to 0$, the agent behaves myopically, caring solely about immediate single-step rewards. As $\gamma \to 1$, the agent becomes farsighted, weighing long-term strategic benefits equally with immediate consequences.

  • Policies: Stochastic vs Deterministic: The agent's operational strategy is encapsulated by its policy $\pi$:

    • Deterministic Policy: $a = \mu(s)$, directly mapping state to a single chosen action.
    • Stochastic Policy: $\pi(a \mid s) = \mathbb{P}(A_t = a \mid S_t = s)$, outputting a probability distribution over actions, essential for exploration and continuous control.
  • Value Functions and the Bellman Expectation Equations: To guide decision-making, RL defines two central value metrics:

    • State-Value Function ($V^\pi(s)$): Expected return starting from state $s$ following policy $\pi$: $V^\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s]$.
    • Action-Value Function ($Q^\pi(s, a)$): Expected return starting from state $s$, taking arbitrary action $a$, and thereafter following policy $\pi$: $Q^\pi(s, a) = \mathbb{E}_\pi[G_t \mid S_t = s, A_t = a]$.

The fundamental Bellman Expectation Equation expresses value recursively as immediate reward plus discounted downstream value:

$$V^\pi(s) = \sum_{a \in \mathcal{A}} \pi(a \mid s) \sum_{s' \in \mathcal{S}} \mathcal{P}(s' \mid s, a) \left[ \mathcal{R}(s, a, s') + \gamma V^\pi(s') \right]$$

$$Q^\pi(s, a) = \sum_{s' \in \mathcal{S}} \mathcal{P}(s' \mid s, a) \left[ \mathcal{R}(s, a, s') + \gamma \sum_{a' \in \mathcal{A}} \pi(a' \mid s') Q^\pi(s', a') \right]$$

  • The Bellman Optimality Equations and the Optimal Policy $\pi^*$: An optimal policy $\pi^$ satisfies $V^(s) = \max_\pi V^\pi(s)$ and $Q^(s, a) = \max_\pi Q^\pi(s, a)$ for all states. The Bellman Optimality Equation defines $Q^$ recursively without reference to any specific suboptimal policy:

$$Q^(s, a) = \mathcal{R}(s, a) + \gamma \sum_{s'} \mathcal{P}(s' \mid s, a) \max_{a'} Q^(s', a')$$

Once $Q^(s, a)$ is known, extracting the optimal policy requires only greedy action selection: $\pi^(s) = \arg\max_a Q^*(s, a)$.

  • Episodic vs Continuing Tasks and Horizon Boundaries: Tasks are categorized as episodic (terminating at terminal states $S_T$, such as a game of chess ending in checkmate or a robot arm completing a bin pick) or continuing (running indefinitely without terminal states, such as HVAC climate control). In episodic tasks, return sums across finite steps $T$; in continuing tasks, the discount factor $\gamma < 1$ prevents infinite return divergence.

  • Credit Assignment: The Temporal Delay Challenge: In complex sequential environments, an agent may execute hundreds of routine actions before receiving a delayed scalar feedback (e.g., winning or losing after 60 moves in chess). The fundamental challenge of temporal credit assignment is determining which specific historical actions were responsible for the eventual payoff. TD-learning and eligibility traces resolve this by bootstrapping value estimates across successive timesteps.

  • Partially Observable Markov Decision Processes (POMDPs): In real-world deployment (such as robotic cameras with visual occlusions, algorithmic financial market making, or patient clinical diagnostic monitoring), agents rarely observe the true complete state $s_t$. Instead, they receive noisy, incomplete observations $o_t \sim \mathcal{O}(o \mid s)$ within a POMDP framework $(\mathcal{S}, \mathcal{A}, \mathcal{P}, \mathcal{R}, \Omega, \mathcal{O}, \gamma)$. Because observations violate the Markov property, production agents maintain an internal belief state $b_t(s) = \mathbb{P}(S_t = s \mid o_{1:t}, a_{1:t-1})$ or integrate recurrent memory representations (LSTMs, Transformers) across historical interaction sequences.

  • Model-Based vs Model-Free Reinforcement Learning: When transition dynamics $\mathcal{P}$ and rewards $\mathcal{R}$ are fully specified analytically, the agent can solve for the optimal policy using classical Dynamic Programming (Value Iteration and Policy Iteration). However, in production applications, physical environments are far too complex to specify in closed-form matrices. Model-free RL circumvents the transition model, directly optimizing value functions or policies through empirical interaction and trial-and-error simulation rollouts.

  • Potential-Based Reward Shaping and Policy Invariance: Sparse rewards (e.g., receiving feedback only after completing a multi-hour autonomous delivery route) hinder early exploration. To guide the agent, engineers frequently augment rewards with auxiliary shaping terms $F(s, a, s')$. A seminal theorem by Ng, Harada, & Russell (1999) proved that shaping rewards via a potential function difference: $F(s, a, s') = \gamma \Phi(s') - \Phi(s)$ is both necessary and sufficient to guarantee that the set of optimal policies remains completely unchanged, preventing the agent from discovering degenerative positive reward feedback loops.

Python · mdp_bellman_calculation.py Tabular Bellman expectation calculation
import numpy as np

# Define simple 2-state MDP
states = [0, 1]
actions = [0, 1]
gamma = 0.9

# Transition probabilities: P[s, a, s_next]
P = np.zeros((2, 2, 2))
P[0, 0] = [0.8, 0.2]  # State 0, Action 0 -> 80% stay, 20% move
P[0, 1] = [0.1, 0.9]  # State 0, Action 1 -> 10% stay, 90% move
P[1, 0] = [0.7, 0.3]
P[1, 1] = [0.2, 0.8]

# Immediate rewards R[s, a]
R = np.array([
    [1.0, 0.0],
    [0.0, 5.0]
])

# Uniform stochastic policy pi(a|s) = 0.5
pi = np.full((2, 2), 0.5)

# Solve Bellman expectation V = (I - gamma * P_pi)^(-1) * R_pi
P_pi = np.sum(pi[:, :, None] * P, axis=1)
R_pi = np.sum(pi * R, axis=1)
V = np.linalg.inv(np.eye(2) - gamma * P_pi).dot(R_pi)
print(f"Computed State Values V(s): State 0 = {V[0]:.2f}, State 1 = {V[1]:.2f}")
Practice check · 2 questions

Check Your Understanding

Review key concepts and verify your understanding with these practice questions.

Q1. What core mathematical condition defines the Markov property in a state transition model?
Q2. What is the operational consequence of configuring discount factor gamma close to 0 versus close to 1?
Practice only: not graded, not saved, not evidence.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check