Part I · Maths · Chapter 2
Algebra, functions and graphs
An AI model is a function: numbers go in, numbers come out. This chapter rebuilds the school algebra you need to read every formula in the rest of the book.
Every model in this book, from a straight line to a large language model, is a function with adjustable numbers inside it. Training means choosing those numbers well. If you can read y = mx + b and say what each letter does, you can read the rest.
Letters that stand for numbers
Start with something you already do. A coffee costs 3 dollars. Two coffees cost 3 × 2 = 6 dollars. Five coffees cost 3 × 5 = 15 dollars. You are repeating one rule with different numbers.
Algebra writes the rule once, with a letter in place of the number that changes:
The letter is a variable: a name for a number we do not want to fix yet. Mathematicians usually drop the × sign between a number and a letter, so 3n means 3 times n.
The order of operations
In 2 + 3 × 4 the multiplication happens first, so the answer is 2 + 12 = 14, not 20. The order is: brackets first, then powers, then multiply and divide, then add and subtract. When in doubt, add brackets: (2 + 3) × 4 = 20.
Powers, roots and negatives
A power is repeated multiplication. 52 ("five squared") is 5 × 5 = 25. 23 is 2 × 2 × 2 = 8. A square root undoes squaring: √25 = 5 because 5 × 5 = 25.
Negative numbers follow two rules worth memorising. A negative times a negative is positive: (−3) × (−4) = 12. So any number squared is never negative: (−3)2 = 9. That fact matters later, because squared errors are always zero or more.
Fractions, decimals and percentages
Money questions are full of these. 7% means 7 per hundred, so 7% = 7 / 100 = 0.07. To take 7% of 2,000, multiply: 2,000 × 0.07 = 140. To grow 2,000 by 7%, multiply by 1.07: 2,000 × 1.07 = 2,140. Remember "multiply by 1 + rate"; chapter 3 builds compound growth on it.
A fund is worth 1,500 and falls by 4%. What is it worth now?
Answer
Falling by 4% means multiplying by 1 − 0.04 = 0.96. So 1,500 × 0.96 = 1,440.
Solving equations
An equation says two things are equal. Solving it means finding the value of the variable that makes it true.
Worked example. Solve 2x + 3 = 11.
- Subtract 3 from both sides: 2x = 8.
- Divide both sides by 2: x = 4.
- Check by putting it back: 2 × 4 + 3 = 11. Correct.
The one rule: whatever you do to one side, do to the other, and the balance stays true. That is all of school algebra in one sentence.
Solve 5x − 7 = 18. Then solve x / 4 + 1 = 3.
Answer
First: add 7 to get 5x = 25, divide by 5 to get x = 5. Second: subtract 1 to get x / 4 = 2, multiply by 4 to get x = 8.
Functions: machines with one output
A function is a rule that takes an input and gives exactly one output. Picture a machine: drop in a number, get a number out.
Take the machine "double it, then add one". Feed it 3: double is 6, add one is 7. Feed it 0: you get 1. Feed it −2: you get −3. We write the machine as
So f(3) = 7, f(0) = 1 and f(−2) = −3. A table of inputs and outputs is a perfectly good way to describe a function:
| x | −2 | −1 | 0 | 1 | 2 | 3 |
|---|---|---|---|---|---|---|
| f(x) = 2x + 1 | −3 | −1 | 1 | 3 | 5 | 7 |
"Exactly one output" is the important part. The same input always gives the same output. (Language models break this on purpose by sampling; chapter 16 explains how.)
Graphs
A graph draws a function. The horizontal axis is the input x; the vertical axis is the output y. Each row of the table becomes a point: (0, 1), (1, 3), (2, 5) and so on. Join the points and you see the function's shape at a glance.
For f(x) = 2x + 1 the points lie on a straight line. Functions whose graphs are straight lines are called linear.
Straight lines: slope and intercept
Every straight line can be written as
Slope is "rise over run": how far the line goes up for each step to the right. Worked example with the line through (1, 3) and (3, 7):
- Run: from x = 1 to x = 3 is 3 − 1 = 2.
- Rise: from y = 3 to y = 7 is 7 − 3 = 4.
- Slope: rise / run = 4 / 2 = 2.
- Intercept: at x = 1, y = 3, so 3 = 2 × 1 + b, giving b = 1. The line is y = 2x + 1, our machine from before.
A positive slope goes up to the right. A negative slope goes down. A slope of zero is flat. Slope is the single most important idea in Part I: chapter 5 extends it to curves, and chapter 6 uses it to train models.
A line passes through (2, 1) and (6, 9). Find its slope and intercept.
Answer
Run = 6 − 2 = 4, rise = 9 − 1 = 8, slope = 8 / 4 = 2. Then 1 = 2 × 2 + b, so b = −3. The line is y = 2x − 3.
Functions of several inputs
Real functions often take several inputs. A flat's rent might be estimated as
For 60 square metres and 2 bedrooms: 400 + 25 × 60 + 150 × 2 = 400 + 1,500 + 300 = 2,200.
The numbers 25 and 150 are weights: how much each input counts. 400 is a starting amount, which machine learning calls the bias. With inputs x1, x2 and weights w1, w2 it reads
Hold this shape in mind. A single neuron in a neural network computes exactly this, then bends the result with an activation function (chapter 12).
Adding many things: the Σ sign
With 384 inputs, writing every term is hopeless. The Greek capital sigma, Σ, means "add these up". Worked example: the weights are 2, 3 and 1; the inputs are 4, 0 and 5.
Term by term: 2 × 4 = 8, 3 × 0 = 0, 1 × 5 = 5. Total 8 + 0 + 5 = 13. In symbols:
This "multiply pairs and add" is called the dot product. It is the most common calculation in all of AI, and chapter 4 gives it a picture.
Work out Σi=14 i2.
Answer
1 + 4 + 9 + 16 = 30.
Curves and composition
Not every function is a straight line. g(x) = x2 makes a U-shaped curve (a parabola): g(−2) = 4, g(0) = 0, g(3) = 9. Its lowest point is at x = 0. Finding the lowest point of a curve is what training a model means, so the parabola will come back many times.
Functions can be chained: feed one machine's output into another. If f(x) = 2x + 1 and g(x) = x2, then g(f(3)) means: first f(3) = 7, then g(7) = 49. This is called composition. A neural network is a long composition: layer after layer, each feeding the next. The chain rule tells us how to find slopes through such chains.
With the same f and g, find f(g(3)). Is it the same as g(f(3))?
Answer
g(3) = 9, then f(9) = 19. It is not 49: the order of the machines matters.
In AI
A model is a function whose rule contains adjustable numbers called parameters. The simplest model is a line, y = wx + b, with two parameters w and b. A large language model is also a function: its input is a list of token ids and its output is a probability for every possible next token. It just has billions of parameters instead of two. Learning means adjusting the parameters until the function's outputs match the data, which is the subject of Part II.
Plain functions with no learned parameters are the safe choice for money figures. The rule can be read and checked, and the same input always gives the same output. A months-of-cover calculator is one line of algebra: months_covered = cash ÷ monthly_expenses. With 12,000 in cash and 3,000 a month of expenses it returns 4 months, and it refuses expenses of zero, because dividing by zero has no answer.
A language model should not do this arithmetic in its head. A well-built system lets the model call such a function as a tool and quote the result (chapter 28 explains why).
In Lab 5 you give a model small plain functions as tools and watch it call them instead of guessing the arithmetic.
Self-check
What do m and b mean in y = mx + b?
m is the slope: how much y rises for each step of 1 in x. b is the intercept: the value of y when x is 0.
Why is a squared number never negative?
Because a positive times a positive is positive, and a negative times a negative is also positive. Only 0 squared is 0.
What does Σi=13 wixi compute for w = (1, 2, 3) and x = (3, 2, 1)?
1 × 3 + 2 × 2 + 3 × 1 = 3 + 4 + 3 = 10.
In what sense is a neural network a composition of functions?
Each layer is a function of the previous layer's output. The whole network is the first layer's function, fed into the second, fed into the third, and so on: f3(f2(f1(x))).
Further reading
- Linear equations & graphs (Khan Academy, Algebra 1) video beginner
Short lessons plus practice on slope, intercepts and plotting straight lines, the exact groundwork this chapter needs. - Functions (Khan Academy, Algebra 1) video beginner
Builds the idea of a function as an input-to-output rule, with notation, domain and range, and graded exercises. - Equation of a Straight Line article beginner
One-page walk through y = mx + b with an interactive graph, handy as a quick refresher on slope and intercept. - What is a Function article beginner
Plain-language picture of functions as machines that map inputs to outputs, good before reading any ML notation.
Free, checked on 2026-10-21. All resources for this chapter.