Interactive exploration of linear regression and the minimization of squared errors.
The goal of Ordinary Least Squares is to find the line that fits the data "best." In statistics, "best" is defined as the line that minimizes the sum of the squared vertical distances (residuals) between the observed data points and the fitted line.
We assume a linear relationship between our independent variable $x$ and dependent variable $y$: $$ y_i = \beta_1 x_i + \beta_0 + \epsilon_i $$ Where $\beta_1$ is the slope, $\beta_0$ is the intercept, and $\epsilon_i$ is the error (residual) for the $i$-th point.
We want to minimize the Sum of Squared Residuals ($S$). The residual for a single point is the difference between the actual $y_i$ and the predicted value $\hat{y}_i = \beta_1 x_i + \beta_0$. $$ S(\beta_0, \beta_1) = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 = \sum_{i=1}^{n} (y_i - (\beta_1 x_i + \beta_0))^2 $$
To find the minimum, we take the partial derivatives of $S$ with respect to $\beta_0$ and $\beta_1$, and set them to zero.
Solving this system of equations yields the closed-form formulas used by the "Snap to OLS" button above: