Contents
Deep Breakdown
What it tells me (Situation Awareness): The least-squares objective is the empirical risk of a linear model under squared error loss. It measures the average squared difference between the model's predictions and the true labels . This is the function you minimise when training linear regression. The connection to Maximum Likelihood is direct: under the assumption that residuals are i.i.d. Gaussian (), minimising the least-squares loss is exactly equivalent to maximising the Gaussian log-likelihood. The factor makes the loss scale-invariant to dataset size, which is important when comparing models across different .
Flowchart
Real-world use
Where it is used
Training any linear regression model, deriving OLS estimators in econometrics, monitoring training loss during gradient descent, comparing model quality across regression methods, and as the base loss function before adding regularisation (L1/L2).
Problem it solves
You need a single scalar that measures how far the model's predictions are from the true values on average. The least-squares loss is differentiable everywhere, convex in (unique global minimum), and directly interpretable as average squared prediction error.
How it solves the problem
For each sample , compute the prediction error , square it to make it non-negative and penalise large errors more, then average over all samples. The result is a convex bowl-shaped surface in -space with a unique minimum at the OLS/MLE solution.
Example in a business
A DS monitors the training loss per epoch while training a demand forecasting model with gradient descent. At epoch 1 the loss is 850 (euros). After 100 epochs it plateaus at 42. The DS plots the train vs. validation loss to diagnose when overfitting begins, then sets early stopping at the minimum validation loss.
The formula, piece by piece
| Symbol | Meaning |
|---|---|
| Number of training samples | |
| True label for sample | |
| Weight vector (parameters) | |
| Feature vector for sample | |
| Model prediction for sample (dot product) | |
| Squared residual for sample | |
| Average over training set | |
| Squared Euclidean norm of the residual vector |
Full formula: MSE form (same formula, different name):
The formula in words
"The least-squares loss is the mean of squared prediction errors — it measures how much the model's line (or hyperplane) deviates from the true values on average, penalising large errors quadratically."
Critical Concepts
- Convexity: is convex in (a positive semi-definite quadratic form). There is exactly one global minimum (or a flat minimum if is rank-deficient).
- Squared vs. absolute loss: squaring penalises outliers more than MAE. If the data has heavy-tailed noise, consider Huber loss or MAE as alternatives.
- vs. vs. no normalisation: different textbooks use different prefactors. This only changes the gradient by a constant and does not affect the minimiser — but it affects the learning rate and regularisation strength interpretation.
- MLE connection: minimising is equivalent to maximising the Gaussian log-likelihood when is constant.
- Gradient: .
At the optimum, — residuals are orthogonal to all features.
Step-by-step calculation (tiny example)
, , , .
Step 1: Compute predictions
Step 2: Compute residuals
Step 3: Compute squared residuals and average The true MLE solution (from normal equations): , giving .
Code examples
Beginner example (Python)
import numpy as np
X = np.array([[1], [2], [3], [4]], dtype=float)
y = np.array([2.0, 3.5, 5.0, 6.5])
theta = np.array([1.5])
def least_squares_loss(theta: np.ndarray, X: np.ndarray, y: np.ndarray) -> float:
residuals = y - X @ theta
return float(np.mean(residuals ** 2))
def ls_gradient(theta: np.ndarray, X: np.ndarray, y: np.ndarray) -> np.ndarray:
return -2.0 / len(y) * X.T @ (y - X @ theta)
print(f"Loss at theta={theta}: {least_squares_loss(theta, X, y):.4f}")
print(f"Gradient at theta={theta}: {ls_gradient(theta, X, y)}")
# Optimal theta
theta_opt = np.linalg.solve(X.T @ X, X.T @ y)
print(f"Optimal theta: {theta_opt[0]:.4f}")
print(f"Optimal loss: {least_squares_loss(theta_opt, X, y):.6f}")Beginner example (R)
X <- matrix(c(1, 2, 3, 4), ncol = 1)
y <- c(2.0, 3.5, 5.0, 6.5)
theta <- 1.5
least_squares_loss <- function(theta, X, y) {
residuals <- y - X %*% theta
mean(residuals^2)
}
cat("Loss at theta=1.5:", round(least_squares_loss(theta, X, y), 4), "\n")
theta_opt <- solve(t(X) %*% X, t(X) %*% y)
cat("Optimal theta:", round(theta_opt, 4), "\n")
cat("Optimal loss:", round(least_squares_loss(theta_opt, X, y), 6), "\n")Professional example (Python)
import numpy as np
import pandas as pd
LEARNING_RATE = 0.01
MAX_ITERATIONS = 1_000
CONVERGENCE_TOL = 1e-6
def gradient_descent_ols(
X: np.ndarray,
y: np.ndarray,
learning_rate: float = LEARNING_RATE,
max_iter: int = MAX_ITERATIONS,
tol: float = CONVERGENCE_TOL,
) -> dict[str, object]:
# Train OLS by gradient descent, tracking loss per iteration.
n, p = X.shape
theta = np.zeros(p)
history = []
for iteration in range(max_iter):
residuals = y - X @ theta
loss = float(np.mean(residuals ** 2))
gradient = -2.0 / n * X.T @ residuals
history.append({"iteration": iteration, "loss": round(loss, 8)})
if np.linalg.norm(gradient) < tol:
break
theta -= learning_rate * gradient
theta_analytical = np.linalg.solve(X.T @ X, X.T @ y)
return {
"theta_gd": theta.round(6),
"theta_analytical": theta_analytical.round(6),
"final_loss": round(float(np.mean((y - X @ theta) ** 2)), 8),
"iterations": len(history),
"history_df": pd.DataFrame(history),
}
if __name__ == "__main__":
rng = np.random.default_rng(seed=42)
n = 300
p = 3
X_sim = rng.standard_normal((n, p))
true_theta = np.array([1.0, -0.5, 2.0])
y_sim = X_sim @ true_theta + rng.normal(0, 0.5, n)
result = gradient_descent_ols(X_sim, y_sim, learning_rate=0.05)
print(f"GD theta: {result['theta_gd']}")
print(f"Analytical theta: {result['theta_analytical']}")
print(f"Iterations: {result['iterations']}")
print(f"Final loss: {result['final_loss']}")
print("\nFirst 5 iterations:")
print(result["history_df"].head(5).to_string(index=False))MML1 pag 304
Self-check: mathematical verification
Re-derivation from the residual definition
For observation , the linear model predicts:
So the residual is:
To aggregate residual magnitudes across observations without sign cancellation, square them:
Summing over all observations gives:
Dividing by gives the empirical-risk version:
This matches the canonical formula exactly.
Matrix-form check
Let be the target vector and the prediction vector.
Then the residual vector is:
The squared Euclidean norm is:
Expanding this norm corresponds exactly to the sum of squared componentwise residuals:
So the matrix form is correct.
Same minimizer for averaged and summed forms Because
and is a positive constant independent of , minimizing is equivalent to minimizing . So both forms yield the same optimal coefficient vector.
Normal-equation connection
For the sum-of-squares objective in matrix form:
the gradient with respect to is:
Setting the gradient to zero gives:
which rearranges to:
If is invertible, the solution is:
So the normal-equation statement is mathematically consistent with the objective.
Gaussian MLE connection Assume
independently. Then the Gaussian log-likelihood, up to constants, is:
For fixed , maximizing the log-likelihood with respect to is equivalent to minimizing:
So OLS and Gaussian MLE agree under the stated assumptions.
Convexity check
Each squared residual is a convex function of , and sums of convex functions remain convex. Therefore the least-squares objective is convex in . This ensures that any local minimum is a global minimum.
Verbal-to-algebra consistency
The verbal explanation says least squares minimizes squared residuals, penalizes large misses, yields OLS, and matches MLE under Gaussian noise. The algebra confirms all of these claims.
Structural verification
- Opening restatement paragraph is present.
- All required sections are present and non-empty.
- Block equations use `
`.
- Symbol-table entries use math mode such as
$X_i$,$w$,$y_i$, and$L(w)$. - Mermaid uses
flowchart TDand node text in["..."]. - Three code blocks are present: Python beginner, R beginner, Python professional.
- Links to normal equations, gradient, Ridge, regression, and forecasting are explicitly included.
- No placeholder text remains.
Common Pitfalls and Edge Cases
- Confusing residuals with squared residuals: The objective minimizes squared residuals, not raw residuals.
- Ignoring outlier sensitivity: Squaring makes large errors disproportionately influential.
- Assuming low training loss guarantees generalization: The model can still overfit, especially with many predictors.
- Using the closed-form solution when is singular: In that case the inverse does not exist and regularization or pseudo-inverse methods are needed.
- Ignoring feature scaling in numerical optimization: Gradient-based methods can converge slowly when predictors are on very different scales.
- Confusing OLS assumptions with the optimization itself: Least squares can always be minimized, but inference properties depend on statistical assumptions.
- Forgetting the intercept handling: The design matrix must include a column of ones if the intercept is part of the model.
- Using least squares when the noise model is inappropriate: Heavy-tailed noise or strong outliers may motivate robust alternatives.
References and Standard Results
- Linear prediction rule,
- Residual definition,
- Least-squares objective definition,
- Residual Sum of Squares definition,
- Matrix identity,
- Normal equations,
- Closed-form OLS solution,
- Gaussian-noise maximum likelihood equivalence to least squares
- Convexity of the least-squares objective