Reading math notation as code
Σ, subscripts, hats and bars, translated into Python you can run and test.
Why it matters
Papers, documentation and the rest of this phase write ideas as formulas: Σ, subscripts, hats and bars. Each symbol is shorthand for something you can already write in Python: a loop, an index into a list, a model's prediction, an average. This lesson is a phrasebook. You'll translate each piece of notation into code, then read whole formulas, like a model's loss, as small programs. After that, a formula is something you can run and test, not something to skim past.
What you'll be able to do
- Translate Σ and Π into a loop, or into sum() and a running product
- Read subscripts as positions in a list, allowing for math counting from 1
- Recognize hats, bars and common Greek letters: ŷ, x̄, μ, σ, α, θ
- Turn a whole formula, such as a model's loss, into Python and check it on tiny numbers
What it covers
- Σ means "add them all up"
- Subscripts are positions
- Hats, bars, stars and Greek letters
- The mean squared error, from its formula
- Functions and arrows
- Reading a whole formula
- Π: multiply them all
- The notation in this phase
Show the answer
10
Σ xᵢ adds up every xᵢ: 4 + 1 + 5 = 10. In Python, that's sum(x), or a loop with a running total.
Σ means "add them all up"
Σ is the Greek capital letter S, for sum. The full form names a counter and its range:
the sum of xᵢ, for i from 1 to n
is read "add up x-sub-i as i goes from 1 to n". The small letter under the Σ is the loop variable, and what follows the Σ is what you add on each pass. In Python it's a loop with a running total, or simply sum():
x = [4, 1, 5] total = 0 for i in range(len(x)): total += x[i] print(total, sum(x))
What follows the Σ can be any expression: Σ xᵢ² adds up the squares, and Σ (yᵢ − 3) subtracts 3 from each value before adding. Translate the expression for one item, then wrap it in the loop.
Two variations are common. A Σ inside another Σ is a loop inside a loop: Σᵢ Σⱼ Wᵢⱼ adds every cell of a table. And a Σ can run over a collection instead of a counter: "the sum over x in S" means for x in S, which is often the cleaner way to write it in Python too.
x = [2, 3, 4] total = 0 for i in range(len(x)): total += x[i] ** 2 print(total)
Show the answer
29
Each value is squared, then added: 4 + 9 + 16 = 29. The square is inside the Σ, so it happens to every item before the adding.
Subscripts are positions
A small letter or number written low after a name is a subscript: xᵢ is "the i-th x", and x₂ is the second one. In code, that's indexing: x[i]. Two subscripts mean a table: Wᵢⱼ is row i, column j, which is W[i][j].
One trap: math usually counts from 1, and Python counts from 0. On paper, x₁ is the first value; in Python, it's x[0]. When you translate, decide once which way you're counting and adjust every index together:
x = [10, 20, 30] print(x[0]) # x₁ on paper: the first value W = [[1, 2], [3, 4]] print(W[1][0]) # W₂₁ on paper: row 2, column 1
Subscripts like xᵢ₋₁ mean "the one before": x[i - 1]. That's how formulas describe things like the change from one day to the next, xᵢ − xᵢ₋₁.
Raised numbers need a second look. x² is a power, but x⁽ⁱ⁾, with brackets around the raised letter, usually labels the i-th example in a dataset rather than raising anything to a power. When in doubt, the surrounding text says which is meant.
A phrasebook in three groups: symbols that loop over many values, symbols that mark a special number, and symbols that compare.
Hats, bars, stars and Greek letters
Small marks on a letter change its meaning. A hat, ŷ ("y-hat"), means an estimate or a prediction: y is the true value and ŷ is what the model guessed. A bar, x̄ ("x-bar"), means the mean. A star, θ*, usually means the best value of something.
Greek letters are just names, and the same ones turn up again and again: μ (mu) for a mean, σ (sigma) for a standard deviation, α (alpha) for a learning rate, λ (lambda) for how strongly to regularize, and θ (theta) for a model's parameters, all its weights at once.
y = [3, 5, 2] y_hat = [2.5, 5, 3] # ŷ: the model's predictions y_bar = sum(y) / len(y) # ȳ: the mean of y, written with a bar print(y_bar) print([y[i] - y_hat[i] for i in range(len(y))]) # yᵢ − ŷᵢ, each error
In code you can't write the marks, so programmers spell them out: y_hat, x_bar, theta_best. Spelling them out consistently makes your code read like the formula it came from.
Show the answer
The mean and the standard deviation
μ (mu) is the usual name for a mean and σ (sigma) for a standard deviation: values centered on 52, typically about 4 away.
The mean squared error, from its formula
The mean squared error is MSE = (1/n) Σ (yᵢ − ŷᵢ)². Translate it: loop over the positions, add up each squared error, then multiply by 1/n. Set mse.
y = [3, 5, 2, 8] # the true values y_hat = [2.5, 5, 3, 7] # the model's predictions, ŷ n = len(y) mse = 0 print(mse)
Functions and arrows
f(x) = 2x + 1 is a function: put x in, get 2x + 1 out. In Python it's a def with one line. A line like f: ℝ → ℝ just says what goes in and what comes out (ℝ means "any real number"), the way a type hint does.
argmax is worth knowing early: argmax f(x) is the input that makes f biggest, not the biggest value itself. A classifier's prediction is the argmax of its scores, the class with the highest score. A few more symbols complete the everyday set: ∈ means "is in" (Python's in), ∝ means "is proportional to", and := or ≜ mean "is defined as":
def f(x): return 2 * x + 1 print(f(3)) scores = {"cat": 0.2, "dog": 0.7, "bird": 0.1} print(max(scores, key=scores.get)) # argmax: the class, not the score
Show the answer
The + b sits outside the Σ, so it's added once
In ŷ = Σ wᵢ xᵢ + b, only wᵢ xᵢ is inside the Σ. The bias is added once, so starting the total at b (or adding it after the loop) matches the formula exactly.
Show the answer
y = 3 * x ** 2 + 1
3x² means 3 times x squared. Powers come before multiplying, so 3 * x ** 2 squares x first, exactly as written on paper.
Reading a whole formula
A formula you've never seen becomes manageable in four steps:
- Name every symbol. What's a list, what's a single number, what's the counter?
- Find the loops. Each Σ or Π is a loop; nested ones are nested loops.
- Find what's done to each item, inside the loop, and what's done after it.
- Write it, then test it on tiny numbers you can check in your head.
Here's softmax, which you'll meet properly later: softmax(z)ᵢ = e^(zᵢ) / Σⱼ e^(zⱼ). Name the parts: z is a list of scores, e^ is math.exp, and the Σ over j adds up every score's exponential. So each output is one score's exponential divided by the total of all of them:
import math z = [2.0, 1.0, 0.1] total = sum(math.exp(v) for v in z) print([round(math.exp(v) / total, 3) for v in z])
The outputs add up to 1, which is a quick sanity check worth running on any formula that's meant to give probabilities.
import math z = [0.0, 0.0] print(math.exp(z[0]) / sum(math.exp(v) for v in z))
Show the answer
0.5
e⁰ is 1 for both scores, so the first one gets 1 / (1 + 1) = 0.5. Equal scores split the probability equally, which is a good tiny test of the formula.
Π: multiply them all
Π, a capital pi, means "multiply these together". Three independent steps each succeed with chance 0.9, 0.8 and 0.5. The chance that all three succeed is Π pᵢ. Set p_all with a running product (start it at 1, not 0).
p = [0.9, 0.8, 0.5] p_all = 0 # the product of every p[i] print(p_all)
The notation in this phase
Each lesson in this phase introduces its own symbols with code next to them: bold letters like v for vectors, ‖v‖ for a vector's length, u · v for a dot product, ∂ for a partial derivative. When you meet a new one, use this lesson's method: name it, find the loop, write it in Python, and try it on tiny numbers. Keep a short phrasebook of your own as you go: symbol, how to say it, and the line of Python it stands for.
Authors use notation because it's short, not because the idea is hard. Most formulas in this course turn into five lines of Python or fewer, and running those lines on tiny numbers is the fastest way to understand them.
Show the answer
The length of the vector v
Double bars around a vector mean its length, also called its norm: how long the arrow is. Single bars, |x|, are the absolute value of a number.
Practice what you just read
- 5 code examples you can edit and run
- a practice exercise that checks your code
- a 6-question quiz
- 5 review cards for spaced repetition
Try a quiz question
What does x̄ (x with a bar over it) usually mean?