Skip to content
LeetQuant
F6 Linear Algebra and Optimisation

F6 · Linear Algebra and Optimisation · Lesson 4 of 5

Gradients, Hessians and Convexity

Gradients and Hessians of quadratic forms and least squares, definiteness tests for classifying critical points, why convexity makes local minima global, and Newton's method.

20 min

The two derivatives that matter

For f:Rn→Rf : \mathbb R^n \to \mathbb R, the gradient ∇f(x)\nabla f(x) is the vector of partial derivatives: it points uphill, and f(x+h)≈f(x)+∇f(x)⊤hf(x + h) \approx f(x) + \nabla f(x)^\top h. The Hessian H(x)=∇2f(x)H(x) = \nabla^2 f(x) is the symmetric matrix of second partials, and it gives the curvature:

f(x+h)≈f(x)+∇f(x)⊤h+12h⊤H(x) h.f(x + h) \approx f(x) + \nabla f(x)^\top h + \tfrac12 h^\top H(x)\,h .

At a critical point (∇f=0\nabla f = 0) the linear term vanishes, so the shape of ff nearby is the shape of the quadratic form h⊤Hhh^\top H h. Classifying optima is therefore a question about the definiteness of HH, which is linear algebra you already know.

Objective: differentiate the quadratic expressions that appear in quant work without writing out components, classify critical points, recognise convex problems, and take a Newton step.

Matrix calculus you need

With aa a constant vector and AA a constant matrix:

f(x)f(x)∇f(x)\nabla f(x)∇2f(x)\nabla^2 f(x)
a⊤xa^\top xaa00
x⊤Axx^\top A x(A+A⊤)x(A + A^\top)xA+A⊤A + A^\top
x⊤Axx^\top A x, AA symmetric2Ax2Ax2A2A
∥y−Xβ∥2\|y - X\beta\|^2 (in β\beta)−2X⊤(y−Xβ)-2X^\top(y - X\beta)2X⊤X2X^\top X

Sanity-check the quadratic form in one dimension: ax2ax^2 has derivative 2ax2ax.

Least squares. Setting the gradient to zero gives the normal equations

X⊤X β^=X⊤y⟹β^=(X⊤X)−1X⊤y  when X has full column rank.X^\top X\,\hat\beta = X^\top y \quad\Longrightarrow\quad \hat\beta = (X^\top X)^{-1}X^\top y \ \text{ when } X \text{ has full column rank.}

The first-order condition says X⊤(y−Xβ^)=0X^\top(y - X\hat\beta) = 0: residuals are orthogonal to every column of XX. With an intercept column, residuals sum to zero. The Hessian 2X⊤X2X^\top X is PSD (since v⊤X⊤Xv=∥Xv∥2≥0v^\top X^\top X v = \|Xv\|^2 \ge 0), so this critical point is a global minimum; it is PD, and the minimum unique, exactly when XX has full column rank.

Portfolio variance. ∇w(w⊤Σw)=2Σw\nabla_w(w^\top\Sigma w) = 2\Sigma w. Component ii, 2(Σw)i2(\Sigma w)_i, is the marginal variance of adding a little of asset ii; the risk contributions wi(Σw)iw_i(\Sigma w)_i sum to the total variance.

Definiteness tests and the second-derivative test

For symmetric HH:

HH isMeansTest
positive definiteh⊤Hh>0h^\top H h > 0 for h≠0h \ne 0all eigenvalues >0> 0; or all leading principal minors >0> 0 (Sylvester); or Cholesky succeeds
positive semi-definiteh⊤Hh≥0h^\top H h \ge 0all eigenvalues ≥0\ge 0; or all principal minors ≥0\ge 0
negative definite−H-H is PDleading minors alternate in sign, starting negative
indefinitetakes both signssome eigenvalue positive and some negative

For 2×22 \times 2: (abbc)\begin{pmatrix} a & b \\ b & c \end{pmatrix} is PD iff a>0a > 0 and ac−b2>0ac - b^2 > 0. A negative determinant means indefinite, whatever the diagonal.

At a critical point: HH PD gives a strict local minimum, HH ND a strict local maximum, HH indefinite a saddle. If HH is only semi-definite the test is inconclusive: x4x^4 and −x4-x^4 both have f′′=0f'' = 0 at the origin.

A trap for PSD: leading minors are not enough. (000−1)\begin{pmatrix} 0 & 0 \\ 0 & -1 \end{pmatrix} has leading minors 00 and 00 but is not PSD.

Worked example

classifying critical points

Find and classify the critical points of f(x,y)=x3−3x+y2f(x, y) = x^3 - 3x + y^2.

Show solution

∇f=(3x2−3, 2y)\nabla f = (3x^2 - 3,\ 2y), which vanishes at (1,0)(1, 0) and (−1,0)(-1, 0).

H=(6x002).H = \begin{pmatrix} 6x & 0 \\ 0 & 2 \end{pmatrix} .

At (1,0)(1, 0): H=diag⁡(6,2)H = \operatorname{diag}(6, 2) is PD, a local minimum with f=−2f = -2.

At (−1,0)(-1, 0): H=diag⁡(−6,2)H = \operatorname{diag}(-6, 2) is indefinite, a saddle: a maximum along xx, a minimum along yy.

The local minimum is not global: f→−∞f \to -\infty as x→−∞x \to -\infty. Nothing about a PD Hessian at one point says anything about the rest of the domain. That is what convexity adds.

Convexity: why local equals global

ff is convex if the chord lies above the graph:

f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y),θ∈[0,1].f(\theta x + (1 - \theta)y) \le \theta f(x) + (1 - \theta)f(y), \qquad \theta \in [0, 1].

Equivalent conditions for smooth ff: the tangent plane lies below the graph, f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top(y - x); or the Hessian is PSD everywhere.

Local implies global. Suppose xx is a local minimum and some yy had f(y)<f(x)f(y) < f(x). Points on the segment, x+θ(y−x)x + \theta(y - x), satisfy f≤(1−θ)f(x)+θf(y)<f(x)f \le (1 - \theta)f(x) + \theta f(y) < f(x) for every θ∈(0,1]\theta \in (0, 1], including θ\theta tiny, i.e. arbitrarily close to xx. That contradicts xx being a local minimum. Also, from the tangent-plane inequality, any point with ∇f(x)=0\nabla f(x) = 0 is a global minimum. Strict convexity (for example, HH PD everywhere) makes the minimiser unique.

Convex building blocks: a⊤x+ba^\top x + b, x⊤Axx^\top A x with AA PSD, any norm, exe^x, −log⁡x-\log x, log⁡∑iexi\log\sum_i e^{x_i}, and non-negative sums and pointwise maxima of convex functions. Least squares, ridge, lasso, logistic regression and minimum-variance portfolios are all convex problems. Mean–variance utility w⊤μ−γ2w⊤Σww^\top\mu - \tfrac\gamma2 w^\top\Sigma w is concave, so maximising it is the same thing. Neural network losses and most calibration objectives are not convex, which is why they have local minima and starting-point dependence.

A feasible set also has to be convex for "local = global" to hold for a constrained problem: linear constraints and budget constraints are fine, a cardinality constraint ("at most 10 names") is not.

Worked example

a least-squares line via the gradient

Fit y=a+bty = a + bt to the points (0,1),(1,3),(2,4),(3,8)(0, 1), (1, 3), (2, 4), (3, 8) by least squares, and confirm the solution is a global minimum.

Show solution

With design matrix XX having columns 1\mathbf 1 and tt:

X⊤X=(n∑t∑t∑t2)=(46614),X⊤y=(∑y∑ty)=(1635).X^\top X = \begin{pmatrix} n & \sum t \\ \sum t & \sum t^2 \end{pmatrix} = \begin{pmatrix} 4 & 6 \\ 6 & 14 \end{pmatrix}, \qquad X^\top y = \begin{pmatrix} \sum y \\ \sum ty \end{pmatrix} = \begin{pmatrix} 16 \\ 35 \end{pmatrix} .

det⁡X⊤X=56−36=20\det X^\top X = 56 - 36 = 20, so

(ab)=120(14−6−64)(1635)=120(1444)=(0.72.2).\begin{pmatrix} a \\ b \end{pmatrix} = \frac{1}{20}\begin{pmatrix} 14 & -6 \\ -6 & 4 \end{pmatrix}\begin{pmatrix} 16 \\ 35 \end{pmatrix} = \frac{1}{20}\begin{pmatrix} 14 \\ 44 \end{pmatrix} = \begin{pmatrix} 0.7 \\ 2.2 \end{pmatrix} .

Residuals: 0.3,0.1,−1.1,0.70.3, 0.1, -1.1, 0.7. They sum to zero and ∑tiei=0+0.1−2.2+2.1=0\sum t_i e_i = 0 + 0.1 - 2.2 + 2.1 = 0, as the first-order conditions require. The residual sum of squares is 1.81.8.

The Hessian 2X⊤X2X^\top X has diagonal 8,288, 28 and determinant 4×20>04 \times 20 > 0, so it is PD at every point: the objective is strictly convex and (0.7,2.2)(0.7, 2.2) is the unique global minimum.

Newton's method and gradient descent

Minimise the local quadratic model: setting the gradient of f(x)+g⊤h+12h⊤Hhf(x) + g^\top h + \tfrac12 h^\top H h to zero gives the Newton step

xk+1=xk−H(xk)−1∇f(xk)(in 1D: xk+1=xk−f′(xk)f′′(xk)).x_{k+1} = x_k - H(x_k)^{-1}\nabla f(x_k) \qquad \Big(\text{in 1D: } x_{k+1} = x_k - \frac{f'(x_k)}{f''(x_k)}\Big).

(Solve H h=−∇fH\,h = -\nabla f with a Cholesky; do not invert.) Properties worth saying in an interview:

  • On a quadratic f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with AA PD, one step lands exactly on the minimiser A−1bA^{-1}b from anywhere.
  • Near a minimum with PD Hessian, convergence is quadratic: the error roughly squares each step, so the number of correct digits doubles.
  • Far from the minimum, or where HH is not PD, the step can go uphill or diverge. Practical versions add a line search or a damping term H+λIH + \lambda I.
  • Each step costs a Hessian and an O(n3)O(n^3) solve; quasi-Newton methods (BFGS) build an approximation from gradients instead.

Newton for root-finding (x−g/g′x - g/g') is the same idea applied to g=f′g = f': that is how implied volatility is usually solved, with vega as the derivative.

Gradient descent xk+1=xk−η∇f(xk)x_{k+1} = x_k - \eta\nabla f(x_k) uses no curvature. On a quadratic with Hessian eigenvalues λmin⁡≤⋯≤λmax⁡\lambda_{\min} \le \dots \le \lambda_{\max}, each eigen-direction is multiplied by 1−ηλi1 - \eta\lambda_i per step, so it converges iff η<2/λmax⁡\eta < 2/\lambda_{\max}, and even the best fixed step, η=2/(λmin⁡+λmax⁡)\eta = 2/(\lambda_{\min} + \lambda_{\max}), shrinks the error only by (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1) per step. Ill-conditioning makes gradient descent slow; Newton is unaffected by it.

Worked example

quadratic convergence of Newton's method

Minimise f(x)=ex−2xf(x) = e^x - 2x with Newton's method from x0=0x_0 = 0.

Show solution

f′(x)=ex−2f'(x) = e^x - 2 and f′′(x)=ex>0f''(x) = e^x > 0, so ff is strictly convex with unique minimiser x∗=ln⁡2≈0.693147x^* = \ln 2 \approx 0.693147. The Newton update is

xk+1=xk−exk−2exk=xk−1+2e−xk.x_{k+1} = x_k - \frac{e^{x_k} - 2}{e^{x_k}} = x_k - 1 + 2e^{-x_k} .
import math

x = 0.0
for k in range(1, 6):
    x = x - (math.exp(x) - 2) / math.exp(x)  # x - f'(x) / f''(x)
    print(k, f"{x:.10f}", f"{abs(x - math.log(2)):.1e}")
1 1.0000000000 3.1e-01
2 0.7357588823 4.3e-02
3 0.6940422999 9.0e-04
4 0.6931475811 4.0e-07
5 0.6931471806 8.0e-14

The error goes 0.31→0.043→9×10−4→4×10−7→8×10−140.31 \to 0.043 \to 9 \times 10^{-4} \to 4 \times 10^{-7} \to 8 \times 10^{-14}: each is roughly half the square of the last (the constant is f′′′/(2f′′)=12f'''/(2f'') = \tfrac12 here). Compare gradient descent on 12(x2+100y2)\tfrac12(x^2 + 100y^2), where κ=100\kappa = 100: with the optimal fixed step the error shrinks by only 99/101≈0.98099/101 \approx 0.980 per step, and cutting it by a factor of 100 takes 231 steps.

Knowledge check

What kind of critical point does f(x,y)=x2+4xy+y2f(x, y) = x^2 + 4xy + y^2 have at the origin?

Knowledge check

You run fixed-step gradient descent on a quadratic whose Hessian has eigenvalues 1 and 100. Above what step size does it diverge?

Knowledge check

Which function of a real variable is NOT convex on its domain?

Knowledge check

How many Newton steps does it take to minimise f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x, with AA positive definite, from an arbitrary start?

Key takeaways

  • ∇(x⊤Ax)=2Ax\nabla(x^\top Ax) = 2Ax and ∇2=2A\nabla^2 = 2A for symmetric AA. Least squares gives the normal equations X⊤Xβ=X⊤yX^\top X\beta = X^\top y: residuals orthogonal to the regressors.
  • At a critical point: Hessian PD means minimum, ND maximum, indefinite saddle, semi-definite inconclusive. For 2×22\times2, a negative determinant means saddle.
  • Convex (Hessian PSD everywhere, convex feasible set) means every local minimum is global, and a zero gradient certifies it.
  • Newton, x−H−1∇fx - H^{-1}\nabla f, is exact in one step on quadratics and converges quadratically near a minimum. Gradient descent needs η<2/λmax⁡\eta < 2/\lambda_{\max} and slows with the condition number.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).