Simple Linear Regression › Formula
Machine Learning from ScratchAll chapters 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:
where:
- : dependent variable (target/response variable) . what you're trying to predict or explain
- : independent variable (predictor/feature/explanatory variable) . what you're using to make predictions
- : intercept term (beta-zero) . the value of when ; where the regression line crosses the y-axis
- : slope coefficient (beta-one) . the change in for each one-unit increase in (positive = positive relationship, negative = negative relationship)
- : 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
In summary, is the outcome you want to predict, is the input feature, is the baseline value of when is zero, tells you how changes as increases, and 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.

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:
where:
- : Sum of Squared Errors (also called Sum of Squared Residuals)
- : observed values
- : predicted values ()
- : regression coefficients to be estimated
- : number of observations
The goal is to find the values of and 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 ()
Let's break down the slope formula to understand why it works:
where:
- : slope coefficient (change in y per unit change in x, also called the regression coefficient)
- : individual x values
- : sample mean of x values
- : individual y values
- : sample mean of y values
- : number of observations
- measures how far each value is from the mean of all values
- measures how far each value is from the mean of all values
- captures the relationship between these deviations
- The numerator sums these products, giving us the total covariance between and
- The denominator sums the squared deviations of from its mean, giving us the variance of
- Dividing covariance by variance gives us the slope that describes the linear relationship
The numerator in the formula for :
calculates the sample covariance between and (up to a scaling factor of ). Here, the "scaling factor" refers to the denominator used when computing the sample covariance: the sum is typically divided by 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 is omitted.
Covariance measures how two variables change together: if both and 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 and . By dividing this covariance by the variance of (the denominator), we obtain the slope , which quantifies how much changes for a unit change in .
Formula for calculating the intercept ()
where:
- : intercept (value of y when x = 0)
- : sample mean of y values
- : slope coefficient
- : sample mean of x values
Here, is the intercept of the regression line.the value of when . The formula shows that to find the intercept, you first calculate the mean of the values () and the mean of the values (), then subtract the product of the slope () and the mean of from the mean of .
- is the sample mean of the values.
- is the sample mean of the values.
This formula ensures that the regression line passes through the point , meaning the average predicted value equals the average observed value. The intercept 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 and to compute the predicted value for any :
Given data points , the prediction for at a given is:
where
and
with
With direct substitution, the formula for the predicted value is:
This formula allows you to compute the predicted value for any directly from the raw data, using only sums over the and values.
Note: In practice, we would never write the prediction formula in this expanded form. Instead, we would first compute the means and , then calculate the slope and intercept , and finally use the compact formula . 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:
This form shows that each prediction is the mean of plus the slope times the deviation of from the mean of . This formulation is mathematically equivalent to the standard form because:
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.