Markov Decision Processes

Choosing a move when some outcomes are random

A Markov decision process (MDP) is a Markov chain with a steering wheel: in most states you pick an action, you pay a cost or collect a reward, and then the next state may still be down to chance.

A plain Markov chain has no choices. The weather just happens. An agent — a player, a robot, a learned policy — has to decide. Some squares on the board let you pick a move and pay its cost. One square might be a dice roll: you do not pick, you only wait to see where you land. The memoryless idea stays the same: what you should do next depends on where you are now, not on the path that brought you here.

States, actions, and chance

A state is still “where we are”. An action is a move you are allowed to make from that state. After you pick, you get a reward (a benefit) or a cost (a negative reward), and you land in a next state.

Most states in the little board below are under your control: from Start you may move to \(2\), or take a cheaper gamble into state \(4\). State \(4\) is pure chance. You do not pick a move there. The process rolls: back to Start with probability \(0.1\), on to \(3\) with probability \(0.4\), or stay put with probability \(0.5\). Exit is a quiet absorbing state: once you are there, you stay.

A 15-second walk through value iteration on this board. Values fill in, and the green arrows settle on a policy: from Start, take the gamble at 4, then head to Exit.

Diagram and video: @mathemetica on X

This is the board from a short explainer by Mathematica (@mathemetica) on X. The video above is that explainer: you can watch the values fill in as value iteration runs, and a policy appear in green. The frames in the margin are stills from the same video, so you can sit with the starting board, a mid-run step, and the settled values.

Video frame of the Markov Decision Process board at iteration 0. Five teal circles, Start 1, 2, 3, 4 and Exit 5, each show the value 0.00. Action labels give costs such as Move to 2, R equals -5, and Move to 4, R equals -2. Purple chance arrows from state 4 are labelled 0.1, 0.4 and 0.5. The Bellman update and gamma equals 0.9 sit at the bottom.

The board at the start of value iteration. Every state value is 0.00, and every action and chance arrow is visible. Gamma is 0.9, and the step counter k is 0.

At the start (\(k=0\)). Every value is \(0\). The arrows already show the costs and the chance probabilities.

Video frame at iteration 7. Start is red with value -2.32, state 2 is gold with value -1.00, state 4 is green with value -0.37 and a yellow ring around it, and states 3 and Exit are teal at 0.00. Green policy arrows run from Start to 4, from 2 to 3, and from 3 to Exit.

Midway through value iteration, at step 7. A yellow ring highlights chance state 4. Start is about -2.32, state 2 is -1.00, state 4 is -0.37, and 3 and Exit are 0.00. Green arrows already mark the emerging policy.

At step \(k=7\), the numbers are moving. A ring marks the chance state being updated. The green arrows are already the policy we will keep.

Video frame at iteration 16. Values have settled at Start -2.35, 2 -1.00, 3 0.00, 4 -0.38, and Exit 0.00. Green arrows show the policy from Start to 4, from 2 to 3, and from 3 to Exit. The Bellman equation and gamma equals 0.9 remain at the bottom.

The board after 16 value-iteration steps. The values have settled: Start -2.35, state 2 -1.00, states 3 and Exit 0.00, chance state 4 -0.38. Green arrows show the best route: Start takes the gamble at 4, then 3 heads to Exit.

At \(k=16\) the values have settled, to the two decimals the video prints. From Start, the best route takes the gamble at \(4\), then heads to Exit.

Diagram and video: @mathemetica on X

Rewards, costs, and a policy

A reward is the number you get for a move. On this board the numbers are costs, so they are negative: moving from Start to \(2\) costs \(5\), taking the door into \(4\) costs only \(2\). Reaching Exit from \(3\) costs nothing. A policy is a habit: in each state, which move do you pick? The green arrows in the video are one policy — the one value iteration will recommend once it has finished its sums.

The discount \(\gamma=0.9\) says that a cost tomorrow counts for \(0.9\) of the same cost today. We care about the future, just a little less than about now. Without a discount, a chain that can wander forever would have no reason to prefer a short route.

The Bellman idea, first in words

Here is the why, before the symbols.

The value of a state is “how well can I expect to do, if I act well from here?” For a state where you pick a move, try each allowed action. For that action, add the reward you get now to the discounted value of the state you land in. If the landing is random, take the average, using the transition probabilities. Then keep the action that gives the best total. That best total is the value of the state.

For a pure chance state there is no “best action”. There is only the average over the roll.

In symbols, the same update is:

\[ V(s)\leftarrow \max_a\sum_{s'}P(s'\mid s,a)\,[R+\gamma V(s')] \]

Read it left to right: a new value for \(s\) is the best action’s expected reward-plus-discounted-next-value. The video writes the same line under the board.

Value iteration, step by step

Value iteration does that update for every state, again and again, starting from all zeros.

  1. Set \(V(s)=0\) for every state.
  2. For each state, compute the right-hand side of the Bellman line, using the current values of the other states.
  3. Write the new numbers in.
  4. Repeat until the numbers barely move.

Then read off the policy: in each state, the best move is the one that won the \(\max\). The values settle; the arrows turn green.

On this board the chance arrows in the video are labelled with probabilities only, not with a reward. If those chance transitions carry reward \(0\), synchronous value iteration reproduces the numbers on the frames (to the two decimals the video prints). The live cell below uses that reading. It does not invent other figures.

A run of that cell prints the values below (and the same best moves). Those mid-run and final values match the video frames at the same \(k\).

\(k\) Start 2 3 4 Exit
0 \(0.00\) \(0.00\) \(0.00\) \(0.00\) \(0.00\)
1 \(-2.00\) \(-1.00\) \(0.00\) \(0.00\) \(0.00\)
4 \(-2.23\) \(-1.00\) \(0.00\) \(-0.31\) \(0.00\)
7 \(-2.32\) \(-1.00\) \(0.00\) \(-0.37\) \(0.00\)
10 \(-2.34\) \(-1.00\) \(0.00\) \(-0.38\) \(0.00\)
16 \(-2.35\) \(-1.00\) \(0.00\) \(-0.38\) \(0.00\)

Best moves: Start → Move to 4 (\(-2.35\)); 2 → Move to 3 (\(-1.00\)); 3 → Move to 5 (\(0.00\)); 4 is chance (\(-0.38\)); Exit stays (\(0.00\)).

From Start, moving to \(4\) (cost \(2\)) is better than moving to \(2\) (cost \(5\)). From \(3\), the door to Exit is free and finishes the trip. That is the policy the post describes: take the gamble at \(4\), then head to Exit.

Summary

Word In one line
State Where the agent is now
Action A move the agent is allowed to pick
Reward / cost The number paid or gained for a move
Chance state A state with no choice; only a roll
Policy A habit: one action for each state
Value \(V(s)\) How well we can expect to do from \(s\), if we act well
Discount \(\gamma\) How much a future reward counts against one today
Value iteration Update every \(V(s)\) again and again, then read off the best moves

FAQ

Why is the value of Start negative? Because every useful path still pays some costs before Exit. “Better” here means “less costly”. \(-2.35\) is much better than the \(-5.9\) you would get by walking to \(2\) instead.

Why not always walk towards Exit as directly as possible? From Start, the “direct” walk through \(2\) is the expensive one. The door into \(4\) is cheaper, even though \(4\) can bounce you back to Start now and then. The average, once you include that risk, is still the better deal.

Is this how a large language model is trained? Not by filling in a five-state table. But the same family of ideas — a state, an action, a reward, a policy that improves — sits under Reinforcement learning for reasoning. This page is the tiny board you can compute by hand, or in the browser, before that story.

See also

References

  • [1] Mathematica (@mathemetica), How does an agent choose its moves when some outcomes are random?, post and 15s video, 1 December 2026. the post on X. The classroom board, the chance state, the Bellman line, and the stills in the margin come from this post. The values in the live cell match the video when chance transitions carry reward \(0\).
  • [2] Richard S. Sutton and Andrew G. Barto, Reinforcement Learning: An Introduction, 2nd ed., MIT Press, 2018, Chapters 3 and 4 (MDPs and dynamic programming). incompleteideas.net (open textbook).
Back to top