Visual walkthrough — Simple linear regression model
This page rebuilds the Simple Linear Regression result from nothing. No formula is borrowed. By the end you will have drawn your way to
and you will know what every single symbol is doing on the picture.
Step 1 — The dots, and what a "line" even means here
WHAT. We start with measured points. Point number is written : its horizontal position (the thing we know, e.g. size) and its vertical position (the thing we want, e.g. price). The little subscript is just a name tag — .
WHY. Before choosing a best line we must agree on what family of lines we are even choosing from. We restrict ourselves to straight lines because we believe "one more unit of adds a roughly fixed amount of ."
PICTURE. Look at the figure. A candidate straight line is
The hat on means predicted — the height the line claims at position , as opposed to the real measured height .

- (intercept) slides the whole line up and down.
- (slope) rotates it — steeper means each step right climbs more.
Two knobs . Turning them gives every possible straight line. Our job: find the one good setting.
Intercept knob
Slope knob
Step 2 — The residual: how wrong one point is
WHAT. For a chosen line, at each data point measure the vertical gap between the real dot and the line directly below/above it:
This gap is the residual.
WHY. We need a number that says "how bad is this line, here?" The natural badness at one point is how far the line missed the dot. We measure it vertically, not perpendicular, because is what we predict — a wrong price is a vertical error.
PICTURE. The red segment is . If the dot sits above the line, ; below, ; exactly on it, .

Step 3 — Squaring: turning gaps into a single bowl of cost
WHAT. Square each residual and sum them. This is the RSS (Residual Sum of Squares):
Term by term: is the gap; makes it positive and grows fast for big gaps; collects all points into one total badness.
WHY squaring and not, say, absolute value?
- Sign killed — , above and below count equally, no cancellation.
- Big misses hurt more — a gap of contributes ; a gap of contributes . Outliers get shouted at.
- Smooth — squares have derivatives everywhere, so calculus can find the bottom. Absolute value has a sharp corner calculus dislikes.
PICTURE. Each residual becomes a literal square; the area of every square is ; total inked area is RSS. Then, as we turn the two knobs, RSS traces out a smooth bowl — one lowest point. That lowest point is the best line.

Why does the bowl have exactly one bottom? RSS is a sum of squares of things linear in — that always makes an upward paraboloid. No fake valleys, no ambiguity.
Step 4 — Reading the bottom of the bowl with calculus
WHAT. At the lowest point of any smooth bowl, the ground is flat — the slope in every direction is zero. We have two knobs, so two flatness conditions:
The symbol (a "curly d") means: wiggle only this one knob, hold the other fixed, and ask how RSS changes. Setting it to says "wiggling this knob no longer lowers the cost — we're at the flat bottom for this direction."
WHY this tool? We want the minimum of a smooth function of two variables. The one universal signature of a minimum on smooth ground is zero slope in both directions. Derivatives are exactly the tool that measures slope. That is why calculus enters here and nothing else.
PICTURE. On the bowl, the bottom is where the tangent plane is perfectly horizontal — the two arrows (one per knob direction) both go flat.

Step 5 — The intercept condition: the line must pass through the centre
WHAT. Do the derivative. Using the chain rule on each squared term, :
The divides away; what's left says the residuals must add to zero. Positive gaps above balance negative gaps below.
Now expand and divide by :
Here and (read "x-bar", "y-bar") are just the averages — the balance point of all the 's and all the 's.
WHY it matters. Rearranged, this equation is — the best line always passes through the centre of mass of the cloud. The intercept isn't free; once you pick a tilt , the height is forced so the line pins the centroid.
PICTURE. The green centroid dot ; every best-fit line is a see-saw pivoting through it.

Residuals of the best line always sum to
The best line always passes through
Step 6 — The slope condition: matching tilt to how x and y move together
WHAT. Do the derivative (same chain rule, but each term now also multiplied by ):
Substitute the intercept we just found, , and the constants collapse into centred quantities:
Solve:
Term by term — this is the heart, so read slowly. Recentre every point on the centroid: let (how far right/left of centre) and (how far up/down of centre).
- Numerator — for each point multiply "how far right" by "how far up." A point up-and-right () or down-and-left () gives a positive vote for an upward slope. A point down-and-right () votes negative. This is the covariance — it measures do and rise together?
- Denominator — how spread out the 's are, the variance of . Always positive.
So . It converts an agreement-of-movement into a per-unit tilt.
PICTURE. Each point coloured by the sign of its vote ; the slope leans toward the majority vote, scaled down by the -spread.

Numerator of measures
Denominator of measures
Step 7 — Edge cases: where the formula strains or breaks
WHAT & WHY. A trustworthy derivation must survive its own boundaries. Three cases:
(a) All identical — division by zero. If every house is the same size, for all , so every and the denominator . The formula divides by zero — undefined. Picture: all dots on one vertical line. There is no "rise per step right" because there is no step right. No slope can be defined; regression genuinely has no answer.
(b) No relationship — slope zero. If for every rightward point there's a mirror one that undoes its vote, the numerator , so . The best line is flat at : knowing tells you nothing, so predict the average. That's the right answer, not a failure.
(c) One outlier dragging the line. Because Step 3 squares errors, one far-off dot contributes a giant square and yanks the line toward itself. The formula still runs — it's the choice to square that gives outliers this power. (See 2.5-Assumptions-and-Diagnostics.)
PICTURE. Three little panels: vertical-line (undefined), symmetric cloud (flat), and one point hijacking the tilt.

The one-picture summary
Everything at once: the dots, the centroid the line pivots through (Step 5), the residual squares whose total we shrink (Step 3), and the final fitted line whose tilt is covariance-over-variance (Step 6).

Recall Feynman retelling — say it like a story
We had a scatter of dots and wanted one straight line to represent them. A line has two knobs: how high it sits () and how steeply it tilts (). To score a line, we drop a vertical stick from each dot to the line — that's the miss, the residual. Adding raw misses is useless because ups cancel downs, so we square each miss into a little square of area, add all the areas, and call that total the cost. As we twist the two knobs, the cost traces a smooth bowl with exactly one bottom. Calculus finds that bottom by demanding the ground be flat in both knob-directions. The height knob's flatness says the misses must sum to zero — which forces the line through the dead centre of the cloud. The tilt knob's flatness says: lean the line by exactly how much and rise together, divided by how much spreads out. If never varies, there's no tilt to find (divide by zero). If and don't move together, the tilt is zero and you just guess the average. That's the whole of simple linear regression — two knobs, one bowl, one flat bottom.
Where this goes next: more than one feature → 2.2.02-Multiple-Linear-Regression; solving the bowl by walking downhill instead of algebra → 2.3-Gradient-Descent-for-Linear-Regression; when a straight line is the wrong shape → 4.2-Overfitting-vs-Underfitting; predicting yes/no instead of a number → 3.1-Logistic-Regression-Model.