Reinforcement Learning Basics


Reinforcement Learning Basics

Maria
Welcome back. Today we’re starting a new chapter: reinforcement learning. We’ll focus on Markov decision processes, value iteration, and policy iteration—the core ideas that organize much of reinforcement learning.

Michael
Before we begin, there’s one important connection to the earlier material. In machine learning, our real goal is generalization: performing well on unseen data, not merely reducing training error.

Maria
Right. The generalization error can be viewed as having three additive components: irreducible error, bias, and variance. Irreducible error comes from noise in the test example, so there’s nothing we can do to remove it. Bias is the model’s systematic error, while variance measures how sensitive the model is to noise in the training data.

Michael
And the practical lesson is that training error and cross-validation error should be examined together. Training error is a useful heuristic for judging bias, while the gap between cross-validation error and training error is a useful heuristic for judging variance.

Maria
That diagnosis matters because different remedies affect bias and variance differently. Increasing model capacity usually reduces bias but may increase variance. Increasing regularization usually reduces variance but may increase bias. So before changing a model, we should ask which problem is larger.

Michael
That brings us to reinforcement learning. In supervised learning, each input comes with a target answer. For an input x, we are given the corresponding y, and the model learns a function mapping x to y.

Maria
In reinforcement learning, the supervision is weaker. Instead of being told the correct action in every situation, an agent receives a reward. Rewards are real-valued: they can be positive or negative, and they indicate how well the agent is doing.

Michael
The agent also makes decisions repeatedly over time. Examples include a robot learning to walk, a program playing chess or Go, or an automated trading agent. After each action, the environment changes and the agent receives a reward, so the goal is to maximize accumulated long-term reward.

Maria
That temporal structure is the key difference from the usual supervised-learning setting. Supervised examples are generally treated as independent, whereas reinforcement learning involves a sequence of connected decisions. An action now can affect the states and rewards that become possible later.

Michael
The standard formal framework for describing this is a Markov decision process, or MDP. An MDP is a five-tuple consisting of S, A, P, gamma, and R.

Maria
Here, S is the set of possible states, and A is the set of possible actions. P represents the transition probabilities, gamma is the discount factor between zero and one, and R is the reward function.

Michael
Imagine an agent moving through a grid. The states are the grid locations where the agent is allowed to stand, while blocked locations are not states. The actions might be move north, south, west, or east.

Maria
The transition model captures uncertainty in movement. Suppose the agent is at one location and chooses north. It might move north with probability 0.8, but because of uncertainty it might move west with probability 0.1 or east with probability 0.1.

Michael
Formally, P gives a probability distribution over next states. Given a current state s and an action a, P of s, a, s-prime tells us the probability that the environment places the agent in state s-prime.

Maria
The reward function specifies what the agent receives. In the grid example, reaching a goal might produce a reward of plus one, entering an undesirable location might produce minus one, and ordinary movements might have small negative rewards.

Michael
The reward can be written as R of s when it depends on the state entered. Sometimes it is written as R of s comma a when different actions have different costs, even if they lead to the same state.

Maria
We also assume the agent begins in an initial state, called s-zero. It takes an action a-zero, and the environment produces a next state s-one according to the transition probabilities. Then the agent takes a-one, reaches s-two, and continues in this way.

Michael
This sequence of states and actions can be called a trial, an episode, or a trajectory. These terms refer to the sequence generated as the agent acts and the environment responds.

Maria
At each step, the agent receives a reward. But future rewards are discounted. The total return has the form reward at the first step, plus gamma times the next reward, plus gamma squared times the following reward, and so on.

Michael
The discount factor encourages the agent to obtain positive rewards sooner rather than later. It also encourages postponing negative rewards. In a financial interpretation, gamma can be viewed similarly to an interest-rate-related discount: receiving value sooner is preferable.

Maria
Now we need two central concepts: a policy and a value function. A policy, written pi, maps a state to an action. It tells the agent what action to take whenever it is in a particular state.

Michael
The value of a state under policy pi, written V-sub-pi of s, is the expected sum of discounted future rewards when we start in state s and follow that policy thereafter.

Maria
That distinction between reward and value is crucial. Reward is the immediate payoff for being in a state or taking an action. Value is the long-term payoff obtained by following a policy, including all future rewards. An agent may sacrifice an immediate reward if doing so leads to a better long-term outcome.

Michael
Because transitions can be stochastic, the future states and rewards are random. That is why the value function uses an expectation: it averages the discounted returns over all possible future trajectories.

Maria
The value function satisfies the Bellman equation. In its recursive form, V-sub-pi of s equals the immediate reward R of s, plus gamma times the expected value of the next state when the action is chosen according to policy pi.

Michael
Written more explicitly, it is R of s plus gamma times the sum over next states s-prime of P of s, pi of s, s-prime, multiplied by V-sub-pi of s-prime. The policy selects the action, the transition model determines the probabilities of successor states, and the value function evaluates those successors.

Maria
If the policy is known, we can calculate its value function. For a finite state space, the values form a vector, and the Bellman equations form a system of linear equations. If P-sub-pi is the transition matrix induced by following policy pi, then the equations can be written as V-sub-pi equals R plus gamma times P-sub-pi times V-sub-pi.

Michael
Solving that system gives V-sub-pi equals the inverse of the identity matrix minus gamma P-sub-pi, multiplied by R. So moving from a policy to its value function can be done by solving a linear system.

Maria
There is also a way to move in the other direction. If we know the values of states, we can define a policy that chooses the action expected to lead to the highest-value successor states.

Michael
The optimal value function, written V-star of s, is the largest value achievable at state s over all possible policies. It satisfies the optimal Bellman equation: the immediate reward plus the maximum, over actions, of gamma times the expected optimal value of the next state.

Maria
The corresponding optimal policy chooses, at each state, the action that maximizes that expected future value. Notice the difference from the policy-specific Bellman equation: instead of using the action prescribed by a fixed policy, we maximize over all available actions.

Michael
That maximum is also why finding the optimal value is not simply another linear-system calculation. A fixed policy gives linear equations, but the optimality equation contains a maximum over actions.

Maria
The first algorithm for solving this is value iteration. We initialize the value of every state, often to zero. Then we repeatedly update every state using the optimal Bellman update: immediate reward plus the maximum expected discounted value over possible actions.

Michael
In symbols, each update replaces V of s with R of s plus the maximum over actions a of gamma times the sum over s-prime of P of s, a, s-prime multiplied by the current V of s-prime. We repeat this process until the values stop changing to the desired precision.

Maria
The reason value iteration converges is that the Bellman update is a contraction mapping. Intuitively, applying it brings different value estimates closer together. Repeated application therefore moves the estimates toward a single fixed point, and that fixed point is the optimal value function V-star.

Michael
Once value iteration has produced the optimal values, we recover the optimal policy by choosing, in every state, the action that maximizes the expected value of the next state.

Maria
The second algorithm is policy iteration. Instead of repeatedly updating values directly, we start with an initial policy, perhaps chosen randomly.

Michael
Policy iteration alternates between two steps. First, policy evaluation calculates V-sub-pi for the current policy by solving the Bellman linear system. Second, policy improvement updates the policy so that each state chooses the action with the greatest expected value under the current value function.

Maria
Those two steps repeat until the policy no longer changes. Once the policy has converged, it is the optimal policy, and its evaluated value function is the exact optimal value function.

Michael
The algorithms have different computational characteristics. Value iteration performs a maximization over actions for each state at every update, while policy iteration includes a policy-evaluation step involving inversion or solution of a state-by-state linear system.

Maria
So for a large state space, the linear-system calculation in policy iteration can be more expensive. On the other hand, policy iteration reaches the exact optimal policy after a finite number of policy improvements, while value iteration gets arbitrarily close to the optimal value as more Bellman updates are performed.

Michael
The big picture is that reinforcement learning revolves around the relationship between policies and values. A policy tells us what to do, a value function tells us the long-term consequences of following a policy, and Bellman equations connect the two.

Maria
Value iteration repeatedly improves value estimates and extracts a policy from them. Policy iteration repeatedly evaluates a policy and then improves it. Together, these methods provide the basic foundation for reasoning about sequential decisions in an MDP.

Ask anything
We've read this whole study set, so questions about it get the best answers.