TExcelTAD
Home › Calculus in Depth › Maximum and Minimum Values

Maximum and Minimum Values

Finding the highest and lowest values of a function of several variables works as in one variable: the candidates are the points where the derivative vanishes, plus the boundary. The new feature is the saddle point, a critical point that is a minimum in one direction and a maximum in another. This lesson proves the critical-point condition, derives the second-derivative test from the Hessian, finds absolute extrema on closed bounded sets, and explains why saddle points dominate the loss landscapes of deep networks.

For one variable see Maximum and Minimum Values; the survey version of optimisation is Optimization.

Local extrema and critical points

ff has a local maximum at (a,b)(a, b) if f(x,y)≤f(a,b)f(x, y) \le f(a, b) for all (x,y)(x, y) in some disk around (a,b)(a, b); a local minimum if f(x,y)≥f(a,b)f(x, y) \ge f(a, b) there. If the inequality holds on the whole domain, the extremum is absolute (global).

Theorem (Fermat, several variables). If ff has a local extremum at an interior point (a,b)(a, b) and its first partials exist there, then fx(a,b)=fy(a,b)=0f_x(a, b) = f_y(a, b) = 0, i.e. ∇f(a,b)=0\nabla f(a, b) = \mathbf{0}.

Proof. g(x)=f(x,b)g(x) = f(x, b) has a local extremum at x=ax = a, so g′(a)=fx(a,b)=0g'(a) = f_x(a, b) = 0 by the one-variable theorem. The same with yy.

A critical point is a point where ∇f=0\nabla f = \mathbf{0} or where a partial derivative does not exist. Not every critical point is an extremum: f(x,y)=x2−y2f(x, y) = x^2 - y^2 has ∇f=0\nabla f = \mathbf{0} at the origin, but ff increases along the xx-axis and decreases along the yy-axis. That is a saddle point.

The second-derivative test

Near a critical point pp, the gradient term of Taylor's expansion vanishes, and

f(p+h)≈f(p)+12hTHh,H=[fxxfxyfxyfyy](the Hessian at p)f(p + h) \approx f(p) + \tfrac{1}{2} h^T H h, \qquad H = \begin{bmatrix} f_{xx} & f_{xy} \\ f_{xy} & f_{yy} \end{bmatrix} \quad \text{(the Hessian at } p\text{)}

So the shape near pp is decided by the quadratic form Q(h)=hTHhQ(h) = h^T H h. Write D=det⁡H=fxxfyy−fxy2D = \det H = f_{xx} f_{yy} - f_{xy}^2.

Second-derivative test. Suppose the second partials of ff are continuous near a critical point (a,b)(a, b) with ∇f(a,b)=0\nabla f(a, b) = \mathbf{0}.

condition conclusion
D>0D > 0 and fxx>0f_{xx} > 0 local minimum
D>0D > 0 and fxx<0f_{xx} < 0 local maximum
D<0D < 0 saddle point
D=0D = 0 no conclusion

Why. Complete the square, with A=fxxA = f_{xx}, B=fxyB = f_{xy}, C=fyyC = f_{yy} and A≠0A \ne 0:

Ah12+2Bh1h2+Ch22=A(h1+BAh2)2+DAh22A h_1^2 + 2B h_1 h_2 + C h_2^2 = A\left(h_1 + \frac{B}{A} h_2\right)^2 + \frac{D}{A} h_2^2

If D>0D > 0, both coefficients have the sign of AA, so QQ is positive for every h≠0h \ne 0 when A>0A > 0 (minimum) and negative when A<0A < 0 (maximum). If D<0D < 0 the two terms have opposite signs, and QQ takes both signs (saddle). Continuity of the second partials makes the same signs hold at nearby points, which turns the quadratic picture into a statement about ff.

Eigenvalue view. HH is symmetric, so it has real eigenvalues λ1,λ2\lambda_1, \lambda_2 with D=λ1λ2D = \lambda_1 \lambda_2 and fxx+fyy=λ1+λ2f_{xx} + f_{yy} = \lambda_1 + \lambda_2. Both positive   ⟺  \iff minimum; both negative   ⟺  \iff maximum; mixed signs   ⟺  \iff saddle. This version works in any number of variables: classify a critical point of f(x1,…,xn)f(x_1, \ldots, x_n) by the signs of the nn eigenvalues of its Hessian.

python · try it yourself
import sympy as sp

x, y = sp.symbols("x y", real=True)
f = x**3 - 3 * x + y**3 - 3 * y**2

crit = sp.solve([sp.diff(f, x), sp.diff(f, y)], [x, y], dict=True)
H = sp.hessian(f, (x, y))
for c in sorted(crit, key=lambda d: (d[x], d[y])):
    Hc = H.subs(c)
    D = Hc.det()
    eig = sorted(v for v, m in Hc.eigenvals().items() for _ in range(m))  # with multiplicity
    kind = "saddle" if D < 0 else ("min" if Hc[0, 0] > 0 else "max") if D > 0 else "?"
    print(f"({c[x]}, {c[y]})  f = {f.subs(c)}  D = {D}  eigenvalues {eig}  -> {kind}")
(-1, 0)  f = 2  D = 36  eigenvalues [-6, -6]  -> max
(-1, 2)  f = -2  D = -36  eigenvalues [-6, 6]  -> saddle
(1, 0)  f = -2  D = -36  eigenvalues [-6, 6]  -> saddle
(1, 2)  f = -6  D = 36  eigenvalues [6, 6]  -> min

The Hessian here is diagonal, diag⁡(6x,6y−6)\operatorname{diag}(6x, 6y - 6), so the eigenvalues can be read off: one local maximum (−1,0)(-1, 0), one local minimum (1,2)(1, 2) and two saddles. (solve returns a list of dicts; SymPy's eigenvals() returns {eigenvalue: multiplicity}, hence the expansion.)

D=0D = 0 really is inconclusive: x4+y4x^4 + y^4 (minimum), −x4−y4-x^4 - y^4 (maximum) and x4−y4x^4 - y^4 (saddle) all have D=0D = 0 at the origin.

Absolute extrema on closed, bounded sets

Extreme Value Theorem. If ff is continuous on a closed and bounded set D⊂R2D \subset \mathbb{R}^2, then ff attains an absolute maximum and an absolute minimum on DD.

(Closed: DD contains its boundary points. Bounded: DD fits in some disk.) Without either condition the theorem fails: f=xf = x on the open square 0<x<10 < x < 1 has no maximum, and f=xf = x on the whole plane has none either.

Closed-interval method in 2D:

  1. Find the values of ff at the critical points inside DD.
  2. Find the extreme values of ff on the boundary of DD (parametrise each piece of the boundary and do one-variable calculus, or use Lagrange multipliers).
  3. The largest of all these values is the absolute maximum, the smallest the absolute minimum.

Example: f(x,y)=x2−2xy+2yf(x, y) = x^2 - 2xy + 2y on the rectangle 0≤x≤30 \le x \le 3, 0≤y≤20 \le y \le 2. Inside: fx=2x−2y=0f_x = 2x - 2y = 0 and fy=−2x+2=0f_y = -2x + 2 = 0 give (1,1)(1, 1), f=1f = 1. On the edges:

y=0:f=x20≤x≤3values 0…9x=3:f=9−4y0≤y≤2values 1…9y=2:f=x2−4x+4=(x−2)2values 0…4x=0:f=2y0≤y≤2values 0…4\begin{aligned} y = 0\text{:} &\quad f = x^2 && 0 \le x \le 3 && \text{values } 0 \ldots 9 \\ x = 3\text{:} &\quad f = 9 - 4y && 0 \le y \le 2 && \text{values } 1 \ldots 9 \\ y = 2\text{:} &\quad f = x^2 - 4x + 4 = (x - 2)^2 && && \text{values } 0 \ldots 4 \\ x = 0\text{:} &\quad f = 2y && 0 \le y \le 2 && \text{values } 0 \ldots 4 \end{aligned}

Absolute maximum 9 at (3,0)(3, 0); absolute minimum 0 at (0,0)(0, 0) and (2,2)(2, 2).

python · try it yourself
import numpy as np

f = lambda x, y: x**2 - 2 * x * y + 2 * y
X, Y = np.meshgrid(np.linspace(0, 3, 301), np.linspace(0, 2, 201))
Z = f(X, Y)
i = np.unravel_index(np.argmax(Z), Z.shape)
j = np.unravel_index(np.argmin(Z), Z.shape)
print(f"grid max {Z[i]:.4f} at ({X[i]:.2f}, {Y[i]:.2f})")
print(f"grid min {Z[j]:.4f} at ({X[j]:.2f}, {Y[j]:.2f})")
print("number of grid points with value < 1e-9:", int(np.sum(np.abs(Z) < 1e-9)))
grid max 9.0000 at (3.00, 0.00)
grid min 0.0000 at (0.00, 0.00)
number of grid points with value < 1e-9: 2

In software: saddles in ML loss landscapes

Training a network means minimising a loss L(w)L(w) over millions of weights. At a critical point, the Hessian has one eigenvalue per weight, and the point is a local minimum only if all of them are positive. If eigenvalue signs behaved like independent coin flips, the chance that all nn are positive would be 2−n2^{-n}; random-matrix arguments and experiments on real networks support the same qualitative picture: in high dimensions, most critical points with high loss are saddles, and the minima that do exist tend to have low loss.

That changes what an optimiser has to do. Plain gradient descent slows down near a saddle (∇L≈0\nabla L \approx 0) but is pushed away along any direction with a negative eigenvalue, unless it starts exactly on the stable set. The noise in stochastic gradient descent and momentum both help it escape. Second-order methods must be careful: Newton's method jumps to the nearest critical point, saddles included, which is one reason deep learning favours first-order methods.

python · try it yourself
import numpy as np

# f(x, y) = x^2 - y^2 + y^4/4 : saddle at (0,0), minima at (0, ±√2)
grad = lambda p: np.array([2 * p[0], -2 * p[1] + p[1]**3])

def run(p, noise, steps=60, eta=0.1, seed=3):
    rng = np.random.default_rng(seed)
    p = np.array(p, float)
    for _ in range(steps):
        p = p - eta * grad(p) + noise * rng.normal(size=2)
    return p

print("start on the stable line, no noise :", np.round(run([1.0, 0.0], 0.0), 4))
print("same start, tiny noise 1e-3         :", np.round(run([1.0, 0.0], 1e-3), 4))
print("minimum value at (0, √2)            :", round(-2 + 4 / 4, 4))
start on the stable line, no noise : [0. 0.]
same start, tiny noise 1e-3         : [-0.0017 -1.4142]
minimum value at (0, √2)            : -1.0

Without noise, gradient descent started on the xx-axis converges to the saddle and stops; a little noise is enough to slide off it into a true minimum (here the one at (0,−2)(0, -\sqrt{2}); which of the two minima it reaches depends on the random kicks). See Gradient Descent and Jacobian and Hessian.

Exercises

Exercise 1: Classify

Find and classify the critical points of f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x.

Answer

One critical point, (2,−1)(2, -1), a local (and global) minimum with f=−3f = -3.

Solution
fx=2x+y−3=0,fy=x+2y=0⟹x=2, y=−1fxx=2, fyy=2, fxy=1,D=4−1=3>0, fxx>0⟹minimumf(2,−1)=4−2+1−6=−3\begin{aligned} &f_x = 2x + y - 3 = 0, \quad f_y = x + 2y = 0 \quad \Longrightarrow \quad x = 2,\ y = -1 \\ &f_{xx} = 2,\ f_{yy} = 2,\ f_{xy} = 1, \quad D = 4 - 1 = 3 > 0,\ f_{xx} > 0 \quad \Longrightarrow \quad \text{minimum} \\ &f(2, -1) = 4 - 2 + 1 - 6 = -3 \end{aligned}

Since ff is a positive-definite quadratic plus a linear term, this local minimum is global.

Exercise 2: Four critical points

Classify the critical points of g(x,y)=x3−12x+y3−3yg(x, y) = x^3 - 12x + y^3 - 3y.

Answer

(2,1)(2, 1) minimum (g=−18g = -18), (−2,−1)(-2, -1) maximum (g=18g = 18), (2,−1)(2, -1) and (−2,1)(-2, 1) saddles.

Solution
gx=3x2−12=0⇒x=±2;gy=3y2−3=0⇒y=±1gxx=6x, gyy=6y, gxy=0,D=36xy(2,1):D=72>0, gxx=12>0→min,g=8−24+1−3=−18(−2,−1):D=72>0, gxx<0→max,g=18(2,−1),(−2,1):D=−72<0→saddles\begin{aligned} &g_x = 3x^2 - 12 = 0 \Rightarrow x = \pm 2; \qquad g_y = 3y^2 - 3 = 0 \Rightarrow y = \pm 1 \\ &g_{xx} = 6x,\ g_{yy} = 6y,\ g_{xy} = 0, \quad D = 36xy \\ &(2, 1)\text{:} \quad D = 72 > 0,\ g_{xx} = 12 > 0 \to \text{min}, \quad g = 8 - 24 + 1 - 3 = -18 \\ &(-2, -1)\text{:} \quad D = 72 > 0,\ g_{xx} < 0 \to \text{max}, \quad g = 18 \\ &(2, -1), (-2, 1)\text{:} \quad D = -72 < 0 \to \text{saddles} \end{aligned}

Exercise 3: Closed region

Find the absolute extrema of f(x,y)=x2+y2−2xf(x, y) = x^2 + y^2 - 2x on the closed disk x2+y2≤4x^2 + y^2 \le 4.

Answer

Minimum −1-1 at (1,0)(1, 0); maximum 8 at (−2,0)(-2, 0).

Solution
inside:fx=2x−2=0, fy=2y=0→(1,0), f=−1boundary:x2+y2=4→f=4−2x,−2≤x≤2max 8 at x=−2, min 0 at x=2compare:−1,0,8\begin{aligned} \text{inside:} &\quad f_x = 2x - 2 = 0,\ f_y = 2y = 0 \to (1, 0),\ f = -1 \\ \text{boundary:} &\quad x^2 + y^2 = 4 \to f = 4 - 2x, \quad -2 \le x \le 2 \\ &\quad \text{max } 8 \text{ at } x = -2, \ \text{min } 0 \text{ at } x = 2 \\ \text{compare:} &\quad -1, 0, 8 \end{aligned}

Exercise 4: Hessian eigenvalues (code)

For f(x,y,z)=x2+y2+z2+2xy−4yzf(x, y, z) = x^2 + y^2 + z^2 + 2xy - 4yz, the origin is a critical point. Use NumPy to compute the Hessian's eigenvalues and classify it.

Answer

Eigenvalues 2−25≈−2.4722 - 2\sqrt{5} \approx -2.472, 2 and 2+25≈6.4722 + 2\sqrt{5} \approx 6.472 (mixed signs), so the origin is a saddle point.

Solution

The Hessian of a quadratic is constant: H=[22022−40−42]H = \begin{bmatrix} 2 & 2 & 0 \\ 2 & 2 & -4 \\ 0 & -4 & 2 \end{bmatrix}.

python · try it yourself
import numpy as np
H = np.array([[2, 2, 0], [2, 2, -4], [0, -4, 2]], float)
print(np.round(np.linalg.eigvalsh(H), 3))
[-2.472  2.     6.472]

Test yourself

At a critical point fxx=4f_{xx} = 4, fyy=1f_{yy} = 1, fxy=3f_{xy} = 3. What is it?

  1. Local minimum
  2. Local maximum
  3. Saddle point
  4. The test is inconclusive

Which hypothesis does the Extreme Value Theorem need besides continuity?

  1. The domain is closed and bounded
  2. ff is differentiable
  3. ff has exactly one critical point
  4. The domain is open

Why are saddle points a bigger issue than poor local minima in deep networks?

  1. Saddles have larger loss gradients
  2. With many weights, a critical point is rarely positive definite in every direction, so most critical points are saddles
  3. Gradient descent cannot be used near minima
  4. Saddles only exist in two dimensions