Simple Linear Regression › Formula

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

Book contents

Section 2 of 10 · 10 min read

Simple Linear Regression › FormulaOne section · Sections in this chapter2 / 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 mathematical foundation of simple linear regression is expressed through a linear equation that describes the relationship between your variables. Let's break this down step by step to understand what each component means and how they work together.

The Basic Linear Model

The core formula for simple linear regression is:

y=β0+β1x+ϵy = \beta_0 + \beta_1 x + \epsilon

where:

  • yy: dependent variable (target/response variable) . what you're trying to predict or explain
  • xx: independent variable (predictor/feature/explanatory variable) . what you're using to make predictions
  • β0\beta_0: intercept term (beta-zero) . the value of yy when x=0x = 0; where the regression line crosses the y-axis
  • β1\beta_1: slope coefficient (beta-one) . the change in yy for each one-unit increase in xx (positive = positive relationship, negative = negative relationship)
  • ϵ\epsilon: error term (epsilon) . the random error component representing the difference between the actual observed value and the model's prediction; captures unmodeled factors, measurement errors, and random variation that influence yy

In summary, yy is the outcome you want to predict, xx is the input feature, β0\beta_0 is the baseline value of yy when xx is zero, β1\beta_1 tells you how yy changes as xx increases, and ϵ\epsilon accounts for everything not explained by the linear relationship.

Visualizing the Linear Model Components

To better understand what the intercept and slope represent, let's visualize them on a regression line. This diagram will help you see where the intercept appears on the graph and how the slope determines the steepness and direction of the line. Pay attention to how the green triangle shows the relationship between changes in x and changes in y.

Out[2]:
Visualization
Scatter plot with regression line showing intercept and slope components.
Understanding the linear model components: intercept and slope. The red line shows the regression equation y = 2 + 2x, where the intercept (β₀ = 2) is where the line crosses the y-axis when x = 0, and the slope (β₁ = 2) represents the change in y for each unit increase in x. The green triangle demonstrates that for every 1-unit increase in x, y increases by 2 units. This visualization helps clarify the geometric meaning of these fundamental parameters in linear regression.

The Least Squares Solution

To find the best-fitting line, we use the method of least squares, which minimizes the sum of squared differences between observed and predicted values. The least squares objective function is:

SSE=∑i=1n(yi−y^i)2=∑i=1n(yi−β0−β1xi)2SSE = \sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_i)^2

where:

  • SSESSE: Sum of Squared Errors (also called Sum of Squared Residuals)
  • yiy_i: observed values
  • y^i\hat{y}_i: predicted values (y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i)
  • β0,β1\beta_0, \beta_1: regression coefficients to be estimated
  • nn: number of observations

The goal is to find the values of β0\beta_0 and β1\beta_1 that minimize this sum. The formulas for calculating the optimal coefficients are.

We have covered the basics of the least squares solution in the Sum of Squared Errors (SSE) section. Refer to it for more a more detailed explanation.

Formula for calculating the slope (β1\beta_1)

Let's break down the slope formula to understand why it works:

β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}

where:

  • β1\beta_1: slope coefficient (change in y per unit change in x, also called the regression coefficient)
  • xix_i: individual x values
  • xˉ\bar{x}: sample mean of x values
  • yiy_i: individual y values
  • yˉ\bar{y}: sample mean of y values
  • nn: number of observations
  1. (xi−xˉ)(x_i - \bar{x}) measures how far each xx value is from the mean of all xx values
  2. (yi−yˉ)(y_i - \bar{y}) measures how far each yy value is from the mean of all yy values
  3. (xi−xˉ)(yi−yˉ)(x_i - \bar{x})(y_i - \bar{y}) captures the relationship between these deviations
  4. The numerator sums these products, giving us the total covariance between xx and yy
  5. The denominator sums the squared deviations of xx from its mean, giving us the variance of xx
  6. Dividing covariance by variance gives us the slope that describes the linear relationship

The numerator in the formula for β1\beta_1:

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

calculates the sample covariance between xx and yy (up to a scaling factor of n−1n-1). Here, the "scaling factor" refers to the denominator used when computing the sample covariance: the sum ∑i=1n(xi−xˉ)(yi−yˉ)\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}) is typically divided by n−1n-1 to obtain the average product of deviations, which corrects for bias in estimating the population covariance from a sample. In the regression slope formula, we use just the numerator (the sum), not the average, so the scaling factor n−1n-1 is omitted.

Covariance measures how two variables change together: if both xx and yy tend to be above or below their means at the same time, the covariance is positive; if one tends to be above its mean when the other is below, the covariance is negative. In the context of regression, this term captures the direction and strength of the linear relationship between xx and yy. By dividing this covariance by the variance of xx (the denominator), we obtain the slope β1\beta_1, which quantifies how much yy changes for a unit change in xx.

Formula for calculating the intercept (β0\beta_0)

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

where:

  • β0\beta_0: intercept (value of y when x = 0)
  • yˉ\bar{y}: sample mean of y values
  • β1\beta_1: slope coefficient
  • xˉ\bar{x}: sample mean of x values

Here, β0\beta_0 is the intercept of the regression line.the value of yy when x=0x = 0. The formula shows that to find the intercept, you first calculate the mean of the yy values (yˉ\bar{y}) and the mean of the xx values (xˉ\bar{x}), then subtract the product of the slope (β1\beta_1) and the mean of xx from the mean of yy.

  • xˉ=1n∑i=1nxi\bar{x} = \dfrac{1}{n} \sum_{i=1}^n x_i is the sample mean of the xx values.
  • yˉ=1n∑i=1nyi\bar{y} = \dfrac{1}{n} \sum_{i=1}^n y_i is the sample mean of the yy values.

This formula ensures that the regression line passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}), meaning the average predicted value equals the average observed value. The intercept β0\beta_0 adjusts the line vertically so that it fits the data according to the least squares criterion.

Direct Substitution Formula

Here is the direct substitution version of the simple linear regression formula, where you can directly plug in the values of xix_i and yiy_i to compute the predicted value y^i\hat{y}_i for any xix_i:

Given nn data points (x1,y1),(x2,y2),…,(xn,yn)(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n), the prediction for yy at a given xix_i is:

y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i

where

β1=∑j=1n(xj−xˉ)(yj−yˉ)∑j=1n(xj−xˉ)2\beta_1 = \frac{\sum_{j=1}^n (x_j - \bar{x})(y_j - \bar{y})}{\sum_{j=1}^n (x_j - \bar{x})^2}

and

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

with

xˉ=1n∑j=1nxjyˉ=1n∑j=1nyj\bar{x} = \frac{1}{n} \sum_{j=1}^n x_j \qquad \bar{y} = \frac{1}{n} \sum_{j=1}^n y_j

With direct substitution, the formula for the predicted value y^i\hat{y}_i is:

y^i=[∑j=1nyjn−(∑j=1nxjyj−1n(∑j=1nxj)(∑j=1nyj))∑j=1nxj2−1n(∑j=1nxj)2⋅∑j=1nxjn]+(∑j=1nxjyj−1n(∑j=1nxj)(∑j=1nyj))∑j=1nxj2−1n(∑j=1nxj)2xi\hat{y}_i = \left[ \frac{\sum_{j=1}^n y_j}{n} - \frac{ \left( \sum_{j=1}^n x_j y_j - \frac{1}{n} \left(\sum_{j=1}^n x_j\right)\left(\sum_{j=1}^n y_j\right) \right) }{ \sum_{j=1}^n x_j^2 - \frac{1}{n} \left(\sum_{j=1}^n x_j\right)^2 } \cdot \frac{\sum_{j=1}^n x_j}{n} \right] + \frac{ \left( \sum_{j=1}^n x_j y_j - \frac{1}{n} \left(\sum_{j=1}^n x_j\right)\left(\sum_{j=1}^n y_j\right) \right) }{ \sum_{j=1}^n x_j^2 - \frac{1}{n} \left(\sum_{j=1}^n x_j\right)^2 } x_i

This formula allows you to compute the predicted value y^i\hat{y}_i for any xix_i directly from the raw data, using only sums over the xjx_j and yjy_j values.

Note: In practice, we would never write the prediction formula in this expanded form. Instead, we would first compute the means xˉ\bar{x} and yˉ\bar{y}, then calculate the slope β1\beta_1 and intercept β0\beta_0, and finally use the compact formula y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i. The expanded version above is shown purely for explanatory purposes, to illustrate how the regression prediction can be written directly in terms of sums over the data. This helps clarify the underlying mechanics, but is not how regression is implemented in real code or statistical software.

Alternative Formulation

The simple linear regression model can also be expressed in a centered form that highlights the relationship between deviations from the mean:

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

This form shows that each prediction y^i\hat{y}_i is the mean of yy plus the slope times the deviation of xix_i from the mean of xx. This formulation is mathematically equivalent to the standard form y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i because:

y^i=β0+β1xi=(yˉ−β1xˉ)+β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 \bar{x} + \beta_1 x_i \\ &= \bar{y} + \beta_1 (x_i - \bar{x}) \end{align*}

This centered form is useful for understanding how the regression line relates to the data's center of mass and how predictions depend on deviations from the mean values.