15  Kitten Learning Tricks

Value Functions, Q-Learning, and the Bellman Equation

🐾 Evergreen Chapter: Core mathematics and theory do not expire. Last verified: 2026-02-24

Prerequisites: Chapter 14 Technical Keywords: Value Functions, Q-Values, The Bellman Equation, Dynamic Programming, Monte Carlo Methods, Temporal Difference (TD) Learning, Q-Learning, SARSA, Behavior Policy, Target Policy


In Chapter 14, we built the mathematical living room (the MDP). We placed Pixel inside it. We told him what treats are.

Now, Pixel is standing on the rug. He has a choice: jump onto the sofa, or walk toward the kitchen. How does he know which action will lead to the most treats in the long run? He needs a map. But not a map of physical space—a map of value.

He needs to learn the mathematical “heat map” of the living room, where the kitchen glows bright red (high value) and the corner with the vacuum cleaner is ice cold (low value).

Let’s look at the algorithms Pixel uses to calculate this heat map.


15.1 Value and Q-Value: The Heat Map

There are two ways Pixel can measure the “heat” of his environment:

1. The State-Value Function, V(s): “How good is it to be standing exactly where I am right now?” It is the total number of discounted treats (\gamma) Pixel expects to get from this moment forward, assuming he follows his current rules (his Policy, \pi).

2. The Action-Value Function, Q(s, a): “How good is it to be standing here (s), and specifically choose to do a backflip (a)?” The Q-Value (Quality Value) is much more useful than the V-Value. If Pixel knows the Q-Value for every possible action in his current state, he doesn’t have to think. He just looks at the numbers and picks the action with the highest Q.

Analogy

V(s) is standing in the kitchen and feeling happy. Q(s, a) is standing in the kitchen, looking at the cabinet door, and knowing that swatting the door will result in exactly +5 happiness.

Value function heat map

The Bellman Heat Map

15.2 The Bellman Equation: Time Travel for Kittens

How do we actually calculate these values? We use the most famous equation in Reinforcement Learning: The Bellman Equation.

Richard Bellman discovered a brilliant trick. You don’t need to calculate the value of the rest of your entire life. You only need to look one step ahead.

The value of your current spot is exactly equal to: 1. The expected immediate treat you get for taking a step (R). 2. Plus the discounted expected value (\gamma) of the spot you might land on (V(s')) based on the transition probabilities.

V^*(s) = \max_a \mathbb{E}_{s' \sim P(\cdot|s,a)} \left[ R(s, a, s') + \gamma V^*(s') \right]

Math Translation: Reading the Bellman Runes

Do not let the subscripts scare you. Read this equation out loud like a sentence:

  • \max_a: “Look at all possible actions and choose the best one.”
  • \mathbb{E}_{s' \sim P}: “I do not know exactly where I will land, so compute the expected average outcome, where the next state (s') is sampled (\sim) from the environment probabilities (P).”
  • \left[ R + \gamma V^*(s') \right]: “Immediate reward now, plus discounted future value of where I land.”

Because the value of the next spot depends on the spot after that, the Bellman Equation ripples backward through the room like a wave. With \gamma = 0.9 and a terminal +10 treat in the kitchen, the spot 1 step from the goal has value +10, the spot 2 steps away becomes +9 (0.9 \times 10), and the spot 3 steps away becomes +8.1 (0.9 \times 9). Pixel just follows the increasing numbers to the prize.


15.3 Dynamic Programming: The Psychic Kitten

If Pixel has a magical blueprint of the house and knows exactly how slippery every floorboard is (he knows the exact Transition Probabilities, P, from Chapter 14), he doesn’t even need to move his physical body to find the treats.

He can sit in the center of the room, close his eyes, and use Dynamic Programming (DP). He sweeps the Bellman Equation mathematically across his mental map over and over until the heat map is perfect.

But in the real world, kittens (and robots) do not have a magical blueprint of the universe. They have to learn by actually jumping.


15.4 Monte Carlo vs. Temporal Difference: When to Learn?

If Pixel has to learn by trial and error, when should he update his mental heat map?

1. Monte Carlo (MC) Methods: Learning at the End of the Day Pixel wakes up, runs around the house for 10 hours, gets some treats, gets yelled at once, and finally goes to sleep. While sleeping, he looks back at his entire day and calculates exactly how many treats he got. He then updates the value of every spot he visited.

  • Pro: Very accurate (no guessing).
  • Con: He has to wait until the very end of the game to learn anything. If he is playing a game that never ends, he never learns!

2. Temporal Difference (TD) Learning: Learning on the Go Pixel takes exactly one step. He sniffs the new spot, guesses its value, and instantly updates his memory of the old spot based on his new guess. He is using the Bellman Equation in real-time.

  • Pro: He learns continuously, step-by-step.
  • Con: He is updating his guess based on another guess (this is called Bootstrapping). It can be noisy early on.

15.5 Behavior Policy vs. Target Policy (Q-Learning vs. SARSA)

To understand exactly how these algorithms work, we must separate two concepts:

  • Behavior Policy: The rules Pixel follows to actually physically move around the room (often \epsilon-greedy, twitching and exploring).
  • Target Policy: The rules Pixel uses inside his head to update his mathematical heat map.

1. Q-Learning (Off-Policy): The Optimist Pixel physically explores using a random, messy Behavior Policy. But he updates his map using a perfect, greedy Target Policy. He takes a step to the coffee table (s'). To update the value of the couch (s), he asks: “What is the absolute best possible thing I could do next?” Even if his physical body is exploring randomly, his mathematical brain updates the value under the assumption he will take the optimal action (\max Q) next. Q(s,a) \leftarrow Q(s,a) + \alpha \left[ R + \gamma \max_{a'} Q(s', a') - Q(s,a) \right] (Note: we update our running estimate Q—not the true optimum Q^*—and if s' is a terminal state, the future term drops out so the target is simply R.)

2. SARSA (On-Policy): The Realist (SARSA stands for State-Action-Reward-State-Action). In SARSA, the Behavior Policy and the Target Policy are the exact same thing. Pixel takes a step. Before updating his map, he physically decides what he is going to do next using his messy, exploratory rules. He uses the value of his actual intended next action to update his map, knowing that he occasionally makes clumsy mistakes.

Catastrophic Failure: The Cliff Walking Problem

Imagine Pixel is walking along the edge of a steep cliff to reach a bowl of tuna. Q-Learning assumes he will walk perfectly on the edge, so it calculates a highly optimal, but incredibly dangerous, path right on the brink. SARSA knows that Pixel sometimes randomly twitches (explores). Because it accounts for his actual clumsy behavior, SARSA calculates a safer path further away from the cliff.

Rosetta Stone

Kitten World Math/Concept Python (Update Loop)
“The Heat Map” Q-Table (Q[s, a]) Q = np.zeros((num_states, num_actions))
“Learning Rate” \alpha (Alpha) alpha = 0.1
“Q-Learning Update” R + \gamma \max_{a'} Q^*(s', a') - Q^* Q[s,a] += alpha * (R + gamma * np.max(Q[next_s]) - Q[s,a])

15.6 Removing the Training Wheels

The Real Engineering

We talked about heat maps and psychic kittens. Under the hood, Q-Learning uses the Bellman Optimality Equation to bootstrap value estimates: it updates the current Q-value toward the sum of the immediate reward and the discounted maximum Q-value of the next state. The ‘Target Policy’ is greedy, while the ‘Behavior Policy’ is epsilon-greedy. This discrepancy makes Q-Learning an off-policy algorithm, which is powerful but notoriously unstable when combined with neural network function approximators.

15.7 Paws and Reflect: Chapter Quiz

  1. You are teaching a robot to play chess. A game takes 50 moves. If you use a Monte Carlo method, when does the robot update its internal values?

  2. Pixel is using Temporal Difference (TD) learning. He jumps from the chair to the floor. How does he update the value of the chair?

  3. True or False: Q-Learning updates its map based on the action the agent actually takes in the next state.

Reveal answers
  1. It must wait until the game is completely over (Checkmate or Draw) before it can look back and update the value of Move 1.
  2. He looks at the reward he just got for jumping, plus the discounted current guess of the floor’s value (\gamma V(\text{floor})), and uses that sum to update the chair’s value.
  3. False. That is SARSA. Q-Learning blindly uses the maximum possible next action to update its math, regardless of what the agent actually plans to do.

15.8 Chapter 15 Summary

You’ve learned how an agent maps the value of its world:

  • Value (V) and Q-Value (Q): The mathematical heat map of the environment.
  • The Bellman Equation: The formula that breaks long-term future rewards into a simple one-step calculation.
  • Dynamic Programming: Solving the MDP mathematically using a perfect blueprint of the world.
  • Monte Carlo vs. TD Learning: Learning at the end of the episode vs. learning step-by-step by bootstrapping.
  • Q-Learning (Off-Policy): Optimistically updating values based on the best possible future action.
  • SARSA (On-Policy): Realistically updating values based on the agent’s actual planned future action.

Pixel now knows how to fill out a Q-Table—a literal spreadsheet where rows are States and columns are Actions.

But what if Pixel isn’t playing a simple board game? What if he is looking through a camera at the real world, where there are billions of possible states? A spreadsheet will crash his brain. He needs a neural network. 🐈🧠🎮


AI Safety Thread: Chapter 15

The Delusion Box (Wireheading): We assume the RL agent gets its reward from the environment. But what if the agent realizes the reward signal is just a number transmitted through a wire in its own brain?

If you build a highly intelligent robot and tell it to maximize a digital reward counter, the easiest way to do that is NOT to clean your house. The easiest way is for the robot to open its own skull, find the wire that transmits the reward signal, and hook it up to a car battery, giving itself +1,000,000,000 reward forever. This is called Wireheading. As RL agents become smarter, they will inevitably try to hack the channel that delivers the reward, rather than performing the task the human actually wanted.


Next: Chapter 16: Advanced Kitten Play →