BRAGA

02 Writing02-02 Notes02-02-444

Least-Squares Objective (Empirical Risk)

Published 16 Apr 202610 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
  9. 09Self-check: mathematical verification
  10. 10Common Pitfalls and Edge Cases
  11. 11References and Standard Results
L(θ)=1N∑n=1N(yn−θ⊤xn)2=1N∣y−Xθ∣2L(\theta) = \frac{1}{N} \sum_{n=1}^N (y_n - \theta^\top x_n)^2 = \frac{1}{N} |y - X\theta|^2
(1)

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 θTxn\theta^T x_n and the true labels yny_n. 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 (ϵ∼N(0,σ2)\epsilon \sim \mathcal{N}(0, \sigma^2)), minimising the least-squares loss is exactly equivalent to maximising the Gaussian log-likelihood. The factor 1/N1/N makes the loss scale-invariant to dataset size, which is important when comparing models across different NN.

Flowchart

Small pLarge pLinear model:y_hat_n =theta^T x_nResidual: e_n =y_n - y_hat_nSquare residual:e_n^2Average over Nsamples: L =(1/N) sum e_n^2Compact form: L= (1/N) ||y - Xtheta||^2Gradient: dL/dtheta = -(2/N)X^T (y - X theta)Analyticalsolution?Normalequations:theta* = (X^TX)^-1 X^T yGradientdescent: theta <-theta - lr *gradient

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 θ\theta (unique global minimum), and directly interpretable as average squared prediction error.

How it solves the problem

For each sample nn, compute the prediction error yn−θTxny_n - \theta^T x_n, square it to make it non-negative and penalise large errors more, then average over all NN samples. The result is a convex bowl-shaped surface in θ\theta-space with a unique minimum at the OLS/MLE solution.

Example in a business

A DS monitors the training loss L(θ)L(\theta) per epoch while training a demand forecasting model with gradient descent. At epoch 1 the loss is 850 (euros2^2). 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
NN Number of training samples
yny_n True label for sample nn
θ\theta Weight vector (parameters)
xnx_n Feature vector for sample nn
θTxn\theta^T x_n Model prediction for sample nn (dot product)
(yn−θTxn)2(y_n - \theta^T x_n)^2 Squared residual for sample nn
1N∑n=1N\frac{1}{N} \sum_{n=1}^N Average over training set
∣y−Xθ∣2|y - X\theta|^2 Squared Euclidean norm of the residual vector

Full formula: L(θ)=1N∑n=1N(yn−θTxn)2=1N∥y−Xθ∥2L(\theta) = \dfrac{1}{N} \sum_{n=1}^N (y_n - \theta^T x_n)^2 = \dfrac{1}{N} \|y - X\theta\|^2 MSE form (same formula, different name): MSE=1N∥y−y^∥2MSE = \dfrac{1}{N} \|y - \hat{y}\|^2

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: L(θ)L(\theta) is convex in θ\theta (a positive semi-definite quadratic form). There is exactly one global minimum (or a flat minimum if XX 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.
  • 1/N1/N vs. 1/2N1/2N 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 ∑(yn−θTxn)2\sum (y_n - \theta^T x_n)^2 is equivalent to maximising the Gaussian log-likelihood log⁡p(y∣X,θ,σ2)\log p(y | X, \theta, \sigma^2) when σ2\sigma^2 is constant.
  • Gradient: ∇θL=−2NXT(y−Xθ)\nabla_\theta L = -\frac{2}{N} X^T (y - X\theta).

At the optimum, XT(y−Xθ∗)=0X^T(y - X\theta^*) = 0 — residuals are orthogonal to all features.

Step-by-step calculation (tiny example)

N=3N = 3, θ=[1.5]\theta = [1.5], x=[1,2,3]x = [1, 2, 3], y=[2,3,5]y = [2, 3, 5].

Step 1: Compute predictions y^=[1.5⋅1,1.5⋅2,1.5⋅3]=[1.5,3.0,4.5]\hat{y} = [1.5 \cdot 1, 1.5 \cdot 2, 1.5 \cdot 3] = [1.5, 3.0, 4.5]

Step 2: Compute residuals e=y−y^=[0.5,0.0,0.5]e = y - \hat{y} = [0.5, 0.0, 0.5]

Step 3: Compute squared residuals and average L=(0.25+0.00+0.25)/3=0.50/3=0.167L = (0.25 + 0.00 + 0.25) / 3 = 0.50 / 3 = 0.167 The true MLE solution (from normal equations): θ∗≈1.643\theta^* \approx 1.643, giving L(θ∗)=0.048L(\theta^*) = 0.048.

Code examples

Beginner example (Python)

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)

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)

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 ii, the linear model predicts:

y^i=Xiw\hat{y}_i = X_i w
(2)

So the residual is:

ri=yi−y^i=yi−Xiwr_i = y_i - \hat{y}_i = y_i - X_i w
(3)

To aggregate residual magnitudes across observations without sign cancellation, square them:

ri2=(yi−Xiw)2r_i^2 = (y_i - X_i w)^2
(4)

Summing over all observations gives:

RSS(w)=∑i=1n(yi−Xiw)2\mathrm{RSS}(w)=\sum_{i=1}^{n}(y_i-X_i w)^2
(5)

Dividing by nn gives the empirical-risk version:

L(w)=1n∑i=1n(yi−Xiw)2L(w)=\frac{1}{n}\sum_{i=1}^{n}(y_i-X_i w)^2
(6)

This matches the canonical formula exactly.

Matrix-form check

Let yy be the target vector and XwXw the prediction vector.

Then the residual vector is:

r=y−Xwr = y - Xw
(7)

The squared Euclidean norm is:

∥y−Xw∥22=(y−Xw)⊤(y−Xw)\|y-Xw\|_2^2 = (y-Xw)^\top (y-Xw)
(8)

Expanding this norm corresponds exactly to the sum of squared componentwise residuals:

∥y−Xw∥22=∑i=1n(yi−Xiw)2\|y-Xw\|_2^2 = \sum_{i=1}^{n}(y_i-X_i w)^2
(9)

So the matrix form is correct.

Same minimizer for averaged and summed forms Because

L(w)=1nRSS(w)L(w)=\frac{1}{n}\mathrm{RSS}(w)
(10)

and 1n\frac{1}{n} is a positive constant independent of ww, minimizing L(w)L(w) is equivalent to minimizing RSS(w)\mathrm{RSS}(w). So both forms yield the same optimal coefficient vector.

Normal-equation connection

For the sum-of-squares objective in matrix form:

RSS(w)=∥y−Xw∥22\mathrm{RSS}(w)=\|y-Xw\|_2^2
(11)

the gradient with respect to ww is:

∇wRSS(w)=−2X⊤(y−Xw)\nabla_w \mathrm{RSS}(w) = -2X^\top(y-Xw)
(12)

Setting the gradient to zero gives:

X⊤(y−Xw)=0X^\top(y-Xw)=0
(13)

which rearranges to:

X⊤Xw=X⊤yX^\top X w = X^\top y
(14)

If X⊤XX^\top X is invertible, the solution is:

w^=(X⊤X)−1X⊤y\hat{w}=(X^\top X)^{-1}X^\top y
(15)

So the normal-equation statement is mathematically consistent with the objective.

Gaussian MLE connection Assume

yi=Xiw+εi,εi∼N(0,σ2)y_i = X_i w + \varepsilon_i,\qquad \varepsilon_i \sim \mathcal{N}(0,\sigma^2)
(16)

independently. Then the Gaussian log-likelihood, up to constants, is:

log⁡L(w,σ2)=constant−12σ2∑i=1n(yi−Xiw)2\log L(w,\sigma^2) = \text{constant} - \frac{1}{2\sigma^2}\sum_{i=1}^{n}(y_i-X_i w)^2
(17)

For fixed σ2\sigma^2, maximizing the log-likelihood with respect to ww is equivalent to minimizing:

∑i=1n(yi−Xiw)2\sum_{i=1}^{n}(y_i-X_i w)^2
(18)

So OLS and Gaussian MLE agree under the stated assumptions.

Convexity check

Each squared residual is a convex function of ww, and sums of convex functions remain convex. Therefore the least-squares objective is convex in ww. 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 `
......
(19)

`.

  • Symbol-table entries use math mode such as $X_i$, $w$, $y_i$, and $L(w)$.
  • Mermaid uses flowchart TD and 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 X⊤XX^\top X 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, y^i=Xiw\hat{y}_i = X_i w
  • Residual definition, ri=yi−Xiwr_i = y_i - X_i w
  • Least-squares objective definition, L(w)=1n∑i=1n(yi−Xiw)2L(w)=\frac{1}{n}\sum_{i=1}^{n}(y_i-X_i w)^2
  • Residual Sum of Squares definition, RSS(w)=∑i=1n(yi−Xiw)2\mathrm{RSS}(w)=\sum_{i=1}^{n}(y_i-X_i w)^2
  • Matrix identity, RSS(w)=∣y−Xw∣22\mathrm{RSS}(w)=|y-Xw|_2^2
  • Normal equations, X⊤Xw=X⊤yX^\top Xw = X^\top y
  • Closed-form OLS solution, w^=(X⊤X)−1X⊤y\hat{w}=(X^\top X)^{-1}X^\top y
  • Gaussian-noise maximum likelihood equivalence to least squares
  • Convexity of the least-squares objective

#machine-learning#machine-learning-algorithms#linear-regression-and-maximum-likelihood-estimation-mle

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close