⏱ 90 minutes60 marksprintable — key stays hidden on paper
Time limit: 90 minutes Total marks: 60 Instructions: Answer all three questions. Show all derivations. Partial credit for correct method. Use ... for math.
Consider the linear model y=Xβ+ε where X∈Rn×p (full column rank), y∈Rn.
(a) Starting from the cost J(β)=21∥y−Xβ∥22, derive the normal equation and the closed-form OLS estimator β^. Show explicitly that the stationary point is a minimum. (6)
(b) Prove that the OLS residual vector r=y−Xβ^ is orthogonal to the column space of X, and interpret y^=Xβ^ geometrically as a projection. Give the projection ("hat") matrix H and prove H2=H and H⊤=H. (6)
(c) For Ridge regression the cost becomes Jλ(β)=21∥y−Xβ∥22+2λ∥β∥22, λ>0. Derive β^ridge and explain why it is always defined even when X⊤X is singular. (4)
(d) Given the tiny dataset (x,y)={(0,1),(1,3),(2,7)} fit y=β0+β1x by OLS. Compute β^0,β^1, the fitted values, residuals, and R2. (6)
(a) Define the sigmoid σ(z)=1+e−z1. Prove σ′(z)=σ(z)(1−σ(z)) and that σ(−z)=1−σ(z). (4)
(b) For binary labels yi∈{0,1} with p^i=σ(w⊤xi), write the binary cross-entropy (log-loss) L(w) for n samples. Derive ∇wL and show it equals ∑i(p^i−yi)xi. Comment on why this gradient is identical in form to linear regression's. (8)
(c) A trained model has w=(w0,w1,w2)=(−3,2,1) (with x0=1 the bias).
(i) Write the equation of the decision boundary in the (x1,x2) plane and state its slope.
(ii) For the point (x1,x2)=(1,2) compute p^ and the predicted class at threshold 0.5. (4)
(d) Interpret the coefficient w1=2 in terms of the odds of the positive class when x1 increases by one unit (holding x2 fixed). Give the numerical odds multiplier. (4)
(a) Write the Elastic Net objective combining L1 and L2 penalties. Explain, using the geometry of the constraint regions, why L1 (Lasso) produces exactly-zero coefficients (sparsity) while L2 (Ridge) does not. (6)
(b) You fit a degree-9 polynomial to 12 noisy points and get training R2=0.999 but poor test performance.
(i) Diagnose the phenomenon and relate it to the bias–variance trade-off.
(ii) Give two concrete remedies and explain the mechanism of each. (5)
(c) A model reports R2=0.82 with n=50 observations and p=8 predictors. Compute the adjustedR2. Explain why adjusted R2 is preferred when comparing models with different numbers of predictors. (4)
(d) State any three of the classical assumptions of linear regression and, for each, name one diagnostic or consequence of its violation. (3)
(a) [6]J=21(y−Xβ)⊤(y−Xβ). Expand and differentiate:
∇βJ=−X⊤(y−Xβ)=0⇒X⊤Xβ=X⊤y.(2 for gradient, 1 for normal eqn)
Since X is full column rank, X⊤X is invertible: β^=(X⊤X)−1X⊤y. (2)
Hessian ∇2J=X⊤X is positive definite (full rank) ⇒ strict minimum. (1)
(b) [6]
Normal equation gives X⊤(y−Xβ^)=X⊤r=0, so r⊥Col(X). (2) Thus y^ is the orthogonal projection of y onto Col(X). (1)H=X(X⊤X)−1X⊤. (1)H2=X(X⊤X)−1X⊤X(X⊤X)−1X⊤=X(X⊤X)−1X⊤=H (idempotent). (1)H⊤=(X⊤)⊤((X⊤X)−1)⊤X⊤=X(X⊤X)−1X⊤=H (symmetric, since X⊤X symmetric). (1)
(c) [4]∇Jλ=−X⊤(y−Xβ)+λβ=0⇒(X⊤X+λI)β=X⊤y. (2)β^ridge=(X⊤X+λI)−1X⊤y. (1)X⊤X is positive semidefinite (eigenvalues ≥0); adding λI shifts all eigenvalues to ≥λ>0, so the matrix is invertible even if X⊤X is singular. (1)
(b) [8]L(w)=−∑i[yilnp^i+(1−yi)ln(1−p^i)]. (2)
Let zi=w⊤xi, p^i=σ(zi). ∂p^i∂L=−p^iyi+1−p^i1−yi=p^i(1−p^i)p^i−yi. (2)∂zi∂p^i=p^i(1−p^i), ∂w∂zi=xi. Chain rule: ∇wL=∑ip^i(1−p^i)p^i−yi⋅p^i(1−p^i)xi=∑i(p^i−yi)xi. (3)
Identical form to linear regression's ∑(y^i−yi)xi — because the sigmoid is the canonical link of the Bernoulli GLM; the p^(1−p^) terms cancel exactly. (1)
(c) [4]
(i) Boundary: p^=0.5⇒z=0: −3+2x1+x2=0⇒x2=3−2x1; slope =−2. (2)
(ii) z=−3+2(1)+1(2)=1; p^=σ(1)=1+e−11≈0.731; >0.5⇒ class 1. (2)
(d) [4]
Log-odds =ln1−pp=w⊤x. A unit increase in x1 raises log-odds by w1=2, so odds multiply by e2≈7.389. (4) (The odds of the positive class become ~7.39× larger, x2 fixed.)
(a) [6]J(β)=2n1∥y−Xβ∥22+λ(α∥β∥1+21−α∥β∥22). (2)
L1 constraint region {∣β1∣+∣β2∣≤t} is a diamond with sharp vertices lying on the axes; the elliptical loss contours typically first touch this region at a vertex, where one coordinate is exactly 0 → sparsity. (2) L2 region {β12+β22≤t} is a smooth circle with no corners; the tangent contact point generically has both coordinates nonzero, so Ridge shrinks but rarely zeroes. (2)
(b) [5]
(i) Overfitting: degree-9 with 12 points has near-interpolation, high variance, low bias on training but large generalization error. (2)
(ii) Any two, e.g.: (1) Reduce polynomial degree / model complexity → lowers variance. (2) Add L2/L1 regularization → penalizes large coefficients, shrinks variance. (3) Get more training data → reduces variance of fit. (4) Cross-validation to select degree/λ. (3)
(c) [4]Radj2=1−(1−R2)n−p−1n−1=1−(1−0.82)4149=1−0.18×1.19512=1−0.21512=0.7849.(3)
Adjusted R2 penalizes extra predictors; unlike R2 (which never decreases when predictors are added) it can fall, giving fair model comparison. (1)
(d) [3] Any three (1 each):
Linearity — violation → systematic curvature in residual plot.
Normality of errors — affects inference/CIs; Q–Q plot.
No multicollinearity — high VIF → unstable coefficients.
[ {"claim":"OLS fit of {(0,1),(1,3),(2,7)} gives beta1=3, beta0=2/3, R2=27/28", "code":"import numpy as np; X=Matrix([[1,0],[1,1],[1,2]]); y=Matrix([1,3,7]); b=(X.T*X).inv()*X.T*y; b0,b1=b[0],b[1]; yhat=X*b; res=y-yhat; ybar=Rational(11,3); ssres=sum([r**2 for r in res]); sstot=sum([(yi-ybar)**2 for yi in y]); R2=1-ssres/sstot; result=(b1==3) and (b0==Rational(2,3)) and (R2==Rational(27,28))"}, {"claim":"sigma(1) approx 0.7310585", "code":"val=1/(1+exp(-1)); result=abs(float(val)-0.7310585786)<1e-6"}, {"claim":"odds multiplier for w1=2 is e^2 approx 7.389056", "code":"result=abs(float(exp(2))-7.389056099)<1e-6"}, {"claim":"adjusted R2 for R2=0.82,n=50,p=8 approx 0.78488", "code":"R2=Rational(82,100); n=50; p=8; adj=1-(1-R2)*Rational(n-1,n-p-1); result=abs(float(adj)-0.784878)<1e-5"}]