Mathematical Prerequisites for Reinforcement Learning
Building the math foundations you need for RL — probability, expected value, derivatives, the log trick, and Monte Carlo estimation — all through one consistent example.
Before diving into Reinforcement Learning, you need a handful of mathematical ideas. This post builds every one of them inside a single, consistent example so nothing feels abstract or disconnected. By the end, you will have all the tools required to derive the core RL algorithm from scratch in Part 2.
The Running Example
Imagine a coin that is not fair. It lands Heads with probability and Tails with probability . If it lands Heads you win ₹10, and if it lands Tails you win ₹0. We will derive every mathematical concept in this post from this single setup.
Probability Distributions
A probability distribution assigns a probability to every possible outcome. For our coin, the distribution says that Heads occurs with probability and Tails occurs with probability .
Two rules must hold for any valid distribution. First, every probability must be between 0 and 1. Second, all probabilities must sum to 1, which ours do: . That is the entire definition of a probability distribution — nothing more. It is simply a table (or function) that tells you how likely each outcome is.
Expected Value
Expected value answers a natural question: on average, how much do I win per toss? The formula multiplies each possible value by its probability, then sums everything up:
For our coin, this becomes:
If , then . Notice that you never actually win ₹3 on any single toss — you either win ₹10 or ₹0. The expected value is a theoretical quantity: it tells you what your average winnings per toss would converge to if you kept tossing the coin thousands of times. That convergence is not a vague intuition — it is a mathematical guarantee we will see later in this post.
The Sigmoid Function
We need a way to turn any real number into a probability, meaning a number between 0 and 1. The sigmoid function does exactly this:
When is a very large negative number, becomes enormous and approaches 0. When , we get . And when is large and positive, vanishes and approaches 1.
:
:
:
Now we can connect this to our coin. If we define , then becomes a knob that controls the coin’s bias. Setting gives a fair coin with . Cranking to a large positive value makes the coin land Heads almost every time. Pushing far negative makes it almost always land Tails. The sigmoid simply converts an unconstrained real number into a valid probability.
Parameterised Expected Value
Since , our expected value now becomes a function of :
We give it the name because in RL this will become the objective function — the quantity we want to maximise. If we want to maximise our winnings, we need as large as possible, which means pushing towards 1, which means making large and positive.
But here is the question: how do we know which direction to push ? In this toy example the answer is obvious — just increase it. But in real problems with millions of parameters, you cannot eyeball the right direction. You need a principled tool, and that tool is the derivative.
Derivatives
The derivative of with respect to answers a precise question: if I nudge up by a tiny amount, does increase or decrease, and by how much? We write this as:
The derivative of the sigmoid function is a known result from calculus:
Since , we get:
Because always lies between 0 and 1, both and are positive, so the derivative is always positive. This confirms what we expected: increasing always increases .
Numerical check
At , the derivative is . At , we have , so the derivative is approximately . The derivative gets smaller as approaches 1, which makes geometric sense — the sigmoid curve is steepest in the middle and flattens out at the extremes.
The Log Trick
This is the key algebraic move that makes Reinforcement Learning work. It looks like a minor rearrangement, but it is what transforms an intractable sum into something we can estimate from samples. Let’s derive it carefully using our coin example.
Setup
We have the expected value defined as:
Taking the derivative directly:
The trick
Now we do something that seems pointless at first: take and multiply-and-divide by itself.
Why would we do this? Because from calculus, the expression is exactly the derivative of :
This is sometimes called the “chain rule applied to the logarithm,” and it is one of those identities that shows up everywhere once you know to look for it. Substituting this identity gives us:
Plugging this back into our derivative of :
Why this matters
Look at the structure of that expression. We have a probability multiplied by some quantity. That is precisely the form of an expectation — we can rewrite it as:
This is the crucial insight. The derivative of the objective is itself an expected value. And expected values can be estimated by sampling — you don’t need to enumerate every possible outcome. You just run the experiment many times and average. In our coin example with two outcomes, the enumeration is trivial. But in Reinforcement Learning, where the “outcomes” are entire game trajectories with billions of possibilities, this conversion from “sum over all outcomes” to “average over samples” is what makes the problem tractable at all.
Maximum Likelihood: A Different Objective
What is maximum likelihood in general?
Suppose you have data. For example, you observe that for the prompt “2 + 3 = ?” the correct answer is “5.” You want your model to assign high probability to that correct answer. Maximum likelihood means: choose parameters that maximize the probability of observed correct answers. Formally:
That is the entire idea — push probability mass toward the correct answer.
What is that in our coin setting?
In our running example, we can simplify things further. Instead of caring about exact sequence tokens, we reduce everything to success vs failure:
- If the coin lands Heads → success
- If the coin lands Tails → failure
So:
Maximum likelihood becomes:
ML here literally means: increase the log of the success probability. Nothing more.
Why log?
Two reasons. First, in statistics, likelihood multiplies probabilities across examples, and log turns products into sums — a computational convenience. But for intuition here, the important part is:
The log creates a scaling factor of . That is what changes behavior.
Intuitive difference between RL and ML
Let’s compare behavior. Suppose two prompts:
- Prompt A:
- Prompt B:
RL gradient. RL computes , so both prompts get the same gradient weight.
ML gradient. ML computes , so the effective weight is :
- Prompt A:
- Prompt B:
ML gives 8× stronger gradient to the hard prompt.
RL says: improve everything equally.
ML says: focus heavily on the hard prompts.
That is the intuitive difference.
Why this feels different from RL
RL optimizes . ML optimizes . These are mathematically different objectives — even though they both increase . The difference is in how strongly they push when is small.
Think of it like this. If a model almost never solves a problem ( small):
- RL gives it normal effort.
- ML says: “This is very wrong — fix this aggressively.”
In Part 3 (MaxRL: From REINFORCE to Maximum Likelihood), we will see that RL and ML are actually two endpoints of a single spectrum, connected through a surprising identity involving pass@ and the power series for .
Monte Carlo Estimation
The problem
When , we can compute directly. But what if we don’t know ? Or what if the number of possible outcomes is enormous — say, all possible games of chess, or all possible conversations a chatbot could have? Then you cannot compute the expected value directly because the sum has too many terms, or the probabilities themselves are unknown.
The solution: sample and average
The idea is beautifully simple. Instead of computing the theoretical average, you run the experiment many times and take the empirical average. Concretely, you toss the coin times, record the winnings , and compute:
This is the Monte Carlo estimator. The name comes from the famous casino in Monaco — the idea is that you are “gambling” to estimate a quantity, relying on randomness to give you an answer.
Concrete example
Suppose . You toss the coin 10 times and get:
Toss 1: Tails → ₹0,
Toss 2: Heads → ₹10,
Toss 3: Tails → ₹0,
Toss 4: Tails → ₹0,
Toss 5: Heads → ₹10,
Toss 6: Tails → ₹0,
Toss 7: Tails → ₹0,
Toss 8: Tails → ₹0,
Toss 9: Heads → ₹10,
Toss 10: Tails → ₹0
That is 3 Heads and 7 Tails — a total of ₹30.
The average is . With just 10 tosses we happened to land exactly on the true value, but usually you won’t be this lucky. The estimate might come out to 2 or 4 on any given run. The point is that as you increase the number of samples, the estimate becomes increasingly reliable.
Why does this work?
The theoretical justification is the Law of Large Numbers, one of the foundational results in probability theory. It guarantees that:
In plain language: as you take more and more samples, the sample average converges to the true expected value. This is not an RL-specific result — it applies to any random process, from coin tosses to stock prices to weather measurements.
Unbiasedness
The Monte Carlo estimator has a stronger property called unbiasedness, which means that the expected value of the estimator equals the true expected value:
The proof is short and uses only basic properties of expectation. Start by writing out the estimator and taking its expectation:
The first equality uses linearity of expectation — the expectation of a sum equals the sum of expectations, regardless of whether the terms are independent. The second equality uses the fact that each is drawn from the same distribution, so for every . That “same distribution” assumption is so important that it deserves its own section.
A second numerical example
Suppose there are only two possible outcomes: outcome A gives value 5 with probability 0.2, and outcome B gives value 1 with probability 0.8. The true expectation is . If you sample many times, roughly 20% of your samples will be 5 and 80% will be 1, and the average will converge to 1.8. That’s Monte Carlo estimation — replacing a theoretical weighted sum with an empirical average.
”Sampled from the Same Distribution”
This phrase appears constantly in RL proofs, and it is easy to gloss over. But the entire mathematical machinery we just built depends on it, so let’s pin down exactly what it means.
When we proved unbiasedness, the key step was writing for all . This is only true if every sample comes from the same probabilistic rule. In our coin example, that means every toss uses the same coin with the same probability . If you secretly swapped the coin between tosses — sometimes using one with and other times one with — then your sample average would not converge to the expected value of either coin. It would converge to some muddled mixture that doesn’t correspond to any well-defined quantity.
Here’s a helpful analogy. Suppose you want to estimate the average height of students in Class A. You measure 5 students from Class A and 5 students from Class B, then average all 10 measurements. That average is not the mean height of Class A, because the samples came from different populations. For the estimate to be valid, all samples must come from one consistent source.
This will matter enormously in Reinforcement Learning. The “coin” in RL is the policy — the rule that decides which actions to take. When we sample trajectories to estimate the gradient, all trajectories must come from the same policy. If we update the policy between samples, we are effectively swapping the coin mid-experiment, and the mathematical guarantees break down. This is why the RL training loop has a very specific structure: fix the policy, sample a batch of trajectories, compute the gradient estimate, then update the policy. The update only happens after all samples for that batch have been collected.
Summary
Everything in this post was built from one coin-toss example. A probability distribution assigns probabilities to outcomes. The expected value computes the weighted average . The sigmoid maps any real number to a probability, which lets us define a parameterised objective — the expected value as a function of parameters. The derivative tells us how changes when changes, giving us a direction to improve. The log trick — the identity — converts that derivative into an expectation. Maximum likelihood offers an alternative objective — maximizing instead of — whose gradient weights each example by , focusing learning aggressively on hard problems where the model currently struggles. This distinction between the RL objective and the ML objective will become central in Part 3. Finally, Monte Carlo estimation lets us approximate expected values by sampling and averaging, with the key guarantee of unbiasedness: on average, the sample estimate equals the truth.
With these tools in hand, we’re ready for Reinforcement Learning from Scratch, where we put them all together to derive the REINFORCE algorithm.
Enjoyed this post?
Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.