Skip to main content

Sigma notation

The capital Greek letter sigma writes a long sum as one symbol, and it reads as a loop: a counter, where it starts and stops, and the term to add. Expand and fold sums, rename the counter, derive three rules, and write the mean, the mean squared error and a distance with it.
M01.7.2L0 Foundation25 minIn review

Before this lesson

M01.7.1 Sequences: lists with a rule M01.2.3 Distance, from the plane to many dimensions

After this lesson you can

  • Read a sum written with a sigma aloud, naming its index, its lower and upper limits and its term, and count its terms.
  • Expand a sigma into its terms and fold a written-out sum back into a sigma, renaming or shifting the index where that helps.
  • Derive the constant-factor, sum-of-sums and constant-term rules by writing out the terms, and use them to simplify a sum.
  • Write the mean, the mean squared error and the distance between two points as sigma expressions and evaluate them on the course’s data.
  • Compute a sum with a loop that keeps a running total, then with sum, numpy.sum and numpy.mean.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

Open almost any machine-learning text and one of the first formulas is the loss of a model. For a model that predicts \(\hat{y}_i\) where the target is \(y_i\), on \(n\) examples, it is written

\[ \closs{L} = \frac{1}{n} \sum_{i=1}^{n} \big(\cmodel{\hat{y}_i} - \ctruth{y_i}\big)^2 . \]

One Greek letter stands in for a whole loop over the data. What exactly does it tell you to do, and how can you work with it without writing out every term?

From the last lesson

1. What is the 20th term of 2, 5, 8, 11, and so on?

Show answer

\({a_{20} = 2 + 19 \times 3 = 59}\).

2. Which term of the doubling sequence 1, 2, 4, 8 is 1024?

Show answer

The 11th. The terms are \({a_n = 2^{\,n-1}}\) and \({1024 = 2^{10}}\), so \({n - 1 = 10}\).

Intuition

The large letter \(\sum\) is the Greek capital sigma, the Greek S, for “sum”. It is an instruction to add things up, and the small writing around it says exactly what to add. Below it sits a counter and where it starts. Above it sits where the counter stops. To its right sits the thing to add for each value of the counter. The figure names each part and runs the loop it stands for, on the course’s four inputs \(x_1\) to \(x_4\), which are 1, 2, 3 and 4.

On the left, a large sigma with 4 above it, i equals 1 below it and a blue x sub i to its right. Pointers name the parts: add up, at the sigma; upper limit, stop at i equals 4, at the 4; lower limit, the counter i starts at 1, at the i equals 1; and the term, what is added for each i, at the x sub i. On the right, under the heading the loop it stands for, a column of vermillion boxes headed total: start 0, then 1, 3, 6 and a yellow 10. Beside each box the counter, i equals 1 to 4, and the term added, x sub 1 equals 1 up to x sub 4 equals 4, and on each green arrow between boxes the amount added, plus 1, plus 2, plus 3, plus 4. Underneath: 1 plus 2 plus 3 plus 4 equals 10.
M01.7.2-F1

A sigma is a loop. The total starts at 0. The counter \(i\) takes the values 1, 2, 3, 4 in turn, and for each value the term \(x_i\) is added on. When the counter passes the upper limit, the loop stops and the total is the value of the sum.

Read the expression aloud as “the sum of \(x_i\) for \(i\) from 1 to 4”. In Python it is a loop that keeps a running total, and the code section writes exactly that loop.

Build it up

Step 1. The parts. In

\[ \sum_{i=1}^{4} x_i \]

the letter \(i\) is the index, a counter. The lower limit 1 is where it starts, the upper limit 4 is where it stops, and \(x_i\) is the term: what gets added for each value of \(i\). The limits are both included, so a sum from 1 to 4 has 4 terms and a sum from 3 to 7 has \({7 - 3 + 1 = 5}\) terms.

Step 2. Writing a sum out. Put each value of the index into the term in turn, and add:

\[ \begin{aligned} \sum_{i=1}^{4} x_i &= x_1 + x_2 + x_3 + x_4 \\ &= 1 + 2 + 3 + 4 = 10 . \end{aligned} \]

The term can be any expression in the index. With \(k^2\) as the term:

\[ \sum_{k=1}^{5} k^2 = 1 + 4 + 9 + 16 + 25 = 55 . \]

Step 3. Folding a sum back. Going the other way, look for what changes from term to term. In \({3 + 6 + 9 + \dots + 30}\) the \(k\)-th term is \(3k\), and the last one, 30, is \({3 \times 10}\), so

\[ 3 + 6 + 9 + \dots + 30 = \sum_{k=1}^{10} 3k . \]

Step 4. The index’s name does not matter. The sums \({\sum_{i=1}^{4} x_i}\) and \({\sum_{j=1}^{4} x_j}\) give the same instructions and the same total. The index is a placeholder, like the name of the loop variable in code.

Step 5. Shifting the index. The doubling numbers \({1 + 2 + 4 + 8 + 16}\) can be counted from 0, with term \(2^k\), or from 1, with term \({2^{\,n-1}}\).

A row of five blue boxes, 1, 2, 4, 8 and 16, joined by plus signs and ending equals 31 in vermillion. Under each box a light grey band holds two rows of labels. Beside the first row, the sum of 2 to the k for k from 0 to 4; under the boxes, k equals 0 to 4 and the powers 2 to the 0 up to 2 to the 4. Beside the second row, the sum of 2 to the n minus 1 for n from 1 to 5; under the boxes, n equals 1 to 5 and the powers written 2 to the 1 minus 1 up to 2 to the 5 minus 1. A green line at the bottom reads: rename the counter, n equals k plus 1: the labels shift, the terms do not.
M01.7.2-F2

One sum, two labellings. Setting \({n = k + 1}\) moves both limits up by 1 and replaces \(k\) by \({n - 1}\) in the term. The five terms, and their total 31, do not change.

So shifting is safe as long as you change the limits and the term together.

The formal statement

NoteDefinition: sigma notation

For whole numbers \({m \le n}\),

\[ \sum_{i=m}^{n} a_i = a_m + a_{m+1} + \dots + a_n . \]

The sum has \({n - m + 1}\) terms. The letter \(i\) is the index, \(m\) the lower limit, \(n\) the upper limit and \(a_i\) the term.

Three rules make sums easy to handle. Each is derived by writing out the terms, using only the laws of arithmetic from M01.1.2.

Rule 1: a constant factor comes outside. For a number \(c\) that is the same in every term,

\[ \sum_{i=1}^{n} c\, a_i = c \sum_{i=1}^{n} a_i . \]

Why. Write out the left side. Every term has the factor \(c\), so the distributive law takes it out of the whole row at once:

\[ \begin{aligned} &c\,a_1 + c\,a_2 + \dots + c\,a_n \\ &\quad = c\,(a_1 + a_2 + \dots + a_n) . \end{aligned} \]

Rule 2: a sum of sums splits.

\[ \sum_{i=1}^{n} (a_i + b_i) = \sum_{i=1}^{n} a_i + \sum_{i=1}^{n} b_i . \]

Why. Write out the left side, take off the brackets, and move every \(b\) to the back. Adding in a different order or grouping gives the same total (the commutative and associative laws):

\[ \begin{aligned} &(a_1 + b_1) + \dots + (a_n + b_n) \\ &\quad = (a_1 + \dots + a_n) + (b_1 + \dots + b_n) . \end{aligned} \]

Rule 3: adding a constant \(n\) times gives \(n\) times it.

\[ \sum_{i=1}^{n} c = n\, c . \]

Why. The term has no \(i\) in it, so every one of the \(n\) terms is \(c\), and \({c + c + \dots + c}\) with \(n\) copies is \({n\,c}\).

Together. Rules 1 and 2, with \({c = -1}\), also split a difference:

\[ \sum_{i=1}^{n} (a_i - b_i) = \sum_{i=1}^{n} a_i - \sum_{i=1}^{n} b_i . \]

There is no such rule for products or squares. The pitfalls below show why.

Worked example

The course’s data are the four points with \({x = 1, 2, 3, 4}\) and \({y = 2, 3, 5, 6}\), and the line \({\hat{y} = 1.4x + 0.5}\), taken as given here (ML01.4 derives it).

The mean. The mean of \(x\) is the sum divided by how many there are:

\[ \begin{aligned} \bar{x} &= \frac{1}{4} \sum_{i=1}^{4} x_i \\ &= \frac{1 + 2 + 3 + 4}{4} = \frac{10}{4} = 2.5 . \end{aligned} \]

In the same way \({\bar{y} = (2 + 3 + 5 + 6)/4 = 4}\), so the mean point is \((2.5, 4)\), as in M01.2.1.

The mean squared error. The line’s predictions are \({\hat{y}_i = 1.4x_i + 0.5}\), which gives 1.9, 3.3, 4.7 and 6.1. Each term of the loss is one error, squared.

Left, a plot titled Four residuals: four blue points at (1, 2), (2, 3), (3, 5) and (4, 6), the vermillion line y-hat equals 1.4 x plus 0.5, and short purple bars from each point to the line, labelled minus 0.1, 0.3, minus 0.3 and 0.1. Right, the formula one quarter of the sum for i from 1 to 4 of (y-hat sub i minus y sub i) squared, then four rows: i equals 1, 1.9 minus 2 equals minus 0.1, squared 0.01; i equals 2, 3.3 minus 3 equals 0.3, squared 0.09; i equals 3, 4.7 minus 5 equals minus 0.3, squared 0.09; i equals 4, 6.1 minus 6 equals 0.1, squared 0.01. Below a line: add the four, 0.2; divide by n equals 4, 0.05 in a yellow box.
M01.7.2-F3

The sigma, one term per point. Each error is a prediction minus a target. Squared, the four errors are 0.01, 0.09, 0.09 and 0.01. They add to 0.2, and dividing by 4 gives 0.05.

Written out, the sigma is

\[ \begin{aligned} L &= \tfrac14 \big[(-0.1)^2 + 0.3^2 \\ &\qquad {} + (-0.3)^2 + 0.1^2\big] \\ &= \tfrac14 \,(0.01 + 0.09 + 0.09 + 0.01) \\ &= \tfrac14 \times 0.2 = 0.05 . \end{aligned} \]

A distance. In M01.2.3 the distance between two points with \(n\) coordinates was written with dots. With a sigma it fits on one line:

\[ d(P, Q) = \sqrt{\sum_{i=1}^{n} (q_i - p_i)^2} . \]

For \({P = (1, 2)}\) and \({Q = (4, 6)}\) the sum is \({3^2 + 4^2 = 25}\), and \({\sqrt{25} = 5}\). In space, from \((1, 2, 3)\) to \((2, 4, 5)\), the sum is \({1 + 4 + 4 = 9}\), and \({\sqrt{9} = 3}\). The square root sits outside the sigma: add every squared change first, then take one root.

Code

A sigma is a loop that keeps a running total. Here it is for the sum and the mean of \(x\).

# the inputs x_1 to x_4
x = [1, 2, 3, 4]
# the targets y_1 to y_4
y = [2, 3, 5, 6]

# the running total
total = 0
# i takes the values 0, 1, 2, 3
for i in range(len(x)):
    # add the term for i
    total = total + x[i]
print("sum of x:", total)
print("mean of x:", total / len(x))
sum of x: 10
mean of x: 2.5

Python’s index runs from 0 to 3, so x[i] covers \(x_1\) to \(x_4\) of the maths. The mean squared error is the same loop with a different term.

# the course's line
w, b = 1.4, 0.5
# running total of squares
squares = 0
for i in range(len(x)):
    # prediction for example i
    y_hat = w * x[i] + b
    squares = squares + (y_hat - y[i]) ** 2
mse = squares / len(x)
print("sum of squared errors:", round(squares, 4))
print("mean squared error:", round(mse, 4))
sum of squared errors: 0.2
mean squared error: 0.05

The total prints as 0.2 after rounding. Unrounded it is a hair off, because a computer stores a number such as 1.4 only approximately (M01.1.1). The distance is one more loop, then a square root.

import math

P, Q = (1, 2), (4, 6)
squares = 0
# one pass per coordinate
for i in range(len(P)):
    squares = squares + (Q[i] - P[i]) ** 2
print("by the sigma:", math.sqrt(squares))
print("by math.dist:", math.dist(P, Q))
by the sigma: 5.0
by math.dist: 5.0

Now the library versions. Python’s sum adds a list for you, and NumPy adds a whole array, or averages it, in one call. The last line is the hook’s formula: the loss in a single line.

import numpy as np

print("sum:", sum(x), "  mean:", sum(x) / len(x))
# NumPy arrays
xs, ys = np.array(x), np.array(y)
print("numpy.sum:", np.sum(xs))
print("numpy.mean:", np.mean(xs), np.mean(ys))
print("loss:", round(np.mean((w * xs + b - ys) ** 2), 4))
sum: 10   mean: 2.5
numpy.sum: 10
numpy.mean: 2.5 4.0
loss: 0.05

Explore

This module’s interactive, the sum builder, lives in lesson M01.7.3. It writes every sum it builds three ways at once: as a sigma, as its terms written out, and as one number.

Pitfalls

Two copies of a 10 by 10 square cut into a 4 by 4 grid of blocks whose sides are 1, 2, 3 and 4, marked along the top and the left edge. Each block is labelled with its area. Left, marked with a cross and titled Square the total: 10 squared equals 100: every block is shaded, the four diagonal squares 1, 4, 9 and 16 in blue and the other twelve, the cross terms 2, 3, 4, 6, 8 and 12 twice each, in purple; underneath: the diagonal 30, plus 70 across it. Right, marked with a tick and titled Square each term: the sum of x sub i squared equals 30: only the four diagonal squares are shaded; underneath: 1 plus 4 plus 9 plus 16 equals 30.
M01.7.2-F4

Wrong beside right, when the goal is \(\sum x_i^2\). Squaring the total counts the whole 10 by 10 square, 100. Adding the squares counts only the four squares on the diagonal, 30.

“The sum of the squares is the square of the sum.”

For \({x = 1, 2, 3, 4}\), the sum of the squares is \({1 + 4 + 9 + 16 = 30}\), but the square of the sum is \({10^2 = 100}\). Squaring the total also counts every product of two different terms, the off-diagonal blocks of the figure, which add to 70. Formulas for spread, such as the variance of S01.3, depend on keeping these two apart.

“A sum of products is the product of the sums.”

For the course’s data,

\[ \begin{aligned} \sum x_i y_i &= 2 + 6 + 15 + 24 \\ &= 47 , \end{aligned} \]

while

\[ \begin{aligned} &\Big(\sum x_i\Big)\Big(\sum y_i\Big) \\ &\quad = 10 \times 16 = 160 . \end{aligned} \]

Rule 1 moves a factor outside only when it is the same in every term, and \(y_i\) changes with \(i\).

“Adding a constant gives the constant.”

A term with no index in it is still added once for each value of the index. So \({\sum_{i=1}^{4} 0.5 = 4 \times 0.5 = 2}\), not 0.5.

“Python’s range(1, n) runs up to n.”

It stops one short. range(1, 4) gives 1, 2, 3, while the sigma from 1 to 4 includes 4. To run over positions 1 to \(n\), write range(1, n + 1), or loop over indices 0 to \({n - 1}\) with range(n) and remember that index \(i\) holds term \({i + 1}\).

Check yourself

Recall

1. Read \({\sum_{i=1}^{4} x_i}\) aloud, and name its four parts.

Show answer

“The sum of \(x_i\) for \(i\) from 1 to 4.” The index is \(i\), the lower limit 1, the upper limit 4, and the term \(x_i\).

2. How many terms does \({\sum_{k=3}^{7} a_k}\) have?

Show answer

Five: \({7 - 3 + 1 = 5}\). They are \(a_3, a_4, a_5, a_6\) and \(a_7\).

3. Write \({1 + 4 + 9 + 16 + 25}\) with a sigma.

Show answer

\({\sum_{k=1}^{5} k^2}\), which is 55.

4. What is \({\sum_{i=1}^{10} 3}\)?

Show answer

\({10 \times 3 = 30}\): the term 3 is added once for each of the ten values of \(i\).

5. Write the mean squared error of predictions \(\hat{y}_i\) against targets \(y_i\), over \(n\) examples, with a sigma.

Show answer

\[ L = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i)^2 . \]

Exercises

1. ★ Write out and evaluate \({\sum_{k=1}^{4} (2k + 1)}\). Then get the same answer from the three rules.

Show solution

Written out: \({3 + 5 + 7 + 9 = 24}\). By the rules, split the sum (rule 2), take out the 2 (rule 1) and add the constant 1 four times (rule 3):

\[ \begin{aligned} \sum_{k=1}^{4} (2k + 1) &= 2 \sum_{k=1}^{4} k + \sum_{k=1}^{4} 1 \\ &= 2 \times 10 + 4 = 24 . \end{aligned} \]

2. ★★ For the course’s inputs, \({\sum x_i = 10}\) and \({\sum x_i^2 = 30}\), with \(i\) from 1 to 4. Use only these two totals and the rules to find \({\sum_{i=1}^{4} (x_i - 2.5)^2}\), then check it term by term.

Show solution

Expand the square:

\[ (x_i - 2.5)^2 = x_i^2 - 5x_i + 6.25 . \]

Then split the sum and use the rules:

\[ \begin{aligned} &\sum_{i=1}^{4} x_i^2 - 5 \sum_{i=1}^{4} x_i + \sum_{i=1}^{4} 6.25 \\ &\quad = 30 - 5 \times 10 + 4 \times 6.25 = 5 . \end{aligned} \]

Term by term, the four squares 2.25, 0.25, 0.25 and 2.25 add to 5. This is the total squared distance of the inputs from their mean, the start of the variance.

3. ★★★ Without using the four errors themselves, show that the errors \({\hat{y}_i - y_i}\) of the line \({\hat{y} = 1.4x + 0.5}\) on the course’s data add to zero. Then say why the loss squares the errors before adding them.

Show solution

Split the sum (rule 2, for a difference), then split \({\hat{y}_i = 1.4x_i + 0.5}\) the same way and use rules 1 and 3:

\[ \begin{aligned} &\sum_{i=1}^{4} (\hat{y}_i - y_i) \\ &\quad = \sum_{i=1}^{4} \hat{y}_i - \sum_{i=1}^{4} y_i \\ &\quad = 1.4 \sum_{i=1}^{4} x_i + 4 \times 0.5 - 16 \\ &\quad = 14 + 2 - 16 = 0 . \end{aligned} \]

The errors \({-0.1, 0.3, -0.3, 0.1}\) do add to 0. A loss that added raw errors would call this line perfect, and would also call perfect a line with errors of \({-5}\) and \({+5}\). Squaring makes every error count as positive, so errors cannot cancel.

Summary card

A sigma is a loop that adds: a counter, its start and stop, and a term.

The anchor figure again: a large sigma from i equals 1 to 4 of x sub i, with its parts named, beside the loop it stands for, a running total that goes 0, 1, 3, 6, 10.
M01.7.2-F1

Start at 0; add one term per value of the index.

  • Definition. \({\sum_{i=m}^{n} a_i = a_m + \dots + a_n}\), with \({n - m + 1}\) terms.
  • Rules. Constants come outside; sums split; \({\sum_{i=1}^{n} c = n\,c}\).
  • No such rule for squares or products: \({\sum x_i^2 \ne \big(\sum x_i\big)^2}\).
  • Mean squared error. \({\frac{1}{n}\sum_{i=1}^{n} (\hat{y}_i - y_i)^2}\), which is 0.05 here.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Jay Abramson and others, Algebra and Trigonometry, 2nd edition (OpenStax), Section 13.4, under “Using Summation Notation”: it calls \(k\) the index of summation, 1 the lower limit of summation and \(n\) the upper limit of summation.
  • Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, Chapter 5, Section 5.1.4, equation 5.4, writes the mean squared error on a test set as \(\frac{1}{m}\) times a sum over the examples of the squared differences between predictions and targets.
  • Python 3.12 documentation, the built-in function sum, which adds a start value, 0 by default, and the items of a list from left to right; and the NumPy reference, numpy.sum.

Next. M01.7.3 Arithmetic and geometric sums adds whole sequences in one line, and watches a geometric total level off.

This lesson unlocks

M01.7.3 Arithmetic and geometric sums ML01.1.4 Learning is choosing the knobs

Your progress is saved in this browser only. No account, no tracking.
Back to top