Reinforcement Learning Basics
Reinforcement Learning: Markov Decision Processes, Value Iteration, and Policy Iteration
1. Reinforcement Learning Versus Supervised Learning
In supervised learning, each training example contains an input and a corresponding target output:
The learning algorithm attempts to learn a hypothesis h that maps inputs to outputs:
The correct output is supplied directly for each input. Training examples are generally treated as independent and identically distributed, or i.i.d. The objective is to learn a function that performs well on unseen examples.
In reinforcement learning, the learner is called an agent and interacts with an environment over time. Instead of being told the correct action for every situation, the agent:
Observes its current state.
Chooses an action.
Transitions to a new state.
Receives a numerical reward.
The reward may be positive or negative and measures how desirable the recent outcome was. Unlike a supervised-learning label, a reward does not directly specify the correct action.
Reinforcement learning learns how to make a sequence of decisions that maximizes long-term accumulated reward.
Key differences
Supervised learning: The correct output is provided for each input.
Reinforcement learning: The agent receives feedback in the form of a reward after acting.
Supervised learning: Examples are typically independent.
Reinforcement learning: Decisions influence future states and future rewards.
Supervised learning: The objective is usually to minimize loss on unseen examples.
Reinforcement learning: The objective is to maximize the expected sum of future rewards.
The sequential nature of reinforcement learning is central. An action that produces a small immediate reward may be preferable if it leads to much larger rewards later. Conversely, an action with a large immediate reward may be poor if it causes bad future outcomes.
Examples of reinforcement-learning agents
A robot learning to walk.
A game-playing program for chess or Go.
An automated trading system making a sequence of financial decisions.
A navigation system choosing movements through an environment.
A control system operating a simulated or physical machine.
2. Markov Decision Processes
The standard mathematical framework for reinforcement learning is the Markov decision process, abbreviated MDP.
An MDP is represented by the five-tuple
where each component describes one part of the decision problem.
2.1 State space
S is the set of all possible states of the environment.
A state contains the information needed to describe the agent’s current situation. For example, in a grid-world environment, a state may specify the grid square currently occupied by the agent.
If the grid contains eleven accessible squares, then the state space has eleven elements:
Blocked locations are not included in the state space because the agent cannot occupy them.
2.2 Action space
A is the set of possible actions available to the agent.
In a grid-world example, the actions might be:
Move north.
Move south.
Move east.
Move west.
The available actions can, in general, depend on the state. For simplicity, many introductory examples assume that every action is available in every state.
2.3 Transition probabilities
The transition model describes how the environment changes after the agent takes an action. It is commonly written as
This quantity is the probability that the agent moves to successor state s′ given that:
The current state is s.
The chosen action is a.
For every fixed state-action pair (s,a), the transition probabilities form a probability distribution over possible successor states:
The transition is generally stochastic, meaning that the same action does not necessarily produce the same next state every time.
For example, suppose the agent is in state (1,3) and chooses “move north.” Because of uncertainty in the environment, the outcomes might be:
Move to (2,3) with probability 0.8.
Move to (1,2) with probability 0.1.
Move to (1,4) with probability 0.1.
All other successor states have probability zero for that state-action pair.
Stochasticity may arise from:
Imperfect robot movement.
Slippery or uncertain terrain.
Unmodeled physical effects.
An opponent’s actions in a game.
Randomness deliberately built into a simulator.
2.4 The Markov property
An MDP assumes the Markov property: the distribution of the next state depends only on the current state and current action, not on the full history of previous states and actions.
Formally,
This assumption allows the agent to make decisions based on the current state without retaining the entire trajectory.
2.5 Discount factor
The discount factor γ satisfies
It determines how much future rewards matter relative to immediate rewards.
A reward received one step in the future is multiplied by γ, a reward received two steps in the future is multiplied by γ2, and so on.
If γ is close to 0, the agent is strongly focused on immediate rewards.
If γ is close to 1, the agent places substantial importance on long-term rewards.
The same γ is typically used at every time step.
The discount factor can be interpreted in several ways:
Time preference: A reward received sooner is preferred to the same reward received later.
Financial discounting: Future income and costs are discounted in a manner analogous to interest rates.
Stability: Discounting helps ensure that an infinite sum of rewards remains finite under suitable conditions.
2.6 Reward function
The reward function specifies the numerical feedback received by the agent.
A simple state-based reward function is
where R(s) is the reward associated with entering or occupying state s.
For example:
Reaching a goal state may produce reward +1.
Reaching an undesirable terminal state may produce reward −1.
Ordinary states may produce a small negative reward such as −0.02.
A state-action reward function may instead be used:
This form is useful when actions have different costs. For example, two actions may lead to the same state but have different energy, time, or financial costs.
The introductory treatment primarily uses state-based rewards, assuming that actions themselves have equal cost unless otherwise stated.
3. Grid-World Example
Consider an agent moving through a grid-like environment.
Each accessible square is a state.
A blocked square is not a valid state.
The agent can move in one of the cardinal directions.
Some states are desirable terminal states.
Some states are undesirable terminal states.
Ordinary states may have small negative rewards to encourage the agent to reach a terminal state quickly.
For example:
Goal state: reward +1.
Failure state: reward −1.
Ordinary state: reward −0.02.
A solution is not merely a single action. It is a strategy for deciding what to do from every possible state. The agent must account for uncertainty in movement and for the long-term consequences of its choices.
4. Trajectories, Episodes, and Accumulated Reward
Suppose the agent begins in an initial state s0. It selects action a0, and the environment produces a new state s1 according to the transition model:
The agent then selects a1, producing
This process continues:
A sequence of states and actions is called a trajectory, episode, or trial. These terms are often used interchangeably.
At every step, the agent receives a reward. Using a state-based reward convention, the discounted return beginning at s0 is
More generally, the return from time t is
The agent’s objective is to choose actions that maximize the expected value of this discounted return.
Immediate reward versus long-term return
Reward: The immediate feedback obtained from the current transition or state.
Return: The accumulated, discounted sequence of current and future rewards.
Value: The expected return obtained from a state when actions are selected according to a specified policy.
This distinction is fundamental. Reinforcement learning is difficult because maximizing immediate reward does not necessarily maximize long-term value.
5. Policies
A policy specifies how the agent chooses an action in each state.
A deterministic policy is a function
where π(s) is the action chosen when the agent is in state s.
For example, a policy might specify:
Move east in state s1.
Move north in state s2.
Move west in state s3.
The policy is applied repeatedly throughout a trajectory. If the agent reaches the same state multiple times, it takes the action prescribed by the policy each time, regardless of how it arrived there or how many times it has previously visited that state.
A policy does not necessarily give the best possible behavior. It is simply a rule for choosing actions. The goal is to find an optimal policy that maximizes expected long-term reward.
6. Value Functions
Given a policy π, the value function Vπ assigns a numerical long-term value to every state.
For a starting state s, the value function is
The subscript π indicates that actions are selected according to policy π.
In words, Vπ(s) is the expected sum of discounted future rewards when:
The agent starts in state s.
The agent follows policy π at every step.
The environment transitions stochastically according to P.
The expectation is necessary because future states—and therefore future rewards—are random when the transition model is stochastic.
Reward and value are different
Reward is immediate feedback; value is the expected long-term benefit of following a policy from a state.
For example, an ordinary state might have a small negative immediate reward because the agent has not yet reached its goal. Nevertheless, that state may have high value if it provides a reliable path to a large positive reward.
Likewise, an action leading to an immediately positive reward may have low value if it places the agent in a situation with poor future outcomes.
Policies and value functions as related objects
A policy determines a value function:
Given a value function, one can construct a policy by choosing actions that lead, in expectation, to states with high value:
These two concepts are central to reinforcement learning. Some algorithms primarily improve value estimates, while others primarily improve the policy.
7. The Bellman Equation for a Fixed Policy
The value function can be decomposed into:
The immediate reward obtained from the current state.
The discounted expected value of future states.
Starting from state s, the first reward is R(s). The policy chooses action π(s), and the environment transitions to successor state s′ with probability P(s′∣s,π(s)).
Therefore,
This is the Bellman equation for a fixed policy.
The equation is recursive because the value of the current state is defined in terms of the values of successor states.
Interpretation
R(s) is the immediate reward.
γ discounts future outcomes.
P(s′∣s,π(s)) gives the probability of reaching s′.
Vπ(s′) is the future value after reaching s′.
The summation computes the expected value over all possible successor states.
The Bellman equation can also be understood as
8. Computing the Value of a Given Policy
Assume the MDP has a finite number of states. Then Vπ can be represented as a vector with one component per state:
Similarly, define the reward vector
For a fixed policy π, define the policy transition matrix Pπ by
Each row describes the transition probabilities from one current state when the action prescribed by π is taken.
The Bellman equations for all states can then be written in vector form:
Rearranging gives
Assuming the matrix is invertible,
This provides a direct method for calculating the value function of a known policy.
Why this works
For a fixed policy:
The reward vector R is known.
The transition matrix Pπ is known.
The discount factor γ is known.
The unknown values Vπ(s) appear linearly.
Thus, evaluating a fixed policy reduces to solving a system of linear equations.
This is the policy evaluation step used in policy iteration.
9. Optimal Value Function
The optimal value function gives the highest achievable value from every state.
It is defined as
Thus, V∗(s) is the maximum expected discounted return obtainable from state s, considering all possible policies.
The optimal value function satisfies the Bellman optimality equation:
The agent chooses the action whose expected successor value is greatest.
Difference from the fixed-policy Bellman equation
For a fixed policy, the action is already determined:
For the optimal value function, the best action must be selected by maximizing over all possible actions:
This maximization makes the optimality equation nonlinear and prevents it from being solved as one ordinary linear system in the same direct way as the fixed-policy Bellman equation.
10. Optimal Policy
Once the optimal value function is known, the optimal policy can be recovered by choosing the action that maximizes expected future value:
The discount factor γ can be included inside the maximization without changing the selected action because it is the same positive constant for every action:
The optimal policy is therefore obtained by acting greedily with respect to the optimal value function.
The optimal policy selects, in each state, the action with the greatest expected long-term value.
11. Value Iteration
Value iteration computes the optimal value function by repeatedly applying the Bellman optimality update.
Algorithm
Initialize the value of every state, commonly to zero:
Repeatedly update every state according to
Continue until the values converge.
Extract the policy using
The update uses the current value estimates on the right-hand side to produce improved value estimates on the left-hand side.
Bellman backup operator
Define the Bellman backup operator B by
Value iteration repeatedly applies this operator:
The operator is called a backup operator because it updates the value of a state using information about possible successor states.
Why value iteration converges
For discounted MDPs, the Bellman optimality operator is a contraction mapping. Informally, applying the operator brings value functions closer together.
For two value functions V1 and V2, the contraction property has the form
Since γ<1 in the usual discounted setting, repeated application of B moves value estimates toward a unique fixed point.
A fixed point V∗ satisfies
This fixed point is the optimal value function because it satisfies the Bellman optimality equation.
The initialization does not need to be zero. Under the contraction property, a suitable initialization will converge toward the same fixed point.
Synchronous and asynchronous variants
Synchronous value iteration
Compute updated values for all states using the old value vector.
Replace all old values simultaneously after the full update.
Asynchronous value iteration
Update one state at a time or update states in sequence.
Use newly updated values immediately when computing later updates.
Both approaches are based on the same Bellman backup idea, although their implementation and convergence behavior can differ.
Approximation versus exact convergence
Value iteration approaches V∗ increasingly closely. In practice, it is stopped when the changes become smaller than a chosen tolerance.
Depending on the implementation and stopping criterion, the algorithm may produce an arbitrarily accurate approximation rather than explicitly reaching the exact mathematical fixed point.
12. Policy Iteration
Policy iteration alternates between evaluating a policy and improving that policy.
Algorithm
Initialize a policy π arbitrarily or randomly.
Policy evaluation: Compute Vπ for the current policy.
Policy improvement: For every state, replace the current action with the action that maximizes expected value under the current value function.
Repeat until the policy no longer changes.
Policy evaluation
For the current policy, calculate
This evaluates the expected long-term return obtained by following the current policy.
Policy improvement
Using the current value function, update the policy at each state:
If R(s) is independent of the action, it does not affect which action maximizes the expression and may be omitted from the argmax:
The improved policy is greedy with respect to the current value function.
Stopping condition
Policy iteration stops when
Once the policy stops changing, it is the optimal policy under the standard finite discounted MDP assumptions. Evaluating that final policy yields the exact optimal value function.
13. Value Iteration Versus Policy Iteration
Value iteration
Directly updates value estimates.
Uses the Bellman optimality equation at every update.
Does not require solving a full linear system at each iteration.
Approaches the optimal value function progressively.
The optimal policy is extracted after or during convergence.
Policy iteration
Alternates between policy evaluation and policy improvement.
Policy evaluation solves a system of linear equations.
Each policy-improvement step chooses actions greedily with respect to the current value function.
Converges to the optimal policy after a finite number of policy changes under standard finite-MDP assumptions.
Once the optimal policy is obtained, its value function is the exact V∗.
Computational considerations
Let
∣S∣ be the number of states.
∣A∣ be the number of actions.
A value-iteration sweep considers each state-action pair and possible successor states. For a dense transition representation, its rough cost is on the order of
Policy evaluation requires solving or inverting a matrix of size ∣S∣×∣S∣. A dense matrix inversion has rough cubic cost:
Policy improvement itself requires evaluating the actions and is roughly on the order of
Therefore:
Policy iteration may be more expensive per iteration for large state spaces because of policy evaluation.
Value iteration may require many updates because it gradually approaches the optimum.
Policy iteration can reach the exact optimal policy after a finite number of policy changes.
Value iteration is often stopped after reaching a desired numerical precision.
The best method depends on the size and structure of the state and action spaces.
14. Relationship Between the Main Quantities
The central relationships in an MDP can be summarized as follows.
Policy to value
A policy determines a transition matrix Pπ, which determines the corresponding value function:
The value is found using
Value to policy
A value function induces a greedy policy by choosing the action with the best expected successor value:
using
Optimal quantities
The optimal value function and optimal policy satisfy
and
Value iteration searches for V∗ through repeated Bellman backups. Policy iteration searches for π∗ by repeatedly evaluating and improving policies.
15. Bias–Variance Trade-Off: Relevant Background
Before reinforcement learning, the learning objective was analyzed through the bias–variance trade-off.
Machine learning systems are evaluated primarily by their generalization error, meaning performance on unseen data rather than performance only on the training set.
The expected test error can be conceptually decomposed into:
Irreducible error
Bias
Variance
Irreducible error
Irreducible error comes from noise in the data, including noise in test examples and labels. It cannot be eliminated merely by choosing a better model.
Bias
Bias is systematic error caused by assumptions that are too restrictive or by a model that is insufficiently flexible.
Examples include:
Consistently underpredicting.
Consistently overpredicting.
Using a model whose parameters are systematically pulled toward inappropriate values.
Failing to represent the true relationship between inputs and outputs.
Variance
Variance measures sensitivity to noise in the training data. A high-variance model changes substantially when trained on different training sets.
Model capacity and regularization
Increasing model capacity generally:
Reduces bias.
May increase variance.
Increasing regularization generally:
Reduces variance.
May increase bias.
Consequently, the correct intervention depends on which problem is dominant.
If the model has high bias, increasing capacity or reducing regularization may help.
If the model has high variance, increasing regularization or obtaining more data may help.
Increasing capacity when variance is already high can worsen generalization.
Increasing regularization when bias is already high can make the model even less flexible.
Training and cross-validation error
A practical heuristic is:
Training error approximates the contribution of bias.
The gap between cross-validation error and training error approximates the contribution of variance.
Thus, model development should examine both errors simultaneously:
Low training error and substantially higher cross-validation error: likely high variance.
High training error and similarly high cross-validation error: likely high bias.
Cross-validation provides a practical estimate of how well the current model generalizes and helps determine which corrective action is most appropriate.
16. Broader Machine-Learning Context
Supervised learning
Supervised learning uses noisy input-output pairs and learns a function
The target variable determines the problem type:
Classification: y is discrete, such as a binary label or one of ten digit classes.
Regression: y is continuous.
Generalized linear models
Generalized linear models extend ordinary linear and logistic regression by allowing the conditional distribution
to belong to the exponential family.
This framework can include:
Binary outcomes.
Multiclass outcomes.
Continuous outcomes.
Count-valued outcomes such as Poisson variables.
Other distributions with suitable structure.
Discriminative and generative models
Discriminative models directly model
or directly learn a decision rule for predicting y from x.
Generative models model how complete examples are generated. Using the chain rule, a joint distribution can be decomposed as
Examples include:
Naive Bayes for discrete input variables.
Gaussian discriminant analysis for real-valued input variables.
When p(x∣y) belongs to the exponential family, the resulting conditional distribution p(y∣x) often has a generalized linear or logistic-regression form.
Nonlinear models
Nonlinearity can be introduced through:
Hand-designed feature maps.
Kernel methods.
Neural networks with learned feature representations.
Kernel methods use symmetric positive-definite functions that implicitly correspond to feature maps. Examples include:
Support vector machines for classification.
Gaussian processes for regression.
For ordinary parameterized models, computational complexity often depends on the number of features. Kernel methods instead tend to depend heavily on the number of training examples. They can represent very rich or effectively infinite-dimensional feature spaces, but may become expensive on very large datasets.
Support vector machines can scale better in some cases because their learned coefficients are often sparse: many training examples receive zero coefficients.
Neural networks can be viewed conceptually as:
A generalized linear model at the final layer.
A learned nonlinear feature map in the preceding layers.
Learning theory
Learning theory provides a more formal understanding of:
Regularization.
Bayesian interpretations of regularized estimation.
Bias and variance.
Uniform convergence.
Regularization can be interpreted in a Bayesian setting as imposing a prior over model parameters and performing maximum a posteriori estimation. Uniform convergence provides theoretical tools for relating empirical performance to expected performance across a class of models.

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