Least Squares Line Fitting

Fundamentals of AI/ML · Prof. Parikshit Pareek & Prof. Jitin Singla · IIT Roorkee
P-squared Lab logo
Integral Lab logo

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: β̂ = (XX)−1Xy. Click to add data points and watch the line — and the matrix arithmetic — update live.

β̂ = arg minβ ‖y − Xβ‖22,   X = [x | 1],   β̂ = (XX)−1Xy
Preset:
data points OLS fit residuals mean (x̄, ȳ)
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

XX =
Xy =
β̂ = (XX)−1Xy =

What to notice during class