Almost every ML formula, from a loss to a matrix product, is a sum or a product over indices, so reading Σ and Π fluently is how you read papers and turn formulas into code.
Mean squared error is a sum over training examples, divided by how many there are. (P3.A2: Linear regression, coming soon)
Read a sum written with Σ (index, limits, expression) and unroll it into a row of additions; do the same for a product written with Π
Count the terms of a sum (from a to b gives b − a + 1) and say what an empty sum and an empty product equal
Use the sum rules (pull out a constant, split a sum of sums, sum of a constant) and avoid the two false ones with numbers
Evaluate a double sum as a grid, swap its order, and write the mean, a weighted sum, mean squared error and a dot product in Σ notation
Translate Σ and Π into Python and NumPy without an off-by-one error
Here is a formula you will meet in your first ML course:
MSE=N1i=1∑N(y^i−yi)2
It looks scary. It is not. It says: “for each example, take the error, square it, add all of those up, then divide by how many examples there are.” The strange symbol ∑ (a capital Greek sigma) just means add up a list of things. By the end of this lesson you can read it, unroll it into plain additions, and write it in Python.
You already know the key link from P1.A2: the log of a product is the sum of the logs. Sums and products written with symbols are what that rule is about, so we meet both here.
A sum is a loop that adds
Start with plain numbers. Add the whole numbers from 1 to 4:
1+2+3+4=10
Writing a long list of additions is tiring, so mathematicians invented a short form. The short form for “add i, where i goes from 1 to 4” is
i=1∑4i
A sum has five parts:
∑: the sum sign. It means “add”.
The indexi (the letter under the sigma): a counter. It takes the values 1,2,3,… one at a time.
The lower limit (under the sigma, here i=1): where the counter starts.
The upper limit (above the sigma, here 4): where the counter stops. The counter takes every whole number from the lower limit to the upper limit, including both.
The expression (to the right, here just i): what you compute for each value of the counter. Each result is a term.
You can read it aloud as: “the sum, for i from 1 to 4, of i.”
Now the expression can be more than i. Take ∑i=14i2. Put i=1,2,3,4 into i2 and add:
i=1∑4i2=12+22+32+42=1+4+9+16=30
Writing the sum out as a row of additions is called unrolling it. Unrolling is the best way to understand any sum: whenever a Σ confuses you, write out the first few terms.
Unroll a sum, one term at a time
4Σi = 1i²
Read aloud: the sum of i², for i from 1 to 4.
Unrolled: 4 terms (4 − 1 + 1 = 4).
i = 11² = 1
+
i = 22² = 4
+
i = 33² = 9
+
i = 44² = 16
Running total: 0 (nothing added yet)
Σ = ? (finish the steps)
Pick an expression and set the lower and upper limits with the sliders. The widget shows the Σ, how to read it aloud, and the unrolled row of terms with their count. Press Step (or Play) to add the terms one at a time: the current term is outlined in blue, the finished ones in green, and the running total is shown below. Set the upper limit below the lower limit to see an empty sum. For the two data choices, x₁ to x₅ are 2, 5, 3, 6, 4.
Indices: subscripts and counting terms
In ML we often have a list of numbers, for example the heights of five people. Instead of giving each its own letter, we use one letter with a subscript (a small number below the line):
x1=2,x2=5,x3=3,x4=6,x5=4
The subscript i in xi is the index: it says which one we mean, “the i-th”. Now a sum over data is natural. Add all five:
i=1∑5xi=x1+x2+x3+x4+x5=2+5+3+6+4=20
How many terms?
From i=a to i=b (both included) there are
b−a+1 terms
Check: i=3 to 7 gives 3,4,5,6,7, which is 7−3+1=5 terms, and ∑i=37i=3+4+5+6+7=25.. The “+ 1” is the part people forget (a fence with 5 sections needs 6 posts).
Empty sums
What if the upper limit is smaller than the lower limit, like ∑i=10xi? Then there are 0−1+1=0 terms. (The count formula b−a+1 works down to b=a−1; if b is even smaller, the sum is still empty.) Nothing is added, so the result is 0. This is called an empty sum, and it equals 0 because 0 is the starting value when you add (adding nothing changes nothing). It is useful: formulas keep working at their edge case, such as a data set with no examples yet.
Changing the limits and the index
Sometimes you want to shift where the counting starts. These two sums are the same list of terms:
i=1∑5xi=j=0∑4xj+1
On the right, j runs from 0 to 4, so xj+1 is x1,x2,…,x5. We re-labelled the index (i=j+1) and moved the limits by the same amount. Both sides equal 20. This is “index juggling”: the rule is that the list of terms must stay the same.
A trap: math starts at 1, Python starts at 0
In math we usually count from 1: x1 is the first number. In Python, a list starts at index 0: x[0] is the first number. So xi is x[i - 1]. And a Python slice x[a:b]stops before index b. Both together are a famous source of off-by-one bugs. For x=[2,5,3,6,4]: the math sum ∑i=24xi=5+3+6=14 is sum(x[1:4]) in Python, not sum(x[2:4]) (which is 9).
Math
Python
x1 (the first)
x[0]
xi
x[i - 1]
∑i=1nxi
sum(x[0:n]), or sum(x) when the list has n entries
∑i=abxi
sum(x[a-1:b])
i=1,…,n
range(1, n + 1)
number of terms b−a+1
len(x[a-1:b])
Many ML papers and libraries (PyTorch, NumPy) are 0-based inside code but write 1-based formulas. Always check which one a formula uses.
Rules for sums
Because a sum is just a long addition, the usual rules of arithmetic apply. We check each one with numbers from the earlier data and from two short lists x=(1,2,3) and y=(4,5,6).
1. A constant factor comes out
i=1∑ncxi=ci=1∑nxi
Why: cx1+cx2+⋯+cxn=c(x1+x2+⋯+xn) (taking out a common factor). Check with c=2 and x=(1,2,3):2+4+6=12=2×6.
2. A sum of sums splits
i=1∑n(xi+yi)=i=1∑nxi+i=1∑nyi
Why: adding in a different order does not change the total. Check: (1+4)+(2+5)+(3+6)=21=6+15.. Rules 1 and 2 together say that Σ is linear: ∑(cxi+dyi)=c∑xi+d∑yi..
3. The sum of a constant
If the expression does not contain i, every term is the same:
i=1∑nc=n timesc+c+⋯+c=nc
Check: ∑i=153=3+3+3+3+3=15=5×3.. The index is not used, but the sum still counts n terms.
Two rules that do NOT work
Rule
Holds?
Numbers
∑cxi=c∑xi
yes
12=2×6
∑(xi+yi)=∑xi+∑yi
yes
21=6+15
∑i=1nc=nc
yes
15=5×3
∑xiyi=(∑xi)(∑yi)
no
32=90
∑xi2=(∑xi)2
no
14=36
Sums you will see all the time in ML
Each of these is just one Σ. Read them by unrolling.
The mean (average) of N numbers: add them and divide by N.
xˉ=N1i=1∑Nxifor (2,5,3,6,4):xˉ=51⋅20=4
A useful fact follows from the rules: the deviations from the mean add to zero, ∑(xi−xˉ)=∑xi−Nxˉ=20−5⋅4=0 (rule 2, then rule 3).
A sum of squared deviations.∑(xi−xˉ)2=(2−4)2+(5−4)2+(3−4)2+(6−4)2+(4−4)2=4+1+1+4+0=10.. Dividing by N gives 10/5=2, the variance (P1.D4). (Strictly, dividing by N gives the population variance; when estimating from a sample, statisticians often divide by N−1 instead.)
A weighted sum. Give each value its own weight wi:
An ordinary mean is the special case where every weight is 1/N. If the weights are probabilities that add to 1, the weighted sum is an expected value (P1.D4).
The dot product of two vectors is a sum of pairwise products:
a⋅b=i=1∑daibi(1,2,3)⋅(4,5,6)=4+10+18=32
This is the sum ∑xiyi from the “does not work” box: it is a perfectly good sum, it just is not a product of two sums. A neuron is a weighted sum plus a bias (P1.B3 teaches the dot product).
Mean squared error, our opening formula, read piece by piece. Take targets y=(3,5,7,9) and predictions y^=(2,5,9,8):
y^i−yi: the error of example i:−1,0,2,−1.
(y^i−yi)2: squared, so that over- and under-shoots both count as positive (and a perfect prediction counts as 0): 1,0,4,1.
∑i=1N: add them over all N=4 examples: 6.
N1: divide by the number of examples to get an average: 6/4=1.5.
Why divide by N? So that the loss does not simply grow because you have more data. (It is the mean of the squared errors, hence the name.) P3.A2 uses this loss.
Products: Π is Σ with multiplication
The capital Greek letter pi, ∏, works just like ∑ but multiplies the terms:
i=1∏4i=1×2×3×4=24
(That is “4 factorial”, written 4!.) The index, limits, and counting of terms (b−a+1) all work as before. Careful: the symbol is not the number π≈3.14.
The empty product equals 1, not 0. When you multiply, the “do-nothing” starting value is 1 (multiplying by 1 changes nothing), just as it was 0 for a sum. So ∏i=10xi=1. This is why 0!=1, and it is why formulas keep working in edge cases.
Where products appear. If a model gives probabilities p1,p2,…,pN to N independent examples, the probability of all of them together is ∏i=1Npi (the likelihood, P1.D10).
Connecting Π and Σ: the log turns one into the other
In P1.A2 you learned ln(ab)=lna+lnb. Applying it again and again gives
ln(i=1∏npi)=i=1∑nlnpi
Check with p=(0.5,0.8,0.25). The product is 0.5×0.8×0.25=0.1, and ln0.1=−2.3026. The sum of logs: ln0.5+ln0.8+ln0.25≈−0.6931−0.2231−1.3863≈−2.3026 (adding the unrounded logs; the rounded ones give −2.3025). The same number. This is exactly why the log-likelihood is a sum (and why it avoids the underflow you saw in P1.A2).
The same idea in reverse: an exponential turns a sum into a product. ∏exi=e∑xi. Check with x=(1,2,3):e1⋅e2⋅e3=e6≈403.43..
Double sums: a sum of sums
Sometimes there are two indices, and we add over both. Here is a double sum:
i=1∑3j=1∑2aibj
Read it from the inside out: for each i, first add over j (the inner sum), and then add those results over i (the outer sum). Take a=(1,2,3) and b=(10,20). Every pair (i,j) is one cell of a grid: rows are i, columns are j, and the cell holds aibj.
b1=10
b2=20
row sum
a1=1
10
20
30
a2=2
20
40
60
a3=3
30
60
90
column sum
60
120
180
Rows first: the inner sum over j gives the row sums 30,60,90, and the outer sum gives 180.
Columns first: swapping the order, add down each column (60 and 120), then add: 180.
The grid always has 3×2=6 cells, and both orders add the same six numbers. For a finite sum, you may swap the order of the two sums:
i∑j∑cij=j∑i∑cij
(This is just “adding in a different order does not change the total”. For sums that go on forever, it can fail, but we only use finite ones here.)
A double sum is a grid
b₁ = 10
b₂ = 20
row sum
a₁ = 1
10
20
30
a₂ = 2
20
40
60
a₃ = 3
30
60
90
column sum
60
120
180
Rows first: 30 + 60 + 90 = 180
Both orders give 180 = 180.
(Σaᵢ)(Σbⱼ) = 6 × 30 = 180: the same total, because every cell factors as aᵢ × bⱼ.
A double sum as a grid. Choose a cell formula, set the number of rows i and columns j, and switch between adding rows first and columns first. The total is the same either way. With the aᵢ × bⱼ cells the total also equals (Σaᵢ)(Σbⱼ); with the other two formulas it does not, and the widget shows the difference.
When the terms factor
Look at the number 180: it equals (1+2+3)×(10+20)=6×30.. This is not an accident. If every cell is a product of an i-part and a j-part, then
i∑j∑aibj=(i∑ai)(j∑bj)
Why: in the inner sum, ai does not depend on j, so it comes out (rule 1): ∑jaibj=ai∑jbj. Now ∑jbj is just a number, so it comes out of the outer sum too. This holds only when the terms factor that way. For ai+bj it fails, and the widget shows it. Do not confuse this with the false rule ∑xiyi=∑xi∑yi: that one has a single index shared by x and y (3 products), while here there are two separate indices (6 products).
A preview of matrix multiplication
A matrix is a grid of numbers. Multiplying two matrices produces a new grid, and each entry is a sum over a shared index k:
Cij=k=1∑2AikBkj
For A=(1324) and B=(5768), the entry C12 (row 1 of A with column 2 of B) is 1⋅6+2⋅8=22. The full product is (19432250). In other words, each entry of a matrix product is a dot product, a Σ. P1.B6 develops this fully; the point here is that you can already read the formula.
Shortcut notations
Papers use several short forms. They all mean “the same Σ, written lazily”.
A sum over a set.∑i∈Sxi means: add xi for every i in the set S (the symbol ∈ means “is in”; sets are in P1.A4). With S={1,3,5} and our data (2,5,3,6,4):x1+x3+x5=2+3+4=9.
A sum with a condition.∑i evenxi adds only the even indices: x2+x4=5+6=11.
No limits.∑ixi means “over all valid values of i”, usually all the entries. When the limits are obvious, they are left out.
Same idea for Π:∏i∈S,∏i.
In code
Math
Python
NumPy
∑ixi
sum(x) or a for loop that adds
x.sum() or np.sum(x)
∏ixi
math.prod(x)
np.prod(x)
∑iaibi
sum(p * q for p, q in zip(a, b))
np.dot(a, b) or (a * b).sum()
empty sum, empty product
sum([]) is 0, math.prod([]) is 1
np.sum([]) is 0.0, np.prod([]) is 1.0
For a 2D array (a grid), you must say which index to sum over, with axis=. On M = np.array([[1,2,3],[4,5,6]]), M.sum() adds all six numbers (21), M.sum(axis=0) adds down each column, giving [5 7 9], and M.sum(axis=1) adds across each row, giving [6 15]. The axis you name is the index that disappears. Shapes and axes are the subject of P1.B14 and P2.2, and P2.3 shows np.einsum, a notation that writes Σ over a repeated index directly. For example np.einsum('i,i->', a, b) is ∑iaibi.
Go deeper Famous sums: Gauss and the pairing trick
How much is 1+2+⋯+100? Write the list forwards and backwards and add column by column:
1100++299++⋯⋯++1001
Each column adds to 101 and there are 100 columns, so twice the sum is 100×101. Hence
i=1∑ni=2n(n+1)n=100:2100⋅101=5050
(The story that the young Gauss found this is a popular legend; the argument itself is what matters.) Another standard result is ∑i=1ni2=6n(n+1)(2n+1);; for n=4 this gives 64⋅5⋅9=30, matching our unrolled example. A third is the geometric sum, where each term multiplies the last by a ratio r=1:
Sums like this appear whenever something is repeated with a decay, such as discounted rewards in reinforcement learning.
Intuition
A Σ is a for-loop that adds. The index is the loop variable, the limits are the start and end (both included), the expression is the body, and the answer is what you have when the loop ends. A Π is the same loop with *= starting from 1. A double sum is a loop inside a loop: a grid. When any formula looks hard, unroll the first two or three terms and the last one. Where the picture breaks: a Σ has no order of execution (adding in any order gives the same total for a finite sum) and it is a single value, not a procedure.
Math
For integers a≤b and a function f of the index,
i=a∑bf(i)=f(a)+f(a+1)+⋯+f(b),b−a+1 terms,
with ∑i=abf(i)=0 when b<a, and ∏i=abf(i)=f(a)⋯f(b),, equal to 1 when b<a. Re-indexing: ∑i=abf(i)=∑j=a+sb+sf(j−s)..
Linearity: ∑(cxi+dyi)=c∑xi+d∑yi.. Constant: ∑i=1nc=nc. Finite double sums: ∑i∑jcij=∑j∑icij, and ∑i∑jaibj=(∑iai)(∑jbj).. Logs and exponentials: ln∏pi=∑lnpi for pi>0, and ∏exi=e∑xi.
Code
import numpy as np, mathx = np.array([2, 5, 3, 6, 4])x.sum() # 20 sum of x_i, i = 1..5x[1:4].sum() # 14 sum of x_i, i = 2..4 (0-based slice)((x - x.mean())**2).sum() # 10.0math.prod(range(1, 5)) # 24 product of i, i = 1..4
The cell below runs these and more, including the double sum both ways.
Try it in Python
Run the cell. Then change the data and the limits, and check that each printed pair that should match still matches.
Try it: loops, sum(), np.sum, off-by-one and double sums
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Challenge: write a loop that computes ∑i=1ni2 for n=4 and checks that it equals n(n+1)(2n+1)/6 (30). Then change the range so it is off by one and see what the check says.
Putting it together
∑ means “add the terms you get as the index runs from the lower limit to the upper limit”; ∏ means the same with multiplication. Unroll to understand, count the terms as b−a+1, remember that empty sums are 0 and empty products are 1, and be careful about 1-based math versus 0-based Python. Sums split and factor in some ways (constants, sums of sums, products of separate indices) but not others (a sum of products, a sum of squares). The mean, weighted sum, dot product and mean squared error are all one Σ, a matrix product entry is a Σ, and the log turns Π into Σ.
From a to b with both ends included there are b−a+1=9−3+1=7 terms: x3,x4,…,x9.
With x=(1,2,3) and y=(4,5,6), which statement is true?
∑xiyi=4+10+18=32, while (∑xi)(∑yi)=6×15=90. Also ∑xi2=14, not 36.
In Python, x = [2, 5, 3, 6, 4]. Which expression equals the math sum ∑i=24xi?
Math x2 is x[1], and a slice stops before its end index, so x2,x3,x4 is x[1:4]: 5+3+6=14.
Which statement about p=(0.5,0.8,0.25) is correct?
The log of a product is the sum of the logs (P1.A2): ln0.1≈−2.3026≈−0.6931−0.2231−1.3863 (to 4 decimals). A log of a sum does not split.
Exercises
Unroll and evaluate ∑i=15i2 and ∑i=14(3i−1).Show answer1+4+9+16+25=55; and 2+5+8+11=26.
How many terms does ∑k=010k have, and what is it? Show answer10−0+1=11 terms. 0+1+⋯+10=55=210⋅11..
Does ∑i=14i equal ∑k=14k? Why? Show answerYes, both are 10. The index is a dummy variable, so the letter does not matter.
What is ∑i=165? What is ∑i=105?Show answer6×5=30 (six terms, each equal to 5). The second is an empty sum, so it is 0.
Given x=(2,5,3,6,4), compute ∑i=15(2xi+1) using the rules, then check by unrolling. Show answerBy the rules: 2⋅20+5⋅1=45. Unrolled: 5+11+7+13+9=45.
Using a=(1,2,3) and b=(4,5,6), compute ∑aibi,∑ai∑bi and ∑ai2. Two of these have tempting shortcuts that are wrong. Which two, and why? Show answer∑aibi=32;∑ai∑bi=6×15=90;∑ai2=14 (while (∑ai)2=36). The careless rules “∑aibi=∑ai∑bi” and “∑ai2=(∑ai)2” are both false.
Write the mean of (4,8,6) in Σ notation and compute it. Then compute ∑(xi−xˉ).Show answerxˉ=31∑i=13xi=18/3=6.. The deviations are −2,2,0, which sum to 0.
A model predicts y^=(1,4,4) for targets y=(2,4,6). Compute the mean squared error. Show answerErrors y^i−yi=−1,0,−2; squares 1,0,4; sum 5; divide by 3: 5/3≈1.667.
Compute ∏i=15i and the empty product ∏i=10i.Show answer1⋅2⋅3⋅4⋅5=120. The empty product is 1.
A model gives probabilities 0.5,0.2,0.4 to three independent examples. Find the product, and the sum of the natural logs, and check they agree. Show answerProduct =0.04, and ln0.04≈−3.2189. Sum of logs: ≈−0.6931−1.6094−0.9163≈−3.2189 (with unrounded logs; the rounded values add to −3.2188). They agree.
A grid has a=(1,2) as rows and b=(3,4,5) as columns, with cell aibj. Find the total two ways, and then with (∑a)(∑b).Show answerRows first: row sums 1⋅12=12 and 2⋅12=24, total 36. Columns first: 3⋅3=9,4⋅3=12,5⋅3=15, total 36. And (1+2)(3+4+5)=3×12=36..
In Python, x = [2, 5, 3, 6, 4]. Write the code for ∑i=35xi and give its value. Show answersum(x[2:5]), which is 3+6+4=13.(x3 is x[2]; the slice stops before index 5.)
Use Cij=∑kAikBkj with A=(1324) and B=(5768) to find C21.Show answerC21=A21B11+A22B21=3⋅5+4⋅7=43..
Use the widgets: in the unrolling widget choose (xi−xˉ)2 with limits 1 to 5. What total do you get, and what is it divided by 5? Show answer10, and 10/5=2, the (population) variance of the data.
Key takeaways
∑i=abf(i) means: put i=a,a+1,…,b into f and add the results. The index i is a dummy variable (any letter works) and there are b−a+1 terms. An empty sum equals 0.
Rules that hold: ∑cxi=c∑xi,∑(xi+yi)=∑xi+∑yi, and ∑i=1nc=nc. Rules that do not: ∑xiyi=(∑xi)(∑yi) and ∑xi2=(∑xi)2.
Mean xˉ=N1∑xi, weighted sum ∑wixi, dot product ∑aibi and mean squared error N1∑(y^i−yi)2 are all one Σ.
∏i=1nf(i) multiplies instead of adds; an empty product is 1; and ln∏pi=∑lnpi, which is why log-likelihoods are sums.
A double sum is a grid; for finite sums the order can be swapped, and if every term factors as aibj it equals (∑ai)(∑bj). A matrix product entry is the nested-sum idea Cij=∑kAikBkj. In Python, math indices start at 1 but lists start at 0.