Simple Linear Regression › Understanding Covariance and Correlation
Machine Learning from ScratchAll chapters Simple Linear Regression › Understanding Covariance and CorrelationOne section · Sections in this chapter3 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The slope formula is based on the concept of covariance between variables. Let's see how the covariance terms work and why they determine the slope of the regression line.


This visualization makes the abstract mathematical concepts of covariance and correlation concrete. The first plot shows how data points in different quadrants contribute to the covariance calculation, while the second plot demonstrates how correlation strength directly determines the regression slope. Understanding these relationships is important for interpreting why the slope formula works and how it captures the linear relationship between variables.
Complete Formula Derivation
The complete formula derivation for the simple linear regression model, showing the prediction for each observation, is:
Where the slope is given by:
And the intercept is:
So, the full prediction equation for every data point is:
Here, the intercept does not appear explicitly because it has been incorporated into the formula using the means of and . Recall that , so when you expand the prediction equation , you get:
This form centers the prediction around the mean values, making the intercept implicit in the calculation. Thus, is not shown separately because its effect is already included through and the adjustment by .
This formula shows that each predicted value is the mean of plus the slope (which measures the relationship between and ) times the deviation of from the mean of .
Mathematical Properties
The least squares solution has several important mathematical properties. First, it's unbiased, meaning that on average, the estimated coefficients will equal the true population values (assuming the model assumptions are met). Second, it's efficient among all unbiased linear estimators, meaning it has the smallest possible variance under the Gauss-Markov conditions (that is, when the errors are uncorrelated, have equal variance, and have zero mean given the predictors.these are known as the "Gauss-Markov assumptions"). Third, the sum of residuals (differences between observed and predicted values) equals zero, and the sum of squared residuals is minimized.
The method also produces a unique solution (unless all values are identical), typically ensuring that there's only one optimal line for any given dataset. This mathematical elegance makes simple linear regression both theoretically sound and practically useful.
Visualizing the Least Squares Method
Let's visualize how it finds the line that minimizes the sum of squared residuals. The key insight is that we're looking for the line where the sum of the squared vertical distances from each point to the line is as small as possible. In this visualization, you'll see three different lines fitted to the same data: the optimal line (red) that minimizes the sum of squared residuals, and two suboptimal lines (blue and green) with higher residual sums. The thick red vertical lines clearly show the residuals (distances from points to the optimal line), while the blue and green dotted lines show residuals for the suboptimal models. The gray squares represent the squared residuals for the optimal line - their areas are proportional to the squared error magnitude, making it easy to see why the least squares method chooses the line with the smallest total squared error.
