Linear & Logistic Regression
Level 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60
Answer all questions. Show all working. Use for inline math and for displayed equations.
Question 1 — OLS from scratch on a tiny dataset (12 marks)
You are given the following four data points for a simple linear regression :
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 2 | 2 | 4 | 5 |
(a) Using the OLS closed-form estimators, compute and . (5)
(b) Compute the fitted values, residuals, and the residual sum of squares (RSS). (4)
(c) Compute for this fit and interpret its meaning in one sentence. (3)
Question 2 — Ridge vs Normal equation (14 marks)
A multiple linear regression uses the design matrix (with a bias column) and target :
(a) State the normal equation and compute the OLS coefficient vector . (6)
(b) Now apply L2 (Ridge) regularization with , using (regularizing all coefficients including bias, for simplicity). Compute the new coefficient vector. (5)
(c) Explain why Ridge shrinks coefficients toward zero but (unlike Lasso) rarely sets them exactly to zero. (3)
Question 3 — Logistic regression decision boundary & log-loss (14 marks)
A trained binary logistic regression classifier has parameters , , for the model
(a) Derive the equation of the decision boundary (where ) in the plane and state its slope and intercept. (4)
(b) For the point with true label , compute and the log-loss (binary cross-entropy) contribution of this single point. Use . (6)
(c) A colleague claims "logistic regression is a linear model." Justify or refute this in terms of what is linear and what is not. (4)
Question 4 — Polynomial regression, overfitting & regularization choice (10 marks)
You fit polynomial regression models of degree to 12 noisy training points.
(a) Sketch (describe qualitatively) how training error and validation error typically behave as increases from 1 to 9, and identify the regime of underfitting vs overfitting. (4)
(b) You want a degree-9 model but must control overfitting AND perform automatic feature selection (drive some coefficients exactly to zero). Which regularization method do you choose and why? Give the objective function you would minimize. (4)
(c) Adjusted for a fit is defined as . For , , and , compute and comment on what the gap between and signals. (2)
Question 5 — Gradient descent update & assumptions (10 marks)
Consider MSE cost .
(a) Derive and and write the gradient descent update rules with learning rate . (4)
(b) Using the dataset from Question 1, starting from and , compute the coefficient values after one gradient descent step. (4)
(c) Name two core assumptions of linear regression that, if violated, would make the OLS standard errors unreliable. (2)
Answer keyMark scheme & solutions
Question 1 (12 marks)
(a) Means: , . : deviations : , : . Products: . . (3) (2)
(b) Fitted : at → . (1) Residuals : . (1) RSS . (2)
(c) . . (2) Interpretation: ~89.6% of the variance in is explained by the linear model. (1)
Question 2 (14 marks)
(a) Normal equation: . (1) , (since , ). (2) . Inverse . . (3) So , .
(b) , . Inverse . . (5) (Both coefficients shrunk relative to OLS.)
(c) Ridge penalty has gradient , which vanishes as , so shrinkage weakens near zero and the optimum sits at a small nonzero value; the smooth quadratic penalty never produces a "corner" at zero. Lasso's has a constant-magnitude subgradient that can pin coefficients exactly at zero. (3)
Question 3 (14 marks)
(a) . (2) Solve for : . Slope , intercept . (2)
(b) . (2) . (2) Log-loss for : . (2)
(c) The model is linear in the parameters/inputs inside — the decision boundary is a hyperplane. But the output probability is a nonlinear function of (the sigmoid). So it is a generalized linear model: linear in the log-odds () but nonlinear in probability space. The colleague is right about the boundary/log-odds but the mapping to probability is nonlinear. (4)
Question 4 (10 marks)
(a) Training error decreases monotonically as rises (more flexibility fits noise). Validation error is U-shaped: high at (underfitting / high bias), minimum at some moderate , then rises at (overfitting / high variance). underfits, overfits. (4)
(b) Choose L1 (Lasso) — its penalty drives some coefficients exactly to zero, giving automatic feature selection while controlling variance. (Elastic Net also acceptable if selection + grouping desired.) (2) Objective: . (2)
(c) . (1) The large drop (0.95 → 0.725) signals the high is inflated by using many parameters () relative to few observations () — a sign of overfitting. (1)
Question 5 (10 marks)
(a) , . (2) Updates: , . (2)
(b) With , , so errors , . . . (2) ; . (2)
(c) Any two: (i) homoscedasticity (constant error variance); (ii) independence of errors (no autocorrelation); (iii) no perfect multicollinearity; (iv) normality of errors (for exact small-sample inference). (2)
[
{"claim":"Q1 OLS slope=1.1, intercept=0.5, RSS=0.70, R2≈0.8963","code":"x=Matrix([1,2,3,4]); y=Matrix([2,2,4,5]); xb=Rational(sum(x),4); yb=Rational(sum(y),4); Sxy=sum((x[i]-xb)*(y[i]-yb) for i in range(4)); Sxx=sum((x[i]-xb)**2 for i in range(4)); b1=Sxy/Sxx; b0=yb-b1*xb; fit=[b0+b1*x[i] for i in range(4)]; RSS=sum((y[i]-fit[i])**2 for i in range(4)); SST=sum((y[i]-yb)**2 for i in range(4)); R2=1-RSS/SST; result=(b1==Rational(11,10)) and (b0==Rational(1,2)) and (RSS==Rational(7,10)) and (abs(float(R2)-0.8963)<1e-3)"},
{"claim":"Q2 OLS beta=[2/3,1], ridge(lambda2)=[16/37,36/37]","code":"X=Matrix([[1,0],[1,2],[1,4]]); y=Matrix([1,2,5]); XtX=X.T*X; Xty=X.T*y; b=XtX.inv()*Xty; br=(XtX+2*eye(2)).inv()*Xty; result=(b==Matrix([Rational(2,3),1])) and (br==Matrix([Rational(16,37),Rational(36,37)]))"},
{"claim":"Q3 phat=sigma(2)=0.8808, logloss=0.1269","code":"import math; p=1/(1+math.exp(-2)); ll=-math.log(p); result=(abs(p-0.8808)<1e-3) and (abs(ll-0.1269)<1e-3)"},
{"claim":"Q4 adjusted R2=0.725","code":"n=12;pp=9;R2=Rational(95,100); adj=1-(1-R2)*Rational(n-1,n-pp-1); result=adj==Rational(725,1000)"},
{"claim":"Q5 one GD step gives beta0=0.325, beta1=0.95","code":"x=[1,2,3,4]; y=[2,2,4,5]; m=4; a=Rational(1,10); err=[0-yi for yi in y]; g0=Rational(sum(err),m); g1=Rational(sum(err[i]*x[i] for i in range(4)),m); b0=0-a*g0; b1=0-a*g1; result=(b0==Rational(325,1000)) and (b1==Rational(95,100))"}
]