Σ and Π are for-loops
A big sigma adds things up in a loop, a big pi multiplies them, and two of them side by side are a nested loop. Run one step at a time and watch.
1. The idea
Σ, a capital Greek sigma, means “add up”. It has three parts: under it, the index and where it starts; above it, where the index stops; to its right, the thing to add for each value of the index. Read Σᵢ₌₁ⁿ xᵢ as: for i from 1 to n, add xᵢ to a running total.
Π, a capital pi, is the same loop with multiplication: start the total at 1 instead of 0, and multiply each term in. Products appear when probabilities of independent events combine. They are usually turned into sums by taking a log, because the log of a product is the sum of the logs, and sums are easier to work with and kinder to floating-point numbers.
Two Σ signs side by side are a nested loop. Σᵢ Σⱼ Wᵢⱼ adds every entry of a matrix: the outer loop over rows, the inner loop over columns.
2. Try it: run the loop
Choose Σ or Π, choose what to add up, and step through the loop. The highlighted line of code is the one that just ran.
3. The math
4. The code (python)
import numpy as np
x = np.array([2.0, 5.0, 1.0, 4.0])
total = 0.0 # the sum of x_i for i = 1..4, as a loop
for i in range(1, 5): # i = 1, 2, 3, 4, as in the maths
total += x[i - 1]
print(total, x.sum()) # 12.0 12.0
product = 1.0 # a product starts at 1, not 0
for xi in x:
product *= xi
print(product, x.prod()) # 40.0 40.0
p = np.array([0.9, 0.8, 0.5]) # the log of a product is the sum of the logs
print(round(np.log(p.prod()), 6), round(np.log(p).sum(), 6)) # -1.021651 -1.0216515. You’ll see this in
- Mean squared error, read with the seven questions a Σ over the dataset
- Reference notes: probability and maximum likelihood a Π of probabilities, turned into a Σ of logs
6. Check yourself
Finished this chapter?
What changed in ML architectures this week
New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.