Math Toolkit 2: Sums, Symbols & Reading Formulas
After this lesson, you will be able to:
- Read the set symbols (in, union, intersection, for all, there exists, such that) that the probability lessons use
- Expand sigma and pi expressions term by term, write them as a Python sum() or loop, and read a real formula such as softmax or the Gaussian density word by word
- Build sigmoid and softmax from exp and a division, and explain why a naive exp overflows for large scores and how subtracting the largest score avoids it
- Describe a slope as rise over run, which is the doorway to the next lessons
#Sets and Symbols
{1, 2, 3} and B = {3, 4}.| Symbol | Read as | Example with the answer |
|---|---|---|
| ∈ | "is in" (and ∉ means "is not in") | 2 ∈ A is true. 5 ∈ A is false, so 5 ∉ A. |
| ∪ | union: everything in either set | A ∪ B = {1, 2, 3, 4} |
| ∩ | intersection: only what is in both | A ∩ B = {3} |
| ∀ | "for all" | "For all x in A, x > 0" is true, because 1, 2 and 3 are all above 0. |
| ∃ | "there exists" | "There exists x in A with x > 2" is true, because x = 3 works. |
| such that | written as a colon or a bar | {x ∈ A : x > 1} reads "the x in A such that x is above 1", which is {2, 3}. |
When a later lesson says an event is a set of outcomes, "A or B" will be the union and "A and B" will be the intersection. Python has the same ideas built in, so you can check each row.
#Sigma and Pi: Sums and Products in One Symbol
Start with a small sum worked by hand: add the first four square numbers, 1 squared, 2 squared, 3 squared and 4 squared. Keep a running total as you go.
| i | Term (i squared) | Running total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 4 | 1 + 4 = 5 |
| 3 | 9 | 5 + 9 = 14 |
| 4 | 16 | 14 + 16 = 30 |
The answer is 30. Sigma is the short way to write that whole table. Here is the plainest case, adding four numbers x_1, x_2, x_3 and x_4:
Here is the same sum of i squared as a plain Python loop. The total starts at 0, and each pass through the loop adds the next term:
total = 0
for i in range(1, 5): # range(1, 5) counts 1, 2, 3, 4 and stops before 5
total = total + i ** 2
print(total) # 30sum(...) adds up everything it is given, so sum(i ** 2 for i in range(1, 5)) is the one-line version of the loop. Later you will also see zip(a, b), which walks along two lists at once and hands you the pair in each position: zip([1, 2, 3], [4, 5, 6]) gives (1, 4), (2, 5), (3, 6).The stepper below does the same table for several expressions.
Pick an expression, press the step button, and watch each term join the running total, then switch to a pi expression and see the running product.
#Four sigma expressions you will meet constantly
Each of these is a sum in disguise. Work through them once and the later lessons will read easily.
Why does only one term survive? Open the derivation to see the sum collapse.
#Pi: the same idea for products
The capital Greek letter pi, written with its big product symbol, means "multiply these" in exactly the same way sigma means "add these". The index, bounds and subscripts work identically.
Now the bridge from Part 1. Take the log of a pi expression and the product rule from the logs section turns it into a sigma expression:
That one line is the whole reason maximum likelihood methods are written as sums of log-probabilities.
The identity above is the product rule from Math Toolkit 1 applied again and again. Open the derivation to see each application.
#Reading a Formula Word by Word
A formula is a sentence. It has a handful of symbols, and once you know the alphabet you can read it left to right. Here is the alphabet that ML uses most.
| Symbol | Name | Typical meaning |
|---|---|---|
| α (alpha) | alpha | a learning rate or a small tuning constant |
| β (beta) | beta | a coefficient, or a momentum setting |
| θ (theta) | theta | the parameters (weights) of a model |
| μ (mu) | mu | the mean of a distribution |
| σ (sigma, lowercase) | sigma | a standard deviation. Do not confuse with capital Σ, which means "sum" |
| λ (lambda) | lambda | a regularisation strength, or a rate |
| ε (epsilon) | epsilon | a tiny number, or noise |
| x with a hat, x̂ | x-hat | an estimate or prediction of x |
| x with a bar, x̄ | x-bar | the mean of x |
| x_i | subscript | the i-th item, or a label such as x_train |
| x squared, x to the n | superscript | a power, or sometimes an index in brackets such as x(i) |
| a over b | fraction bar | divide the top by the bottom |
| arg max | argument of the maximum | the input that gives the largest output, not the largest output itself |
Two reading habits help. First, the same letter can mean different things in different books, so check the line near the formula that says what each symbol stands for. Second, brackets and bars group things. Work the inside first.
#Reading softmax
Softmax is the function that turns a list of raw scores z into probabilities. Before the formula, build it by hand for just two scores, z = [1, 0].
- Make each score positive with exp: exp(1) = 2.718 and exp(0) = 1.
- Add them to get the total: 2.718 + 1 = 3.718.
- Divide each by the total: 2.718 / 3.718 = 0.731 and 1 / 3.718 = 0.269.
The two answers add to 1, and the larger score got the larger share. The formula is that recipe written for any number K of scores (K is just how many classes there are).
Read it word by word:
- softmax(z) with subscript i means "the answer for class number i".
- The top, e to the z_i, takes class i's score and makes it positive.
- The fraction bar says: divide.
- The bottom is a sigma. Its index j runs from 1 to K, meaning "all K classes". It adds up e to the z_j for every class.
- Together: class i's share of the total of all the positive scores.
Two claims from the panic note above need a proof: the answers are between 0 and 1, and they add to exactly 1.
#Reading the Gaussian density
| Piece | What it computes | Value at x = 1 |
|---|---|---|
| x minus mu | distance from the centre | 1 |
| squared | so left and right count the same | 1 |
| divided by two sigma squared | scale by the width | 1 / 2 = 0.5 |
| minus, then exp | 1 at the centre, shrinking outward | exp(-0.5) = 0.6065 |
| times one over sigma root two pi | a constant (pi is the circle number, 3.14159) | 0.3989 x 0.6065 = 0.2420 |
Stack those five pieces together and you have the formula.
Word by word, working from the middle outward:
- x minus mu is how far the point is from the centre.
- Squared, so left and right count the same.
- Divided by two sigma squared, so a wide curve (big sigma) treats the same distance as less surprising.
- A minus sign, then exp. At the centre the exponent is 0 and exp(0) = 1. Away from the centre it goes negative, so exp gives a number between 0 and 1.
- The front factor rescales everything so the total area under the curve is exactly 1. The area under a curve is the amount of space between the curve and the flat axis beneath it. For a probability curve that area is the total probability of every possible outcome, so it must come to exactly 1.
The front constant was stated, not derived. Open the derivation to see why it must be one over sigma root two pi.
#Sigmoid and Softmax: Exp Plus Normalise
| Class | Score z | exp(z) | Probability |
|---|---|---|---|
| A | 2.0 | 7.389 | 7.389 / 11.213 = 0.659 |
| B | 1.0 | 2.718 | 2.718 / 11.213 = 0.242 |
| C | 0.1 | 1.105 | 1.105 / 11.213 = 0.099 |
| Total | 11.213 | 1.000 |
The biggest score gets the biggest share, but every class keeps some probability. Because exp grows quickly, a score gap of one point already gives A nearly three times B's share.
Change one score in the widget above and watch all the probabilities move together, then add 100 to every score and see that nothing changes.
That add-100 switch also exposes a practical trap. The probabilities do not change, but the naive exp of a large score can overflow. A 32-bit float, common on GPUs, tops out near e^88.7, so exp(102) is already inf there, and inf divided by inf is NaN. Plain Python floats are 64-bit and reach much further: math.exp(709) is about 8.2e307, and math.exp(710) raises an OverflowError (NumPy returns inf instead). So the widget's overflow demo is a 32-bit story, and in Python the same trouble starts only near exp(710). The standard fix works for both. Subtract the largest score from every score before applying exp. The largest exponent becomes 0, so nothing overflows, and the probabilities are unchanged because exp(z - m) = exp(z) / exp(m) and the common factor cancels in the division.
Subtracting the largest score is allowed because adding the same constant to every score changes nothing. Open the derivation to see why.
Notice the same σ letter again. Here, sigma(t) is the sigmoid function. Context tells you which sigma you are looking at: capital Σ is a sum, lowercase σ is usually a standard deviation, and σ followed by a bracket is often the sigmoid.
Two short derivations explain the claims about sigmoid made above: that it is softmax for two classes, and that flipping the score swaps the two classes.
Why ML cares: softmax and sigmoid sit at the end of nearly every classifier, and cross-entropy then takes the log of the probability they produce. Exp and log are two halves of one pipeline.
#A Slope Is Rise Over Run
For a straight line like f(x) = 2x + 1 the slope is the same everywhere: 2. For a curve it changes. Take f(x) = x squared and look at the point x = 3 with a shrinking run:
| Between | Rise | Run | Slope |
|---|---|---|---|
| x = 3 and x = 4 | 16 - 9 = 7 | 1 | 7.0 |
| x = 3 and x = 3.1 | 9.61 - 9 = 0.61 | 0.1 | 6.1 |
| x = 3 and x = 3.01 | 9.0601 - 9 = 0.0601 | 0.01 | 6.01 |
As the run shrinks, the slope settles toward 6. That settled value is the slope of the curve exactly at x = 3. Chasing it as the run shrinks to nothing is the idea behind derivatives, and it is where the next lessons pick up. In ML, the slope of the loss tells a model which way to nudge its weights to improve.
The settling value 6 is not luck. Open the derivation to see the slope as a formula in the run.
#Try It Yourself
Tests: Verify the naive product equals 0.0 (underflow), the log-likelihood is a normal finite negative number, the average per event is the log-likelihood divided by 400, and the cross-entropy for p = 0.7 on the true class is about 0.3567.
When you run the solution you should see the naive product print as 0.0, so the "product is zero" check prints True. The log-likelihood is -794.38, the average per event is -1.9859, and the cross-entropy is 0.3567.
#Key Takeaways
- Sigma means sum and pi means product. The log of a pi is a sigma, so a likelihood becomes a log-likelihood.
- Mean, dot product, variance and cross-entropy are all sigma expressions, and each one has a one-line Python sum() version.
- Softmax and sigmoid are exp followed by a normalising division. A slope is rise over run, and a derivative is what that slope settles to when the run shrinks.
- Python floats are 64-bit and overflow near exp(710), while a 32-bit float overflows near exp(88.7). Subtracting the largest score before exp avoids both and leaves the softmax probabilities unchanged.
#Quick Check
What does the sum of i squared, for i running from 1 to 4, equal?