Fundamentals of AI/ML · Prof. Parikshit Pareek & Prof. Jitin Singla · IIT Roorkee
Given n points (xi, yi), the ordinary least squares (OLS) line minimises the sum of squared vertical distances from each point to the line. The minimiser has a closed form: β̂ = (X⊤X)−1X⊤y. Click to add data points and watch the line — and the matrix arithmetic — update live.
Click anywhere to add a point. Click on an existing point to remove it.
Display options
Fitted line
n = 0 points
—
Slope m = —
Intercept c = —
Error metrics
SSR = Σri2 = —
RMSE = √(SSR/n) = —
R2 = 1 − SSR/SST = —
Normal equations
X⊤X =
X⊤y =
β̂ = (X⊤X)−1X⊤y =
What to notice during class
The OLS line passes through the centroid (x̄, ȳ). Always. This is a one-line consequence of the normal equation for the intercept.
Residuals are vertical, not perpendicular. OLS minimises vertical distance — that's why it changes if you swap x and y. (Total least squares would minimise perpendicular distance — different problem.)
One outlier destroys the fit. Try the "Linear + outlier" preset. The single far-away point swings the entire line because squared error penalises it enormously.
R2 doesn't mean the model is correct. Try the "Quadratic" preset: R2 may still be high, but the line is wrong — the residuals show a U-shape, not random scatter. This is the visual diagnostic.
The matrix formulation generalises. The same β̂ = (X⊤X)−1X⊤y holds for multivariate regression, polynomial fitting, and beyond. Only the design matrix X changes.
Connection to projection. X(X⊤X)−1X⊤ is the hat matrix H — the projection onto the column space of X. This is exactly the MTE Q7 idempotent matrix.