Level 3 — ProductionCalculus & Optimization Basics

Calculus & Optimization Basics

45 minutes60 marksprintable — key stays hidden on paper

Level 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Show all working. Where "explain out loud" is requested, write the reasoning as if narrating to a peer. Code may be written in plain Python/NumPy pseudocode from memory.


Question 1 — Gradient, Hessian, and critical-point classification (12 marks)

Consider f(x,y)=x33xy+y3f(x,y) = x^3 - 3xy + y^3.

  1. Derive the gradient f\nabla f from first principles of the partial-derivative rules. (2)
  2. Find all critical points. (3)
  3. Derive the Hessian H(x,y)H(x,y). (2)
  4. Classify each critical point (min / max / saddle) using the Hessian, and explain out loud the test you use and why the sign of the determinant and leading minor matter. (5)

Question 2 — Taylor approximation from scratch (10 marks)

Let g(x)=ln(1+x)g(x) = \ln(1+x).

  1. Derive the second-order Taylor expansion of gg about x=0x=0, showing the derivative computations. (4)
  2. Use it to approximate ln(1.2)\ln(1.2) and give the absolute error vs the true value. (3)
  3. Write the general multivariate second-order Taylor formula for f(x)f(\mathbf{x}) about x0\mathbf{x}_0 and state what each term (gradient, Hessian) contributes. (3)

Question 3 — Gradient descent: derive and code (12 marks)

For f(w)=12(w4)2f(w) = \tfrac{1}{2}(w-4)^2:

  1. State the gradient descent update rule and derive the closed-form recurrence for wt+1w_{t+1} in terms of wtw_t and learning rate η\eta. (3)
  2. Show algebraically the condition on η\eta for convergence to w\*=4w^\*=4, and explain what happens for η\eta too large. (4)
  3. Write from memory a NumPy function gradient_descent(w0, eta, steps) that returns the final ww. (5)

Question 4 — Lagrange multipliers derivation (10 marks)

Minimize f(x,y)=x2+y2f(x,y) = x^2 + y^2 subject to x+2y=5x + 2y = 5.

  1. Set up the Lagrangian and derive the stationarity conditions. (4)
  2. Solve for x,y,λx, y, \lambda. (4)
  3. Explain out loud the geometric meaning of λ\lambda and why f=λg\nabla f = \lambda \nabla g at the optimum. (2)

Question 5 — Chain rule for backprop (10 marks)

A tiny network computes: z=wx+bz = wx + b, a=σ(z)a = \sigma(z) where σ(z)=11+ez\sigma(z)=\frac{1}{1+e^{-z}}, and loss L=12(ay)2L = \tfrac{1}{2}(a - y)^2.

  1. Derive Lw\dfrac{\partial L}{\partial w} and Lb\dfrac{\partial L}{\partial b} using the multivariate chain rule, listing each intermediate derivative. (6)
  2. Given x=1,w=0.5,b=0,y=1x=1, w=0.5, b=0, y=1, compute the numeric value of Lw\dfrac{\partial L}{\partial w}. (4)

Question 6 — Convexity & directional derivative (6 marks)

  1. Prove f(x)=x2f(x)=x^2 is convex using the second-derivative test, and state why convexity guarantees any local minimum is global. (3)
  2. For f(x,y)=x2+3y2f(x,y)=x^2+3y^2 at point (1,1)(1,1), compute the directional derivative in the direction u=(3,4)\mathbf{u}=(3,4) (unit-normalize first). (3)

Answer keyMark scheme & solutions

Question 1 (12)

1. Gradient (2) f=(fx,fy)=(3x23y,  3x+3y2)\nabla f = \left(\frac{\partial f}{\partial x}, \frac{\partial f}{\partial y}\right) = (3x^2 - 3y,\; -3x + 3y^2) (1 mark each partial.)

2. Critical points (3) Set both to zero: x2=yx^2 = y and y2=xy^2 = x. Substitute y=x2y=x^2 into y2=xy^2=x: x4=xx(x31)=0x=0x^4 = x \Rightarrow x(x^3-1)=0 \Rightarrow x=0 or x=1x=1.

  • x=0y=0x=0 \Rightarrow y=0: point (0,0)(0,0).
  • x=1y=1x=1 \Rightarrow y=1: point (1,1)(1,1). (1 for equations, 1 each point.)

3. Hessian (2) H=(6x336y)H = \begin{pmatrix} 6x & -3 \\ -3 & 6y \end{pmatrix}

4. Classification (5) Det =36xy9= 36xy - 9.

  • At (0,0)(0,0): det=9<0\det = -9 < 0 \Rightarrow saddle point. (2)
  • At (1,1)(1,1): det=369=27>0\det = 36-9 = 27 > 0, and leading minor fxx=6>0f_{xx}=6>0 \Rightarrow local minimum. (2)

Explain out loud (1): The second-derivative test uses the Hessian as a local quadratic model. A negative determinant means the two curvature eigenvalues have opposite signs → surface curves up in one direction and down in another → saddle. A positive determinant with positive fxxf_{xx} means both eigenvalues positive → bowl → local min. The leading minor sign disambiguates min vs max when det>0.


Question 2 (10)

1. Taylor derivation (4) g(0)=0g(0)=0; g(x)=11+xg'(x)=\frac{1}{1+x}, g(0)=1g'(0)=1; g(x)=1(1+x)2g''(x)=-\frac{1}{(1+x)^2}, g(0)=1g''(0)=-1. g(x)0+1x+12x2=xx22g(x) \approx 0 + 1\cdot x + \frac{-1}{2}x^2 = x - \frac{x^2}{2}

2. Approximate ln1.2\ln 1.2 (3) x=0.2x=0.2: 0.20.042=0.20.02=0.180.2 - \frac{0.04}{2} = 0.2 - 0.02 = 0.18. True ln1.2=0.182321...\ln 1.2 = 0.182321...; absolute error 0.002321\approx 0.002321. (2 for approx, 1 for error.)

3. Multivariate formula (3) f(x)f(x0)+f(x0)(xx0)+12(xx0)H(x0)(xx0)f(\mathbf{x}) \approx f(\mathbf{x}_0) + \nabla f(\mathbf{x}_0)^\top (\mathbf{x}-\mathbf{x}_0) + \tfrac{1}{2}(\mathbf{x}-\mathbf{x}_0)^\top H(\mathbf{x}_0)(\mathbf{x}-\mathbf{x}_0) Gradient term = linear (first-order slope); Hessian term = quadratic curvature correction.


Question 3 (12)

1. Update rule & recurrence (3) f(w)=(w4)f'(w) = (w-4). Update: wt+1=wtηf(wt)=wtη(wt4)w_{t+1} = w_t - \eta f'(w_t) = w_t - \eta(w_t - 4). wt+1=(1η)wt+4ηw_{t+1} = (1-\eta)w_t + 4\eta

2. Convergence condition (4) Error et=wt4e_t = w_t - 4. Subtract 4: et+1=(1η)ete_{t+1} = (1-\eta)e_t, so et=(1η)te0e_t = (1-\eta)^t e_0. Converges iff 1η<10<η<2|1-\eta| < 1 \Rightarrow 0 < \eta < 2. (3) For η>2\eta > 2: 1η>1|1-\eta|>1 → error grows, diverges (oscillating with increasing amplitude). At η=2\eta=2 it oscillates without decaying. (1)

3. Code (5)

import numpy as np
def gradient_descent(w0, eta, steps):
    w = w0
    for _ in range(steps):
        grad = w - 4          # f'(w)
        w = w - eta * grad
    return w

(2 loop, 1 correct gradient, 1 update, 1 return.)


Question 4 (10)

1. Lagrangian (4) L=x2+y2λ(x+2y5)\mathcal{L} = x^2 + y^2 - \lambda(x + 2y - 5). Stationarity: x:2xλ=0,y:2y2λ=0,λ:x+2y5=0\partial_x: 2x - \lambda = 0,\quad \partial_y: 2y - 2\lambda = 0,\quad \partial_\lambda: x+2y-5=0

2. Solve (4) From first two: x=λ/2x = \lambda/2, y=λy = \lambda. Constraint: λ/2+2λ=55λ2=5λ=2\lambda/2 + 2\lambda = 5 \Rightarrow \tfrac{5\lambda}{2}=5 \Rightarrow \lambda = 2. So x=1, y=2x=1,\ y=2. Minimum value f=1+4=5f=1+4=5.

3. Explain (2) λ\lambda is the sensitivity of the optimal objective to relaxing the constraint (shadow price): dfdc=λ\frac{df^*}{dc}=\lambda. At the optimum along the constraint, moving without violating gg cannot decrease ff, which requires f\nabla f have no component tangent to the constraint curve — i.e. f\nabla f is parallel to g\nabla g, giving f=λg\nabla f=\lambda\nabla g.


Question 5 (10)

1. Chain rule (6) Intermediates:

  • La=ay\frac{\partial L}{\partial a} = a - y
  • az=σ(z)(1σ(z))=a(1a)\frac{\partial a}{\partial z} = \sigma(z)(1-\sigma(z)) = a(1-a)
  • zw=x\frac{\partial z}{\partial w} = x, zb=1\frac{\partial z}{\partial b}=1

Lw=(ay)a(1a)x,Lb=(ay)a(1a)\frac{\partial L}{\partial w} = (a-y)\,a(1-a)\,x,\qquad \frac{\partial L}{\partial b} = (a-y)\,a(1-a) (2 per intermediate group + assembly.)

2. Numeric (4) z=0.51+0=0.5z = 0.5\cdot1 + 0 = 0.5; a=σ(0.5)=11+e0.50.622459a = \sigma(0.5) = \frac{1}{1+e^{-0.5}} \approx 0.622459. (ay)=0.6224591=0.377541(a-y) = 0.622459 - 1 = -0.377541. a(1a)=0.622459×0.3775410.234958a(1-a) = 0.622459 \times 0.377541 \approx 0.234958. Lw=0.377541×0.234958×10.088722\frac{\partial L}{\partial w} = -0.377541 \times 0.234958 \times 1 \approx -0.088722.


Question 6 (6)

1. Convexity (3) f(x)=2>0f''(x)=2>0 for all xx → convex. For a convex function the epigraph is a convex set and any local minimum satisfies f(x)f(x)f(x)\ge f(x^*) everywhere, so the local minimum is global (no other lower valley can exist).

2. Directional derivative (3) f=(2x,6y)=(2,6)\nabla f = (2x, 6y) = (2,6) at (1,1)(1,1). Unit vector: u=5\|\mathbf{u}\|=5, u^=(0.6,0.8)\hat{\mathbf{u}}=(0.6,0.8). Du^f=fu^=2(0.6)+6(0.8)=1.2+4.8=6.0D_{\hat u}f = \nabla f\cdot\hat u = 2(0.6)+6(0.8) = 1.2 + 4.8 = 6.0.


[
  {"claim":"Q1 Hessian det at (1,1) is 27 -> local min", "code":"x,y=1,1; det=36*x*y-9; result=(det==27 and 6*x>0)"},
  {"claim":"Q2 Taylor approx of ln(1.2) = 0.18", "code":"x=Rational(2,10); approx=x-x**2/2; result=(approx==Rational(18,100))"},
  {"claim":"Q4 Lagrange solution x=1,y=2,lambda=2 satisfies constraint and stationarity", "code":"x,y,lam=1,2,2; result=(2*x-lam==0 and 2*y-2*lam==0 and x+2*y-5==0)"},
  {"claim":"Q5 dL/dw approx -0.08872", "code":"z=Rational(1,2); a=1/(1+exp(-z)); dLdw=(a-1)*a*(1-a)*1; result=(abs(float(dLdw)-(-0.088722))<1e-4)"},
  {"claim":"Q6 directional derivative equals 6.0", "code":"gx,gy=2,6; ux,uy=Rational(6,10),Rational(8,10); D=gx*ux+gy*uy; result=(D==6)"}
]