Skip to content
AI-grafen
DAI developerMathematics· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

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.

∑i=14i2=12+22+32+42=30\sum_{i=1}^{4} i^2 = 1^2 + 2^2 + 3^2 + 4^2 = 30

PartMeans
iithe counter — the name of the thing being counted up
i=1i=1where it starts
44where it ends (inclusive)
i2i^2what is added up for each ii

Σ 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:

NotationMeans
∑i=1nxi\sum_{i=1}^{n} x_isum x1x_1 to xnx_n
∑ixi\sum_i x_isum over every ii that is understood from the context
∑x∈Sf(x)\sum_{x \in S} f(x)sum over every element of the set SS
∑i≠jaij\sum_{i \neq j} a_{ij}every pair except those where i=ji = j
∏i=1nxi\prod_{i=1}^{n} x_ithe same thing but multiply (capital pi)

Rules that get used all the time:

∑i(ai+bi)=∑iai+∑ibi,∑ic ai=c∑iai\sum_i (a_i + b_i) = \sum_i a_i + \sum_i b_i, \qquad \sum_i c\,a_i = c\sum_i a_i

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: ∑i=1nc=nc\sum_{i=1}^{n} c = nc.

Double indices. A matrix is summed over rows and columns:

∑i=1m∑j=1naij\sum_{i=1}^{m}\sum_{j=1}^{n} a_{ij}

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:

FormulaWhat it does
xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_ithe mean
MSE=1n∑i=1n(yi−y^i)2\mathrm{MSE} = \frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2the mean squared error
a⋅b=∑i=1naibi\mathbf{a}\cdot\mathbf{b} = \sum_{i=1}^{n} a_i b_ithe 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

All the sources and licences