Simple Linear Regression › Understanding Covariance and Correlation

Michael BrenndoerferApril 7, 202510 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 29Simple Linear Regression › Understanding Covariance and Correlation
Reading section 3 of 10 · Understanding Covariance and Correlation · 10 minSection 3 of 10 · 10 min

Book contents

Section 3 of 10 · 10 min read

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 β1=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2\beta_1 = \frac{\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^n (x_i - \bar{x})^2} is based on the concept of covariance between variables. Let's see how the covariance terms (xi−xˉ)(yi−yˉ)(x_i - \bar{x})(y_i - \bar{y}) work and why they determine the slope of the regression line.

Out[3]:
Visualization
Quadrant analysis showing positive and negative contributions to covariance.
Understanding covariance through quadrant analysis. The plot shows how data points in different quadrants relative to the means (x̄, ȳ) contribute to the covariance calculation. Points in the top-right and bottom-left quadrants (green) contribute positively to covariance, while points in the top-left and bottom-right quadrants (red) contribute negatively. The size of each colored rectangle represents the magnitude of the (x_i - x̄)(y_i - ȳ) term. This visualization helps explain why positive covariance leads to positive slope and negative covariance leads to negative slope in linear regression.
Scatter plots showing correlation strength and regression slopes.
Correlation strength and slope relationship. The plot demonstrates how different correlation strengths affect the regression slope. Strong positive correlation (top) results in a steep positive slope, while weak correlation (middle) produces a shallow slope. Negative correlation (bottom) results in a negative slope. The correlation coefficient r ranges from -1 to 1, and the slope β₁ is proportional to r, scaled by the ratio of standard deviations. This visualization shows why correlation strength directly determines how much y changes for each unit change in x.

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:

y^i=β0+β1xi=(yˉ−β1xˉ)+β1xi=yˉ+β1(xi−xˉ)\begin{align*} \hat{y}_i &= \beta_0 + \beta_1 x_i \\ &= (\bar{y} - \beta_1 \bar{x}) + \beta_1 x_i \\ &= \bar{y} + \beta_1 (x_i - \bar{x}) \end{align*}

Where the slope β1\beta_1 is given by:

β1=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2\beta_1 = \frac{\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^n (x_i - \bar{x})^2}

And the intercept β0\beta_0 is:

β0=yˉ−β1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}

So, the full prediction equation for every data point ii is:

y^i=yˉ+∑j=1n(xj−xˉ)(yj−yˉ)∑j=1n(xj−xˉ)2(xi−xˉ)\hat{y}_i = \bar{y} + \frac{\sum_{j=1}^n (x_j - \bar{x})(y_j - \bar{y})}{\sum_{j=1}^n (x_j - \bar{x})^2} (x_i - \bar{x})

Here, the intercept β0\beta_0 does not appear explicitly because it has been incorporated into the formula using the means of xx and yy. Recall that β0=yˉ−β1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}, so when you expand the prediction equation y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i, you get:

y^i=(yˉ−β1xˉ)+β1xi=yˉ+β1(xi−xˉ)\hat{y}_i = (\bar{y} - \beta_1 \bar{x}) + \beta_1 x_i = \bar{y} + \beta_1 (x_i - \bar{x})

This form centers the prediction around the mean values, making the intercept implicit in the calculation. Thus, β0\beta_0 is not shown separately because its effect is already included through yˉ\bar{y} and the adjustment by xˉ\bar{x}.

This formula shows that each predicted value y^i\hat{y}_i is the mean of yy plus the slope (which measures the relationship between xx and yy) times the deviation of xix_i from the mean of xx.

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 xx 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.

Out[4]:
Visualization
Scatter plot showing least squares method with residuals and squared errors.
The least squares method in action: finding the optimal regression line. The red line shows the fitted line with the minimum sum of squared residuals, while the blue and green lines represent suboptimal fits with higher SSR values. The thick red vertical lines show 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, with their areas proportional to the squared error magnitude. Here''s why the least squares method works: it finds the line that minimizes the total squared distance between all data points and the fitted line.