Least Squares Calculator

Slope (m)—
Intercept (b)—
Line equation—
Correlation (r)—

How Least Squares Calculator is calculated: Slope m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²; intercept b = ȳ − m × x̄; line y = mx + b

What least squares does with a scatter of points

Least squares is the method that draws the single straight line that fits a cloud of points better than any other straight line. Given pairs of numbers — hours studied and marks scored, advertising spent and sales made — it finds the line that comes closest to all the points at once. That line is the best summary of the relationship between the two variables.

For the default data on this page, x is 1, 2, 3, 4, 5 and y is 2, 4, 5, 4, 5. The least squares line is y = 0.60x + 2.20. It says that each extra unit of x is worth about 0.60 units of y, starting from 2.20 when x is zero. The correlation is 0.77, which means the points follow the line fairly closely.

This calculator takes your x values and y values and returns the slope, the intercept, the equation of the line and the correlation coefficient. Everything runs in your browser, so your data never leaves your device.

How the least squares line is worked out

The line has the form y = mx + b, where m is the slope and b is the intercept. The slope is the covariance of x and y divided by the variance of x: m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)². The intercept is then b = ȳ − m × x̄, which forces the line through the average point (x̄, ȳ).

For the default data, the mean of x is 3 and the mean of y is 4. The products (x − 3)(y − 4) are 4, 0, 0, 0 and 2, adding up to 6. The squared deviations of x are 4, 1, 0, 1 and 4, adding up to 10. The slope is 6 ÷ 10 = 0.60, and the intercept is 4 − 0.60 × 3 = 2.20. The line is y = 0.60x + 2.20.

The name comes from what the line minimises: the sum of the squared vertical distances from each point to the line. Squaring stops positive and negative misses from cancelling out, and it punishes big misses far more than small ones. No other straight line gives a smaller total of squared misses — that is the sense in which this line is the best fit.

Slope and intercept: reading the two numbers

The slope is the input people misread most often, because it looks like a plain number but it is really a rate. A slope of 0.60 means that when x rises by 1, y rises by 0.60 on average. If x is hours studied and y is marks, each extra hour is worth 0.60 marks. If x is advertising in thousands of rupees and y is sales, each extra thousand is worth 0.60 units of sales.

The intercept is the value of y when x is zero — 2.20 for the default data. It is often a mathematical anchor rather than a real prediction: if x is years of experience, nobody has zero experience in a useful sense, and the intercept is just where the line happens to cross the axis. Quote the slope for decisions and treat the intercept as the line's starting point.

A slope of zero means no linear relationship at all — the best line is flat, and x tells you nothing about y. A negative slope means y falls as x rises. The size of the slope depends on the units: measure x in paise instead of rupees and the slope changes a hundredfold, even though the relationship is identical. Compare slopes only when the units match.

A worked example: five points, one line

Run the default data through the steps. The x values are 1, 2, 3, 4, 5 and the y values are 2, 4, 5, 4, 5. The mean of x is 3 and the mean of y is 4. The deviations of x from its mean are −2, −1, 0, 1, 2. The deviations of y are −2, 0, 1, 0, 1.

Multiply the deviations pair by pair: (−2 × −2) = 4, (−1 × 0) = 0, (0 × 1) = 0, (1 × 0) = 0, (2 × 1) = 2. The sum is 6. The squared deviations of x are 4, 1, 0, 1, 4, summing to 10. The slope is 6 ÷ 10 = 0.60. The intercept is 4 − 0.60 × 3 = 2.20, so the line is y = 0.60x + 2.20.

The correlation is 0.77. It comes from the same sums: r = 6 ÷ √(10 × 6) = 6 ÷ 7.75 = 0.77. A correlation of 0.77 means a strong positive relationship — as x rises, y tends to rise — but the points do not sit exactly on the line. At x = 3 the line predicts 4.00, while the actual y is 5, a miss of 1. That miss is part of the scatter least squares accepts.

Who uses a least squares line, and why

Teachers use it to link study time with marks. Plot hours studied against scores, fit the line, and the slope says how much each extra hour is worth. It turns a hunch — “studying more helps” — into a number a student can act on. A average calculator gives the middle of one list; least squares links two lists together.

Shops use it to forecast sales from advertising. If five months of data give a line of sales = 0.60 × advertising + 2.20, then a month with 10 units of advertising predicts sales of about 8.20. The line turns past spending into a plan for next month's budget. An exponent calculator handles the powers behind growth curves; least squares handles the straight-line trend underneath them.

Scientists use it to calibrate instruments. Measure a known standard at several settings, fit the line, and the slope and intercept become the calibration: every future reading is converted through the same line. The correlation tells them whether the instrument is behaving — a correlation near 1 means a clean, reliable calibration.

Least squares vs correlation, and what neither proves

The slope and the correlation describe the same relationship from different angles. The slope says how much y changes per unit of x — 0.60 in the default example. The correlation says how tightly the points hug the line, on a scale from −1 to 1 — 0.77 here. A steep slope with a low correlation means a strong effect buried in noise; a shallow slope with a high correlation means a weak effect measured precisely.

Neither proves cause. Ice cream sales and drownings rise together every summer, and a least squares line through them would look convincing — but the cause is the heat, not the ice cream. Least squares finds association, never causation. Before acting on a line, ask whether a third factor could be driving both variables.

The line also only describes the range of data you have. Predicting y for an x far outside the observed range — extrapolating — is risky, because the relationship may bend or break out there. A line fitted to study hours from 1 to 5 says nothing reliable about 50 hours. A probability calculator can put odds on a prediction landing in a given band, but it cannot fix an extrapolation that has left the data behind.

Common least squares mistakes

The first mistake is pairing the lists wrongly. The x and y lists must have the same length, and the first x goes with the first y, the second with the second, and so on. If the lists differ in length, this calculator returns blank results — check the pairing before anything else, because a shifted column ruins the whole line.

The second mistake is fitting a line to curved data. If the points bend — rising fast, then flattening — a straight line is the wrong shape, and the slope will mislead. Plot the points first; if they curve, a straight line is not the tool. Least squares only answers straight-line questions.

The third mistake is trusting a line fitted to two or three points. With two points the line passes exactly through both, the correlation is a perfect 1, and the result looks flawless — but it is meaningless, because any two points define a line. You need enough points for the scatter to show, ideally ten or more, before the slope means anything.

The fourth mistake is ignoring outliers. One wild point can drag the slope a long way, because squaring punishes big misses hardest. If one point sits far from the rest, check whether it is a typo before trusting the line. A CAGR calculator faces the same problem in reverse — a single unusual year can dominate a growth rate — so in both cases, look at the raw data first.

How to use this least squares calculator

Type or paste your x values into the first box and your y values into the second, separated by commas, spaces, or one per line. The two lists must have the same length and at least two pairs. The slope, intercept, equation and correlation update live as you type.

Read the equation as your prediction rule: put any x into y = 0.60x + 2.20 and you get the predicted y. Read the correlation to judge the fit: near 1 or −1 is tight, near 0 is no relationship, and 0.77 is a strong positive link. Remember the line predicts the trend, not each point exactly.

Everything runs in your browser. No account, no upload, no data sent anywhere — your numbers never leave your device. Use it whenever two lists move together and you want the relationship as a number: study and marks, spending and sales, temperature and demand. The scatter plot shows the cloud; the least squares line draws the sense through it.

Frequently Asked Questions

The slope is 0.60, the intercept is 2.20 and the line is y = 0.60x + 2.20, with a correlation of 0.77. The slope is 6 ÷ 10, where 6 is the sum of (x − 3)(y − 4) and 10 is the sum of (x − 3)². The intercept is 4 − 0.60 × 3 = 2.20.

The slope is how much y changes, on average, when x rises by 1. A slope of 0.60 means each extra unit of x is worth 0.60 units of y. The intercept is the predicted y when x is zero — often just a mathematical anchor, not a real prediction.

The correlation, r, runs from −1 to 1 and measures how tightly the points hug the line. Near 1 or −1 means a strong straight-line relationship; near 0 means none. The default data has r = 0.77, a strong positive link. Correlation never proves that x causes y.

Yes, and this calculator does. If the two lists have different lengths or fewer than two pairs, it returns blank results. The first x must pair with the first y, the second with the second, and so on — a shifted column gives a wrong line, so check the pairing first.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.