Velamuvi AIAcademy

Lesson 9 of 12 · Reading Maths

Summation Notation

Learn to read summation notation: the Σ symbol, the index and its limits, the mean and mean squared error formulas, and the product symbol Π, with each one turned into a Python loop.

  • 10 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • How to read the Σ sum symbol and its parts
  • How every sum turns into a simple Python loop
  • How to read the formulas for the mean and the mean squared error
  • What the product symbol Π means

A short way to say "add them all up"

AI papers are full of formulas that add up many values. Writing x₁ + x₂ + x₃ + ... + x₁₀₀₀ would be impossibly long, so mathematicians use one symbol.

  n
  Σ  xᵢ     =   x₁ + x₂ + x₃ + ... + xₙ
 i=1

Read it out loud as: "the sum of x-i, for i from 1 to n".

The parts of a sum

Every sum is a for loop

This is the most useful idea in this lesson: Σ is a for loop with a running total.

Try it yourselfPython
x = [4, 7, 1, 8]          # x₁ to x₄ (Python counts from 0)

total = 0
for i in range(len(x)):
    total += x[i]

print("Sum by loop:", total)
print("Sum by sum():", sum(x))

The sum can also contain a calculation. For example, the sum of the squares of the numbers 1 to 5:

  5
  Σ  i²   =   1 + 4 + 9 + 16 + 25   =   55
 i=1
Try it yourselfPython
total = 0
for i in range(1, 6):        # i from 1 to 5: range stops BEFORE 6
    total += i ** 2
print(total)

Remember that the upper limit is included in maths, but range() stops before its end value. So "from 1 to 5" is range(1, 6).

The mean: your first real formula

        1    n
  x̄  =  -    Σ  xᵢ
        n   i=1

Read it as: "x-bar equals one over n, times the sum of x-i". In plain words: add them all up, then divide by n.

Try it yourselfPython
scores = [72, 85, 90, 64, 89]
n = len(scores)

total = 0
for i in range(n):
    total += scores[i]
mean = total / n

print("Mean score:", mean)

Mean squared error

Here is a formula you will meet in your very first machine learning model. It measures how far a model's predictions ŷᵢ (y-hat) are from the true values yᵢ.

         1    n
  MSE =  -    Σ  (yᵢ - ŷᵢ)²
         n   i=1

Read it piece by piece, from the inside out:

  1. yᵢ - ŷᵢ: the error for one example.
  2. ( )²: square it, so negative and positive errors both count, and big errors count extra.
  3. Σ: add up the squared errors for all examples.
  4. 1/n: divide by the number of examples to get the average.
Try it yourselfPython
actual = [3.0, 5.0, 2.5, 7.0]
predicted = [2.5, 5.0, 4.0, 8.0]

n = len(actual)
total = 0
for i in range(n):
    error = actual[i] - predicted[i]
    total += error ** 2

mse = total / n
print("MSE:", mse)

You just turned a formula from a machine learning textbook into working code. That skill, reading a formula and translating it, is what this module is about.

The product symbol

  4
  Π  i   =   1 × 2 × 3 × 4   =   24
 i=1
Try it yourselfPython
product = 1                  # start at 1 for a product, not 0
for i in range(1, 5):
    product *= i
print(product)

Products of probabilities appear in AI, and as the Exponents lesson showed, they are usually turned into sums of logarithms.

Common mistakes

Practice exercise

Turn a real machine learning formula into a few lines of Python.

Your solutionPython
# Translate the formula for mean absolute error (MAE) into Python:
#
#           1    n
#   MAE  =  -    Σ  |yᵢ - ŷᵢ|
#           n   i=1
#
# |...| means absolute value: use abs().
actual = [10, 12, 8, 15]
predicted = [11, 10, 8, 18]

# Output must be exactly:
# Total absolute error: 6
# MAE: 1.5
Knowledge check

What is the value of Σ (2i) for i from 1 to 3?

Summary

  • Σ means add up. The index runs from the lower limit to the upper limit, which is included.
  • A subscript such as xᵢ picks item i from a list, like x[i] in Python.
  • Every Σ is a for loop with a running total.
  • The mean is (1/n) Σ xᵢ, and the mean squared error is (1/n) Σ (yᵢ - ŷᵢ)².
  • Π means multiply them all together. Start a running product at 1.

Next: the Greek letters you will meet in AI, and what each one usually means.

Finished reading? Tick it off to track your progress.