Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 4: Least Squares and Conditioning
Optimization
01Week 1: Optimization for Learning
02Week 2: Convex Sets and Functions
03Week 3: Gradient Descent Theory
04Week 4: Least Squares and Conditioning
05Week 5: Classification Losses
06Week 6: Stochastic Gradient Descent
07Week 7: Momentum and Acceleration
08Week 8: Adaptive First-Order Methods
09Week 9: Practical Training Dynamics
10Week 10: Nonsmooth and Composite Objectives
11Week 11: Constraints: Projections and Penalties
12Week 12: Duality for Practitioners
13Week 13: Nonconvex Deep Learning
14Week 14: Capstone — Theory Meets Training
Week 04· Optimization· Model problems22 min read

Week 4: Least Squares and Conditioning

✦Learning Outcomes
  • Write the ordinary least squares and ridge regression objectives as convex quadratics
  • Compare the closed-form solution to GD iteration counts
  • Compute the condition number of XTXX^T XXTX from eigenvalues and predict GD convergence speed
  • Explain why ill-conditioned problems need more iterations (or better methods)
◆Prerequisites

Background: Weeks 1–3 (GD, convexity, smoothness, condition number). The least squares formulation connects to Linear Algebra's normal equations and QR material.

Later weeks: SGD (Week 6) will be benchmarked on LS-style objectives where the closed form gives us the exact answer for comparison.

Least Squares as Optimization#

Given a design matrix X∈Rn×dX \in \mathbb{R}^{n \times d}X∈Rn×d and labels y∈Rny \in \mathbb{R}^ny∈Rn, ordinary least squares (OLS) minimises:

f(w)=12∥Xw−y∥22=12∑i=1n(wTxi−yi)2f(w) = \frac{1}{2}\|Xw - y\|_2^2 = \frac{1}{2} \sum_{i=1}^n (w^T x_i - y_i)^2f(w)=21​∥Xw−y∥22​=21​∑i=1n​(wTxi​−yi​)2

Expanding (the 12\frac{1}{2}21​ is a convenient factor that cancels with the derivative of the square):

f(w)=12wT(XTX)w−yTXw+12yTyf(w) = \frac{1}{2} w^T (X^T X) w - y^T X w + \frac{1}{2} y^T yf(w)=21​wT(XTX)w−yTXw+21​yTy

This is a convex quadratic in www — the Hessian is XTXX^T XXTX, which is always PSD. The gradient is:

∇f(w)=XTXw−XTy=XT(Xw−y)\nabla f(w) = X^T X w - X^T y = X^T (X w - y)∇f(w)=XTXw−XTy=XT(Xw−y)

GD update: wk+1=wk−ηXT(Xwk−y)w_{k+1} = w_k - \eta X^T (X w_k - y)wk+1​=wk​−ηXT(Xwk​−y). Since the Hessian XTXX^T XXTX is constant, fff is LLL-smooth with L=λmax⁡(XTX)L = \lambda_{\max}(X^T X)L=λmax​(XTX), the largest eigenvalue of the Gram matrix.

Worked example — n=3,d=2n=3, d=2n=3,d=2:

X=(101102),y=(236)X = \begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 0 & 2 \end{pmatrix}, \quad y = \begin{pmatrix} 2 \\ 3 \\ 6 \end{pmatrix}X=​110​012​​,y=​236​​

XTX=(2115),XTy=(515)X^T X = \begin{pmatrix} 2 & 1 \\ 1 & 5 \end{pmatrix}, \quad X^T y = \begin{pmatrix} 5 \\ 15 \end{pmatrix}XTX=(21​15​),XTy=(515​)

Eigenvalues of XTXX^T XXTX: λ∈{7±102}≈{5.08,1.92}\lambda \in \{\frac{7 \pm \sqrt{10}}{2}\} \approx \{5.08, 1.92\}λ∈{27±10​​}≈{5.08,1.92}. So L≈5.08L \approx 5.08L≈5.08, μ≈1.92\mu \approx 1.92μ≈1.92, κ≈2.65\kappa \approx 2.65κ≈2.65 — well-conditioned.

Closed Form vs Iterative#

The OLS solution solves the normal equations:

XTXw∗=XTy  ⟹  w∗=(XTX)−1XTyX^T X w^* = X^T y \quad \implies \quad w^* = (X^T X)^{-1} X^T yXTXw∗=XTy⟹w∗=(XTX)−1XTy

When XTXX^T XXTX is invertible (i.e. XXX has full column rank), this gives the exact minimiser in one linear solve.

So why ever use gradient descent?

  1. nnn is huge: Computing XTXX^T XXTX costs O(nd2)\mathcal{O}(n d^2)O(nd2). If n=109n = 10^9n=109, this is prohibitive — but computing ∇fi(w)=(wTxi−yi)xi\nabla f_i(w) = (w^T x_i - y_i) x_i∇fi​(w)=(wTxi​−yi​)xi​ for one sample is O(d)\mathcal{O}(d)O(d), which is why SGD (Week 6) shines.
  2. ddd is huge: Inverting XTXX^T XXTX costs O(d3)\mathcal{O}(d^3)O(d3). If d=106d = 10^6d=106, the closed form is impossible — but a GD step is O(nd)\mathcal{O}(nd)O(nd).
  3. Regularisation changes the game: Adding terms beyond ℓ2\ell_2ℓ2​ (like ℓ1\ell_1ℓ1​ in Week 10) breaks the closed form entirely.

The tradeoff: the closed form gives exact answers in one shot for small-to-moderate ddd. Iterative methods trade exactness for scalability and flexibility.

Ridge Regression#

Adding ℓ2\ell_2ℓ2​ regularisation gives ridge regression:

f(w)=12∥Xw−y∥22+λ2∥w∥22f(w) = \frac{1}{2}\|Xw - y\|_2^2 + \frac{\lambda}{2}\|w\|_2^2f(w)=21​∥Xw−y∥22​+2λ​∥w∥22​

The gradient becomes ∇f(w)=XT(Xw−y)+λw\nabla f(w) = X^T(Xw - y) + \lambda w∇f(w)=XT(Xw−y)+λw, and the Hessian is XTX+λIX^T X + \lambda IXTX+λI.

The crucial effect: even if XTXX^T XXTX is nearly singular (some eigenvalues near zero), adding λI\lambda IλI shifts all eigenvalues up by λ\lambdaλ:

λi(XTX+λI)=λi(XTX)+λ\lambda_i(X^T X + \lambda I) = \lambda_i(X^T X) + \lambdaλi​(XTX+λI)=λi​(XTX)+λ

This guarantees strong convexity with μ≥λ>0\mu \geq \lambda > 0μ≥λ>0 and improves the condition number:

κridge=λmax⁡+λλmin⁡+λ≤κOLS\kappa_{\text{ridge}} = \frac{\lambda_{\max} + \lambda}{\lambda_{\min} + \lambda} \leq \kappa_{\text{OLS}}κridge​=λmin​+λλmax​+λ​≤κOLS​

Regularisation makes GD converge faster — a happy synergy between statistical regularisation and optimisation efficiency.

The closed form is wridge∗=(XTX+λI)−1XTyw^*_{\text{ridge}} = (X^T X + \lambda I)^{-1} X^T ywridge∗​=(XTX+λI)−1XTy.

Condition Number and Convergence#

Recall from Week 3: for strongly convex fff, GD's convergence factor is κ−1κ+1\frac{\kappa - 1}{\kappa + 1}κ+1κ−1​.

Practical intuition — iterations needed to reach tolerance ε\varepsilonε:

| κ\kappaκ | Problem type | Iterations (approx) | |---|---|---| | 1 | Perfectly spherical | 1 | | 2–10 | Well-conditioned | 10–100 | | 100 | Moderately ill-conditioned | 1,000+ | | 10,000 | Ill-conditioned | 100,000+ | | 10610^6106 | Very ill-conditioned | Intractable for plain GD |

When κ\kappaκ is large, we look to momentum (reduces effective κ\kappaκ to κ\sqrt{\kappa}κ​, Week 7), Newton/quasi-Newton methods (directly use Hessian to undo ill-conditioning), or adaptive methods (per-parameter step sizes, Week 8).

Numerical Example — κ=1\kappa = 1κ=1 vs κ=100\kappa = 100κ=100#

Consider two least squares problems in R2\mathbb{R}^2R2, both with optimum w∗=(0,0)w^* = (0, 0)w∗=(0,0):

  • Well-conditioned: XTX=(1001)X^T X = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}XTX=(10​01​), κ=1\kappa = 1κ=1
  • Ill-conditioned: XTX=(100001)X^T X = \begin{pmatrix} 100 & 0 \\ 0 & 1 \end{pmatrix}XTX=(1000​01​), κ=100\kappa = 100κ=100

GD with η=1/L\eta = 1/Lη=1/L on the ill-conditioned problem will shrink w1w_1w1​ quickly (large gradient component) but w2w_2w2​ very slowly (small gradient component). The iterates trace a zigzag down the narrow valley.

Exercise · Fill in the blank

Given eigenvalues [1, 100] of X^T X, the condition number κ = ___.

Question 1 of 4

The OLS objective f(w) = ½‖Xw−y‖² is convex because its Hessian X^T X is:

Always PSD
Always positive definite
Diagonal
Orthogonal

Browser lab#

Run GD on LS with condition numbers κ=1\kappa = 1κ=1, 101010, and 100100100. Plot ∥wk−w∗∥\|w_k - w^*\|∥wk​−w∗∥ vs iteration on a log scale and compare convergence speed.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
d = 2
n = 200

kappas = [1, 10, 100]
fig, axes = plt.subplots(1, 3, figsize=(14, 4.5))

for ax, kappa in zip(axes, kappas):
    diag = np.array([np.sqrt(kappa), 1.0])
    w_true = rng.normal(size=d)
    X = rng.normal(size=(n, d)) @ np.diag(diag)
    y = X @ w_true

    XTX = X.T @ X
    L = np.linalg.eigvalsh(XTX).max()
    eta = 1.0 / L

    w = np.zeros(d)
    K = 200
    dists = [np.linalg.norm(w - w_true)]
    for _ in range(K):
        g = X.T @ (X @ w - y)
        w = w - eta * g
        dists.append(np.linalg.norm(w - w_true))

    ax.semilogy(dists, linewidth=1.5)
    ax.set_xlabel("Iteration")
    ax.set_ylabel(r"$\\|w_k - w^*\\|$")
    ax.set_title(f"$\\kappa \\approx {kappa}$")
    ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()
← Previous
Week 3: Gradient Descent Theory
Next →
Week 5: Classification Losses
On this page
  • Least Squares as Optimization
  • Closed Form vs Iterative
  • Ridge Regression
  • Condition Number and Convergence
  • Numerical Example — kappa = 1 vs kappa = 100
  • Browser lab