Skip to content
AI360Xpert
Beta
Playgrounds

Foundations

Linear Regression (OLS)

How Ordinary Least Squares (OLS) balances the pull from every data point to mathematically guarantee the single best-fitting line.

Stage 1 of 4: The Data

Slope is 1.00, intercept is 0.00, MSE is 0.7188.

  • Data Point

We have a dataset of points. The goal is to draw a straight line that captures the trend.

Check your understanding

Fit a straight line through data points by minimizing the squared errors, using the exact closed-form math.

Linear regression is the foundation of predictive modeling. Given a set of points (like square footage and house price), we want to find the single straight line that best captures the trend.

But how do we define "best"? We define it by looking at the errors: the vertical distance from each data point to our line. If we square these errors (which heavily penalizes points that are far away from the line) and average them, we get the Mean Squared Error (MSE).

The goal of Linear Regression is to find the slope and intercept that make this MSE as small as possible. Because squaring the errors creates a smooth, bowl-shaped mathematical surface, we can actually use calculus to jump directly to the exact answer without guessing. This direct mathematical solution is called Ordinary Least Squares (OLS).

Try dragging the line toward just two nearby points until it passes through them exactly. The fit looks perfect for those two — and the total squared error across every other point gets much worse. That tension, between fitting a few points closely and fitting all of them reasonably, is exactly what squaring and averaging resolves in one step: OLS does not favor any single point, it finds the one line that minimizes the sum of every squared error at once.

Reference

Residual
The vertical distance from a data point to the prediction line.
MSE
Mean Squared Error. The average of all squared residuals.
OLS
Ordinary Least Squares. The closed-form math that perfectly minimizes MSE.
Closed form
A solution reached by one direct formula rather than iterative search — no learning rate, no convergence check, exact on the first computation.
Leverage
How much a single point can pull the fitted line toward itself — greatest for points far from the centre of the data.

Break it on purpose

If you pull the line manually to fit just a few points perfectly, the errors on the other points explode. OLS compromises to minimize the total squared error.