Skip to content
AI360XpertBeta
Playgrounds

Foundations

Linear Regression (OLS)

Least squares chooses the line that minimises squared vertical residuals for the observed points.

Guided steps 1 / 4

Stage 1 of 4: The Data

Synthetic observations. Lines outside the plot window are clipped; calculations use their full values.

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

Use slope and intercept to move the line.

  • Data Point
  • Prediction Line
Adjust parameters

Change in prediction for one unit of x

Predicted value at x = 0

Slope1.00Slope: 1.00
Intercept0.00Intercept: 0.00

Can you move the line towardss the points?

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

Slope

The change in the prediction for a one-unit increase in the input.

Check this stage

Check your understanding

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

Move a line, then measure its mistakes

Use Slope to change how fast predictions rise and Intercept to shift them up or down. These eight observations are synthetic: they keep the arithmetic small enough to inspect.

In Manual Fit and Residuals, predict which side of the line the first point lies on. A residual is observed minus predicted, measured vertically. If the observation is 3 and the prediction is 1, the residual is +2. Its square is 4.

Two perfect points can hide a poor fit

Open Mean Squared Error and try Fit only two points. The line meets the last two original observations, but the other residuals grow. MSE averages the squared residuals across all eight points. Use Back to working to restore your previous experiment.

The second plot sweeps the slope while holding your current intercept fixed. Changing the intercept changes that entire slice of the loss surface. The global OLS marker appears only when the intercept matches the optimum, so it belongs to the displayed slice.

Find the least-squares compromise

In Ordinary Least Squares, press Snap to OLS. The formula computes the slope and intercept that minimise squared residuals for the current observations. Inputs vary here, so this solution is unique. If every input were identical, the slope and intercept would not be uniquely identifiable.

Move the last observation with its x/y controls, or drag it on the plot. While snapped, the fitted line updates automatically. A point far from the other inputs has potential leverage; how much it actually changes the fit also depends on its target value. Move either line control to return to manual fitting.

Open Under the hood to inspect every observation, prediction, residual and squared residual. The table uses the same calculation as the plots. The challenge follows the current observations, so its target changes when you edit the point.

A good training fit has limits

Least squares optimises this training score. It does not establish that a straight line is appropriate, that an unusual observation is wrong, or that the trend continues beyond the observed inputs. Check residual patterns and independent data before trusting predictions. Squared residuals give unusual targets substantial influence; absolute-error methods reduce that sensitivity but can still be affected by high-leverage observations.

Continue to gradient descent to reach the same objective through repeated updates, or learning curves to compare training fit with evidence from held-out data.

Stage recap

  1. The Data

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

  2. Manual Fit and Residuals

    Use the slope and intercept controls to move the line. The vertical distance from each point to the line is the error (or residual).

  3. Mean Squared Error

    To score the line, we square each error and average them. Squaring heavily penalises points that are far away from the line.

  4. Ordinary Least Squares

    Instead of guessing, OLS uses a mathematical formula to jump directly to the exact slope and intercept that minimises the MSE.

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 least-squares solution for the current observations.
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 towards itself — greatest for points far from the centre of the data.

Break it on purpose

A line that fits two points exactly can leave much larger residuals elsewhere. Compare its mean squared error with the least-squares fit.