ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Reinforcement Learning
Chapter 1: Foundations of Reinforcement Learning
What is Reinforcement Learning?
Agents and Environments
States, Actions, and Rewards
Policies: Mapping States to Actions
The RL Workflow: Interaction Loops
Types of RL Tasks: Episodic vs Continuing
Comparing RL with Other Learning Types
Setting up Your Python Environment for RL
Quiz for Chapter 1
Chapter 2: Markov Decision Processes (MDPs)
Modeling Sequential Decision Making
Formal Definition of an MDP
State Transition Probabilities
Reward Functions
Return: Cumulative Future Rewards
Discounting Future Rewards
Policies and Value Functions (Vπ, Qπ)
Finding Optimal Policies
Quiz for Chapter 2
Chapter 3: Estimating Value Functions
The Bellman Expectation Equation
The Bellman Optimality Equation
Solving Bellman Equations (Overview)
Dynamic Programming: Policy Iteration
Dynamic Programming: Value Iteration
Limitations of Dynamic Programming
Quiz for Chapter 3
Chapter 4: Monte Carlo Methods
Learning from Complete Episodes
Monte Carlo Prediction: Estimating Vπ
Monte Carlo Control: Estimating Qπ
On-Policy vs Off-Policy Learning
MC Control without Exploring Starts
On-Policy First-Visit MC Control Implementation
Off-Policy MC Prediction and Control Intro
Practice: Implementing MC Prediction
Quiz for Chapter 4
Chapter 5: Temporal-Difference Learning
Learning from Incomplete Episodes
TD(0) Prediction: Estimating Vπ
Advantages of TD Learning over MC
SARSA: On-Policy TD Control
Q-Learning: Off-Policy TD Control
Comparing SARSA and Q-Learning
Expected SARSA
Hands-on Practical: Implementing Q-Learning
Quiz for Chapter 5
Chapter 6: Function Approximation in RL
Handling Large State Spaces
Value Function Approximation (VFA)
Feature Vectors for State Representation
Linear Methods for VFA
Gradient Descent for Parameter Learning
Semi-gradient TD Methods
Using Neural Networks for VFA
Practice: Applying Linear VFA
Quiz for Chapter 6
Chapter 7: Introduction to Deep Q-Networks (DQN)
Combining Q-Learning with Deep Learning
Challenges with Neural Networks in RL
Experience Replay Mechanism
Fixed Q-Targets (Target Networks)
The DQN Algorithm Structure
Architectural Considerations for DQNs
Hands-on Practical: Building a Basic DQN
Quiz for Chapter 7
Chapter 8: Introduction to Policy Gradient Methods
Learning Policies Directly
Policy Gradient Theorem (Concept)
REINFORCE Algorithm
Baselines for Variance Reduction
Actor-Critic Methods Overview
Comparing Value-Based and Policy-Based Methods
Practice: Implementing REINFORCE
Quiz for Chapter 8

Monte Carlo Control: Estimating Qπ

Monte Carlo (MC) methods estimate the state-value function Vπ(s)V^\pi(s)Vπ(s) for a given policy π\piπ by averaging the returns observed after visiting state sss. This process, called MC prediction or evaluation, tells us how good it is to be in a particular state under the current policy. However, our ultimate aim in Reinforcement Learning is usually control finding the optimal policy π∗\pi^*π∗ that maximizes the expected return from all states.

To achieve control using MC methods, especially when we don't have a model of the environment (i.e., we don't know the transition probabilities p(s′,r∣s,a)p(s', r | s, a)p(s′,r∣s,a)), estimating the state-value function VπV^\piVπ is often not sufficient. Why? Because improving the policy requires knowing the value of choosing different actions in a given state. If we only know Vπ(s)V^\pi(s)Vπ(s), we can't easily determine which action aaa leads to the best outcome without knowing the environment's dynamics. We'd need the model to compute ∑s′,rp(s′,r∣s,a)[r+γVπ(s′)]\sum_{s', r} p(s', r | s, a) [r + \gamma V^\pi(s')]∑s′,r​p(s′,r∣s,a)[r+γVπ(s′)].

This is where the action-value function Qπ(s,a)Q^\pi(s, a)Qπ(s,a) becomes essential. Recall that Qπ(s,a)Q^\pi(s, a)Qπ(s,a) represents the expected return starting from state sss, taking action aaa, and subsequently following policy π\piπ. If we have an accurate estimate of Qπ(s,a)Q^\pi(s, a)Qπ(s,a) for all actions aaa available in state sss, choosing the best action is straightforward: we simply select the action with the highest estimated QQQ-value.

π′(s)=argmaxaQ(s,a)\pi'(s) = \underset{a}{\text{argmax}} Q(s, a)π′(s)=aargmax​Q(s,a)

Therefore, for model-free control using MC methods, we shift our focus from estimating Vπ(s)V^\pi(s)Vπ(s) to estimating Qπ(s,a)Q^\pi(s, a)Qπ(s,a).

Estimating Action-Values (QπQ^\piQπ) with Monte Carlo

The core idea remains the same as MC prediction: we learn from complete episodes of experience and average the observed returns. However, instead of averaging returns for each state visited, we average returns for each state-action pair (s,a)(s, a)(s,a) visited.

Let's examine the first-visit MC method applied to estimating QπQ^\piQπ. We run multiple episodes following policy π\piπ. For each episode, we look at every state-action pair (St,At)(S_t, A_t)(St​,At​) that occurred. If it's the first time this specific pair (St,At)(S_t, A_t)(St​,At​) was visited during this episode, we calculate the total return GtG_tGt​ from that point until the end of the episode:

Gt=Rt+1+γRt+2+γ2Rt+3+⋯+γT−t−1RTG_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots + \gamma^{T-t-1} R_TGt​=Rt+1​+γRt+2​+γ2Rt+3​+⋯+γT−t−1RT​

where TTT is the terminal time step of the episode.

We maintain an estimate Q(s,a)Q(s, a)Q(s,a) for every state-action pair. Similar to MC prediction for VπV^\piVπ, we can update our estimate Q(St,At)Q(S_t, A_t)Q(St​,At​) by averaging all the returns GtG_tGt​ observed after first visits to (St,At)(S_t, A_t)(St​,At​) across all episodes.

A practical way to implement this is using an incremental update rule. We keep track of the number of times each state-action pair (s,a)(s, a)(s,a) has been visited for the first time across episodes, let's call this count N(s,a)N(s, a)N(s,a). After generating an episode and calculating the return GtG_tGt​ following the first visit to (St,At)(S_t, A_t)(St​,At​), we update our estimate Q(St,At)Q(S_t, A_t)Q(St​,At​) as follows:

  1. Increment the count: N(St,At)←N(St,At)+1N(S_t, A_t) \leftarrow N(S_t, A_t) + 1N(St​,At​)←N(St​,At​)+1
  2. Update the estimate: Q(St,At)←Q(St,At)+1N(St,At)(Gt−Q(St,At))Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \frac{1}{N(S_t, A_t)} (G_t - Q(S_t, A_t))Q(St​,At​)←Q(St​,At​)+N(St​,At​)1​(Gt​−Q(St​,At​))

This formula adjusts the current estimate Q(St,At)Q(S_t, A_t)Q(St​,At​) in the direction of the newly observed return GtG_tGt​. The step size 1N(St,At)\frac{1}{N(S_t, A_t)}N(St​,At​)1​ ensures that we are essentially calculating a running average; early returns have a larger impact, while later returns cause smaller adjustments as our estimate becomes more confident. Often, a constant step size α\alphaα is used instead of 1N(s,a)\frac{1}{N(s, a)}N(s,a)1​ for non-stationary problems (where the environment or policy might change):

Q(St,At)←Q(St,At)+α(Gt−Q(St,At))Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \alpha (G_t - Q(S_t, A_t))Q(St​,At​)←Q(St​,At​)+α(Gt​−Q(St​,At​))

where 0<α≤10 < \alpha \le 10<α≤1 is a learning rate parameter.

The Challenge of Exploration

Now we have a way to estimate Qπ(s,a)Q^\pi(s, a)Qπ(s,a), the action-values for a given policy π\piπ. But how do we use this to find the optimal policy π∗\pi^*π∗?

The natural inclination is to follow the pattern of Generalized Policy Iteration (GPI), which involves alternating between policy evaluation (estimating the value function for the current policy) and policy improvement (making the policy greedy with respect to the current value function estimates).

With MC methods, this would look like:

  1. Policy Evaluation: Use MC to estimate Qπ(s,a)Q^\pi(s, a)Qπ(s,a) by running many episodes under policy π\piπ and averaging returns for state-action pairs.
  2. Policy Improvement: Update the policy π\piπ to be greedy with respect to the estimated QQQ-values: π(s)←argmaxaQ(s,a)\pi(s) \leftarrow \underset{a}{\text{argmax}} Q(s, a)π(s)←aargmax​Q(s,a) for all sss.
  3. Repeat until the policy and value function stabilize.

However, there's a significant complication: exploration. If the policy π\piπ becomes deterministic and greedy early on (e.g., it always chooses action a1a_1a1​ from state sss), then we will only ever observe returns for the pair (s,a1)(s, a_1)(s,a1​). We will never gather experience (and thus never update the QQQ-value estimates) for other potentially better actions (like a2,a3,…a_2, a_3, \dotsa2​,a3​,…) from state sss. The agent might get stuck in a suboptimal loop because it never explores alternatives.

To guarantee that MC methods converge to the optimal policy, we need to ensure that all state-action pairs continue to be visited. This is known as maintaining exploration.

"One theoretical approach is the assumption of exploring starts. This assumes that every episode starts in a randomly chosen state-action pair (s,a)(s, a)(s,a), and only then follows the policy π\piπ. This ensures that every pair has a non-zero probability of being selected as the starting point, guaranteeing eventual exploration of all pairs. While useful for theoretical proofs, exploring starts are often not feasible in practical applications (e.g., you might not be able to arbitrarily set the initial state and action of a robot)."

Therefore, practical MC control algorithms need mechanisms to ensure ongoing exploration even without relying on exploring starts. This often involves using policies that are not fully deterministic or greedy, such as ϵ\epsilonϵ-greedy policies, which usually act greedily but sometimes (with a small probability ϵ\epsilonϵ) choose a random action. We will discuss these specific algorithms and the distinction between on-policy and off-policy learning, which relate directly to how exploration is handled, in the following sections.

  • Reinforcement Learning: An Introduction, Richard S. Sutton, Andrew G. Barto, 2018 (MIT Press) - The foundational textbook on Reinforcement Learning, providing comprehensive coverage of Monte Carlo methods for prediction and control.
  • CS234: Reinforcement Learning | Lecture Notes: Monte Carlo Methods, Emma Brunskill, 2019 (Stanford University) - Lecture notes from a prominent university course, covering Monte Carlo methods for prediction and control, including Q-value estimation and the exploration problem.
  • Spinning Up in Deep RL: Key Concepts in RL, Joshua Achiam and OpenAI, 2018 (OpenAI) - A clear tutorial from a leading AI research organization, providing an introduction to core reinforcement learning concepts, including Monte Carlo methods for value estimation.

© 2026 ApX Machine Learning