Reading math notation as code
Why this 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.
- 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
Σ 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.