AnyLearn
All lessons
AIadvanced

Markov Decision Processes

Master the mathematical skeleton of reinforcement learning. Learn how the agent-environment loop formalizes decision-making, why the Markov property is the key assumption, and how Bellman expectation equations link policies to value functions.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 10

The agent-environment loop

Reinforcement learning frames decision-making as a loop: at every discrete timestep tt, the agent observes a state sts_t, picks an action ata_t, and the environment replies with a reward rtr_t and a new state st+1s_{t+1}. Repeat.

No supervised labels. No explicit model to invert. The only training signal is the scalar reward stream. This stripped-down interface is surprisingly expressive: it subsumes chess, robot locomotion, protein-folding search, and ad-auction bidding under one formalism.

The key design choice is the reward function r:S×ARr: \mathcal{S} \times \mathcal{A} \to \mathbb{R}. Get it wrong and your agent will find loopholes you didn't anticipate — an insight sometimes called the specification problem.

Full lesson text

All 10 steps on one page, for reading, reference, and search.

Show

1. The agent-environment loop

Reinforcement learning frames decision-making as a loop: at every discrete timestep tt, the agent observes a state sts_t, picks an action ata_t, and the environment replies with a reward rtr_t and a new state st+1s_{t+1}. Repeat.

No supervised labels. No explicit model to invert. The only training signal is the scalar reward stream. This stripped-down interface is surprisingly expressive: it subsumes chess, robot locomotion, protein-folding search, and ad-auction bidding under one formalism.

The key design choice is the reward function r:S×ARr: \mathcal{S} \times \mathcal{A} \to \mathbb{R}. Get it wrong and your agent will find loopholes you didn't anticipate — an insight sometimes called the specification problem.

2. Agent-environment loop

The four signals that define every MDP interaction.

flowchart LR
  A["Agent"]
  E["Environment"]
  S["State s_t"]
  R["Reward r_t"]
  N["Next state s_{t+1}"]
  Act["Action a_t"]
  A --> Act
  Act --> E
  E --> R
  E --> N
  N --> S
  S --> A
  R --> A

3. The Markov property

A Markov Decision Process (MDP) is the tuple (S,A,P,R,γ)(\mathcal{S}, \mathcal{A}, P, R, \gamma) where P(ss,a)P(s' \mid s, a) is the transition kernel. The defining assumption:

P(st+1=sst,at,st1,at1,)  =  P(st+1=sst,at).P(s_{t+1} = s' \mid s_t, a_t, s_{t-1}, a_{t-1}, \ldots) \;=\; P(s_{t+1} = s' \mid s_t, a_t).

The future is independent of the past given the present. This is the Markov property. It says the state sts_t is a sufficient statistic for the agent's history — you can throw away every previous observation without losing anything.

In practice, raw sensory data (pixels, partial observations) often violates the Markov property. The fix: augment the state with history, use recurrent networks, or switch to the POMDP framework. But MDPs are the foundation everything else builds on.

4. Policies, return, and discounting

A policy π(as)\pi(a \mid s) is a probability distribution over actions given the current state. Deterministic policies write π(s)=a\pi(s) = a. The agent's goal is to find a policy that maximises the expected return — the sum of future rewards.

Because infinite horizons make raw sums diverge, we introduce a discount factor γ[0,1)\gamma \in [0, 1):

Gt  =  rt+γrt+1+γ2rt+2+=k=0γkrt+k.G_t \;=\; r_t + \gamma\, r_{t+1} + \gamma^2 r_{t+2} + \cdots = \sum_{k=0}^{\infty} \gamma^k r_{t+k}.

γ\gamma close to 1 values the future nearly as much as the present (far-sighted). γ\gamma close to 0 is myopic — only the immediate reward matters. The geometric series guarantees Gtrmax/(1γ)G_t \le r_{\max} / (1 - \gamma) whenever rewards are bounded, keeping the math finite.

5. State-value function $V^\pi$

The state-value function of policy π\pi is the expected return starting from state ss:

Vπ(s)  =  Eπ ⁣[Gtst=s]  =  Eπ ⁣[k=0γkrt+k  \/  st=s].V^\pi(s) \;=\; \mathbb{E}_\pi\!\left[G_t \mid s_t = s\right] \;=\; \mathbb{E}_\pi\!\left[\sum_{k=0}^{\infty} \gamma^k r_{t+k} \;\/\; s_t = s\right].

Think of Vπ(s)V^\pi(s) as asking: "if I follow π\pi forever starting from ss, how much reward do I expect to accumulate?" High VπV^\pi means the state is a good position under π\pi.

Common confusion: VπV^\pi depends on the policy. Two agents in the same environment can have wildly different value functions if they behave differently. The optimal value V(s)=maxπVπ(s)V^\star(s) = \max_\pi V^\pi(s) is the ceiling that the best possible policy achieves.

6. Action-value function $Q^\pi$

The action-value function answers a more granular question: "how much return do I expect if I take action aa in state ss right now, then follow π\pi thereafter?"

Qπ(s,a)  =  Eπ ⁣[Gtst=s,at=a].Q^\pi(s, a) \;=\; \mathbb{E}_\pi\!\left[G_t \mid s_t = s,\, a_t = a\right].

The relationship between QQ and VV is immediate:

Vπ(s)  =  aπ(as)Qπ(s,a).V^\pi(s) \;=\; \sum_a \pi(a \mid s)\, Q^\pi(s, a).

This identity is important: if you have QπQ^\pi, you can recover VπV^\pi by averaging over the policy. Conversely, to improve π\pi, look for states where some action aa' has Qπ(s,a)>Vπ(s)Q^\pi(s, a') > V^\pi(s) — taking aa' in ss beats following π\pi. That observation drives policy improvement.

7. Bellman expectation equation for $V^\pi$

The Bellman equations are the central recursion of RL. They express "now" in terms of "one step later." For the state-value function:

Vπ(s)  =  aπ(as)sP(ss,a) ⁣[R(s,a,s)+γVπ(s)].V^\pi(s) \;=\; \sum_a \pi(a \mid s) \sum_{s'} P(s' \mid s, a)\!\left[R(s, a, s') + \gamma\, V^\pi(s')\right].

Unpacking: the agent averages over actions (using π\pi), then over next states (using PP), collecting the immediate reward RR and the discounted future value γVπ(s)\gamma V^\pi(s').

This is a linear system in {Vπ(s)}sS\{V^\pi(s)\}_{s \in \mathcal{S}}. For small finite MDPs you can solve it directly via matrix inversion. For large spaces you need approximation — but the equation stays the same; only the solver changes.

8. Bellman expectation equation for $Q^\pi$

The same one-step look-ahead applies to action values:

Qπ(s,a)  =  sP(ss,a) ⁣[R(s,a,s)+γaπ(as)Qπ(s,a)].Q^\pi(s, a) \;=\; \sum_{s'} P(s' \mid s, a)\!\left[R(s, a, s') + \gamma \sum_{a'} \pi(a' \mid s')\, Q^\pi(s', a')\right].

The inner sum aπ(as)Qπ(s,a)\sum_{a'} \pi(a' \mid s') Q^\pi(s', a') is just Vπ(s)V^\pi(s'), so this collapses to Qπ(s,a)=sP[R+γVπ(s)]Q^\pi(s, a) = \sum_{s'} P[R + \gamma V^\pi(s')] — consistent with the V/QV/Q identity above.

Why prefer QQ over VV? Because with QQ, choosing the best action requires no model: π(s)=argmaxaQ(s,a)\pi^\star(s) = \arg\max_a Q^\star(s, a). With only VV, you'd need to query PP to evaluate each action — an expensive or impossible step when the model is unknown.

9. MDP components: a concrete example

Consider a 2×2 grid where an agent must reach a goal square.

ComponentExample
S\mathcal{S}4 grid cells (0,0)–(1,1)
A\mathcal{A}{up, down, left, right}
P(ss,a)P(s'\|s,a)0.8 intended + 0.1 each sideways slip
R(s,a,s)R(s,a,s')+1 at goal, −0.01 per step
γ\gamma0.95

Stochastic transitions (the 0.8/0.1/0.1 split) are realistic: motors slip, networks drop packets, opponents randomize. The per-step penalty 0.01-0.01 discourages dawdling even with γ\gamma near 1. The tension between exploration and exploitation emerges naturally: you must visit enough cells to learn PP before you can plan well.

10. From equations to algorithms

The Bellman equations define what we want; they don't yet say how to compute it.

Three families of algorithms solve MDPs:

  • Dynamic programming (policy iteration, value iteration): exact, requires full knowledge of PP and RR, scales to moderate state spaces.
  • Monte Carlo methods: sample complete episodes, average returns — no model needed, high variance.
  • Temporal-difference learning (Q-learning, SARSA): bootstrap from next-step estimates, update after each transition — the modern workhorse.

All three are grounded in the same Bellman equations. The differences are in how they estimate the right-hand side — from the true model, from full returns, or from one-step bootstrapped estimates. The upcoming lessons cover each family in depth.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. The Markov property in an MDP states that:
    • The reward depends only on the current action, not the state.
    • The next state depends only on the current state and action, not on history.
    • The policy must be deterministic to satisfy the Markov property.
    • Transition probabilities must be uniform over next states.
  2. Why do we discount future rewards with $\gamma < 1$?
    • To ensure the agent prefers actions that maximize immediate reward only.
    • To make the infinite-horizon return $G_t$ a finite, well-defined quantity.
    • Because rewards farther in the future are always less reliable estimates.
    • To convert a stochastic MDP into a deterministic one.
  3. Which relationship correctly links $V^\pi$ and $Q^\pi$?
    • $Q^\pi(s, a) = V^\pi(s) + \gamma V^\pi(s')$ for some next state $s'$.
    • $V^\pi(s) = \max_a Q^\pi(s, a)$ for any policy $\pi$.
    • $V^\pi(s) = \sum_a \pi(a \mid s)\, Q^\pi(s, a)$.
    • $Q^\pi(s, a) = \sum_{s'} P(s' \mid s, a) V^\pi(s')$.
  4. Why is $Q^\pi(s, a)$ more useful than $V^\pi(s)$ in model-free settings?
    • $Q$ values converge faster than $V$ values during training.
    • With $Q^\star$, the greedy policy $\pi^\star(s) = \arg\max_a Q^\star(s,a)$ needs no transition model.
    • $Q$ values are always lower-variance estimates than $V$ values.
    • $V^\pi$ is not well-defined for stochastic policies.
  5. The Bellman expectation equation for $V^\pi$ is a:
    • Non-linear system due to the product of $\pi$ and $P$ terms.
    • Linear system in $\{V^\pi(s)\}$ that can be solved by matrix inversion for small MDPs.
    • Differential equation that requires numerical integration to solve.
    • Contraction mapping only under the optimal policy $\pi^\star$.

Related lessons

AI
advanced

Market Making: Inventory, Adverse Selection, and What RL Adds

A market maker quotes both sides and profits from the spread, but every fill leaves an unwanted position and the counterparties who trade most eagerly are the ones who know something. This lesson simulates the inventory-skew trade-off, showing a 63 percent cut in exposure for 2.5 percent of profit, and locates where a learned policy genuinely helps.

10 steps·~15 min
AI
advanced

The Simulator Problem: Why a Backtest Cannot Answer This

Reinforcement learning needs an environment that responds to the agent, and historical market data is a fixed recording that does not. This lesson shows that replaying the same day gives a fill rate anywhere from 38 to 73 percent depending on an assumption the data cannot settle, and covers what to do about it.

10 steps·~15 min
AI
advanced

Reward Design: Where Execution Agents Go Wrong

An agent optimises the reward you wrote, not the objective you meant, and in execution the gap between those is unusually easy to open. This lesson computes how a mis-sized penalty makes leaving part of the order unexecuted rationally optimal, and covers the shaping that provably does not change the policy.

10 steps·~15 min
AI
advanced

Framing Execution as a Markov Decision Process

A static execution schedule is an open-loop policy: it commits to a plan before seeing anything. This lesson formulates execution as an MDP so the plan can react, and computes the ceiling on what any adaptive policy could win, which turns out to collapse as market impact grows.

10 steps·~15 min