2.2.4 · D1Linear & Logistic Regression

Foundations — Cost function (MSE) and gradient descent fitting

2,635 words12 min readBack to topic

This page assumes you know nothing. Before you can read the parent note, every squiggle in it must first become a picture in your head. We build them one at a time, each resting on the one before.


1. Dots on a grid — the data

Everything starts with data: a list of measurements. Say we recorded, for three houses, their size and their price. Each house is one row.

Picture: a flat grid. The horizontal axis is , the vertical axis is . Each example becomes one dot whose left-right position is and whose up-down position is .

Figure — Cost function (MSE) and gradient descent fitting

Why the topic needs this: the whole job is to draw a line through these dots. No dots, nothing to fit.

Recall

What does the subscript in mean? ::: It is a counter/label picking out which example — example number — not a mathematical operation.


2. How many dots — the number

Picture: count the dots in the previous figure. There are 3. So .

Why the topic needs this: later we average the errors. To average you must divide by the count, and that count is .


3. The Greek letters — , ,

Mathematicians use Greek letters as short names. They are just names, like using a and b in code.

3a. What and actually do

A straight line needs exactly two pieces of information:

  • — the height where the line crosses the vertical axis (the "intercept"). Slide the whole line up or down.
  • — the tilt / steepness (the "slope"). How much rises for every 1 step right in .
Figure — Cost function (MSE) and gradient descent fitting

For now, just picture these two knobs setting a line's height and tilt on the grid. In section 4 we give the line a proper name, , and write its equation. The full construction lives in 2.2.03-Hypothesis-representation-in-linear-regression.

3b. The subscript which knob we mean

When we have several knobs , we need a way to say "pick out one particular knob to talk about." That job goes to the letter .

3c. What means as a picture

Read left to right: "start the counter at (bottom), stop at (top), plug each value of into the expression, and add all the results into one number."

Why the topic needs it: the cost adds up the miss of every dot into one score, and is the compact way to write "add over all examples".


4. The prediction and the hat

We also write . The hat on ("y-hat") is a universal code for "this is an estimate, not the truth." So:

  • = the real value (a dot).
  • = the value the line gives at that same (a point on the line, directly above/below the dot).
Figure — Cost function (MSE) and gradient descent fitting

Why the topic needs it: the gap between the dot and the line is the whole source of "wrongness". No hat, no way to name our guess.

Recall

What is the difference between and ? ::: is the true measured value (the dot); is the line's prediction at the same (a point on the line).


5. The error / residual

Sign matters and covers all cases:

  • Line above the dot → is positive.
  • Line below the dot → is negative.
  • Line exactly through the dot → (perfect on that point).

Picture: the vertical red segments in figure s03 are the . Their length is how wrong we are; their direction (up/down) is the sign.


6. Squaring, and why ""

The little raised means multiply the number by itself: , .

Two things squaring buys us, as a picture:

  1. Everything becomes positive — a gap of and a gap of both become , so they can't cancel.
  2. Big misses hurt disproportionately — the curve is a smooth bowl (a parabola). Doubling the error quadruples the penalty.
Figure — Cost function (MSE) and gradient descent fitting

Why a smooth bowl matters: a bowl has a single lowest point and a gentle slope everywhere, so we can "roll downhill" to the bottom. The absolute-value shape has a sharp kink at zero where "which way is downhill?" has no clean answer. Smoothness = we can use slopes (next).


7. The cost function — the MSE score

Now we can build the single number the whole topic tries to shrink. We already have, for each example: a residual , then its square . Add all those squares with , and divide by the number of examples to get an average — that average is the Mean Squared Error (MSE): literally the mean (average) of the squared errors.

Why the topic needs it: is the height of the landscape we are about to descend. Everything from here on is "make smaller."

Recall

In words, what does measure? ::: Half the average squared vertical miss of the line across all dots — one number saying how bad the current knob settings are.


8. Slope, derivative, and the gradient

To "roll downhill" we need to measure steepness of that -landscape: at the spot where we're standing, does the cost go up or down if we tweak a knob a little, and by how much?

Picture: stand on the bowl surface. The slope is the tilt of the ground in the direction. Positive slope = ground rises to the right = go left to descend. Negative slope = go right. Zero slope = you're at the flat bottom.

Why the topic needs it: the derivative is the compass. Without it we'd have to guess blindly which way makes the line better.


9. The update arrow and the step size

(the step size) scales how far we move:

  • too big → we leap over the valley bottom and can bounce higher — divergence.
  • too small → correct direction but crawling — many steps.
  • just right → steady march to the bottom.

More on tuning it lives in 4.2.03-Learning-rate-schedules.


Prerequisite map

Dots on a grid xi yi

Count n

A straight line theta0 theta1

Index j picks which knob

Prediction h of x and y-hat

Residual ei equals guess minus actual

Square the errors bowl shape

Average all errors half MSE cost J of theta

Slope derivative gradient nabla J

Update rule with step size alpha

Gradient descent fitting

Each box is a symbol you now own; follow the arrows and you have rebuilt the whole parent note from zero.


Equipment checklist

Test yourself — say the answer aloud before revealing.

  • and ::: The input and the true output of example number — one dot on the grid.
  • ::: How many examples (dots) we have; we divide by it to average.
  • ::: The line's height / intercept — where it crosses the vertical axis.
  • ::: The line's tilt / slope — rise in per unit step in .
  • The index ::: A placeholder picking which knob — ranges over (here two knobs, and ).
  • ::: The hypothesis — the line's predicted height at , equal to ; our guess.
  • ::: The prediction for example (a point on the line), as opposed to the true .
  • ::: The signed vertical gap between line and dot — positive above, negative below, zero on it.
  • ::: A loop that adds one term per example into a single total.
  • Squaring ::: Makes errors positive, punishes big misses more, and gives a smooth bowl we can descend.
  • ::: Half the Mean Squared Error — ; the landscape height we shrink (raw MSE is twice this).
  • ::: The steepness of the cost in the direction of knob — our downhill compass.
  • ::: The arrow of steepest uphill (all the slopes together); we step opposite to it.
  • ::: "Overwrite this knob with a new value" — an instruction, not an equation.
  • ::: The step size; too big diverges, too small crawls.
  • Simultaneous update ::: Compute every slope on the OLD values, then overwrite all knobs at once.

When every reveal feels obvious, you are ready for the main note and its neighbours 2.2.05-Normal-equation and 3.1.02-Convex-optimization.