Part I · Ch. 2 — The Math Toolkit

Part I · Chapter 2 of 13

The Math Toolkit

Vectors, matrices, dot products, slopes, and chance: the five tools everything uses

You’ll need:Learning from Data

Review prerequisite concepts
  • learning = fitting a function f to data

Five tools, then we build

Fitting a function takes five tools: hold data, compare inputs, transform them, find a downhill direction, and describe uncertainty. Each one gets a picture and arithmetic you can check.

This toolkit stands alone. Each tool links to the LLM book for the slower build.

A wide shelf of five outlined cards in a row. Card 1 'Vectors', a single mint arrow rising to the right with '[3, 2]' beside it, job word 'hold'. Card 2 'Dot product', two arrows (mint and amber) sharing a tail with the hint 'x + = 9', job 'compare'. Card 3 'Matrices', a two-by-two violet grid of the numbers 1, 2 over 3, 0 with a small arrow leaving it, job 'transform'. Card 4 'Derivatives', a small mint parabola with a short amber tangent and a downhill arrow, job 'go downhill'. Card 5 'Probability', three small bars in mint, amber and violet at heights 0.5, 0.3 and 0.2, job 'spread belief'.
The whole toolkit. Vectors hold our data, the dot product compares two of them, matrices transform them, derivatives point downhill on a landscape of wrongness, and probability spreads belief across outcomes. Every model on this site is built from these five.
A list becomes an arrow, two arrows become one number, a matrix moves every point at once, and a slope says which way to step. (2:31)

Tool 1. Vectors: arrows and lists

A vector is a list of numbers we can draw as an arrow. Walk 3 blocks east and 2 north from the origin.

Three blocks east and two north reach the arrow's tip 3 blocks east2 blocks north[3, 2]
Three blocks east, two north: the list and arrow describe the same trip.

Vectors use bold lowercase letters. Our pair is $\mathbf{v}=[3,2]$ and $\mathbf{u}=[1,3]$; components $v_1,v_2$ are the entries. In the figures, the input arrow is mint and its partner amber.

Add matching components, or place the arrows tip-to-tail:

$$\mathbf{v} + \mathbf{u} = \begin{bmatrix} 3 \\ 2 \end{bmatrix} + \begin{bmatrix} 1 \\ 3 \end{bmatrix} = \begin{bmatrix} 3 + 1 \\ 2 + 3 \end{bmatrix} = \begin{bmatrix} 4 \\ 5 \end{bmatrix}$$

In words: line up the two lists, add the top numbers to get 4, add the bottom numbers to get 5, and the answer is the vector [4, 5]. That one trip lands where the two trips end up.

  1. First tripWalk 3 east, 2 north.
  2. Second tripFrom that tip, walk 1 east, 3 north.
  3. Combined tripFinish 4 east, 5 north of the origin.

Scaling is multiplying a vector by a plain single number, a scalar. It stretches every component at once.

$$2\mathbf{v} = \begin{bmatrix} 2 \times 3 \\ 2 \times 2 \end{bmatrix} = \begin{bmatrix} 6 \\ 4 \end{bmatrix}$$

In words: double each number, so [3, 2] becomes [6, 4], an arrow twice as long pointing the same way. A positive scalar never changes the direction, which is why direction alone can carry information.

The length is the direct distance to the tip: the long side of a right triangle. Square each component, add, then take the square root.

Two square grids side by side. On the left, a mint arrow runs from the origin to the point 3 across and 2 up; a dashed blue line runs 3 blocks along the bottom labeled '3 blocks east' and a dashed blue line runs 2 blocks up from there labeled '2 blocks north', with a small right-angle marker where they meet. The mint arrow is the long side of that triangle and carries the label 'as the crow flies'. Under the panel: the length of v equals the square root of 3 squared plus 2 squared, equals the square root of 13, about 3.61. On the right, the same construction for an amber arrow reaching 1 block east and 3 blocks north, labeled the length of u equals the square root of 1 squared plus 3 squared, equals the square root of 10, about 3.16.
The arrow cuts across the right triangle: lengths are about $3.61$ and $3.16$ blocks.
$$\lVert \mathbf{v} \rVert = \sqrt{3^2 + 2^2} = \sqrt{9 + 4} = \sqrt{13} \approx 3.61$$

In words: 3 squared is 9, 2 squared is 4, together they make 13, and the square root of 13 is about 3.61, so the arrow reaches about 3.61 blocks as the crow flies. The double bars around $\mathbf{v}$ are how we write "the length of v."

The same recipe gives $\lVert\mathbf{u}\rVert=\sqrt{1+9}=\sqrt{10}\approx3.16$. We need both lengths for cosine similarity next.

A vector with $d$ components uses the same slot-by-slot arithmetic in $d$ dimensions. Drag the widget’s arrows; the longer explanation is Numbers That Point.

A 300-dimensional vector is a list of 300 numbers. Every operation repeats the same arithmetic in each slot. The pictures stop at three dimensions; the arithmetic never changes.

Tool 2. The dot product: one number for agreement

The dot product answers one question with a single number: how much do these two things agree? If our inputs are arrows, the question becomes "do these two arrows point the same way?"

Three small square plots side by side, each with symmetric axes crossing at the origin and the same mint arrow pointing to (3, 2). Left panel: an amber arrow to (1, 3), header 'agree', value 'v · u = 9'. Middle panel: an amber arrow to (2, -3) with a small right-angle marker at the origin, header 'unrelated', value 'v · u = 0'. Right panel: an amber arrow to (-1, -3), header 'oppose', value 'v · u = -9'.
The dot product is positive for agreement, zero for perpendicular arrows, and negative for opposition.

The recipe is to multiply matching components, then add. It is written with a center dot, $\mathbf{v} \cdot \mathbf{u}$, read "v dot u." On our running pair, every digit shown:

$$\mathbf{v} \cdot \mathbf{u} = \begin{bmatrix} 3 \\ 2 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 3 \end{bmatrix} = 3 \times 1 + 2 \times 3 = 9$$

In words: multiply the top numbers (3 times 1 is 3), multiply the bottom numbers (2 times 3 is 6), add those together, and get 9. A positive answer means the arrows agree: they point the same general way.

Worked example: the three verdicts

Keep $\mathbf v=[3,2]$ fixed and swap the partner. A right angle gives no agreement; reversing the partner changes the sign:

PartnerDot productVerdict
$[1,3]$$3×1+2×3=9$Agree
$[2,-3]$$3×2+2×(-3)=0$Unrelated
$[-1,-3]$$3×(-1)+2×(-3)=-9$Oppose

Matching signs contribute positively; opposite signs contribute negatively. Large components contribute more, so the total depends on both direction and length.

The lengths we just measured refine the comparison. The raw dot product grows with length as well as with alignment, so to compare pure directions we divide out both arrows' lengths and get cosine similarity, a score locked between −1 and +1. The dot product 9 divided by both lengths, $3.61 \times 3.16 \approx 11.4$, gives about 0.79. The arrows point the same general way.

  1. Dot product$3×1+2×3=9$.
  2. Remove length$3.61×3.16≈11.4$.
  3. Compare directions$9/11.4≈0.79$.

The full treatment (projection, the geometry, why the answer is a cosine at all) is the sister site's Measuring Similarity. The general form of the recipe is the two products added, $\mathbf{v} \cdot \mathbf{u} = v_1 u_1 + v_2 u_2$.

AngleDot-product signMeaning
Less than a right anglePositiveSome agreement
Right angleZeroPerpendicular
More than a right angleNegativeSome opposition

Perpendicular arrows have a zero dot product: no agreement and no opposition. Opposite arrows point along the same line in reverse and give a negative dot product. “Unrelated to X” and “the opposite of X” are different, and the sign tells them apart.

Tool 3. Matrices: machines that transform vectors

A matrix is a grid of numbers that is also a machine: feed it a vector, it hands back a new vector. This is where a model's learned numbers live. Our matrix is $\mathbf{W} = \left[\begin{smallmatrix} 1 & 2 \\ 3 & 0 \end{smallmatrix}\right]$. In the figures below it is drawn in violet, because weights and parameters are always violet on this site.

RowColumn 1Column 2
112
230
$$\mathbf{W} = \begin{bmatrix} 1 & 2 \\ 3 & 0 \end{bmatrix}$$

In words: a two-by-two grid, meaning two rows and two columns, rows named first. The four numbers are the machine's settings.

Matrices use bold uppercase letters; entries are addressed by row $i$, then column $j$. A 4096-by-4096 matrix follows the same recipe.

The procedure runs on Tool 2. Feed $\mathbf{W}$ the mint vector $\mathbf{v} = [3, 2]$. Each row of $\mathbf{W}$ takes a dot product with the input: row 1's dot product becomes output slot 1, row 2's becomes output slot 2.

Each row dots the same input.
InputWeights (row)Output
[3, 2][1, 2]7
[3, 2][3, 0]9
$$\mathbf{W}\mathbf{v} = \begin{bmatrix} 1 & 2 \\ 3 & 0 \end{bmatrix} \begin{bmatrix} 3 \\ 2 \end{bmatrix} = \begin{bmatrix} 1 \times 3 + 2 \times 2 \\ 3 \times 3 + 0 \times 2 \end{bmatrix} = \begin{bmatrix} 7 \\ 9 \end{bmatrix}$$

In words: row 1 is [1, 2], and [1, 2] dot [3, 2] is 3 plus 4, which is 7. Row 2 is [3, 0], and [3, 0] dot [3, 2] is 9 plus 0, which is 9. Stack the answers and the output vector is [7, 9]. A matrix does dot products in bulk, one per row.

A left-to-right schematic. On the left, the matrix W as a two-by-two grid of violet numbers 1, 2 over 3, 0, with row 1 outlined in mint and row 2 in blue. A thin arrow leads to the middle, where the input vector v is a column of mint numbers 3 over 2. Another arrow leads to the right, where the output Wv is a column of amber numbers 7 over 9. Two curved arrows connect row 1 to the output 7 and row 2 to the output 9. Beneath, two lines of arithmetic, each led by a colored dot: 'row 1: 1x3 + 2x2 = 7' and 'row 2: 3x3 + 0x2 = 9'.
The two rows dot $[3,2]$ to produce $[7,9]$.

Each row gives the mixing proportions for one output; these entries are the model’s weights, adjusted during training.

Feed a matrix the whole plane instead of one vector and it warps the grid, stretching, leaning and rotating space, while straight lines stay straight and the origin stays put. Different entries give a different warp. The slow build (the three faces of a matrix, rotation, the columns trick) is the sister site's Grids That Transform.

Tool 4. Derivatives: which way is downhill

Left: a straight mint ramp climbing from the origin. Two white dots sit on it. A dashed blue horizontal segment between them is labeled 'run: 1 pace' and a dashed amber vertical segment from the end of it up to the second dot is labeled 'rise: up 2'. Beneath the panel: slope equals rise divided by run equals 2 divided by 1 equals 2. Right, under the heading 'the sign says which way': three short line segments — a mint one tilting up labeled 'uphill, slope positive', a gray flat one labeled 'flat, slope zero', and a blue one tilting down labeled 'downhill, slope negative'.
Slope divides the rise by the run, with the sign recording uphill or downhill.

To walk downhill in fog, feel the ground’s local tilt. Training uses the same idea to reduce error.

A slope is rise divided by run: one step right and two up gives $2/1=2$. Positive slopes rise, negative slopes fall, and zero means locally flat.

For $f(x)=x^2$, the derivative $\frac{df}{dx}$, read “the derivative of f with respect to x,” gives the slope at one point:

$$\frac{df}{dx} = 2x$$

In words: the slope at any point is twice the position. At $x = 1$ the slope is 2, at $x = 3$ it is 6, and at $x = 0$ it is 0. Slope zero is flat, and flat is where a valley bottoms out.

Left: the mint parabola f(x) = x squared, with faint dashed guides marking the point at x = 3, height 9, where a blue dot sits. An amber straight line touches the curve at that dot, labeled 'slope 6'. A small box drawn around the dot is labeled 'this step, blown up' with an arrow. Right: that box magnified. At this magnification the curve looks almost straight. A blue dot at (3, 9) and another at (3.1, 9.61) sit on it, joined by a dashed blue horizontal leg labeled 'run 0.1' and a dashed amber vertical leg labeled 'rise 0.61'. Above them: 0.61 divided by 0.1 equals 6.1, and in muted text, shrink the run to 0.01 and it reads 6.01 — homing in on 6, which is 2 times 3.
Near $x=3$, shrinking the measuring interval brings the measured slope toward $6$.

Measure the slope

Run from 3Rise from 9Rise / run
$0.1$$0.61$$6.1$
$0.01$$0.0601$$6.01$
Toward zeroToward zeroToward $6=2×3$

At $x=1$ the limiting slope is $2$; at $x=4$, it is $8$.

Turning that tilt into a walk is the rule behind training. Step opposite the slope: your new position is your old position minus a small step-size times the slope. That step-size is the learning rate, written $\eta$, the Greek letter eta.

$$x_{\text{new}} = x - \eta \, \frac{df}{dx}$$

In words: to get your next position, take the slope where you stand, shrink it by the learning rate, and subtract it. The minus sign is what sends you downhill instead of up.

Worked example: one descent

A mint parabola f(x) = x squared. Blue dots sit on the curve at x = 4 (height 16), x = 2 (height 4), x = 1 (height 1) and x = 0.5, joined by small blue hopping arrows descending into the bowl, with the falling heights 16, 4 and 1 labeled beside them. A short amber tangent line touches the curve at x = 4 with the label 'slope = 8'. Near the bottom a muted note reads 'flat here: slope 0 — the bottom' with an arrow pointing at the origin.
With rate $0.25$, descent follows $4→2→1→0.5$, taking smaller steps as the slope shrinks.

Take $f(x) = x^2$, start at $x = 4$, and use a learning rate of $\eta = 0.25$. That is a quarter of the slope per step, big enough to make visible progress and small enough that a slope as steep as 8 does not throw us clean over the valley.

  1. Point$x=4$, $f(x)=16$; use $\eta=0.25$.
  2. Slope$2×4=8$.
  3. Step$−0.25×8=−2$.
  4. New point$4−2=2$; next: slope $4$, step $−1$, point $1$, then $0.5$.

In words: the steps shrink on their own, because the slope shrinks as the ground flattens near the bottom.

A gradient, written $\nabla L$ and read “the gradient of the loss,” collects one slope per parameter. Loss measures wrongness. Subtract the scaled gradient component by component. For the slow build, read Slopes and Descent.

Tool 5. Probability: belief spread across outcomes

A good model rarely gets to be certain. Asked "cat or dog?" about a blurry photo, it should answer with a spread: "80 percent cat, 20 percent dog." Probability is the language for that spread.

Possible answerCatDog
Certain cat$1$$0$
Blurry photo$0.8$$0.2$
Equal belief$0.5$$0.5$

A bag holds 10 marbles: 5 mint, 3 amber, 2 violet. You draw one without looking. The probability of drawing mint is the share of mint marbles:

$$p(\text{mint}) = \frac{5}{10} = 0.5$$

In words: five of the ten marbles are mint, so the chance of mint is five-tenths, or 0.5. The same counting gives $p(\text{amber}) = 0.3$ and $p(\text{violet}) = 0.2$.

Left panel: ten filled circles inside a thin rounded 'bag' outline — five mint, then three amber, then two violet, grouped by color, with the caption '5 mint · 3 amber · 2 violet'. Right panel: a bar chart with three bars at heights 0.5, 0.3 and 0.2 colored mint, amber and violet, labeled mint, amber and violet, with a y-axis from 0 to 1 labeled 'probability p' and a note 'bars always total 1.00'.
Ten marbles, three bars. The counts on the left become the shares on the right: $p(\text{mint}) = 0.5$, $p(\text{amber}) = 0.3$, $p(\text{violet}) = 0.2$. However the belief is divided, the bars always total 1.

Two rules govern every distribution. Every probability sits between 0 and 1, and across all the outcomes they add up to 1. You have one unit of belief to divide among the outcomes, and no way to make more of it.

$$p(\text{mint}) + p(\text{amber}) + p(\text{violet}) = 0.5 + 0.3 + 0.2 = 1$$

In words: add the three shares, a half plus three-tenths plus two-tenths, and they land on 1, one whole unit of belief accounted for.

OutcomeCountShare
Mint5$0.5$
Amber3$0.3$
Violet2$0.2$

The full list of outcomes with their shares is a probability distribution, and it draws as a bar chart whose heights sum to 1. The probability of something given what you already know, written with a vertical bar and read "given," can differ completely from the plain probability; that is conditional probability. And a distribution can be turned into one concrete choice by sampling, which means rolling a weighted die whose faces match the bars.

  1. DistributionMint 0.5, amber 0.3, violet 0.2.
  2. SampleDraw one marble without looking.
  3. One outcomeAmber can win this draw even though mint is more likely.

The slow build (belief, conditioning, the lumpiness of small samples) is the sister site's Chance and Choice. Chapter 4 gives a model a way to turn a raw score into a probability like these, and multi-class prediction spreads belief across many bars at once.

Where you'll meet this

WhereWhat it looks likeChapter link
Line fittingHold each data point as a small vector, measure wrongness as a landscape, and use derivatives to walk downhill.Fitting a line
ClassificationA dot product becomes a probability.Drawing boundaries
NetworksStack matrix operations and activations.The neuron

What you now know

  • Vectors hold numbers and can represent arrows.
  • Dot products multiply matching entries and add.
  • Matrices transform inputs with one dot product per row.
  • Derivatives measure slopes; gradients collect them for many parameters.
  • Gradient descent subtracts a scaled gradient.
  • Probabilities divide one unit of belief among outcomes.

Where we’re headed. Next we fit a straight line and watch these tools turn examples into a learned rule.