Mathematics, Statistics & Geometry

Linear Regression Calculator

Perform Ordinary Least Squares (OLS) regression on any dataset. Calculate the line of best fit, R-squared variance, and Pearson correlation.

Line of Best Fit
y = 3.4000x - 1.4000
Slope (m)3.4
Y-Intercept (b)-1.4
R-Squared (R²)0.99
Correlation (r)0.995
Calculation StepsInputs: N = 5 pairs Sum(X) = 15 Sum(Y) = 44 Sum(XY) = 166 Sum(X²) = 55 Calculate Slope (m): m = [N·ΣXY - (ΣX)(ΣY)] / [N·ΣX² - (ΣX)²] m = [5(166) - (15)(44)] / [5(55) - (15)²] m = 170 / 50 = 3.4000 Calculate Intercept (b): b = [ΣY - m(ΣX)] / N b = [44 - (3.4000)(15)] / 5 = -1.4000 Equation: y = 3.4000x - 1.4000

Calculated locally in your browser. Fast, secure, and private.

Finding the Line of Best Fit

The Linear Regression Calculator is the foundational tool for data science and predictive analytics. By executing an Ordinary Least Squares (OLS) algorithm, it finds the exact mathematical line that best represents the trend in your scattered dataset.

y^=mx+bwherem=N(xy)xyN(x2)(x)2\begin{aligned} \hat{y} = mx + b \quad \text{where} \quad m = \frac{N\sum(xy) - \sum x \sum y}{N\sum(x^2) - (\sum x)^2} \end{aligned}

Where:
m=
The calculated rate of change for the line of best fit
b=
The starting point on the y-axis
N=
The total number of (x,y) data pairs

Understanding Variance and Error

No real-world data is perfect. If you plot the heights and weights of 100 people, it will look like a scattered cloud of dots. The OLS algorithm draws a line through that cloud, measures the vertical distance from every single dot to the line (the "error"), squares those distances, and adjusts the line's slope until that total squared error is as small as mathematically possible.

Real-World Applications

  • Business Forecasting: Predicting next month's sales volume based on historical advertising spend (X = Ads, Y = Sales).
  • Epidemiology: Determining the correlation between average daily temperature and the transmission rate of seasonal flu.
  • Machine Learning: Linear regression is literally the first predictive algorithm taught in AI and machine learning bootcamps.

Frequently Asked Questions

Linear regression is a statistical method that attempts to model the relationship between two variables by fitting a linear equation (a straight line) to observed data.

OLS is the mathematical technique used to find the 'Line of Best Fit'. It calculates the line that minimizes the sum of the squared distances (errors) between the actual data points and the line.

R² is the 'Coefficient of Determination'. It is a percentage between 0 and 1 that tells you how much of the variance in Y is predictable from X. An R² of 0.85 means the line fits 85% of the data perfectly.

Pearson's 'r' ranges from -1 to 1. A value of 1 means perfect positive correlation (as X goes up, Y goes up). A value of -1 means perfect negative correlation. 0 means no linear relationship.

Yes! Once you have the equation y = mx + b, you can plug in any new X value to predict what the corresponding Y value will be.