Type at least 2 letters. Enter opens the first result, Esc closes.

    Start here: notation and Python · Lesson 2 · The language of math, and its code

    Reading math notation: sets, functions, indices, sums and products

    Beginner 55 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: school algebra (solve 2x+3=72x + 3 = 7). Lesson 01 is optional. Time: about 55 minutes, plus the notebook. This lesson has many symbols: do not try to learn the tables at once. Add the recall cards to your review; they bring the symbols back over the next weeks.


    Why this matters #

    Here is the mean squared error of a linear model, as it appears in many books and papers:

    L(w,b)=1m∑i=1m(w⊤x(i)+b−y(i))2.L(w, b) = \frac{1}{m} \sum_{i=1}^{m} \bigl( w^\top x^{(i)} + b - y^{(i)} \bigr)^2 .

    To read it you need sums (∑\sum), indices (x(i)x^{(i)}, y(i)y^{(i)}), vectors and the transpose (w⊤xw^\top x), functions of parameters (L(w,b)L(w, b)) and the idea of a mean (1m∑\frac{1}{m}\sum). All later tracks write their ideas this way: the gradient in Calculus, the likelihood in Probability and Statistics, attention in Linear Algebra lesson 15. Notation is the entry ticket. The good news: the set of symbols is small, and every symbol is a short sentence in plain words.

    1. Predict first #

    2. The picture #

    How to read a formula #

    Take the prediction of a linear model, y^=w⊤x+b\hat y = w^\top x + b. Read it in three passes.

    1. Say it aloud: "y hat equals w transpose x plus b".
    2. Find the roles. The input xx changes from example to example. The parameters ww and bb are fixed while we make a prediction (training chooses them). The output y^\hat y is what we compute. The hat says "an estimate".
    3. Check the shapes. If xx has nn entries, ww must have nn entries too, and w⊤xw^\top x is one number. So y^\hat y is one number.

    When a formula looks hard, do these three passes, then compute it for a tiny case (two or three numbers). The figures below do this for the most common symbols.

    Sums: Σ\Sigma is a loop #

    The capital Greek letter Σ\Sigma (sigma) means "add up". In ∑i=1nai\sum_{i=1}^{n} a_i:

    • ii is the index (the loop variable). It starts at the lower bound 1 (below Σ\Sigma) and goes up by 1 to the upper bound nn (above Σ\Sigma);
    • aia_i is the term: for each value of ii, compute it and add it to the total.

    For example,

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

    In Python it is a for loop with a running total.

    Interactive figure: loading…

    Guess the total, then press Step. Each step gives the index its next value, computes the term, and adds it to the running total; the yellow bar is the term just added. Try all five sums. For "mean", the sum is divided by mm at the end. For the double sum, watch each row: row ii adds up to ai⋅(b1+b2+b3)a_i \cdot (b_1 + b_2 + b_3).

    Quick check

    What is ∑i=13(2i+1)\sum_{i=1}^{3} (2i + 1)?

    Indices: which number in the table? #

    Data in ML is a table. Each row is one example (a sample), for example one house. Each column is one feature, for example the area. With mm samples and nn features the table is an m×nm \times n matrix XX.

    • XijX_{ij} is the entry in row ii and column jj: feature jj of sample ii. Rows come first.
    • x(i)x^{(i)} is sample ii: the whole row ii, a list (vector) of nn numbers. The superscript in parentheses is a label, not a power.
    • xj(i)x^{(i)}_j is entry jj of sample ii, the same number as XijX_{ij}.

    Math counts from 1. Python counts from 0. So XijX_{ij} is X[i-1, j-1] in NumPy (lesson 03 explains the syntax).

    Interactive figure: loading…

    Click a number, a row header or a column header. The readout gives the math name with indices from 1 and the Python index from 0. Rows are samples (blue tint), columns are features (orange tint), and the selection is yellow.

    Quick check

    In the table of the figure, what is x2(4)x^{(4)}_2?

    max and argmax: a value and a place #

    For a function ff on an interval, max⁡xf(x)\max_x f(x) is the largest value of ff: a height. The arg⁡max⁡xf(x)\arg\max_x f(x) is the set of inputs where this largest value is reached: places on the horizontal axis. "Arg" is short for argument, another word for input.

    Interactive figure: loading…

    Drag the blue point to the top of the graph. The aqua dashed line is the maximum value (a height); the yellow marks on the horizontal axis are the argmax (the places). Try "a tie": the maximum is one number, but the argmax has two elements. Try "max at the end": the largest value is at the end of the interval. Then switch "Find" to min.

    Quick check

    Let L(θ)=(θ−3)2+1L(\theta) = (\theta - 3)^2 + 1 for all real θ\theta. What are min⁡θL(θ)\min_\theta L(\theta) and arg⁡min⁡θL(θ)\arg\min_\theta L(\theta)?

    3. Definitions and main results #

    Numbers and sets #

    A set is a collection of distinct objects, its elements. Order and repeats do not matter: {1,2,2,3}={3,1,2}\{1, 2, 2, 3\} = \{3, 1, 2\}, a set with 3 elements.

    Symbol Read it as Example
    N\mathbb{N} the natural numbers {1,2,3,… }\{1, 2, 3, \dots\} (some books include 0)
    Z\mathbb{Z} the integers {…,−2,−1,0,1,2,… }\{\dots, -2, -1, 0, 1, 2, \dots\}
    R\mathbb{R} the real numbers −1.5-1.5, 00, 2\sqrt 2, π\pi
    x∈Ax \in A xx is an element of AA 2∈{1,2,3}2 \in \{1, 2, 3\}
    x∉Ax \notin A xx is not an element of AA 5∉{1,2,3}5 \notin \{1, 2, 3\}
    A⊆BA \subseteq B AA is a subset of BB: every element of AA is in BB {1,2}⊆{1,2,3}\{1, 2\} \subseteq \{1, 2, 3\}
    A∪BA \cup B union: in AA or in BB (or both) {1,2}∪{2,5}={1,2,5}\{1, 2\} \cup \{2, 5\} = \{1, 2, 5\}
    A∩BA \cap B intersection: in AA and in BB {1,2}∩{2,5}={2}\{1, 2\} \cap \{2, 5\} = \{2\}
    A∖BA \setminus B difference: in AA but not in BB {1,2}∖{2,5}={1}\{1, 2\} \setminus \{2, 5\} = \{1\}
    ∅\emptyset the empty set {1}∩{2}=∅\{1\} \cap \{2\} = \emptyset
    ∣A∣\lvert A \rvert the size (number of elements) ∣{1,2,2,3}∣=3\lvert \{1, 2, 2, 3\} \rvert = 3
    A×BA \times B Cartesian product: all pairs (a,b)(a, b) {1,2}×{0}={(1,0),(2,0)}\{1, 2\} \times \{0\} = \{(1, 0), (2, 0)\}

    Set-builder notation describes a set by a rule: {x∈R:x>0}\{x \in \mathbb{R} : x > 0\} is read "the set of all real xx such that x>0x > 0". The colon (some books use a bar ∣\mid) means "such that".

    Intervals are sets of real numbers between two ends. A square bracket includes the end, a round bracket excludes it: [a,b]={x∈R:a≤x≤b}[a, b] = \{x \in \mathbb{R} : a \le x \le b\}, (a,b]={x∈R:a<x≤b}(a, b] = \{x \in \mathbb{R} : a < x \le b\}, and (0,∞)={x∈R:x>0}(0, \infty) = \{x \in \mathbb{R} : x > 0\} (∞\infty is never included).

    Rn\mathbb{R}^n is the set of all lists of nn real numbers, Rn=R×⋯×R\mathbb{R}^n = \mathbb{R} \times \dots \times \mathbb{R}. So (3,−1)∈R2(3, -1) \in \mathbb{R}^2 is a point of the plane, and a house with 3 features is a point of R3\mathbb{R}^3. The complement of AA is Ac=U∖AA^c = U \setminus A, where UU is the set of everything we talk about (the universe).

    Interactive figure: loading…

    Pick an operation and find its region. Circle AA and circle BB sit in the universe U={1,…,10}U = \{1, \dots, 10\}; the result is shaded yellow and listed in set notation. Try "A⊆BA \subseteq B": the region "in AA but not in BB" is empty. Try "disjoint": A∩B=∅A \cap B = \emptyset. For the union, check the rule ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert.

    The rule in the caption is the inclusion–exclusion rule for two finite sets. Why it is true: ∣A∣+∣B∣\lvert A \rvert + \lvert B \rvert counts each element of A∩BA \cap B twice (once in AA, once in BB) and every other element of A∪BA \cup B once. Subtracting ∣A∩B∣\lvert A \cap B \rvert removes the second count. ■\blacksquare

    Functions #

    A function f:X→Yf: X \to Y assigns to each input x∈Xx \in X exactly one output f(x)∈Yf(x) \in Y. XX is the domain (allowed inputs), YY is the codomain (the type of the outputs). The range (or image) is the set of outputs that really occur: {f(x):x∈X}\{f(x) : x \in X\}.

    • f:R→Rf: \mathbb{R} \to \mathbb{R}, x↦x2x \mapsto x^2 is read "ff maps xx to x2x^2". Its codomain is R\mathbb{R}, but its range is [0,∞)[0, \infty), because a square is never negative.
    • ff versus f(x)f(x). ff is the function (the whole rule); f(x)f(x) is one number, the output at xx.
    • A rule that sends one input to two outputs is not a function. "x↦x \mapsto a number whose square is xx" sends 4 to both 2 and −2-2; the function x\sqrt{x} fixes this by choosing the non-negative one.
    • Several inputs. f:R2→Rf: \mathbb{R}^2 \to \mathbb{R}, f(x1,x2)=x1+2x2f(x_1, x_2) = x_1 + 2x_2 takes two numbers (one point of the plane) and gives one number.
    • Parameters after a semicolon. f(x;θ)f(x; \theta) is a function of the input xx whose shape is set by the parameters θ\theta. A linear model is f(x;w,b)=w⊤x+bf(x; w, b) = w^\top x + b. Training changes θ\theta; prediction changes xx. A model is a function.

    Composition. (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)): first apply gg, then apply ff to the result. Read f∘gf \circ g from right to left: the function next to xx acts first. In general f∘g≠g∘ff \circ g \ne g \circ f. A neural network is a composition of layers: f(x)=f3(f2(f1(x)))f(x) = f_3(f_2(f_1(x))), written f3∘f2∘f1f_3 \circ f_2 \circ f_1.

    Inverse. If every yy in the range comes from exactly one xx, then ff has an inverse function f−1f^{-1} with f−1(f(x))=xf^{-1}(f(x)) = x for every xx in the domain. For f(x)=2x+1f(x) = 2x + 1: solve y=2x+1y = 2x + 1 for xx to get f−1(y)=(y−1)/2f^{-1}(y) = (y - 1)/2. The function x↦x2x \mapsto x^2 on R\mathbb{R} has no inverse, since 2 and −2-2 both give 4; on the domain [0,∞)[0, \infty) its inverse is y\sqrt{y}. Note: f−1f^{-1} is not 1/f1/f.

    Interactive figure: loading…

    Compare the two orders. The top row is f∘gf \circ g (first gg), the bottom row is g∘fg \circ f (first ff). The input xx is blue, the value between the machines is aqua, the output is violet. Change xx and the pair of functions; find an input where both orders agree. The last pair is a function and its inverse.

    Quick check

    Let f(x)=x2f(x) = x^2 and g(x)=x−3g(x) = x - 3. What is (g∘f)(1)(g \circ f)(1)?

    Indices, vectors and matrices #

    • A vector x∈Rnx \in \mathbb{R}^n is a list of nn numbers x1,…,xnx_1, \dots, x_n (subscripts count the entries). In this site, as in most ML books, a vector is a column; to save space we write x=(x1,…,xn)x = (x_1, \dots, x_n) in a line.
    • The transpose x⊤x^\top turns the column into a row. For two vectors of the same length, w⊤x=w1x1+⋯+wnxn=∑j=1nwjxjw^\top x = w_1 x_1 + \dots + w_n x_n = \sum_{j=1}^{n} w_j x_j, one number (the dot product; books also write w⋅xw \cdot x or ⟨w,x⟩\langle w, x \rangle).
    • A matrix A∈Rm×nA \in \mathbb{R}^{m \times n} has mm rows and nn columns. Rows first: AijA_{ij} is in row ii, column jj. In NumPy, A.shape == (m, n) and AijA_{ij} is A[i-1, j-1].
    • The data matrix X∈Rm×nX \in \mathbb{R}^{m \times n} has the samples as rows: Xij=xj(i)X_{ij} = x^{(i)}_j. The labels (targets) form a vector y=(y(1),…,y(m))y = (y^{(1)}, \dots, y^{(m)}).
    • x(i)x^{(i)} is not a power. x(2)x^{(2)} is sample 2. A square is written without parentheses, x2x^2, or, for the square of an entry, (xj(i))2\bigl(x^{(i)}_j\bigr)^2.

    Linear Algebra lesson 01 shows what matrices do; here we only need to read them.

    Sums #

    Definition. For numbers am,am+1,…,ana_m, a_{m+1}, \dots, a_n (with integers m≤nm \le n),

    ∑i=mnai=am+am+1+⋯+an.\sum_{i=m}^{n} a_i = a_m + a_{m+1} + \dots + a_n .

    If n<mn < m there are no terms, and the empty sum is 0. The index is a dummy variable: the name does not matter, ∑i=1nai=∑k=1nak\sum_{i=1}^{n} a_i = \sum_{k=1}^{n} a_k, and ii has no meaning outside the sum. A sum over a set is written ∑i∈Sai\sum_{i \in S} a_i: one term for each element of SS.

    Rules. For numbers aia_i, bib_i and a constant cc (a number that does not depend on ii):

    Rule Formula Small check
    constant term ∑i=1nc=nc\sum_{i=1}^{n} c = n c ∑i=145=20\sum_{i=1}^{4} 5 = 20
    linearity ∑i=1n(c ai+bi)=c∑i=1nai+∑i=1nbi\sum_{i=1}^{n} (c\,a_i + b_i) = c \sum_{i=1}^{n} a_i + \sum_{i=1}^{n} b_i ∑i=13(2i+1)=2⋅6+3=15\sum_{i=1}^{3} (2i + 1) = 2 \cdot 6 + 3 = 15
    splitting ∑i=1nai=∑i=1kai+∑i=k+1nai\sum_{i=1}^{n} a_i = \sum_{i=1}^{k} a_i + \sum_{i=k+1}^{n} a_i for 1≤k<n1 \le k < n ∑i=14i=3+7\sum_{i=1}^{4} i = 3 + 7
    shifting the index ∑i=1nai=∑k=0n−1ak+1\sum_{i=1}^{n} a_i = \sum_{k=0}^{n-1} a_{k+1} (put k=i−1k = i - 1) the Python loop for k in range(n)
    swapping a double sum ∑i=1m∑j=1naij=∑j=1n∑i=1maij\sum_{i=1}^{m} \sum_{j=1}^{n} a_{ij} = \sum_{j=1}^{n} \sum_{i=1}^{m} a_{ij} add a table by rows or by columns
    product of sums ∑i=1m∑j=1naibj=(∑i=1mai)(∑j=1nbj)\sum_{i=1}^{m} \sum_{j=1}^{n} a_i b_j = \bigl(\sum_{i=1}^{m} a_i\bigr) \bigl(\sum_{j=1}^{n} b_j\bigr) a=(1,2)a = (1, 2), b=(1,2,3)b = (1, 2, 3): 18=3⋅618 = 3 \cdot 6

    Proof of linearity. Write the sum out and regroup (finite sums can be reordered):

    (ca1+b1)+⋯+(can+bn)=(ca1+⋯+can)+(b1+⋯+bn)=c (a1+⋯+an)+(b1+⋯+bn).\begin{aligned} (c a_1 + b_1) + \dots + (c a_n + b_n) &= (c a_1 + \dots + c a_n) + (b_1 + \dots + b_n) \\ &= c\,(a_1 + \dots + a_n) + (b_1 + \dots + b_n). \end{aligned}

    The constant-term rule is the case ai=1a_i = 1, bi=0b_i = 0: ∑c=c⋅n\sum c = c \cdot n. ■\blacksquare

    Proof of the product of sums. The factor aia_i does not depend on jj, so linearity (pull out a constant) gives ∑jaibj=ai∑jbj\sum_{j} a_i b_j = a_i \sum_{j} b_j. Then B=∑jbjB = \sum_j b_j does not depend on ii, so ∑iaiB=B∑iai\sum_i a_i B = B \sum_i a_i. Together: ∑i∑jaibj=∑iaiB=(∑iai)B\sum_i \sum_j a_i b_j = \sum_i a_i B = \bigl(\sum_i a_i\bigr) B. ■\blacksquare

    A warning. The product of sums has all pairs (i,j)(i, j). The sum ∑iaibi\sum_i a_i b_i has only the pairs with i=ji = j. They are different: for a=(1,2)a = (1, 2), b=(3,4)b = (3, 4) we get ∑iaibi=3+8=11\sum_i a_i b_i = 3 + 8 = 11, but (∑ai)(∑bi)=3⋅7=21\bigl(\sum a_i\bigr)\bigl(\sum b_i\bigr) = 3 \cdot 7 = 21.

    Two closed forms. ∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \dfrac{n(n+1)}{2} and ∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \dfrac{n(n+1)(2n+1)}{6}.

    Why 1+2+⋯+n=n(n+1)/21 + 2 + \dots + n = n(n+1)/2
    1. Call the sum TT. Write it forwards and backwards: T=1+2+⋯+nT = 1 + 2 + \dots + n and T=n+(n−1)+⋯+1T = n + (n-1) + \dots + 1.
    2. Add the two lines term by term. Each pair adds to n+1n + 1: 1+n1 + n, 2+(n−1)2 + (n - 1), and so on.
    3. There are nn pairs, so 2T=n(n+1)2T = n(n+1), and T=n(n+1)/2T = n(n+1)/2. ■\blacksquare

    For n=100n = 100: T=5050T = 5050. Lesson 09 proves the same formula a second way, by induction; the same method proves the formula for ∑i2\sum i^2. The notebook checks both formulas for many nn.

    Sums in ML. The dot product w⊤x=∑j=1nwjxjw^\top x = \sum_{j=1}^{n} w_j x_j (one sum over features). The mean xˉ=1m∑i=1mx(i)\bar x = \frac{1}{m} \sum_{i=1}^{m} x^{(i)} (one sum over samples). The mean squared error of "Why this matters" has a sum over samples, and inside it a dot product, which is a sum over features.

    Quick check

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

    Products #

    ∏i=1nai=a1⋅a2⋯an\prod_{i=1}^{n} a_i = a_1 \cdot a_2 \cdots a_n (Π\Pi is the capital Greek letter pi). The empty product is 1. The factorial is n!=∏i=1nin! = \prod_{i=1}^{n} i, so 5!=1205! = 120 and 0!=10! = 1.

    In ML: the likelihood. If the samples are independent, the probability of the whole data set is the product of the probabilities of the samples: L(θ)=∏i=1mp(x(i);θ)L(\theta) = \prod_{i=1}^{m} p(x^{(i)}; \theta). For a coin with P(heads)=0.7P(\text{heads}) = 0.7 and the data heads, heads, tails: L=0.7⋅0.7⋅0.3=0.147L = 0.7 \cdot 0.7 \cdot 0.3 = 0.147. Products of many small numbers become tiny, so ML works with the logarithm, which turns the product into a sum: log⁡∏iai=∑ilog⁡ai\log \prod_i a_i = \sum_i \log a_i for positive aia_i. Here log⁡0.147=2log⁡0.7+log⁡0.3≈−1.917\log 0.147 = 2 \log 0.7 + \log 0.3 \approx -1.917 (lesson 04 explains the logarithm).

    max, min, argmax, argmin #

    For a function ff on a set DD (often an interval):

    • max⁡x∈Df(x)\max_{x \in D} f(x) is the largest value of ff on DD, if there is one: a number MM with f(x)≤Mf(x) \le M for every x∈Dx \in D and f(x0)=Mf(x_0) = M for some x0∈Dx_0 \in D. min⁡\min is defined the same way.
    • arg⁡max⁡x∈Df(x)={x∈D:f(x)=max⁡z∈Df(z)}\arg\max_{x \in D} f(x) = \{x \in D : f(x) = \max_{z \in D} f(z)\} is the set of places where the maximum is reached. It can have several elements (a tie). When it has one element, books write x⋆=arg⁡max⁡xf(x)x^\star = \arg\max_x f(x) for that element.

    A maximum may not exist. On the open interval (0,1)(0, 1), f(x)=xf(x) = x gets as close to 1 as you like but never reaches it, so there is no maximum. On a closed interval [a,b][a, b], a continuous function always has a maximum and a minimum (the extreme value theorem); they can be at the ends.

    Two patterns appear everywhere in ML:

    • Training: θ⋆=arg⁡min⁡θL(θ)\theta^\star = \arg\min_\theta L(\theta), the parameters with the smallest loss. We want the place θ⋆\theta^\star (the model), not the value L(θ⋆)L(\theta^\star).
    • Prediction: a classifier computes a probability pkp_k for each class kk and predicts y^=arg⁡max⁡kpk\hat y = \arg\max_k p_k, the class (a place), not the probability (a value).

    NumPy's np.argmax returns only the first index of a tie; the math argmax is the whole set.

    Logic shorthand and decorations #

    Symbol Read it as Example
    ∀\forall for all ∀x∈R:x2≥0\forall x \in \mathbb{R}: x^2 \ge 0
    ∃\exists there exists ∃x∈R:x2=2\exists x \in \mathbb{R}: x^2 = 2
    ⇒\Rightarrow implies x>2⇒x2>4x > 2 \Rightarrow x^2 > 4
    ⇔\Leftrightarrow, "iff" if and only if x2=0⇔x=0x^2 = 0 \Leftrightarrow x = 0
    :=:= is defined as xˉ:=1m∑ix(i)\bar x := \frac{1}{m}\sum_i x^{(i)}
    ≈\approx is approximately π≈3.14\pi \approx 3.14
    ∝\propto is proportional to (equal up to a constant factor) p(θ∣D)∝p(D∣θ) p(θ)p(\theta \mid D) \propto p(D \mid \theta)\, p(\theta)
    s.t. such that; in optimization, "subject to" min⁡xf(x)\min_x f(x) s.t. x≥0x \ge 0
    y^\hat y y hat: an estimate or a prediction y^=w⊤x+b\hat y = w^\top x + b
    xˉ\bar x x bar: a mean xˉ=5\bar x = 5
    x⋆x^\star x star: the best (optimal) value θ⋆=arg⁡min⁡θL(θ)\theta^\star = \arg\min_\theta L(\theta)
    x~\tilde x x tilde: a changed or noisy version of xx x~=x+ε\tilde x = x + \varepsilon

    Note that ⇒\Rightarrow goes one way only: x>2⇒x2>4x > 2 \Rightarrow x^2 > 4 is true, but x2>4x^2 > 4 does not imply x>2x > 2 (take x=−3x = -3). The bars ∣⋅∣\lvert \cdot \rvert have several meanings: ∣−3∣=3\lvert -3 \rvert = 3 (absolute value), ∣S∣\lvert S \rvert (size of a set), and in some books ∣A∣\lvert A \rvert (determinant of a matrix). The length (norm) of a vector has double bars: ∥x∥\lVert x \rVert.

    Greek letters in ML #

    Letter Name Usual meaning in ML
    α\alpha alpha a step size or learning rate; a significance level in statistics
    β\beta beta coefficients of a regression; momentum factors β1,β2\beta_1, \beta_2 in Adam
    γ\gamma gamma the discount factor in reinforcement learning; a scale parameter
    δ\delta, Δ\Delta delta a small number; Δ\Delta = a change or difference, Δx=xnew−xold\Delta x = x_{\text{new}} - x_{\text{old}}
    ε\varepsilon epsilon a small positive number; a noise term
    η\eta eta the learning rate
    θ\theta theta the parameters of a model
    λ\lambda lambda the strength of a regularization term; an eigenvalue
    μ\mu mu a mean
    ν\nu nu degrees of freedom; a second mean or measure
    π\pi pi the number 3.14159…3.14159\dots; a policy in reinforcement learning
    ρ\rho rho a correlation coefficient
    σ\sigma, Σ\Sigma sigma σ\sigma: standard deviation, or the sigmoid function; Σ\Sigma: a sum, or a covariance matrix
    τ\tau tau a temperature; a time constant
    ϕ\phi, φ\varphi phi a feature map; the density of the standard normal distribution
    ψ\psi psi a second feature map or network
    ω\omega, Ω\Omega omega a frequency; Ω\Omega: a set of all outcomes

    The same letter can mean different things. σ(z)\sigma(z) with an argument is usually the sigmoid function; σ\sigma alone, next to μ\mu, is a standard deviation. Σ\Sigma with bounds below and above is a sum; Σ\Sigma alone, next to μ\mu in a Gaussian, is a covariance matrix. λ\lambda is a regularization strength in a loss and an eigenvalue in linear algebra. The context decides; good papers define each symbol when it first appears.

    Two more symbols: expectation and big-O

    Expectation. P(A)P(A) is the probability of an event AA. For a random number XX with finitely many values, the expectation (mean) is E[X]=∑xx P(X=x)\mathbb{E}[X] = \sum_x x \, P(X = x), a weighted sum. For a fair die, E[X]=(1+2+⋯+6)/6=3.5\mathbb{E}[X] = (1 + 2 + \dots + 6)/6 = 3.5. Lesson 08 introduces these ideas.

    Big-O. f(n)=O(g(n))f(n) = O(g(n)) means: for large nn, f(n)f(n) is at most a constant times g(n)g(n) (there are C>0C > 0 and n0n_0 with ∣f(n)∣≤C g(n)\lvert f(n) \rvert \le C\, g(n) for all n≥n0n \ge n_0). It describes how cost grows. Adding nn numbers costs O(n)O(n) operations; multiplying two n×nn \times n matrices with the usual loops costs n3n^3 multiplications, O(n3)O(n^3). 3n2+10n=O(n2)3n^2 + 10n = O(n^2), because 3n2+10n≤4n23n^2 + 10n \le 4n^2 for n≥10n \ge 10.

    Answers to section 1. (1) 3+5+7=153 + 5 + 7 = 15. (2) No: ∑iaibi=1⋅3+2⋅4=11\sum_i a_i b_i = 1 \cdot 3 + 2 \cdot 4 = 11, but (1+2)(3+4)=21(1 + 2)(3 + 4) = 21. The product of sums has all four pairs aibja_i b_j; the first sum has only the pairs with i=ji = j. (3) No. In ML papers x(2)x^{(2)} is sample number 2, a label in parentheses; the square is x2x^2.

    4. Worked example #

    (a) Read and compute a loss. Three houses, two features each, and their prices:

    X=(102103),y=(245),w=(1,1),b=1.X = \begin{pmatrix} 1 & 0 \\ 2 & 1 \\ 0 & 3 \end{pmatrix}, \qquad y = \begin{pmatrix} 2 \\ 4 \\ 5 \end{pmatrix}, \qquad w = (1, 1), \qquad b = 1 .

    Compute L(w,b)=1m∑i=1m(w⊤x(i)+b−y(i))2L(w, b) = \frac{1}{m} \sum_{i=1}^{m} \bigl(w^\top x^{(i)} + b - y^{(i)}\bigr)^2.

    1. Shapes. X∈R3×2X \in \mathbb{R}^{3 \times 2}, so m=3m = 3 samples and n=2n = 2 features. ww has 2 entries, so w⊤x(i)w^\top x^{(i)} is defined.
    2. Samples. x(1)=(1,0)x^{(1)} = (1, 0), x(2)=(2,1)x^{(2)} = (2, 1), x(3)=(0,3)x^{(3)} = (0, 3). For example x1(2)=X21=2x^{(2)}_1 = X_{21} = 2.
    3. Predictions y^(i)=w⊤x(i)+b=w1x1(i)+w2x2(i)+b\hat y^{(i)} = w^\top x^{(i)} + b = w_1 x^{(i)}_1 + w_2 x^{(i)}_2 + b:
      • y^(1)=1⋅1+1⋅0+1=2\hat y^{(1)} = 1 \cdot 1 + 1 \cdot 0 + 1 = 2;
      • y^(2)=1⋅2+1⋅1+1=4\hat y^{(2)} = 1 \cdot 2 + 1 \cdot 1 + 1 = 4;
      • y^(3)=1⋅0+1⋅3+1=4\hat y^{(3)} = 1 \cdot 0 + 1 \cdot 3 + 1 = 4.
    4. Errors y^(i)−y(i)\hat y^{(i)} - y^{(i)}: 2−2=02 - 2 = 0, 4−4=04 - 4 = 0, 4−5=−14 - 5 = -1.
    5. Squares: 00, 00, 11. Sum: ∑i=13(… )2=1\sum_{i=1}^{3} (\dots)^2 = 1.
    6. Mean: L(w,b)=13⋅1=13≈0.333L(w, b) = \frac{1}{3} \cdot 1 = \frac13 \approx 0.333.

    (b) A double sum. a=(1,2)a = (1, 2), b=(1,2,3)b = (1, 2, 3). Expand ∑i=12∑j=13aibj\sum_{i=1}^{2} \sum_{j=1}^{3} a_i b_j row by row:

    • i=1i = 1: a1b1+a1b2+a1b3=1+2+3=6a_1 b_1 + a_1 b_2 + a_1 b_3 = 1 + 2 + 3 = 6;
    • i=2i = 2: a2b1+a2b2+a2b3=2+4+6=12a_2 b_1 + a_2 b_2 + a_2 b_3 = 2 + 4 + 6 = 12;
    • total: 6+12=186 + 12 = 18, and indeed (1+2)(1+2+3)=3⋅6=18(1 + 2)(1 + 2 + 3) = 3 \cdot 6 = 18.

    (c) max and argmax on a grid. Let L(θ)=(θ−2)2+1L(\theta) = (\theta - 2)^2 + 1 on the grid θ∈{0,1,2,3,4}\theta \in \{0, 1, 2, 3, 4\}. The values are 5,2,1,2,55, 2, 1, 2, 5. So min⁡θL(θ)=1\min_\theta L(\theta) = 1 and arg⁡min⁡θL(θ)={2}\arg\min_\theta L(\theta) = \{2\}; max⁡θL(θ)=5\max_\theta L(\theta) = 5 and arg⁡max⁡θL(θ)={0,4}\arg\max_\theta L(\theta) = \{0, 4\} (a tie). Now take L(θ)=(θ−1.5)2L(\theta) = (\theta - 1.5)^2 on the same grid: the values are 2.25,0.25,0.25,2.25,6.252.25, 0.25, 0.25, 2.25, 6.25, so the minimum is 0.250.25 and the argmin is {1,2}\{1, 2\}.

    5. In code #

    Python counts from 0, so every math index is shifted by one. sum(...) and np.sum play the role of Σ\Sigma. (Lesson 03 teaches NumPy properly.)

    import numpy as np
    
    X = np.array([[1, 0], [2, 1], [0, 3]])   # 3 samples (rows), 2 features (columns)
    y = np.array([2, 4, 5])
    w = np.array([1, 1]); b = 1
    m, n = X.shape
    print(m, n)                    # 3 2
    print(X[1, 0])                 # x^(2)_1 = 2   (row 2 -> index 1, column 1 -> index 0)
    
    # the loss, written as the two sums of the formula
    total = 0
    for i in range(m):                                      # sum over samples
        y_hat = sum(w[j] * X[i, j] for j in range(n)) + b   # w^T x^(i) + b: sum over features
        total += (y_hat - y[i]) ** 2
    print(total / m)               # 0.3333333333333333
    
    # a double sum equals the product of two sums
    a, bb = np.array([1, 2]), np.array([1, 2, 3])
    print(sum(a[i] * bb[j] for i in range(2) for j in range(3)), np.sum(a) * np.sum(bb))   # 18 18
    
    # argmin on a grid: np.argmin returns only the FIRST index of a tie
    grid = np.array([0, 1, 2, 3, 4])
    L = (grid - 1.5) ** 2
    print(L.min(), grid[np.argmin(L)], grid[L == L.min()])   # 0.25 1 [1 2]
    

    6. Where it shows up #

    • Every loss function is a mean over samples: 1m∑i=1mℓ(f(x(i);θ),y(i))\frac{1}{m} \sum_{i=1}^{m} \ell\bigl(f(x^{(i)}; \theta), y^{(i)}\bigr), where ℓ\ell is the loss of one sample. Mean squared error for regression, cross-entropy for classification (Information Theory).
    • Training is θ⋆=arg⁡min⁡θL(θ)\theta^\star = \arg\min_\theta L(\theta), found with gradient descent (Optimization); prediction with a classifier is y^=arg⁡max⁡kpk\hat y = \arg\max_k p_k.
    • Maximum likelihood maximizes ∏ip(x(i);θ)\prod_i p(x^{(i)}; \theta), in practice the sum ∑ilog⁡p(x(i);θ)\sum_i \log p(x^{(i)}; \theta) (Probability and Statistics).
    • Neural networks are compositions fL∘⋯∘f1f_L \circ \dots \circ f_1 of layers, each a function with its own parameters (Deep Learning).
    • Attention in transformers is written softmax⁡ ⁣(QK⊤/dk)V\operatorname{softmax}\!\bigl(QK^\top / \sqrt{d_k}\bigr) V: matrices, a transpose and a function applied row by row. Each entry of QK⊤QK^\top is a dot product, a sum over the dkd_k dimensions: (QK⊤)st=∑k=1dkQskKtk(QK^\top)_{st} = \sum_{k=1}^{d_k} Q_{sk} K_{tk}.
    • ML for science: a simulation grid or a time series is indexed the same way: uiju_{ij} at grid point (i,j)(i, j), xtx_t at time step tt; numerical schemes are sums over neighbours.

    7. Common mistakes #

    • Reading x(i)x^{(i)} as a power. It is sample ii. The parentheses are the signal.
    • ∑iaibi=(∑iai)(∑ibi)\sum_i a_i b_i = \bigl(\sum_i a_i\bigr)\bigl(\sum_i b_i\bigr). False. The product of sums has all pairs aibja_i b_j. Check with a=(1,2)a = (1, 2), b=(3,4)b = (3, 4): 11 versus 21.
    • Adding a constant once. ∑i=1n(ai+c)=∑iai+nc\sum_{i=1}^{n} (a_i + c) = \sum_i a_i + nc, not ∑iai+c\sum_i a_i + c.
    • Confusing max and argmax. The max is a value (a height); the argmax is a set of places. The trained model is the argmin, not the minimum loss.
    • Reading f∘gf \circ g from left to right. (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)): gg acts first.
    • Off-by-one indices. Math XijX_{ij} is Python X[i-1, j-1]; ∑i=1n\sum_{i=1}^{n} is range(n) with the index shifted, or range(1, n + 1).
    • Rows and columns swapped. A∈Rm×nA \in \mathbb{R}^{m \times n} has mm rows. AijA_{ij}: row first.
    • Writing a set with order or repeats. {1,2,2}={2,1}\{1, 2, 2\} = \{2, 1\} has 2 elements. Use a list or a vector (1,2,2)(1, 2, 2) when order and repeats matter.
    • f−1(x)=1/f(x)f^{-1}(x) = 1/f(x). The inverse function undoes ff; it is not the reciprocal.

    8. Check yourself #

    1. Read aloud and compute: ∣{x∈Z:−2≤x<3}∣\lvert \{x \in \mathbb{Z} : -2 \le x < 3\} \rvert.
      Answer"The number of integers xx with −2≤x<3-2 \le x < 3." The set is {−2,−1,0,1,2}\{-2, -1, 0, 1, 2\} (3 is excluded), so the size is 5.
    2. Compute ∑i=14(3i−1)\sum_{i=1}^{4} (3i - 1) in two ways: term by term, and with the rules.
      AnswerTerm by term: 2+5+8+11=262 + 5 + 8 + 11 = 26. With the rules: 3∑i=14i−∑i=141=3⋅10−4=263 \sum_{i=1}^{4} i - \sum_{i=1}^{4} 1 = 3 \cdot 10 - 4 = 26.
    3. Let f(x)=2xf(x) = 2x and g(x)=x+5g(x) = x + 5. Give formulas for f∘gf \circ g and g∘fg \circ f.
      Answer(f∘g)(x)=f(x+5)=2x+10(f \circ g)(x) = f(x + 5) = 2x + 10. (g∘f)(x)=g(2x)=2x+5(g \circ f)(x) = g(2x) = 2x + 5. They differ.
    4. What is the range of f:R→Rf: \mathbb{R} \to \mathbb{R}, f(x)=x2+1f(x) = x^2 + 1? What is arg⁡min⁡xf(x)\arg\min_x f(x)?
      AnswerThe range is [1,∞)[1, \infty), because x2≥0x^2 \ge 0 and every value y≥1y \ge 1 is reached at x=y−1x = \sqrt{y - 1}. The minimum value is 1 and the argmin is {0}\{0\}.
    5. X∈R100×3X \in \mathbb{R}^{100 \times 3} is a data matrix. How many samples and features? Write x2(7)x^{(7)}_2 as a NumPy expression.
      Answer100 samples (rows) and 3 features (columns). x2(7)=X72x^{(7)}_2 = X_{72} is `X[6, 1]`.
    6. Compute ∏i=14ii+1\prod_{i=1}^{4} \frac{i}{i+1}.
      Answer12⋅23⋅34⋅45=1⋅2⋅3⋅42⋅3⋅4⋅5=15\frac12 \cdot \frac23 \cdot \frac34 \cdot \frac45 = \frac{1 \cdot 2 \cdot 3 \cdot 4}{2 \cdot 3 \cdot 4 \cdot 5} = \frac15. Every factor except the first numerator and the last denominator cancels.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. How do you read {x∈R:x>0}\{x \in \mathbb{R} : x > 0\}, and which interval is it?

    2. State the linearity rule of sums.

    3. What is ∑i=1nc\sum_{i=1}^{n} c for a constant cc?

    4. What does ∑i=1m∑j=1naibj\sum_{i=1}^{m}\sum_{j=1}^{n} a_i b_j equal, and why?

    5. Compute ∏i=14i\prod_{i=1}^{4} i.

    6. What is the difference between max⁡xf(x)\max_x f(x) and arg⁡max⁡xf(x)\arg\max_x f(x)?

    7. In ML notation, what are x(i)x^{(i)} and xj(i)x^{(i)}_j?

    8. A∈Rm×nA \in \mathbb{R}^{m \times n}. What is its NumPy shape, and which Python index gives AijA_{ij}?

    9. In (f∘g)(x)(f \circ g)(x), which function acts first?

    10. Give two meanings of σ\sigma in ML texts.

    Open the review →

    9. Interview angle #

    • "Write the mean squared error of linear regression." — L(w,b)=1m∑i=1m(w⊤x(i)+b−y(i))2L(w, b) = \frac{1}{m}\sum_{i=1}^{m}\bigl(w^\top x^{(i)} + b - y^{(i)}\bigr)^2; in matrix form 1m∥Xw+b1−y∥2\frac{1}{m}\lVert Xw + b\mathbf{1} - y \rVert^2. Say what mm, x(i)x^{(i)} and w⊤xw^\top x are.
    • "What is the difference between max and argmax? What does np.argmax do with ties?" — The max is a value, the argmax a set of places; np.argmax returns the first index of a tie.
    • "Why do we maximize the log-likelihood instead of the likelihood?" — The log turns the product over samples into a sum, which avoids numbers that underflow to 0 and is easier to differentiate; the log is increasing, so the argmax does not change (lesson 04).
    • "What is the shape of XWXW for X∈Rm×nX \in \mathbb{R}^{m \times n} and W∈Rn×kW \in \mathbb{R}^{n \times k}?" — m×km \times k. Shape questions are the most common notation questions in coding interviews.

    10. Practice #

    The notebook for this lesson runs in Colab or in your browser. It uses only plain Python, np.array and np.sum, and every line has a comment. Five short exercises, each with a check cell and a folded solution:

    1. Write ∑i=1ni2\sum_{i=1}^{n} i^2 as a loop and with sum(...), and check the closed form for many nn.
    2. Check that ∑i∑jaibj=(∑a)(∑b)\sum_i \sum_j a_i b_j = (\sum a)(\sum b) and that ∑iaibi\sum_i a_i b_i is different.
    3. Read entries, samples and features of a data table with 1-based math indices.
    4. Compute union, intersection, difference and complement with Python sets, and check inclusion–exclusion.
    5. Find the maximum and the whole argmax of a function on a grid, including a tie.

    11. Watch and read #

    How to read the index, the bounds and the term of a Σ-sum, with examples.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Union and intersection with Venn diagrams, as in the set figure of this lesson.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Composition as one function feeding into another; watch the order of evaluation.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The argmin and argmax operators as used in machine learning.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Goodfellow, Bengio and Courville, Deep Learning, Notation: the notation page of a standard ML textbook; read it after this lesson and check that you know each line.
    • Deisenroth, Faisal and Ong, Mathematics for Machine Learning (free PDF): the table of symbols at the start and the beginning of chapter 2.
    • Lehman, Leighton and Meyer, Mathematics for Computer Science (free PDF), chapter 4 (sets, sequences, functions) and chapter 14 on sums.
    • Andrew Ng and Tengyu Ma, CS229 lecture notes, part I: the x(i)x^{(i)}, y(i)y^{(i)} notation for training data, used in many ML courses.