Learn › Part A: Foundations
1. The maths toolkit
Maths is just a way to be exact
Let us calm one thing down first. The maths in this course is not a wall to climb. It is a handful of tools, and every one of them is something you have met in school. A learning agent does very ordinary things: it keeps score, it works out averages, it asks "which way is better?", and it nudges its choices a little. Maths is simply the language for saying those things precisely, so that a computer can do them.
We will go through six tools. For each one you will see what it means in plain words, a small example worked by hand, and a hint of where it shows up later. You will not need to memorise anything. If you understand the example, you have the tool.
Tool 1: adding things up
Reinforcement learning is full of totals: total reward, total score, total number of wins. So we need a tidy way to write "add these up".
That tidy way is the Greek capital letter sigma, written , which just means "sum". Here is how to read it:
In words: "add up , with going from 1 up to 5". The little number underneath is where starts, and the one on top is where it stops. Here could stand for the reward you got on pull number .
A small example. Suppose five pulls gave the rewards 1, 0, 1, 1, 0. Then the sum is , and the average is the sum divided by how many there are: . You will do exactly this in the bandit chapter.
Here is a famous one for fun. What is ? A schoolboy named Gauss is said to have noticed that you can pair the numbers from the two ends: , then , then , and so on. Every pair makes 101, and there are 50 pairs, so the total is . For the first whole numbers the same trick gives
You may remember this as the sum of an arithmetic progression. Check it with : .
Tool 2: chance and expected value
Probability is a number between 0 and 1 that says how likely something is. A probability of 0 means impossible, and 1 means certain. A fair die shows each face with probability . The probabilities of everything that could happen always add up to 1: six faces, each , make .
Two rules will carry us a long way.
- Independent events multiply. Two events are independent when one does not affect the other, like two separate die rolls. The chance of rolling a six and then another six is , which is about 0.028.
- "At least one" is easiest through the opposite. What is the chance of at least one six in two rolls? Counting all the ways is messy. Instead, find the chance of no sixes, which is , and subtract it from 1: , about 0.306.
Now the star of the show: expected value. It is the long-run average of something random. Roll a die many times and average the results. What does the average head toward?
Weight each outcome by its probability, then add:
In general, if the outcomes are with probabilities , then
Notice that 3.5 is not a face of the die. You will never roll it. Expected value is not "what will happen". It is "what happens on average, over a very long run".
Let us use it for a decision. A slot machine costs 1 coin to play. It pays 5 coins with probability 0.1 and nothing otherwise. Is it worth playing? The expected result of one play is
On average you lose half a coin every play. Some plays you will win big, but over many plays the average wins. You will meet slot machines like this in the bandit chapter, where each machine's hidden "true value" is exactly its expected payout.
An average of a few random things can be far from the expected value, but an average of many settles down. We rolled a die 10,000 times on a computer. The average of the first 10 rolls was 2.8. The average of the first 100 was 3.62. The average of all 10,000 was about 3.50. The more you roll, the closer you get to 3.5. This is called the law of large numbers, and it is why running many attempts works.
Tool 3: a future that counts for less
In RL, a reward now is usually worth more than the same reward later. That is how people feel about it too: you would rather have 100 rupees today than 100 rupees next year. Later rewards are less certain, and it also encourages the agent to be quick.
We capture this with a discount factor, written (the Greek letter gamma), a number between 0 and 1. Let us use . A reward you get one step later counts for 0.9 of its value. Two steps later, it counts for . Three steps later, . Each step into the future multiplies by another 0.9.
Now suppose you get a reward of 1 every single step, forever. What is that whole stream worth? It is
This is a geometric series (the "GP" you know from school), and there is a lovely trick for adding it up. Multiply by :
Now subtract the second line from the first. Almost everything cancels, leaving just the 1:
With , that is . Even though the rewards go on forever, their total value is only 10, because each one counts for a bit less than the last. In fact you are nearly there already after 50 steps: the first 50 terms add up to , which is about 9.95.
So a discount factor of 0.9 means the agent effectively cares about the next 10 steps or so. A bigger , like 0.99, makes it far-sighted (its horizon is steps). A smaller one makes it short-sighted. We will use this in the chapter on Markov decision processes.
Tool 4: powers, e and logarithms
You know powers: , and powers add when you multiply, .
There is a special number, written , which is about 2.718. Like , it turns up naturally, in this case whenever something grows or shrinks in proportion to its own size. The function grows very fast as gets bigger, and shrinks quickly toward 0.
A logarithm is the opposite of a power. The natural logarithm, written , undoes :
Two rules are all we will use:
The first says that logarithms turn multiplication into addition. Let us see why that is handy. The chance of three heads in a row is . Taking logs, . A long chain of multiplied probabilities becomes a plain sum, which is far easier to work with. Here is another: .
Two more facts to keep: the log of a number between 0 and 1 is negative, and (it appears in the bandit chapter's UCB formula). Logarithms will come back when an agent learns to pick its actions with probabilities.
Tool 5: slopes
A function is a machine: put a number in, get a number out. For example, . Put in 3 and you get .
The slope of a curve tells you how steeply it is rising or falling. Between two points, the slope is "rise over run": how much it climbed, divided by how far across you went.
The derivative is the slope at a single point. Here is how you find it, by squeezing. Take the curve at , and measure the slope to a nearby point, making the gap smaller each time:
| Gap | Slope between and the nearby point |
|---|---|
| 1 | |
| 0.1 | |
| 0.01 |
The slopes head toward 6. So the slope of at is exactly 6. We write that . You know the shortcut from school: the derivative of is , so for it is , and . Constants disappear, and sums and constant multiples work term by term, so the derivative of is .
Try it yourself. Slide the point along the curves below and watch the slope.
Slope explorer
f(x) = x² − 4x + 5
x = 3.80 · height = 4.24 · slope = 3.60 · going up
Did you see the line go flat? On the parabola, that happens at , where the slope is . And that is the very lowest point, where . This is the maxima and minima idea from school: at the top of a hill or the bottom of a valley, the slope is zero. To find the lowest point, set the derivative to zero and solve.
Why does RL care? Because learning is largely about making a mistake smaller. The slope tells you which direction is "downhill". If you nudge your numbers a little way downhill, the mistake shrinks, and you repeat. That idea is called gradient descent, and it is the engine inside nearly all modern machine learning. We build it in a later chapter.
One last rule, the chain rule, for functions inside functions. Take . It is a square (the outer part) of (the inner part). Its derivative is the derivative of the outer part, evaluated at the inner part, times the derivative of the inner part:
At that is . Neural networks are long chains of functions inside functions, and the chain rule is how they work out how to adjust each piece.
Tool 6: lists and grids of numbers
A vector is just an ordered list of numbers, like . A computer agent might describe what it sees as a list of numbers, such as how far away the nearest opponent is.
The dot product of two vectors of the same length multiplies matching entries and adds them up:
It is a weighted sum: each input multiplied by how much it matters, then added. That is exactly what one artificial neuron does.
A matrix is a grid of numbers. To multiply a matrix by a vector, take the dot product of each row with the vector:
A whole layer of a neural network is little more than this, which is why computers do it so quickly. In Python, the symbol @ does it for you.
Putting the tools together
Let us finish with a small question that uses two of the tools at once. A lucky slot machine pays 1 coin with probability 0.7 on each pull. You pull it once every step, forever, and later rewards are discounted by . How much is that whole stream of pulls worth, on average?
Each pull is worth 0.7 on average (expected value, tool 2). The discounting turns the stream into a geometric series that adds up to a factor of 10 (tool 3). Since expected values add, the total is
That small calculation is the seed of an idea we will grow for several chapters: the value of a situation is the expected total of the discounted rewards that follow it.
Here is where each tool will come back:
| Tool | Where you will use it |
|---|---|
| Sums | everywhere: totals, averages, returns |
| Expected value | bandits, value functions, Q-learning |
| Geometric series | discounting, in Markov decision processes |
| Logarithms | exploration bonuses, policy gradients |
| Slopes and the chain rule | gradient descent and neural networks |
| Dot products and matrices | neural networks |
Run it yourself
Every example above is in a small Python file you can download at the bottom of this page.
toolkit_examples.pyruns each example and prints the results, including the dice-rolling experiment. It needs only NumPy.check_numbers.pychecks every number quoted in this chapter, so the text and the code cannot quietly disagree.
If you have not used Python before, do not worry. The next chapter gets you set up.
Check yourself
Work each one out on paper first.
Compute the sum of the squares of the first four whole numbers: 1² + 2² + 3² + 4².
It is .
A game costs 3 coins to play. It pays 10 coins with probability 0.2 and nothing otherwise. What is the expected profit per play?
The expected payout is . Subtract the cost of 3: the expected profit is . You lose one coin per play on average, so it is not worth playing.
With a discount factor γ = 0.5, what is 1 + γ + γ² + ...? What does a small γ like this say about the agent?
The sum is . A small makes the future count for very little, so the agent is short-sighted: it cares almost only about the next step or two.
Find the lowest point of f(x) = x² − 6x + 10.
The slope is . Set it to zero: gives . Then . So the lowest point is at , where the value is 1.
Compute the dot product of [2, 0, 1] and [3, 5, 4].
Multiply matching entries and add: .
Recap
- Sums () add things up, and an average is a sum divided by the count.
- Expected value is the long-run average, found by weighting each outcome by its probability. Expected values add. Averages of many trials settle near it.
- A discount factor makes later rewards count less, and the geometric series gives .
- Logarithms turn multiplication into addition, and undoes .
- The derivative is the slope at a point. It is zero at the bottom of a valley, and it tells you which way is downhill. The chain rule handles functions inside functions.
- A dot product is a weighted sum, and a matrix times a vector is a bunch of dot products.
Where next
You now have every maths tool the course needs. Next we get you running real code: a short, gentle tour of Python and NumPy, ending with your first experiment.
Code for this chapter
Run these on your own machine. Each one is short and commented.
- check_numbers.py 3.3 KB
- toolkit_examples.py 3.0 KB