2.2.3 · D1Linear & Logistic Regression

Foundations — Ordinary least squares derivation

2,665 words12 min readBack to topic

This page is the ground floor. The parent note throws a lot of notation at you very fast: , , , , gradients, transposes. If any of those looked like hieroglyphs, start here. We build every single symbol from a picture before the parent is allowed to use it.


0. The scene, before any symbols

Picture a scatter of dots on graph paper. Horizontal axis = something we know (hours studied). Vertical axis = something we want to predict (exam score). Each dot is one student.

Figure — Ordinary least squares derivation

Our whole job is to draw one line through this cloud. Then, given a new student's study hours, we read the height of the line to guess their score. OLS is just a precise rule for choosing which line — the one whose total up-and-down miss from the dots is as small as possible.

Everything below is the vocabulary needed to say that sentence in mathematics.


1. A data point: , , and the subscript

Why does the topic need this? Because we will have to sum over all dots, and to write "add up something for every dot" cleanly we need a way to say "the -th one".


2. How many dots, how many measurements: and

If we only know "hours studied", then . If we also know "hours slept" and "attendance percent", then . Each feature is one more axis of the room the dots live in.

Recall Why can't we just picture

? Because a 5-feature dot needs 5 axes of position plus 1 for the output = 6 dimensions, and paper is 2-D. ::: We trust the algebra to do what the pictures did in low dimensions.


3. The vector : a dot with several coordinates, and

When there is more than one feature, a single dot's input is not one number but a list of numbers — one per feature. We call an ordered list of numbers a vector.

So has two subscripts: first says which dot, second says which feature. Read it as ", dot , feature ".

Why the topic needs this: real problems have many features, and writing as one vector lets us handle 1 feature or 10,000 features with the same symbol.


4. Parameters and the intercept

Figure — Ordinary least squares derivation

5. Superscript : the transpose, and the dot product

The parent writes the prediction as . Two new pieces of notation: the little , and the hat.

5a. Transpose — turn a column into a row

Why bother? Because to multiply a row by a column in the matrix rules, the shapes have to line up: row-vector times column-vector gives a single number.

5b. The product — multiply matching entries and add

This one number is the prediction: "start at the intercept, then add each slope times its feature". This operation — multiply-matching-and-sum — is called the dot product.

Recall If

and , what is ? . ::: (Here the leading in is the bias slot, so the intercept just adds on.)


6. The prediction hat:

So there are two heights above each dot's horizontal position: the real dot () and the point on our line directly above/below it (). The gap between them is the whole story of OLS.


7. The residual and why we square it

Figure — Ordinary least squares derivation

Why not absolute value , which is also always positive? Because the squaring gives a smooth bowl-shaped cost with a clean derivative — you'll see in the parent that differentiates to , which makes the calculus solvable in one step. Absolute value has a sharp corner and no clean formula.


8. The summation sign and the cost

Now the parent's cost function reads in plain English:


9. Stacking into matrices: , , and

Writing a separate line per dot is tedious. Linear algebra lets us bundle all dots into single objects.

Why the column of 1s? Because . If the first feature is always , then automatically becomes the intercept — no special-case code. That single trick lets us write all predictions at once:


10. The norm : total squared length

Set (the full column of residuals). Then is exactly the sum of squared residuals from Section 8 — just written compactly. So the entire cost collapses to:

Every symbol in that line now has a picture behind it: is the grid of dots, the knobs, the predictions, the residual arrows, the total squared length of those arrows.


11. What "gradient = 0" will mean (a preview)

The parent finds the bottom of the bowl by setting the gradient to zero. Full detail lives in the derivation, but here's the foundation:

That single condition, written out with all the matrix rules, becomes the parent's normal equation . You are now equipped to read that derivation symbol by symbol.


Prerequisite map

Number line R and vectors Rn

Data point xi and yi

Parameters theta and intercept

Prediction via dot product theta dot xi

Residual predicted minus true

Square then sum the cost J

Stack rows into matrix X and vector y

X theta gives all predictions

Norm form of J

Gradient equals zero at the bottom

Normal equation OLS solution


Equipment checklist

Test yourself — say each answer out loud before revealing.

What does the subscript in point to?
A specific data point (dot number ); it never does arithmetic.
What is the difference between and ?
is the true observed output; is our model's prediction.
In , what do the two subscripts mean?
First = which dot (), second = which feature (feature 2).
What operation does perform?
A dot product: multiply each knob by its matching feature and add them all into one number.
What does the transpose do to a column vector?
Flips it into a row vector so shapes line up for multiplication.
Why do we square residuals instead of just adding them?
So up-misses and down-misses can't cancel, and big misses are penalised more; it also gives a smooth, differentiable cost.
What does instruct you to do?
Loop from 1 to , computing the term each time and adding them together.
What is the purpose of the all-1s first column in ?
It multiplies the intercept , so the bias is handled by the same matrix machinery as the slopes.
What does give you in one stroke?
The whole column of predictions for every dot at once.
What does equal in words?
The sum of squared residuals — total squared length of all the miss-arrows.
Why is shaped like a bowl?
Because squaring makes the cost a smooth quadratic; its single lowest point is the best line.
Where is the cost lowest in terms of the gradient?
Where the gradient (the uphill arrow) is zero — the flat bottom of the bowl.

Prerequisites to revisit if any answer stalled: Linear Regression Fundamentals, Feature Scaling, and for what comes next Gradient Descent, Pseudoinverse, Ridge Regression, Maximum Likelihood Estimation.