Linear Regression
Least-squares regression chooses the line that minimizes the sum of squared vertical distances. Change the noise and toggle residuals to see what the fit is optimizing.
The amber line is the unique least-squares fit: it minimizes the sum of squared vertical residuals. Pink points have the largest residuals. More noise lowers R², but the fit still passes through the means of x and y.
What you are seeing
Each dot is an observation. The line predicts y from x.
A residual is the vertical gap between a point and the line. Least squares minimizes the sum of squared residuals.
The best-fit slope is covariance divided by variance of x; the intercept makes the line pass through the means.
Why it matters
Linear regression is the starting point for statistics, econometrics, and machine learning. It shows how a model turns data into a prediction and a measurable error.
Complexity & details
Closed-form fit: O(n) time and O(1) extra space for one feature. Prediction is O(1) per point.
Frequently asked questions
Why square the residuals?+
Squaring makes positive and negative errors contribute equally and penalizes large errors more. It also leads to a clean closed-form solution.
What does R² mean?+
R² is the fraction of variance in y explained by the model. A value near 1 means the line captures most of the variation.