Level 4 — ApplicationLinear & Logistic Regression

Linear & Logistic Regression

60 minutes60 marksprintable — key stays hidden on paper

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 y=β0+β1xy = \beta_0 + \beta_1 x:

xx 1 2 3 4
yy 2 2 4 5

(a) Using the OLS closed-form estimators, compute β^1\hat\beta_1 and β^0\hat\beta_0. (5)

(b) Compute the fitted values, residuals, and the residual sum of squares (RSS). (4)

(c) Compute R2R^2 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 XX (with a bias column) and target yy:

X=[101214],y=[125]X = \begin{bmatrix} 1 & 0 \\ 1 & 2 \\ 1 & 4 \end{bmatrix}, \qquad y = \begin{bmatrix} 1 \\ 2 \\ 5 \end{bmatrix}

(a) State the normal equation and compute the OLS coefficient vector β^=(XX)1Xy\hat\beta = (X^\top X)^{-1}X^\top y. (6)

(b) Now apply L2 (Ridge) regularization with λ=2\lambda = 2, using β^ridge=(XX+λI)1Xy\hat\beta_{ridge} = (X^\top X + \lambda I)^{-1}X^\top y (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 w0=3w_0 = -3, w1=1w_1 = 1, w2=2w_2 = 2 for the model p^=σ(w0+w1x1+w2x2),σ(z)=11+ez.\hat p = \sigma(w_0 + w_1 x_1 + w_2 x_2), \qquad \sigma(z) = \frac{1}{1+e^{-z}}.

(a) Derive the equation of the decision boundary (where p^=0.5\hat p = 0.5) in the (x1,x2)(x_1, x_2) plane and state its slope and intercept. (4)

(b) For the point (x1,x2)=(1,2)(x_1, x_2) = (1, 2) with true label y=1y = 1, compute p^\hat p and the log-loss (binary cross-entropy) contribution of this single point. Use ln\ln. (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 d=1,3,9d = 1, 3, 9 to 12 noisy training points.

(a) Sketch (describe qualitatively) how training error and validation error typically behave as dd 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 R2R^2 for a fit is defined as Rˉ2=1(1R2)n1np1\bar R^2 = 1 - (1-R^2)\frac{n-1}{n-p-1}. For n=12n = 12, p=9p = 9, and R2=0.95R^2 = 0.95, compute Rˉ2\bar R^2 and comment on what the gap between R2R^2 and Rˉ2\bar R^2 signals. (2)


Question 5 — Gradient descent update & assumptions (10 marks)

Consider MSE cost J(β)=12mi=1m(β0+β1xiyi)2J(\beta) = \frac{1}{2m}\sum_{i=1}^m (\beta_0 + \beta_1 x_i - y_i)^2.

(a) Derive J/β0\partial J/\partial \beta_0 and J/β1\partial J/\partial \beta_1 and write the gradient descent update rules with learning rate α\alpha. (4)

(b) Using the dataset from Question 1, starting from β0=0,β1=0\beta_0 = 0, \beta_1 = 0 and α=0.1\alpha = 0.1, 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: xˉ=2.5\bar x = 2.5, yˉ=3.25\bar y = 3.25. Sxy=(xixˉ)(yiyˉ)S_{xy} = \sum (x_i-\bar x)(y_i-\bar y): deviations xx: (1.5,0.5,0.5,1.5)(-1.5,-0.5,0.5,1.5), yy: (1.25,1.25,0.75,1.75)(-1.25,-1.25,0.75,1.75). Products: 1.875+0.625+0.375+2.625=5.51.875 + 0.625 + 0.375 + 2.625 = 5.5. Sxx=(xixˉ)2=2.25+0.25+0.25+2.25=5S_{xx} = \sum(x_i-\bar x)^2 = 2.25+0.25+0.25+2.25 = 5. β^1=5.5/5=1.1\hat\beta_1 = 5.5/5 = 1.1 (3) β^0=yˉβ^1xˉ=3.251.1(2.5)=3.252.75=0.5\hat\beta_0 = \bar y - \hat\beta_1\bar x = 3.25 - 1.1(2.5) = 3.25 - 2.75 = 0.5 (2)

(b) Fitted y^=0.5+1.1x\hat y = 0.5 + 1.1x: at x=1,2,3,4x=1,2,3,41.6,2.7,3.8,4.91.6, 2.7, 3.8, 4.9. (1) Residuals yy^y-\hat y: 0.4,0.7,0.2,0.10.4, -0.7, 0.2, 0.1. (1) RSS =0.16+0.49+0.04+0.01=0.70= 0.16 + 0.49 + 0.04 + 0.01 = 0.70. (2)

(c) SStot=(yiyˉ)2=1.5625+1.5625+0.5625+3.0625=6.75SS_{tot} = \sum(y_i-\bar y)^2 = 1.5625+1.5625+0.5625+3.0625 = 6.75. R2=10.70/6.75=10.10370=0.8963R^2 = 1 - 0.70/6.75 = 1 - 0.10370 = 0.8963. (2) Interpretation: ~89.6% of the variance in yy is explained by the linear model. (1)


Question 2 (14 marks)

(a) Normal equation: XXβ^=XyX^\top X\,\hat\beta = X^\top y. (1) XX=[36620]X^\top X = \begin{bmatrix} 3 & 6 \\ 6 & 20 \end{bmatrix}, Xy=[824]X^\top y = \begin{bmatrix} 8 \\ 24 \end{bmatrix} (since y=8\sum y=8, xy=0+4+20=24\sum x y = 0+4+20=24). (2) det=6036=24\det = 60-36 = 24. Inverse =124[20663]= \frac{1}{24}\begin{bmatrix} 20 & -6 \\ -6 & 3 \end{bmatrix}. β^=124[20862468+324]=124[1624]=[0.66671]\hat\beta = \frac{1}{24}\begin{bmatrix} 20\cdot8 - 6\cdot24 \\ -6\cdot8 + 3\cdot24 \end{bmatrix} = \frac{1}{24}\begin{bmatrix} 16 \\ 24 \end{bmatrix} = \begin{bmatrix} 0.6667 \\ 1 \end{bmatrix}. (3) So β^0=2/3\hat\beta_0 = 2/3, β^1=1\hat\beta_1 = 1.

(b) XX+2I=[56622]X^\top X + 2I = \begin{bmatrix} 5 & 6 \\ 6 & 22 \end{bmatrix}, det=11036=74\det = 110-36 = 74. Inverse =174[22665]= \frac{1}{74}\begin{bmatrix} 22 & -6 \\ -6 & 5 \end{bmatrix}. β^ridge=174[22862468+524]=174[3272]=[0.43240.9730]\hat\beta_{ridge} = \frac{1}{74}\begin{bmatrix} 22\cdot8 - 6\cdot24 \\ -6\cdot8 + 5\cdot24 \end{bmatrix} = \frac{1}{74}\begin{bmatrix} 32 \\ 72 \end{bmatrix} = \begin{bmatrix} 0.4324 \\ 0.9730 \end{bmatrix}. (5) (Both coefficients shrunk relative to OLS.)

(c) Ridge penalty λβj2\lambda\sum\beta_j^2 has gradient 2λβj2\lambda\beta_j, which vanishes as βj0\beta_j\to0, 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 βj|\beta_j| has a constant-magnitude subgradient that can pin coefficients exactly at zero. (3)


Question 3 (14 marks)

(a) p^=0.5z=03+x1+2x2=0\hat p = 0.5 \Rightarrow z = 0 \Rightarrow -3 + x_1 + 2x_2 = 0. (2) Solve for x2x_2: x2=3x12=1.50.5x1x_2 = \frac{3 - x_1}{2} = 1.5 - 0.5x_1. Slope =0.5= -0.5, intercept =1.5= 1.5. (2)

(b) z=3+1(1)+2(2)=2z = -3 + 1(1) + 2(2) = 2. (2) p^=σ(2)=11+e2=11+0.13534=0.8808\hat p = \sigma(2) = \frac{1}{1+e^{-2}} = \frac{1}{1+0.13534} = 0.8808. (2) Log-loss for y=1y=1: lnp^=ln(0.8808)=0.1269-\ln \hat p = -\ln(0.8808) = 0.1269. (2)

(c) The model is linear in the parameters/inputs inside z=wxz = w^\top x — the decision boundary is a hyperplane. But the output probability is a nonlinear function of zz (the sigmoid). So it is a generalized linear model: linear in the log-odds (lnp1p=wx\ln\frac{p}{1-p} = w^\top x) 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 dd rises (more flexibility fits noise). Validation error is U-shaped: high at d=1d=1 (underfitting / high bias), minimum at some moderate dd, then rises at d=9d=9 (overfitting / high variance). d=1d=1 underfits, d=9d=9 overfits. (4)

(b) Choose L1 (Lasso) — its 1\ell_1 penalty drives some coefficients exactly to zero, giving automatic feature selection while controlling variance. (Elastic Net also acceptable if selection + grouping desired.) (2) Objective: minβ12mi=1m(y^iyi)2+λj=19βj\displaystyle \min_\beta \frac{1}{2m}\sum_{i=1}^m (\hat y_i - y_i)^2 + \lambda\sum_{j=1}^{9}|\beta_j|. (2)

(c) Rˉ2=1(10.95)1211291=10.05112=10.275=0.725\bar R^2 = 1 - (1-0.95)\frac{12-1}{12-9-1} = 1 - 0.05\cdot\frac{11}{2} = 1 - 0.275 = 0.725. (1) The large drop (0.95 → 0.725) signals the high R2R^2 is inflated by using many parameters (p=9p=9) relative to few observations (n=12n=12) — a sign of overfitting. (1)


Question 5 (10 marks)

(a) Jβ0=1m(y^iyi)\frac{\partial J}{\partial\beta_0} = \frac{1}{m}\sum(\hat y_i - y_i), Jβ1=1m(y^iyi)xi\frac{\partial J}{\partial\beta_1} = \frac{1}{m}\sum(\hat y_i - y_i)x_i. (2) Updates: β0β0α1m(y^iyi)\beta_0 \leftarrow \beta_0 - \alpha\frac{1}{m}\sum(\hat y_i-y_i), β1β1α1m(y^iyi)xi\beta_1 \leftarrow \beta_1 - \alpha\frac{1}{m}\sum(\hat y_i-y_i)x_i. (2)

(b) With β=0\beta=0, y^i=0\hat y_i = 0, so errors =yi=(2,2,4,5)= -y_i = (-2,-2,-4,-5), m=4m=4. Jβ0=14(2245)=13/4=3.25\frac{\partial J}{\partial\beta_0} = \frac{1}{4}(-2-2-4-5) = -13/4 = -3.25. Jβ1=14(21224354)=14(241220)=38/4=9.5\frac{\partial J}{\partial\beta_1} = \frac{1}{4}(-2\cdot1 -2\cdot2 -4\cdot3 -5\cdot4) = \frac{1}{4}(-2-4-12-20)=-38/4 = -9.5. (2) β0=00.1(3.25)=0.325\beta_0 = 0 - 0.1(-3.25) = 0.325; β1=00.1(9.5)=0.95\beta_1 = 0 - 0.1(-9.5) = 0.95. (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))"}
]