Index Ai Academy
Phase 2 · Math, when you need it · Reading math

Reading math notation as code

Σ, subscripts, hats and bars, translated into Python you can run and test.

About 25 minutes · Free lesson

Start this lesson

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

  1. Σ means "add them all up"
  2. Subscripts are positions
  3. Hats, bars, stars and Greek letters
  4. The mean squared error, from its formula
  5. Functions and arrows
  6. Reading a whole formula
  7. Π: multiply them all
  8. The notation in this phase
Quick check
Before any explaining, a guess. The symbol Σ, a capital Greek sigma, means "add these up". With x = [4, 1, 5], what is Σ xᵢ?
  1. 10
  2. 45
  3. 20
  4. 3
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))
It runs right here in your browser, with nothing to install.

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.

Quick check
On paper: Σ xᵢ², with x = [2, 3, 4]. This code computes it. What does it print?
x = [2, 3, 4]
total = 0
for i in range(len(x)):
    total += x[i] ** 2
print(total)
  1. 9
  2. 18
  3. 29
  4. 81
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.

Try it now

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.

Quick check
A paper reports a dataset with μ = 52 and σ = 4. What are they most likely to be?
  1. Two predictions made by a model
  2. A learning rate and a batch size
  3. The mean and the standard deviation
  4. The number of rows and columns
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.

Drill

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)
In the lesson, your code is checked as you write it.

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
Quick check
In that program, why does the total start at b instead of 0?
  1. The + b sits outside the Σ, so it's added once
  2. Python requires every total to start at b
  3. To make the loop run one extra time
  4. b has to be the first value of the list x
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.

Quick check
On paper: y = 3x² + 1. As in the formulas lesson, which line of Python matches it?
  1. y = 3x2 + 1
  2. y = (3 * x) ** 2 + 1
  3. y = 3 * x * 2 + 1
  4. y = 3 * x ** 2 + 1
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:

  1. Name every symbol. What's a list, what's a single number, what's the counter?
  2. Find the loops. Each Σ or Π is a loop; nested ones are nested loops.
  3. Find what's done to each item, inside the loop, and what's done after it.
  4. 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.

Quick check
With two equal scores, what should softmax give for the first one? What does this print?
import math
z = [0.0, 0.0]
print(math.exp(z[0]) / sum(math.exp(v) for v in z))
  1. 0.0
  2. 0.5
  3. 1.0
  4. 2.0
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.

Drill

Π: 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)
In the lesson, your code is checked as you write it.

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.

Quick check
In a lesson about vectors, what does ‖v‖ most likely mean?
  1. The first element of the vector v
  2. The absolute value of each part of v
  3. The vector v, written twice
  4. The length of the vector v
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

Start this lesson

Try a quiz question

What does x̄ (x with a bar over it) usually mean?

  1. The mean of x
  2. A prediction of x
  3. The largest value of x
  4. x multiplied by itself

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson