This is a self-testing companion to the OLS derivation. Work each problem BEFORE opening its solution. Problems climb from L1 (Recognition) — "do you know what the symbols mean?" — up to L5 (Mastery) — "can you extend the method to new territory?".
Everything you need was built in the parent note. Two quick reminders in plain words:
Here m = number of data points, n = number of real features (so n+1 columns after we glue on the bias). New notation as it appears: A−1 = the matrix that undoes A (i.e. A−1A=I, the identity, the "do nothing" matrix); detA = the determinant, a single number that is 0 exactly when A has no inverse.
Step 1 — build X and y (first column is the bias 1s):
X=111012,y=133Step 2 — XTX (WHY: the normal equation needs it):
XTX=[3335],XTy=[79]
(Top-left =1+1+1=3; the 3s are 0+1+2; bottom-right =0+1+4=5. For XTy: 1+3+3=7 and 0+3+6=9.)
Step 3 — invert.det=3⋅5−3⋅3=6.
(XTX)−1=61[5−3−33]Step 4 — solveθ∗=(XTX)−1XTy:
θ∗=61[5−3−33][79]=61[35−27−21+27]=61[86]=[4/31]Answer:θ0=4/3, θ1=1, so y^=34+x. At x=1: y^=7/3≈2.33.
Look at the fitted line in the figure below — it splits the gap between the point at (1,3) (above) and the trend, exactly the balancing act least squares performs.
Row of x's (0,1,2): 0⋅31+1⋅(−32)+2⋅31=−32+32=0. ✓
Geometric meaning: the residual vector is perpendicular to every column of X — the leftover error points straight out of the space of fittable lines, so no adjustment of θ can shrink it. That perpendicularity is literally why it's called the "normal" equation.
Look for a linear dependence among columns. Call them c0=(1,1,1), c1=(2,3,4), c2=(5,7,9).
Test c2=ac0+bc1: solving 5=a+2b, 7=a+3b gives b=2,a=1; check the third row 1+2⋅4=9. ✓
So c2=c0+2c1 exactly — the columns are linearly dependent, meaning X does not have full column rank.
Therefore det(XTX)=0 and the inverse does not exist. The cost has infinitely many minimisers (any shift of θ along the dependence direction leaves Xθ unchanged).
Cure: drop the redundant feature, or add regularisation (Ridge Regression).
Recall Solution 3.2
The metres column and the feet column are scalar multiples of each other, so they count as one independent direction. With the bias that gives rank(X)=2, but X has 3 columns — so it is rank-deficient (rank<n+1).
This is the "perfectly correlated features" failure (°C vs °F is the same story). XTX is singular; use Feature Scaling won't save you — you must remove one of the duplicated features or regularise.
New cost:J(θ)=2m1[(Xθ−y)T(Xθ−y)+λθTθ].
Gradient (using the same rules from the parent: ∂θ(θTAθ)=2Aθ, ∂θ(aTθ)=a):
∇θJ=m1[XTXθ−XTy+λθ].Set to zero:XTXθ+λθ=XTy⇒(XTX+λI)θ=XTy.
θ∗=(XTX+λI)−1XTyWhy invertible:XTX is always positive semi-definite (eigenvalues ≥0). Adding λI bumps every eigenvalue up by λ>0, so none is zero — the matrix becomes positive definite and always invertible. See Ridge Regression.
Recall Solution 4.2
WHAT: the probability of one point is the Gaussian density
p(yi∣xi,θ)=2πσ1exp(−2σ2(yi−θTxi)2).WHY log: the full likelihood is the product over all m points; taking log turns the product into a sum we can differentiate, and log is increasing so the maximiser is unchanged.
logL=−mlog(2πσ)−2σ21∑i=1m(yi−θTxi)2.
Only the last term depends on θ, and it carries a minus sign. So maximisinglogL = minimising∑i(yi−θTxi)2 — the least-squares sum. Hence OLS is maximum likelihood under Gaussian noise. See Maximum Likelihood Estimation.
WHY QR: because QTQ=I, the messy XTX collapses:
XTX=(QR)T(QR)=RTQTQR=RTR.
Plug into XTXθ=XTy: RTRθ=RTQTy. Cancel the invertible RT from the left:
Rθ=QTy.
Because R is triangular you solve by back-substitution — no inverse, and it is numerically far kinder (no squaring of the condition number). See QR Decomposition and Pseudoinverse.
Numeric check: for the 2.1 data a QR factorisation gives QTy and an upper-triangular R whose back-substitution yields θ=(4/3,1) — identical to the direct-inverse answer, as it must (both solve the same linear system). ✓
Recall Solution 5.2
Here X=[12] is 1×2: fewer rows than columns (m<n+1). Now XTX is 2×2 but singular; instead XXT (a 1×1 number) is invertible. The correct Moore–Penrose pseudoinverse is the right form:
θ=XT(XXT)−1y.
Compute XXT=12+22=5, so (XXT)−1=51, and y=5:
θ=[12]⋅51⋅5=[12].Check:Xθ=1⋅1+2⋅2=5=y ✓, and among the infinitely many θ that hit the point, this one has the smallest∥θ∥ — the minimum-norm solution the pseudoinverse always picks.
Recall Self-test: state each answer from memory
Exercise 2.1 fitted line ::: y^=34+x
Why is XTX always square ::: it is (n+1)×(n+1), so we can ask if it inverts
Condition for (XTX)−1 to exist ::: X has full column rank (independent columns)
Ridge normal equation ::: θ=(XTX+λI)−1XTy
QR-reduced system ::: Rθ=QTy, solved by back-substitution
Wide-matrix (m<n+1) solution ::: θ=XT(XXT)−1y