Formula Bro

Math and physics formulas, explained like you are new.


Linear Regression

In one sentence: Linear regression predicts a number by drawing the straight line that sits closest to the data points, so you can read off a guess for any new input.

ŷ=wx+b\hat{y} = w x + b

Read it out loud: “y hat equals w times x, plus b. The predicted y is the slope w times the input x, plus the intercept b. The hat over y means the value is a prediction, not a measurement.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Slope and intercept of a straight line

A straight line can be written as y=wx+by = w x + b. The slope ww says how much yy rises each time xx goes up by one. A slope of 0.7 means a step of 1 in xx adds 0.7 to yy. The intercept bb is the height of the line where x=0x = 0, the point where the line crosses the vertical axis. A slope of 0 is a flat line, and a negative slope is a line that goes downhill.

A bar over a letter, like x‾\bar{x}, means average

A bar placed over a letter, read ‘x bar’, is the average of that list. So x‾\bar{x} is the average of all the xx values. To find it, add up the values and divide by how many there are. The average of 1, 2, 3 and 4 is 1+2+3+44=2.5\frac{1 + 2 + 3 + 4}{4} = 2.5. The average is the balance point of the list, the number that the values spread around.

The sum sign ∑\sum over data points

The Greek capital sigma, ∑\sum, means add up the list. ∑i\sum_{i} means add up over every data point, where ii counts the points: point 1, point 2, and so on. The sum sign is shorthand for writing every term out in full and adding them, so each formula stays short even when there are many data points.

Deviations from the average, like xi−x‾x_i - \bar{x}

A deviation is how far a value sits from the average. If the average hours studied is 2.5 and a student studied 1 hour, the deviation is 1−2.5=−1.51 - 2.5 = -1.5, a negative number because the student is below average. Deviations are positive above the average, negative below it, and they always add up to 0 across the whole list.

Residuals and least squares

A residual is the miss for one data point: the observed value minus the predicted value, yi−ŷiy_i - \hat{y}_i. A line that fits well has small residuals. Least squares is the rule for picking the best line. It squares each residual, adds the squares, and chooses the slope and intercept that make the total as small as possible. Squaring keeps positive and negative misses from canceling out.

What each symbol means

SymbolMeaning

ŷ\hat{y}

The predicted output, the value the line gives for a chosen input xx. The hat marks it as a prediction rather than a measured value. It has the same unit as yy, such as points out of 10 for a quiz score.

xx

The input, a number you already know, such as hours of study. It has whatever unit the input is measured in, for example hours. The line uses xx to produce the prediction, so every guess starts from a chosen value of xx.

ww

The slope, also called the weight. It says how much the prediction changes when xx goes up by one unit. Its unit is the unit of ŷ\hat{y} divided by the unit of xx, so a slope of 0.7 points per hour means each extra hour adds 0.7 points.

bb

The intercept, also called the bias. It is the prediction when xx is 0, and it sets the height where the line crosses the vertical axis. Its unit is the same as the unit of ŷ\hat{y}, such as points on the quiz score scale.

The formula piece by piece

wxw x

Start with the input scaled by the slope. Multiplying xx by ww says how far the prediction moves for this input, because every extra unit of xx adds another ww to the result. A bigger xx gives a bigger change, at a steady rate.

wx+bw x + b

Add the intercept to the scaled input. The intercept lifts or drops the whole line, so the prediction starts at the height bb before the input changes it. Together these two parts give the height of the straight line above any input.

ŷ=wx+b\hat{y} = w x + b

The whole formula in one sentence: the predicted output equals the slope times the input, plus the intercept, so every input gives one point on a straight line.

w=∑i(xi−x‾)(yi−y‾)∑i(xi−x‾)2w = \frac{\sum_{i} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i} (x_i - \bar{x})^2}

The standard least-squares formula for the best slope, written with the averages. The top adds up how the inputs and outputs move together, and the bottom adds up how much the inputs spread out. Dividing the first by the second gives the slope.

What the formula is doing, and why

Linear regression is the simplest way to use one number to predict another. You start with pairs of values: an input xx and the output yy that you actually observed. The formula ŷ=wx+b\hat{y} = w x + b describes a straight line. For any input xx, multiply by the slope ww, add the intercept bb, and you get the predicted output ŷ\hat{y}.

Why choose the least-squares line? The hard part is choosing ww and bb. For each data point, the residual is the observed value minus the predicted value, yi−ŷiy_i - \hat{y}_i. Least squares picks the line that makes the sum of the squared residuals as small as possible. Squaring stops positive and negative misses from canceling out, and it punishes big misses more than small ones.

Why the slope formula looks like that? Let x‾\bar{x} and y‾\bar{y} be the averages of the inputs and the outputs. The top of the slope formula adds up, over every point, the product of the two deviations, (xi−x‾)(yi−y‾)(x_i - \bar{x})(y_i - \bar{y}). Points that are above average in both values push the sum up, and points that are below average in both also push it up. The bottom adds up the squared input deviations, which is how much the inputs spread out. The intercept then follows from the averages: b=y‾−wx‾b = \bar{y} - w \bar{x}. The fitted line always passes through the point (x‾,y‾)(\bar{x}, \bar{y}).

What does the slope tell you? A positive slope means the output tends to rise as the input rises. A negative slope means it tends to fall. A slope near zero says the input tells you little about the output. The line is only as good as the data behind it: when the real relationship curves, a straight line will miss the pattern.

An everyday picture

Think of a teacher with a scatter of dots on graph paper, one dot per student, with hours studied across the bottom and quiz score up the side. Linear regression is the ruler laid across the dots so the line sits as close to all of them as possible. To estimate a new student’s score, find their hours along the bottom and read the height of the ruler above that spot.

Worked example: Predicting a quiz score from hours of study

Four students report their study hours xx and get a quiz score yy out of 10. The data are x=(1,2,3,4)x = (1, 2, 3, 4) hours and y=(2,4,5,4)y = (2, 4, 5, 4) points. We will find the best line and then predict the score for a student who studies 5 hours. The four points are far too few for a real study, so treat this as a demonstration of the method.

  1. Find the average hours. The average of the inputs is x‾=1+2+3+44=2.5\bar{x} = \frac{1 + 2 + 3 + 4}{4} = 2.5 hours. This is the balance point of the study times, and it is the first thing the slope formula needs.
  2. Find the average score. The average of the outputs is y‾=2+4+5+44=3.75\bar{y} = \frac{2 + 4 + 5 + 4}{4} = 3.75 points. The line will always pass through the point made of both averages, (2.5,3.75)(2.5, 3.75).
  3. Find each deviation from the average. For the hours, 1−2.5=−1.51 - 2.5 = -1.5, 2−2.5=−0.52 - 2.5 = -0.5, 3−2.5=0.53 - 2.5 = 0.5 and 4−2.5=1.54 - 2.5 = 1.5. For the scores, 2−3.75=−1.752 - 3.75 = -1.75, 4−3.75=0.254 - 3.75 = 0.25, 5−3.75=1.255 - 3.75 = 1.25 and 4−3.75=0.254 - 3.75 = 0.25. Negative deviations mean below average.
  4. Add up the products of the deviations. The top of the slope formula is ∑(xi−x‾)(yi−y‾)\sum (x_i - \bar{x})(y_i - \bar{y}). Multiplying the matching deviations gives (−1.5)(−1.75)=2.625(-1.5)(-1.75) = 2.625, (−0.5)(0.25)=−0.125(-0.5)(0.25) = -0.125, (0.5)(1.25)=0.625(0.5)(1.25) = 0.625 and (1.5)(0.25)=0.375(1.5)(0.25) = 0.375. Their total is 3.53.5, which says the hours and scores move together.
  5. Add up the squared hour deviations. The bottom of the slope formula is ∑(xi−x‾)2\sum (x_i - \bar{x})^2, which measures how much the hours spread out. We get 2.25+0.25+0.25+2.25=5.02.25 + 0.25 + 0.25 + 2.25 = 5.0.
  6. Divide to get the slope. The slope is w=3.55.0=0.7w = \frac{3.5}{5.0} = 0.7 points per hour. Each extra hour of study is linked, on average, with about 0.7 more points on the quiz.
  7. Find the intercept. The intercept is b=y‾−wx‾=3.75−0.7×2.5=3.75−1.75=2.0b = \bar{y} - w \bar{x} = 3.75 - 0.7 \times 2.5 = 3.75 - 1.75 = 2.0 points. This is the predicted score for zero hours of study, and it places the line so that it passes through the averages.
  8. Predict for 5 hours. Put x=5x = 5 into the line: ŷ=0.7×5+2.0=3.5+2.0=5.5\hat{y} = 0.7 \times 5 + 2.0 = 3.5 + 2.0 = 5.5 points. The prediction is made by reading the height of the line above 5 hours on the bottom axis.

Answer: The best line is ŷ=0.7x+2.0\hat{y} = 0.7x + 2.0. A student who studies 5 hours is predicted to score about 5.5 points out of 10. Each extra hour adds about 0.7 points to the prediction. Four data points is far too few for a real study, so treat this as a demonstration of the method.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

What does least squares mean?

It is the rule for choosing the line. Among all straight lines, pick the one that makes the sum of the squared vertical distances from the points to the line as small as possible.

Is linear regression the same as correlation?

No. Correlation measures how tightly the points cluster around a line, on a scale from −1-1 to 11. Regression gives the actual line, with a slope and an intercept you can use to predict.

How is linear regression different from logistic regression?

Linear regression predicts a number such as a score or a price. Logistic regression predicts the probability of a yes-or-no outcome, using an S-shaped curve.

Why is it called linear?

The prediction changes at a constant rate. Each extra unit of xx adds the same amount, ww, to the prediction, which is exactly what a straight line does.

Sources

  1. Linear regression, Wikipedia
  2. Least squares, Wikipedia

| All AI & ML formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: ai-ml/linear-regression.md.