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.

Not signed in โ€” your progress and quiz score won't be saved.
Lesson 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ร—Aโ†’Rr: \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ร—Aโ†’Rr: \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(sโ€ฒโˆฃs,a)P(s' \mid s, a) is the transition kernel. The defining assumption:

P(st+1=sโ€ฒโˆฃst,at,stโˆ’1,atโˆ’1,โ€ฆ)โ€…โ€Š=โ€…โ€ŠP(st+1=sโ€ฒโˆฃst,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 ฯ€(aโˆฃs)\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 Gtโ‰คrmaxโก/(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ฯ€โ€‰โฃ[Gtโˆฃst=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ฯ€โ€‰โฃ[Gtโˆฃst=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ฯ€(aโˆฃs)โ€‰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 aโ€ฒa' has Qฯ€(s,aโ€ฒ)>Vฯ€(s)Q^\pi(s, a') > V^\pi(s) โ€” taking aโ€ฒa' 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ฯ€(aโˆฃs)โˆ‘sโ€ฒP(sโ€ฒโˆฃs,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)}sโˆˆS\{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)โ€…โ€Š=โ€…โ€Šโˆ‘sโ€ฒP(sโ€ฒโˆฃs,a)โ€‰โฃ[R(s,a,sโ€ฒ)+ฮณโˆ‘aโ€ฒฯ€(aโ€ฒโˆฃsโ€ฒ)โ€‰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โ€ฒฯ€(aโ€ฒโˆฃsโ€ฒ)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)=โˆ‘sโ€ฒP[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)=argโกmaxโกaQโ‹†(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(sโ€ฒโˆฅs,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