Linear & Logistic Regression
Time limit: 45 minutes Total marks: 60 Instructions: Show all derivations from first principles. Where code is asked, pseudo-code or NumPy-style is acceptable but must be correct and runnable in spirit. Use for math.
Q1. Ordinary Least Squares — full derivation (12 marks)
For simple linear regression :
(a) Write the residual sum of squares cost and derive the OLS estimates by setting the partial derivatives to zero. Show every algebraic step. (8)
(b) Show that the fitted line passes through . (2)
(c) State one modelling assumption that guarantees OLS is the Best Linear Unbiased Estimator (BLUE). (2)
Q2. Normal equation & ridge (10 marks)
(a) Starting from the vectorised cost , derive the normal equation and give the closed-form solution . (4)
(b) Repeat for L2 (ridge) cost and give the ridge closed-form. (3)
(c) Explain, in one or two sentences, why the ridge solution is invertible even when is singular. (3)
Q3. Gradient descent from memory (10 marks)
(a) For MSE cost , derive the gradient . (3)
(b) Write NumPy-style pseudocode for batch gradient descent that fits (include the update rule, learning rate, and iteration loop). (5)
(c) Explain what happens to convergence if the learning rate is (i) too large, (ii) too small. (2)
Q4. Logistic regression & log-loss (12 marks)
(a) Define the sigmoid and prove that . (3)
(b) Write the binary cross-entropy (log-loss) for one example, then derive the gradient of the total log-loss w.r.t. the weights , showing that it simplifies to . (6)
(c) Given weights and bias for features , compute the predicted probability and state the predicted class at threshold 0.5. (3)
Q5. Decision boundary & softmax (10 marks)
(a) Show that the decision boundary of binary logistic regression at threshold 0.5 is the linear surface . (3)
(b) Write the softmax function for classes and state the predicted class rule. (3)
(c) For logits compute the softmax probabilities (leave in exponential form then numeric to 3 dp) and give the predicted class. (4)
Q6. Model evaluation & regularization reasoning (6 marks)
(a) Define and adjusted ; explain why adjusted is preferred when comparing models with different numbers of predictors. (3)
(b) Contrast L1 (Lasso) and L2 (Ridge): which induces sparsity and why? One sentence on when Elastic Net is preferred. (3)
Answer keyMark scheme & solutions
Q1 (12)
(a) . (1)
Partial derivatives: (2) (2) From the first equation: . (1) Substituting and simplifying gives: (2)
(b) From , at : . Hence line passes through . (2)
(c) Any of: errors have zero mean, constant variance (homoscedasticity), are uncorrelated; predictors non-random / full rank (Gauss–Markov conditions). (2)
Q2 (10)
(a) . Gradient: (2) (2)
(b) . (1) (2)
(c) is positive semi-definite; adding () shifts all eigenvalues up by , making the matrix positive-definite hence invertible. (3)
Q3 (10)
(a) . Derivation: differentiate . (3)
(b)
def gd(X, y, lr=0.01, n_iter=1000):
m, n = X.shape
beta = np.zeros(n)
for _ in range(n_iter):
grad = (1/m) * X.T @ (X @ beta - y)
beta -= lr * grad
return beta(5: correct init 1, grad 2, update 1, loop 1)
(c) (i) Too large → overshoots minimum, cost may oscillate or diverge. (ii) Too small → convergence very slow, may need excessive iterations. (2)
Q4 (12)
(a) . (1) . (2)
(b) Per example: with . (2) ; chain with gives . (3) Summing over examples and averaging: . (1)
(c) , . At threshold 0.5, class = 1 (or boundary case; either accepted with justification). (3)
Q5 (10)
(a) Predict class 1 when . Boundary is , linear in . (3)
(b) ; predict . (3)
(c) ; sum . . Predicted class = 0 (first). (4)
Q6 (6)
(a) . . Adjusted penalizes adding predictors, so it doesn't rise spuriously from extra features—fairer for model comparison. (3)
(b) L1 induces sparsity: its diamond-shaped constraint has corners on axes so coefficients hit exactly zero; L2 shrinks but rarely zeroes. Elastic Net preferred with many correlated features (groups selection + stability). (3)
[
{"claim":"sigmoid(0)=0.5","code":"z=0; result=(1/(1+exp(-z))==Rational(1,2))"},
{"claim":"softmax of [2,1,0] first prob approx 0.665","code":"import math; z=[2,1,0]; e=[math.exp(v) for v in z]; s=sum(e); result=abs(e[0]/s-0.665)<0.01"},
{"claim":"ridge shifts eigenvalues by lambda making invertible","code":"lam=2; M=Matrix([[0,0],[0,0]])+lam*eye(2); result=(M.det()!=0)"},
{"claim":"OLS beta1 for x=[1,2,3],y=[2,4,6] equals 2","code":"import numpy as np; x=np.array([1,2,3]); y=np.array([2,4,6]); b1=np.sum((x-x.mean())*(y-y.mean()))/np.sum((x-x.mean())**2); result=abs(b1-2)<1e-9"}
]