Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsPre-calculus Refresher

Summation, products and indices

P1.A335 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

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.

Before this: Exponents and logarithms

After this lesson you can

  • 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=1N∑i=1N(y^i−yi)2\text{MSE} = \frac{1}{N}\sum_{i=1}^{N}\left(\hat{y}_i - y_i\right)^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 ∑\sum (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=101 + 2 + 3 + 4 = 10

Writing a long list of additions is tiring, so mathematicians invented a short form. The short form for “add ii, where ii goes from 1 to 4” is

∑i=14i\sum_{i=1}^{4} i

A sum has five parts:

  • ∑\sum: the sum sign. It means “add”.
  • The index ii (the letter under the sigma): a counter. It takes the values 1,2,3,…1, 2, 3, \dots one at a time.
  • The lower limit (under the sigma, here i=1i = 1): where the counter starts.
  • The upper limit (above the sigma, here 44): 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 ii): what you compute for each value of the counter. Each result is a term.

You can read it aloud as: “the sum, for ii from 1 to 4, of ii.”

Now the expression can be more than ii. Take ∑i=14i2\sum_{i=1}^{4} i^2. Put i=1,2,3,4i = 1, 2, 3, 4 into i2i^2 and add:

∑i=14i2=12+22+32+42=1+4+9+16=30\begin{aligned} \sum_{i=1}^{4} i^2 &= 1^2 + 2^2 + 3^2 + 4^2 \\ &= 1 + 4 + 9 + 16 \\ &= 30 \end{aligned}

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

Read aloud: the sum of i², for i from 1 to 4.

Unrolled: 4 terms (4 − 1 + 1 = 4).

  1. i = 1 1² = 1
  2. i = 2 2² = 4
  3. i = 3 3² = 9
  4. i = 4 4² = 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\begin{gathered} x_1 = 2,\quad x_2 = 5,\quad x_3 = 3, \\ x_4 = 6,\quad x_5 = 4 \end{gathered}

The subscript ii in xix_i is the index: it says which one we mean, “the ii-th”. Now a sum over data is natural. Add all five:

∑i=15xi=x1+x2+x3+x4+x5=2+5+3+6+4=20\begin{aligned} \sum_{i=1}^{5} x_i &= x_1 + x_2 + x_3 + x_4 + x_5 \\ &= 2 + 5 + 3 + 6 + 4 \\ &= 20 \end{aligned}

How many terms?

From i=ai = a to i=bi = b (both included) there are

b−a+1 termsb - a + 1 \text{ terms}

Check: i=3i = 3 to 77 gives 3,4,5,6,73, 4, 5, 6, 7, which is 7−3+1=57 - 3 + 1 = 5 terms, and ∑i=37i=3+4+5+6+7=25\sum_{i=3}^{7} i = 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\sum_{i=1}^{0} x_i? Then there are 0−1+1=00 - 1 + 1 = 0 terms. (The count formula b−a+1b - a + 1 works down to b=a−1b = a - 1; if bb 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=15xi  =  ∑j=04xj+1\sum_{i=1}^{5} x_i \;=\; \sum_{j=0}^{4} x_{j+1}

On the right, jj runs from 0 to 4, so xj+1x_{j+1} is x1,x2,…,x5x_1, x_2, \dots, x_5. We re-labelled the index (i=j+1i = 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: x1x_1 is the first number. In Python, a list starts at index 0: x[0] is the first number. So xix_i 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]x = [2, 5, 3, 6, 4]: the math sum ∑i=24xi=5+3+6=14\sum_{i=2}^{4} x_i = 5 + 3 + 6 = 14 is sum(x[1:4]) in Python, not sum(x[2:4]) (which is 9).

MathPython
x1x_1 (the first)x[0]
xix_ix[i - 1]
∑i=1nxi\sum_{i=1}^{n} x_isum(x[0:n]), or sum(x) when the list has nn entries
∑i=abxi\sum_{i=a}^{b} x_isum(x[a-1:b])
i=1,…,ni = 1, \dots, nrange(1, n + 1)
number of terms b−a+1b - a + 1len(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)x = (1, 2, 3) and y=(4,5,6)y = (4, 5, 6).

1. A constant factor comes out

∑i=1nc xi=c∑i=1nxi\sum_{i=1}^{n} c\,x_i = c\sum_{i=1}^{n} x_i

Why: cx1+cx2+⋯+cxn=c(x1+x2+⋯+xn)c x_1 + c x_2 + \dots + c x_n = c(x_1 + x_2 + \dots + x_n) (taking out a common factor). Check with c=2c = 2 and x=(1,2,3)x = (1, 2, 3): 2+4+6=12=2×62 + 4 + 6 = 12 = 2 \times 6.

2. A sum of sums splits

∑i=1n(xi+yi)=∑i=1nxi+∑i=1nyi\sum_{i=1}^{n} (x_i + y_i) = \sum_{i=1}^{n} x_i + \sum_{i=1}^{n} y_i

Why: adding in a different order does not change the total. Check: (1+4)+(2+5)+(3+6)=21=6+15(1+4) + (2+5) + (3+6) = 21 = 6 + 15. Rules 1 and 2 together say that Σ is linear: ∑(c xi+d yi)=c∑xi+d∑yi\sum (c\,x_i + d\,y_i) = c\sum x_i + d\sum y_i.

3. The sum of a constant

If the expression does not contain ii, every term is the same:

∑i=1nc=c+c+⋯+c⏟n times=n c\sum_{i=1}^{n} c = \underbrace{c + c + \dots + c}_{n \text{ times}} = n\,c

Check: ∑i=153=3+3+3+3+3=15=5×3\sum_{i=1}^{5} 3 = 3 + 3 + 3 + 3 + 3 = 15 = 5 \times 3. The index is not used, but the sum still counts nn terms.

Two rules that do NOT work

RuleHolds?Numbers
∑c xi=c∑xi\sum c\,x_i = c\sum x_iyes12=2×612 = 2 \times 6
∑(xi+yi)=∑xi+∑yi\sum (x_i + y_i) = \sum x_i + \sum y_iyes21=6+1521 = 6 + 15
∑i=1nc=nc\sum_{i=1}^{n} c = ncyes15=5×315 = 5 \times 3
∑xiyi=(∑xi)(∑yi)\sum x_i y_i = (\sum x_i)(\sum y_i)no32≠9032 \ne 90
∑xi2=(∑xi)2\sum x_i^2 = (\sum x_i)^2no14≠3614 \ne 36

Sums you will see all the time in ML

Each of these is just one Σ. Read them by unrolling.

The mean (average) of NN numbers: add them and divide by NN.

xˉ=1N∑i=1Nxifor (2,5,3,6,4): xˉ=15⋅20=4\begin{gathered} \bar{x} = \frac{1}{N}\sum_{i=1}^{N} x_i \\ \text{for } (2,5,3,6,4):\ \bar{x} = \tfrac{1}{5}\cdot 20 = 4 \end{gathered}

A useful fact follows from the rules: the deviations from the mean add to zero, ∑(xi−xˉ)=∑xi−Nxˉ=20−5⋅4=0\sum (x_i - \bar{x}) = \sum x_i - N\bar{x} = 20 - 5\cdot 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\sum (x_i - \bar{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 NN gives 10/5=210/5 = 2, the variance (P1.D4). (Strictly, dividing by NN gives the population variance; when estimating from a sample, statisticians often divide by N−1N - 1 instead.)

A weighted sum. Give each value its own weight wiw_i:

∑i=1nwixiwith w=(0.5,0.3,0.2),x=(10,20,30): 5+6+6=17\begin{gathered} \sum_{i=1}^{n} w_i x_i \\ \text{with } w = (0.5, 0.3, 0.2), \\ x = (10, 20, 30):\ 5 + 6 + 6 = 17 \end{gathered}

An ordinary mean is the special case where every weight is 1/N1/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=1daibi(1,2,3)⋅(4,5,6)=4+10+18=32\begin{gathered} \mathbf{a}\cdot\mathbf{b} = \sum_{i=1}^{d} a_i b_i \\ (1,2,3)\cdot(4,5,6) = 4 + 10 + 18 = 32 \end{gathered}

This is the sum ∑xiyi\sum x_i y_i 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)y = (3, 5, 7, 9) and predictions y^=(2,5,9,8)\hat{y} = (2, 5, 9, 8):

  • y^i−yi\hat{y}_i - y_i: the error of example ii: −1,0,2,−1-1, 0, 2, -1.
  • (y^i−yi)2(\hat{y}_i - y_i)^2: squared, so that over- and under-shoots both count as positive (and a perfect prediction counts as 0): 1,0,4,11, 0, 4, 1.
  • ∑i=1N\sum_{i=1}^{N}: add them over all N=4N = 4 examples: 66.
  • 1N\frac{1}{N}: divide by the number of examples to get an average: 6/4=1.56/4 = 1.5.

Why divide by NN? 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, ∏\prod, works just like ∑\sum but multiplies the terms:

∏i=14i=1×2×3×4=24\prod_{i=1}^{4} i = 1 \times 2 \times 3 \times 4 = 24

(That is “4 factorial”, written 4!4!.) The index, limits, and counting of terms (b−a+1b - a + 1) all work as before. Careful: the symbol is not the number π≈3.14\pi \approx 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\prod_{i=1}^{0} x_i = 1. This is why 0!=10! = 1, and it is why formulas keep working in edge cases.

Where products appear. If a model gives probabilities p1,p2,…,pNp_1, p_2, \dots, p_N to NN independent examples, the probability of all of them together is ∏i=1Npi\prod_{i=1}^{N} p_i (the likelihood, P1.D10).

Connecting Π and Σ: the log turns one into the other

In P1.A2 you learned ln⁡(ab)=ln⁡a+ln⁡b\ln(ab) = \ln a + \ln b. Applying it again and again gives

ln⁡ ⁣(∏i=1npi)=∑i=1nln⁡pi\ln\!\left(\prod_{i=1}^{n} p_i\right) = \sum_{i=1}^{n} \ln p_i

Check with p=(0.5,0.8,0.25)p = (0.5, 0.8, 0.25). The product is 0.5×0.8×0.25=0.10.5 \times 0.8 \times 0.25 = 0.1, and ln⁡0.1=−2.3026\ln 0.1 = -2.3026. The sum of logs: ln⁡0.5+ln⁡0.8+ln⁡0.25≈−0.6931−0.2231−1.3863≈−2.3026\ln 0.5 + \ln 0.8 + \ln 0.25 \approx -0.6931 - 0.2231 - 1.3863 \approx -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\prod e^{x_i} = e^{\sum x_i}. Check with x=(1,2,3)x = (1,2,3): e1⋅e2⋅e3=e6≈403.43e^1 \cdot e^2 \cdot e^3 = e^6 \approx 403.43.

Double sums: a sum of sums

Sometimes there are two indices, and we add over both. Here is a double sum:

∑i=13∑j=12aibj\sum_{i=1}^{3}\sum_{j=1}^{2} a_i b_j

Read it from the inside out: for each ii, first add over jj (the inner sum), and then add those results over ii (the outer sum). Take a=(1,2,3)a = (1,2,3) and b=(10,20)b = (10, 20). Every pair (i,j)(i, j) is one cell of a grid: rows are ii, columns are jj, and the cell holds aibja_i b_j.

b1=10b_1 = 10b2=20b_2 = 20row sum
a1=1a_1 = 1102030
a2=2a_2 = 2204060
a3=3a_3 = 3306090
column sum60120180
  • Rows first: the inner sum over jj gives the row sums 30,60,9030, 60, 90, and the outer sum gives 180180.
  • Columns first: swapping the order, add down each column (6060 and 120120), then add: 180180.

The grid always has 3×2=63 \times 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∑jcij=∑j∑icij\sum_{i}\sum_{j} c_{ij} = \sum_{j}\sum_{i} c_{ij}

(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₁ = 10b₂ = 20row sum
a₁ = 1102030
a₂ = 2204060
a₃ = 3306090
column sum60120180

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 180180: it equals (1+2+3)×(10+20)=6×30(1+2+3)\times(10+20) = 6 \times 30. This is not an accident. If every cell is a product of an ii-part and a jj-part, then

∑i∑jaibj=(∑iai)(∑jbj)\sum_{i}\sum_{j} a_i b_j = \left(\sum_{i} a_i\right)\left(\sum_{j} b_j\right)

Why: in the inner sum, aia_i does not depend on jj, so it comes out (rule 1): ∑jaibj=ai∑jbj\sum_j a_i b_j = a_i\sum_j b_j. Now ∑jbj\sum_j b_j is just a number, so it comes out of the outer sum too. This holds only when the terms factor that way. For ai+bja_i + b_j it fails, and the widget shows it. Do not confuse this with the false rule ∑xiyi≠∑xi∑yi\sum x_i y_i \ne \sum x_i \sum y_i: that one has a single index shared by xx and yy (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 kk:

Cij=∑k=12AikBkjC_{ij} = \sum_{k=1}^{2} A_{ik}B_{kj}

For A=(1234)\mathbf{A} = \begin{pmatrix}1&2\\3&4\end{pmatrix} and B=(5678)\mathbf{B} = \begin{pmatrix}5&6\\7&8\end{pmatrix}, the entry C12C_{12} (row 1 of A\mathbf{A} with column 2 of B\mathbf{B}) is 1⋅6+2⋅8=221\cdot 6 + 2\cdot 8 = 22. The full product is (19224350)\begin{pmatrix}19&22\\43&50\end{pmatrix}. 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\sum_{i \in S} x_i means: add xix_i for every ii in the set SS (the symbol ∈\in means “is in”; sets are in P1.A4). With S={1,3,5}S = \{1, 3, 5\} and our data (2,5,3,6,4)(2,5,3,6,4): x1+x3+x5=2+3+4=9x_1 + x_3 + x_5 = 2 + 3 + 4 = 9.
  • A sum with a condition. ∑i evenxi\sum_{i \text{ even}} x_i adds only the even indices: x2+x4=5+6=11x_2 + x_4 = 5 + 6 = 11.
  • No limits. ∑ixi\sum_i x_i means “over all valid values of ii”, usually all the entries. When the limits are obvious, they are left out.
  • Same idea for Π: ∏i∈S\prod_{i \in S}, ∏i\prod_i.

In code

MathPythonNumPy
∑ixi\sum_i x_isum(x) or a for loop that addsx.sum() or np.sum(x)
∏ixi\prod_i x_imath.prod(x)np.prod(x)
∑iaibi\sum_i a_i b_isum(p * q for p, q in zip(a, b))np.dot(a, b) or (a * b).sum()
empty sum, empty productsum([]) is 0, math.prod([]) is 1np.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\sum_i a_i b_i.

Go deeper Famous sums: Gauss and the pairing trick

How much is 1+2+⋯+1001 + 2 + \dots + 100? Write the list forwards and backwards and add column by column:

1 ⁣+ ⁣2 ⁣+ ⁣⋯ ⁣+ ⁣100100 ⁣+ ⁣99 ⁣+ ⁣⋯ ⁣+ ⁣1\begin{array}{ccccccc} 1 &\!+\!& 2 &\!+\!& \cdots &\!+\!& 100\\ 100 &\!+\!& 99 &\!+\!& \cdots &\!+\!& 1 \end{array}

Each column adds to 101101 and there are 100100 columns, so twice the sum is 100×101100 \times 101. Hence

∑i=1ni=n(n+1)2n=100: 100⋅1012=5050\begin{gathered} \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \\ n = 100:\ \frac{100\cdot 101}{2} = 5050 \end{gathered}

(The story that the young Gauss found this is a popular legend; the argument itself is what matters.) Another standard result is ∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}; for n=4n = 4 this gives 4⋅5⋅96=30\frac{4\cdot 5\cdot 9}{6} = 30, matching our unrolled example. A third is the geometric sum, where each term multiplies the last by a ratio r≠1r \ne 1:

∑i=0n−1ri=1−rn1−rr=0.5, n=4:1+0.5+0.25+0.125=1.875=1−0.06250.5\begin{gathered} \sum_{i=0}^{n-1} r^i = \frac{1 - r^n}{1 - r} \\ r = 0.5,\ n = 4: \\ 1 + 0.5 + 0.25 + 0.125 = 1.875 \\ \quad= \frac{1 - 0.0625}{0.5} \end{gathered}

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≤ba \le b and a function ff of the index,

∑i=abf(i)=f(a)+f(a+1)+⋯+f(b),b−a+1 terms,\begin{gathered} \sum_{i=a}^{b} f(i) \\ = f(a) + f(a+1) + \dots + f(b), \\ b - a + 1 \text{ terms}, \end{gathered}

with ∑i=abf(i)=0\sum_{i=a}^{b} f(i) = 0 when b<ab \lt a, and ∏i=abf(i)=f(a)⋯f(b)\prod_{i=a}^{b} f(i) = f(a)\cdots f(b), equal to 11 when b<ab \lt a. Re-indexing: ∑i=abf(i)=∑j=a+sb+sf(j−s)\sum_{i=a}^{b} f(i) = \sum_{j=a+s}^{b+s} f(j - s).

Linearity: ∑(c xi+d yi)=c∑xi+d∑yi\sum (c\,x_i + d\,y_i) = c\sum x_i + d\sum y_i. Constant: ∑i=1nc=nc\sum_{i=1}^{n} c = nc. Finite double sums: ∑i∑jcij=∑j∑icij\sum_i\sum_j c_{ij} = \sum_j\sum_i c_{ij}, and ∑i∑jaibj=(∑iai)(∑jbj)\sum_i\sum_j a_i b_j = \left(\sum_i a_i\right)\left(\sum_j b_j\right). Logs and exponentials: ln⁡∏pi=∑ln⁡pi\ln\prod p_i = \sum\ln p_i for pi>0p_i \gt 0, and ∏exi=e∑xi\prod e^{x_i} = e^{\sum x_i}.

Code

import numpy as np, math
x = np.array([2, 5, 3, 6, 4])

x.sum()                  # 20   sum of x_i, i = 1..5
x[1:4].sum()             # 14   sum of x_i, i = 2..4  (0-based slice)
((x - x.mean())**2).sum()  # 10.0
math.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\sum_{i=1}^{n} i^2 for n=4n = 4 and checks that it equals n(n+1)(2n+1)/6n(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

∑\sum means “add the terms you get as the index runs from the lower limit to the upper limit”; ∏\prod means the same with multiplication. Unroll to understand, count the terms as b−a+1b - 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 Σ.

Check your understanding

  1. What is ∑i=14(2i+1)\sum_{i=1}^{4} (2i + 1)?

  2. How many terms does ∑i=39xi\sum_{i=3}^{9} x_i have?

  3. With x=(1,2,3)x = (1, 2, 3) and y=(4,5,6)y = (4, 5, 6), which statement is true?

  4. In Python, x = [2, 5, 3, 6, 4]. Which expression equals the math sum ∑i=24xi\sum_{i=2}^{4} x_i?

  5. Which statement about p=(0.5,0.8,0.25)p = (0.5, 0.8, 0.25) is correct?

Exercises

  1. Unroll and evaluate ∑i=15i2\sum_{i=1}^{5} i^2 and ∑i=14(3i−1)\sum_{i=1}^{4} (3i - 1).
    Show answer1+4+9+16+25=551 + 4 + 9 + 16 + 25 = 55; and 2+5+8+11=262 + 5 + 8 + 11 = 26.
  2. How many terms does ∑k=010k\sum_{k=0}^{10} k have, and what is it?
    Show answer10−0+1=1110 - 0 + 1 = 11 terms. 0+1+⋯+10=55=10⋅1120 + 1 + \dots + 10 = 55 = \frac{10\cdot 11}{2}.
  3. Does ∑i=14i\sum_{i=1}^{4} i equal ∑k=14k\sum_{k=1}^{4} k? Why?
    Show answerYes, both are 10. The index is a dummy variable, so the letter does not matter.
  4. What is ∑i=165\sum_{i=1}^{6} 5? What is ∑i=105\sum_{i=1}^{0} 5?
    Show answer6×5=306 \times 5 = 30 (six terms, each equal to 5). The second is an empty sum, so it is 00.
  5. Given x=(2,5,3,6,4)x = (2, 5, 3, 6, 4), compute ∑i=15(2xi+1)\sum_{i=1}^{5} (2x_i + 1) using the rules, then check by unrolling.
    Show answerBy the rules: 2⋅20+5⋅1=452 \cdot 20 + 5 \cdot 1 = 45. Unrolled: 5+11+7+13+9=455 + 11 + 7 + 13 + 9 = 45.
  6. Using a=(1,2,3)a = (1, 2, 3) and b=(4,5,6)b = (4, 5, 6), compute ∑aibi\sum a_i b_i, ∑ai∑bi\sum a_i \sum b_i and ∑ai2\sum a_i^2. Two of these have tempting shortcuts that are wrong. Which two, and why?
    Show answer∑aibi=32\sum a_i b_i = 32; ∑ai∑bi=6×15=90\sum a_i\sum b_i = 6 \times 15 = 90; ∑ai2=14\sum a_i^2 = 14 (while (∑ai)2=36(\sum a_i)^2 = 36). The careless rules “∑aibi=∑ai∑bi\sum a_ib_i = \sum a_i\sum b_i” and “∑ai2=(∑ai)2\sum a_i^2 = (\sum a_i)^2” are both false.
  7. Write the mean of (4,8,6)(4, 8, 6) in Σ notation and compute it. Then compute ∑(xi−xˉ)\sum (x_i - \bar{x}).
    Show answerxˉ=13∑i=13xi=18/3=6\bar{x} = \frac{1}{3}\sum_{i=1}^{3} x_i = 18/3 = 6. The deviations are −2,2,0-2, 2, 0, which sum to 00.
  8. A model predicts y^=(1,4,4)\hat{y} = (1, 4, 4) for targets y=(2,4,6)y = (2, 4, 6). Compute the mean squared error.
    Show answerErrors y^i−yi=−1,0,−2\hat{y}_i - y_i = -1, 0, -2; squares 1,0,41, 0, 4; sum 55; divide by 3: 5/3≈1.6675/3 \approx 1.667.
  9. Compute ∏i=15i\prod_{i=1}^{5} i and the empty product ∏i=10i\prod_{i=1}^{0} i.
    Show answer1⋅2⋅3⋅4⋅5=1201\cdot2\cdot3\cdot4\cdot5 = 120. The empty product is 11.
  10. A model gives probabilities 0.5,0.2,0.40.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= 0.04, and ln⁡0.04≈−3.2189\ln 0.04 \approx -3.2189. Sum of logs: ≈−0.6931−1.6094−0.9163≈−3.2189\approx -0.6931 - 1.6094 - 0.9163 \approx -3.2189 (with unrounded logs; the rounded values add to −3.2188). They agree.
  11. A grid has a=(1,2)a = (1, 2) as rows and b=(3,4,5)b = (3, 4, 5) as columns, with cell aibja_i b_j. Find the total two ways, and then with (∑a)(∑b)(\sum a)(\sum b).
    Show answerRows first: row sums 1⋅12=121 \cdot 12 = 12 and 2⋅12=242 \cdot 12 = 24, total 3636. Columns first: 3⋅3=93 \cdot 3 = 9, 4⋅3=124 \cdot 3 = 12, 5⋅3=155 \cdot 3 = 15, total 3636. And (1+2)(3+4+5)=3×12=36(1+2)(3+4+5) = 3 \times 12 = 36.
  12. In Python, x = [2, 5, 3, 6, 4]. Write the code for ∑i=35xi\sum_{i=3}^{5} x_i and give its value.
    Show answersum(x[2:5]), which is 3+6+4=133 + 6 + 4 = 13. (x3x_3 is x[2]; the slice stops before index 5.)
  13. Use Cij=∑kAikBkjC_{ij} = \sum_k A_{ik}B_{kj} with A=(1234)\mathbf{A} = \begin{pmatrix}1&2\\3&4\end{pmatrix} and B=(5678)\mathbf{B} = \begin{pmatrix}5&6\\7&8\end{pmatrix} to find C21C_{21}.
    Show answerC21=A21B11+A22B21=3⋅5+4⋅7=43C_{21} = A_{21}B_{11} + A_{22}B_{21} = 3\cdot5 + 4\cdot7 = 43.
  14. Use the widgets: in the unrolling widget choose (xi−xˉ)2(x_i - \bar{x})^2 with limits 1 to 5. What total do you get, and what is it divided by 5?
    Show answer10, and 10/5=210/5 = 2, the (population) variance of the data.

Key takeaways

  • ∑i=abf(i)\sum_{i=a}^{b} f(i) means: put i=a,a+1,…,bi = a, a+1, \dots, b into ff and add the results. The index ii is a dummy variable (any letter works) and there are b−a+1b - a + 1 terms. An empty sum equals 0.
  • Rules that hold: ∑c xi=c∑xi\sum c\,x_i = c\sum x_i, ∑(xi+yi)=∑xi+∑yi\sum (x_i + y_i) = \sum x_i + \sum y_i, and ∑i=1nc=nc\sum_{i=1}^{n} c = nc. Rules that do not: ∑xiyi≠(∑xi)(∑yi)\sum x_i y_i \ne (\sum x_i)(\sum y_i) and ∑xi2≠(∑xi)2\sum x_i^2 \ne (\sum x_i)^2.
  • Mean xˉ=1N∑xi\bar{x} = \frac{1}{N}\sum x_i, weighted sum ∑wixi\sum w_i x_i, dot product ∑aibi\sum a_i b_i and mean squared error 1N∑(y^i−yi)2\frac{1}{N}\sum (\hat{y}_i - y_i)^2 are all one Σ.
  • ∏i=1nf(i)\prod_{i=1}^{n} f(i) multiplies instead of adds; an empty product is 1; and ln⁡∏pi=∑ln⁡pi\ln\prod p_i = \sum \ln p_i, 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 aibja_i b_j it equals (∑ai)(∑bj)(\sum a_i)(\sum b_j). A matrix product entry is the nested-sum idea Cij=∑kAikBkjC_{ij} = \sum_k A_{ik}B_{kj}. In Python, math indices start at 1 but lists start at 0.
Found a mistake? Tell us