TExcelTAD
Home › Optimization › Optimality Conditions

Optimality Conditions

How do you know you have found a minimum? Optimality conditions turn that question into equations and inequalities you can check, and every algorithm in part 2 is a way of solving them.

First-order necessary condition

If x∗\mathbf{x}^* is a local minimum of a differentiable ff (no constraints), then

∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}

Proof idea: if the gradient were not zero, a small step along −∇f-\nabla f would lower ff. Points with zero gradient are stationary points; they can be minima, maxima or saddle points.

Second-order conditions

The Hessian classifies stationary points:

At a stationary point Conclusion
∇2f(x∗)≻0\nabla^2 f(\mathbf{x}^*) \succ 0 (all eigenvalues >0> 0) strict local minimum (sufficient)
∇2f(x∗)⪰0\nabla^2 f(\mathbf{x}^*) \succeq 0 necessary for a local minimum, but not sufficient
eigenvalues of both signs saddle point
∇2f(x∗)≺0\nabla^2 f(\mathbf{x}^*) \prec 0 strict local maximum

The second-order Taylor expansion explains the table: near a stationary point, f(x∗+d)≈f(x∗)+12dT∇2f(x∗)df(\mathbf{x}^* + \mathbf{d}) \approx f(\mathbf{x}^*) + \frac{1}{2}\mathbf{d}^T\nabla^2 f(\mathbf{x}^*)\mathbf{d}, and the sign of the quadratic form decides whether ff goes up in every direction.

python · try it yourself
import numpy as np

# f(x, y) = x^3 - 3x + y^2 - 2y : stationary points where 3x^2 - 3 = 0 and 2y - 2 = 0
f = lambda x, y: x**3 - 3 * x + y**2 - 2 * y
for x in (-1.0, 1.0):
    y = 1.0
    H = np.array([[6 * x, 0.0], [0.0, 2.0]])
    eig = np.linalg.eigvalsh(H)
    kind = "local min" if eig.min() > 0 else "saddle" if eig.min() < 0 < eig.max() else "max/degenerate"
    print(f"({x:+.0f}, {y:.0f}): f = {f(x, y):+.1f}, Hessian eigenvalues {eig}, {kind}")
(-1, 1): f = +1.0, Hessian eigenvalues [-6.  2.], saddle
(+1, 1): f = -3.0, Hessian eigenvalues [2. 6.], local min

When the second-order test is silent

If the Hessian is only semidefinite, higher-order terms decide. f(x)=x4f(x) = x^4 and f(x)=x3f(x) = x^3 both have f′(0)=f′′(0)=0f'(0) = f''(0) = 0; the first has a minimum at 0, the second does not. Algorithms treat such points as "probably fine" and stop when the gradient is small.

Convex problems: the first-order condition is enough

For a convex differentiable ff, ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0} is necessary and sufficient for a global minimum. For a convex ff over a convex set CC, the condition becomes a variational inequality:

∇f(x∗)T(y−x∗)≥0for all y∈C\nabla f(\mathbf{x}^*)^T(\mathbf{y} - \mathbf{x}^*) \ge 0 \quad \text{for all } \mathbf{y} \in C

In words: from the optimum, no feasible direction goes downhill. On a box this says each coordinate is either interior with zero partial derivative or at a bound with the partial derivative pointing outward.

python · try it yourself
import numpy as np

# minimize (x - 2)^2 + (y + 1)^2 over the box 0 <= x, y <= 1
grad = lambda p: np.array([2 * (p[0] - 2), 2 * (p[1] + 1)])
x_star = np.clip([2.0, -1.0], 0, 1)          # unconstrained minimum, clipped
g = grad(x_star)
print("x* =", x_star, " gradient =", g)
rng = np.random.default_rng(0)
Y = rng.uniform(0, 1, size=(10000, 2))
print("min of grad^T (y - x*) over 10000 box points:", round(float(((Y - x_star) @ g).min()), 4))
x* = [1. 0.]  gradient = [-2.  2.]
min of grad^T (y - x*) over 10000 box points: 0.0192

At (1,0)(1, 0) the gradient (−2,2)(-2, 2) points out of the box in both coordinates, so every feasible direction has a non-negative directional derivative. (Clipping the unconstrained minimum is only correct here because the objective is separable with equal scaling; in general you need Projected Gradient Descent or the KKT conditions.)

Stopping tests in practice

Algorithms stop when the optimality conditions hold approximately: ∥∇f(xk)∥≤ε\lVert \nabla f(\mathbf{x}_k) \rVert \le \varepsilon, often relative to the starting gradient, plus limits on iterations and on the change in ff. For constrained problems the test is the KKT residual (Inequality Constraints and the KKT Conditions).

In software

  • SciPy's minimize reports jac (the final gradient) and stops on gtol; checking np.linalg.norm(res.jac) is a quick sanity check.
  • Deep learning training rarely reaches a stationary point: it stops on a validation metric, and in high dimensions most stationary points are saddles, not bad local minima.

Exercises

Exercise 1: Classify

Find and classify the stationary points of f(x,y)=x2−y2f(x, y) = x^2 - y^2.

Answer

Only (0,0)(0, 0), a saddle point.

Solution

∇f=(2x,−2y)=0\nabla f = (2x, -2y) = \mathbf{0} only at the origin. The Hessian diag⁡(2,−2)\operatorname{diag}(2, -2) has eigenvalues of both signs, so ff rises along xx and falls along yy.

Exercise 2: One variable

Find the minimum of f(x)=x−ln⁡xf(x) = x - \ln x for x>0x > 0 and prove it is global.

Answer

x∗=1x^* = 1, f(1)=1f(1) = 1.

Solution

f′(x)=1−1/x=0f'(x) = 1 - 1/x = 0 gives x=1x = 1, and f′′(x)=1/x2>0f''(x) = 1/x^2 > 0 for all x>0x > 0, so ff is convex on its domain and the stationary point is the global minimum.

Exercise 3: Check a candidate (code)

Is (1,1)(1, 1) a strict local minimum of the Rosenbrock function? Check the gradient and the Hessian eigenvalues.

Answer

Yes: the gradient is zero and the Hessian eigenvalues are about 0.4 and 1001.6.

Solution
python · try it yourself
import numpy as np
x, y = 1.0, 1.0
g = np.array([-2 * (1 - x) - 400 * x * (y - x**2), 200 * (y - x**2)])
H = np.array([[2 - 400 * y + 1200 * x**2, -400 * x], [-400 * x, 200.0]])
print(g + 0, [round(float(v), 4) for v in np.linalg.eigvalsh(H)])
[0. 0.] [0.3994, 1001.6006]

Both eigenvalues are positive, so the sufficient condition holds. The ratio of about 2500 is the condition number that makes this valley hard for gradient descent.

Test yourself

At a stationary point the Hessian has eigenvalues 33 and −1-1. The point is…

  1. a local minimum
  2. a local maximum
  3. a saddle point
  4. a global minimum

Which statement is true for a convex differentiable ff?

  1. ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0} is necessary and sufficient for a global minimum
  2. It may have several separate local minima with different values
  3. Its Hessian is always positive definite
  4. It has no stationary points

∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0} and ∇2f(x∗)⪰0\nabla^2 f(\mathbf{x}^*) \succeq 0 (semidefinite, with a zero eigenvalue). Then x∗\mathbf{x}^*…

  1. must be a strict minimum
  2. must be a saddle
  3. may or may not be a local minimum
  4. must be a maximum