The summation sign and indices
Be able to read and write expressions with Σ and double indices — the notation behind nearly every ML formula.
Prerequisites
Intuition
Σ (capital sigma) simply means «add up». Everything around it says what is to be added up and how many times.
| Part | Means |
|---|---|
| the counter — the name of the thing being counted up | |
| where it starts | |
| where it ends (inclusive) | |
| what is added up for each |
Σ is a for loop. That is the whole secret:
total = 0
for i in range(1, 5): # 1 up to and including 4
total += i ** 2
When you see a formula with Σ in a paper — read it as a loop, and it immediately becomes less frightening.
Formal
Common variants you will run into:
| Notation | Means |
|---|---|
| sum to | |
| sum over every that is understood from the context | |
| sum over every element of the set | |
| every pair except those where | |
| the same thing but multiply (capital pi) |
Rules that get used all the time:
A constant that does not depend on the counter can be taken outside. A constant inside the sum gets multiplied by the number of terms: .
Double indices. A matrix is summed over rows and columns:
The inner sum runs to completion for every value of the outer one — exactly like nested loops. If the limits are independent of each other, the order may be swapped freely.
Three formulas you will recognise if you read ML:
| Formula | What it does |
|---|---|
| the mean | |
| the mean squared error | |
| the dot product |
All three are «add something up for every data point and divide by the count». Once you have seen that, the notation is no longer in the way.
Code
# Σ and a for loop are the same thing
total = 0
for i in range(1, 5):
total += i ** 2
print(total) # 30
print(sum(i ** 2 for i in range(1, 5))) # 30 — the same thing, shorter
# The mean: (1/n) Σ x_i
x = [3, 7, 7, 2, 11]
print(sum(x) / len(x)) # 6.0
# The dot product: Σ a_i · b_i
a, b = [1, 2, 3], [4, 5, 6]
print(sum(ai * bi for ai, bi in zip(a, b))) # 32
# Double indices: ΣΣ a_ij — nested loops
A = [[1, 2, 3],
[4, 5, 6]]
print(sum(A[i][j] for i in range(2) for j in range(3))) # 21
# The order does not matter when the limits are independent
print(sum(sum(row) for row in A)) # 21 — row by row
print(sum(sum(A[i][j] for i in range(2)) for j in range(3))) # 21 — column by column
# MSE — the formula you will see most often of all
y = [3.0, 5.0, 2.0, 8.0]
y_hat = [2.5, 5.5, 2.0, 7.0]
mse = sum((yi - hi) ** 2 for yi, hi in zip(y, y_hat)) / len(y)
print(round(mse, 4)) # 0.375
Mastery means
- Reads and writes sums with Σ
- Handles double indices and swaps their order
- Translates between Σ notation and a loop
Sign in to do the exercises and build your mastery up.
Sources
- Matteboken (Mattecentrum) — free to read, non-profit association
- Khan Academy — matematik — CC BY-NC-SA 3.0
- Wikipedia — Summation (CC BY-SA 4.0) — CC BY-SA 4.0