TAI BUI
Back to curriculum
BuildPython

Convex Optimization

Convex problems have exactly one valley: walk downhill and you reach the bottom, guaranteed. Neural networks have millions of valleys and no such promise. Know which one you're standing in before you pick an optimizer.

TIME: ~90 minutesPREREQUISITES: Phase 1, Lessons 04 (Calculus for ML), 08 (Optimization)

Learning Objectives

  • Test whether a function is convex using the definition, second derivative, and Hessian criteria
  • Implement Newton's method and compare its quadratic convergence against gradient descent
  • Solve constrained optimization problems using Lagrange multipliers and interpret KKT conditions
  • Explain why neural network loss landscapes are non-convex yet SGD still finds good solutions

The Problem

Lesson 08 gave you gradient descent, momentum, and Adam: optimizers that walk downhill on any surface with no guarantees about where they end up. On a non-convex landscape, gradient descent can land in a bad local minimum, stall on a saddle point, or oscillate forever. You used it anyway, because neural networks are non-convex and there is no alternative.

Plenty of problems in machine learning aren't like that, though. Linear regression, logistic regression, SVMs, LASSO, ridge regression: all convex. For these, something stronger than walking downhill and hoping exists: optimization with mathematical guarantees. A convex problem has exactly one valley, so any algorithm that walks downhill reaches the global minimum. No restarts needed, no learning rate schedules, no prayer.

Convexity earns its keep in three ways. It tells you upfront whether a problem is easy (convex) or hard (non-convex). It hands you faster tools, like Newton's method, whose guarantees only hold in the easy case. And it explains ideas that show up throughout ML even outside pure convex problems: regularization as a constraint, duality in SVMs, and why deep learning still works despite breaking every nice property convexity would have given it.

Warm-up0/3
1. Picture a marble dropped into a perfectly round bowl versus one dropped into a crumpled paper bag standing upright. In which container are you guaranteed the marble settles at the same lowest point no matter where you drop it?Warm-up
A bowl and a crumpled paper bag shown side by side in cross section, each with a marble poised above it at a different starting point, not yet dropped.
2. As you learned in Lesson 08, two hikers start at different spots on a mountain range with several separate valleys at different elevations, and both walk downhill until they can't go down any further. Would you expect them to necessarily end up in the same valley?Warm-up
A mountain range profile with several valleys at different depths, two hikers marked at different starting slopes, about to walk downhill.
3. In calculus, what does a positive second derivative of a function at a point tell you about its graph there?Warm-up
A curve with one point P marked, with a note that the second derivative is positive there, asking what that tells you about the graph near P.

The Concept

Convex sets

A set S is convex if for any two points in S, the line segment between them also lies entirely in S.

Convex setsNot convex
Rectangle: any two points inside can be connected by a line segment that stays insideStar/crescent shape: a line between two interior points can pass outside the set
Triangle: same property holds for all interior pointsDonut/annulus: the hole means some line segments leave the set
The line segment between any two points stays within the setThe line segment between some pairs of points exits the set

Formal test: for any points x, y in S and any t in [0, 1], the point tx + (1-t)y is also in S.

Examples of convex sets:

  • A line, a plane, all of R^n
  • A ball (circle, sphere, hypersphere)
  • A halfspace: {x : a^T x <= b}
  • The intersection of any number of convex sets

Examples of non-convex sets:

  • A donut (annulus)
  • The union of two disjoint circles
  • Any set with a "dent" or "hole"

Convex functions

A function f is convex if its domain is a convex set and for any two points x, y in its domain and any t in [0, 1]:

f(tx + (1-t)y) <= t*f(x) + (1-t)*f(y)

Geometrically: the line segment between any two points on the graph lies above or on the graph.

PropertyConvex functionNon-convex function
Line segment testThe line between any two points on the graph lies above or on the curveThe line between some points on the graph dips below the curve
ShapeSingle bowl/valley curving upwardMultiple peaks and valleys with mixed curvature
Local minimaEvery local minimum is the global minimumMultiple local minima may exist at different heights

Common convex functions:

  • f(x) = x^2 (parabola)
  • f(x) = |x| (absolute value)
  • f(x) = e^x (exponential)
  • f(x) = max(0, x) (ReLU, though piecewise linear)
  • f(x) = -log(x) for x > 0 (negative log)
  • Any linear function f(x) = a^T x + b (both convex and concave)

Testing for convexity

Three practical tests, from easiest to most rigorous.

Test 1: Second derivative test (1D). If f''(x) >= 0 for all x, then f is convex.

  • f(x) = x^2: f''(x) = 2 >= 0. Convex.
  • f(x) = x^3: f''(x) = 6x. Negative for x < 0. Not convex.
  • f(x) = e^x: f''(x) = e^x > 0. Convex.

Test 2: Hessian test (multivariate). If the Hessian matrix H(x) is positive semidefinite for all x, then f is convex. The Hessian is the matrix of second partial derivatives.

Test 3: Definition test. Check the inequality f(tx + (1-t)y) <= t*f(x) + (1-t)*f(y) directly. Useful for functions where derivatives are hard to compute.

Why convexity matters

The central theorem of convex optimization:

For a convex function, every local minimum is a global minimum.

This means gradient descent cannot get trapped. Any downhill path leads to the same answer. The algorithm is guaranteed to converge to the optimal solution.

Mermaid diagram

Consequences:

  • No need for random restarts
  • No need for sophisticated learning rate schedules
  • Convergence proofs are possible (rate depends on function properties)
  • The solution is unique (up to flat regions)

Convex vs non-convex in ML

ProblemConvex?Why
Linear regression (MSE)YesLoss is quadratic in weights
Logistic regressionYesLog-loss is convex in weights
SVM (hinge loss)YesMaximum of linear functions
LASSO (L1 regression)YesSum of convex functions is convex
Ridge regression (L2)YesQuadratic + quadratic = convex
Neural network (any loss)NoNonlinear activations create non-convex landscape
k-means clusteringNoDiscrete assignment step
Matrix factorizationNoProduct of unknowns

Linear models with convex losses are convex. The moment you add hidden layers with nonlinear activations, convexity breaks.

The Hessian matrix

The Hessian H of a function f: R^n -> R is the n x n matrix of second partial derivatives.

H[i][j] = d^2 f / (dx_i dx_j)

For f(x, y) = x^2 + 3xy + y^2:

df/dx = 2x + 3y       d^2f/dx^2 = 2      d^2f/dxdy = 3
df/dy = 3x + 2y       d^2f/dydx = 3      d^2f/dy^2 = 2

H = [ 2  3 ]
    [ 3  2 ]

The Hessian tells you about curvature:

  • Eigenvalues all positive: the function curves upward in every direction (convex at that point)
  • Eigenvalues all negative: curves downward in every direction (concave, a local max)
  • Mixed signs: saddle point (curves up in some directions, down in others)
  • Zero eigenvalue: flat in that direction (degenerate)

For convexity, the Hessian must be positive semidefinite (all eigenvalues >= 0) everywhere, not just at one point.

Newton's method

Gradient descent uses first-order information: the gradient. Newton's method goes a step further and uses second-order information, the Hessian, fitting a quadratic approximation at the current point and jumping straight to that quadratic's minimum.

Update rule:
  x_new = x - H^(-1) * gradient

Compare to gradient descent:
  x_new = x - lr * gradient

Newton's method replaces the scalar learning rate with the inverse Hessian. This automatically adjusts the step size and direction based on local curvature.

Mermaid diagram

Advantages:

  • Quadratic convergence near the minimum (error squares each step)
  • No learning rate to tune
  • Scale-invariant (works regardless of how you parameterize the problem)

Disadvantages:

  • Computing the Hessian costs O(n^2) memory and O(n^3) to invert
  • For a neural network with 1 million weights, that is 10^12 entries and 10^18 operations
  • Not practical for deep learning

Constrained optimization

Unconstrained optimization: minimize f(x) over all x. Constrained optimization: minimize f(x) subject to constraints.

Real problems have constraints. You want to minimize cost but your budget is limited. You want to minimize error but your model complexity is bounded.

Mermaid diagram

Lagrange multipliers

The method of Lagrange multipliers converts a constrained problem into an unconstrained one.

Problem: minimize f(x) subject to g(x) = 0.

Solution: introduce a new variable (the Lagrange multiplier lambda) and solve the unconstrained problem:

L(x, lambda) = f(x) + lambda * g(x)

At the solution, the gradient of L is zero:

dL/dx = df/dx + lambda * dg/dx = 0
dL/dlambda = g(x) = 0

Geometric intuition: at the constrained minimum, the gradient of f must be parallel to the gradient of the constraint g. If they were not parallel, you could move along the constraint surface and reduce f further.

Mermaid diagram

Example: minimize f(x,y) = x^2 + y^2 subject to x + y = 1.

L = x^2 + y^2 + lambda(x + y - 1)

dL/dx = 2x + lambda = 0  =>  x = -lambda/2
dL/dy = 2y + lambda = 0  =>  y = -lambda/2
dL/dlambda = x + y - 1 = 0

From first two: x = y
Substituting: 2x = 1, so x = y = 0.5, lambda = -1

The closest point on the line x + y = 1 to the origin is (0.5, 0.5).

KKT conditions

The Karush-Kuhn-Tucker conditions extend Lagrange multipliers to inequality constraints.

Problem: minimize f(x) subject to g_i(x) <= 0 for i = 1, ..., m.

The KKT conditions (necessary for optimality):

1. Stationarity:    df/dx + sum(lambda_i * dg_i/dx) = 0
2. Primal feasibility:  g_i(x) <= 0  for all i
3. Dual feasibility:    lambda_i >= 0  for all i
4. Complementary slackness:  lambda_i * g_i(x) = 0  for all i

Complementary slackness is the key insight: either the constraint is active (g_i = 0, the solution sits on the boundary) or the multiplier is zero (the constraint does not matter). A constraint that does not affect the solution has lambda = 0.

KKT conditions are central to SVMs. The support vectors are the data points where the constraint is active (lambda > 0). All other data points have lambda = 0 and do not affect the decision boundary.

Regularization as constrained optimization

L1 and L2 regularization aren't arbitrary tricks: they're constrained optimization problems wearing a disguise.

L2 regularization (Ridge):

minimize  Loss(w)  subject to  ||w||^2 <= t

Equivalent unconstrained form:
minimize  Loss(w) + lambda * ||w||^2

The constraint ||w||^2 <= t defines a ball (circle in 2D, sphere in 3D). The solution is where the loss contours first touch this ball.

L1 regularization (LASSO):

minimize  Loss(w)  subject to  ||w||_1 <= t

Equivalent unconstrained form:
minimize  Loss(w) + lambda * ||w||_1

The constraint ||w||_1 <= t defines a diamond (rotated square in 2D).

PropertyL2 constraint (circle)L1 constraint (diamond)
Constraint shapeCircle (sphere in higher dims)Diamond (rotated square in 2D)
Where loss contour touchesSmooth boundary — any point on the circleCorner — aligned with an axis
Solution behaviorWeights are small but nonzeroSome weights are exactly zero (sparse)
ResultWeight shrinkageFeature selection

This explains why L1 produces sparse models (feature selection) while L2 only shrinks weights. The diamond has corners aligned with axes. Loss contours are more likely to touch a corner, setting one or more weights exactly to zero.

Duality

Every constrained optimization problem (the primal) has a companion problem (the dual). For convex problems, the primal and dual have the same optimal value. This is strong duality.

The Lagrangian dual function:

Primal: minimize f(x) subject to g(x) <= 0
Lagrangian: L(x, lambda) = f(x) + lambda * g(x)
Dual function: d(lambda) = min_x L(x, lambda)
Dual problem: maximize d(lambda) subject to lambda >= 0

Why duality matters:

  • The dual problem is sometimes easier to solve than the primal
  • SVMs are solved in their dual form, where the problem depends on dot products between data points (enabling the kernel trick)
  • The dual provides a lower bound on the primal optimum, useful for checking solution quality

For SVMs specifically:

Primal: find w, b that maximize the margin 2/||w|| subject to
        y_i(w^T x_i + b) >= 1 for all i

Dual:   maximize sum(alpha_i) - 0.5 * sum_ij(alpha_i * alpha_j * y_i * y_j * x_i^T x_j)
        subject to alpha_i >= 0 and sum(alpha_i * y_i) = 0

The dual only involves dot products x_i^T x_j.
Replace x_i^T x_j with K(x_i, x_j) to get the kernel trick.

Why deep learning works despite non-convexity

Neural network loss functions are wildly non-convex. By every classical measure, optimizing them should fail, yet stochastic gradient descent finds good solutions reliably.

Most local minima are good enough. In high-dimensional spaces, random critical points (where the gradient is zero) are overwhelmingly saddle points, not local minima. The few local minima that exist tend to have loss values close to the global minimum. Getting trapped in a terrible local minimum is extremely unlikely when the parameter space has millions of dimensions.

Saddle points are the real obstacle, not local minima. In a function with n parameters, a saddle point has a mix of positive and negative curvature directions. For a random critical point in high dimensions, the probability of all n eigenvalues being positive (local minimum) is roughly 2^(-n). Almost all critical points are saddle points, and SGD's noise helps escape them.

Overparameterization smooths the landscape. Networks with more parameters than training examples have smoother, more connected loss surfaces. Wider networks have fewer bad local minima. This is counterintuitive but empirically consistent.

Loss landscape structure:

PropertyLow-dimensional spaceHigh-dimensional space
LandscapeMany isolated peaks and valleysSmoothly connected valleys
MinimaMany isolated local minimaFew bad local minima; most are near-optimal
NavigationHard to find global minimumMany paths lead to good solutions
Critical pointsMix of local minima and saddle pointsOverwhelmingly saddle points, not local minima

Stochastic noise acts as implicit regularization. Mini-batch SGD adds noise that prevents settling into sharp minima. Sharp minima overfit; flat minima generalize. The noise biases optimization toward flat regions of the loss landscape.

Second-order methods in practice

Pure Newton's method is impractical for large models. Several approximations make second-order information usable.

L-BFGS (Limited-memory BFGS): Approximates the inverse Hessian using the last m gradient differences. Requires O(mn) memory instead of O(n^2). Works well for problems with up to ~10,000 parameters. Used in classical ML (logistic regression, CRFs) but not deep learning.

Natural gradient: Uses the Fisher information matrix (expected Hessian of the log-likelihood) instead of the standard Hessian. This accounts for the geometry of probability distributions. K-FAC (Kronecker-Factored Approximate Curvature) approximates the Fisher matrix as a Kronecker product, making it practical for neural networks.

Hessian-free optimization: Uses conjugate gradient to solve Hx = g without ever forming H. Only requires Hessian-vector products, which can be computed in O(n) time via automatic differentiation.

Diagonal approximations: Adam's second moment is a diagonal approximation of the Hessian's diagonal. AdaHessian extends this by using actual Hessian diagonal elements via Hutchinson's estimator.

MethodMemoryPer-step costWhen to use
Gradient descentO(n)O(n)Baseline, large models
Newton's methodO(n^2)O(n^3)Small convex problems
L-BFGSO(mn)O(mn)Medium convex problems
AdamO(n)O(n)Deep learning default
K-FACO(n)O(n) per layerResearch, large-batch training
Check: The Concept0/34
1. What is a convex set?Define
A rectangle labelled set S, with two points A and B inside it joined by a straight line segment that stays within the rectangle, asking what this kind of set is called.
2. What does it mean for a function to be convex?Define
A single bowl-shaped curve with two marked points on it joined by a straight segment that lies on or above the curve, asking what kind of function has this property.
3. What is a local minimum?Define
A bumpy landscape with several dips of different depths, with point X marked in one of the shallower dips and a dashed line showing only its immediate neighbors, asking whether X is a minimum of that local kind.
4. What is a global minimum?Define
A bumpy landscape with point Y marked at the lowest dip, with a note asking whether Y is the lowest point across the entire curve shown, not just compared with its neighbors.
5. What is the Hessian matrix of a function f: R^n -> R?Define
Four boxes holding the second partial derivatives of f(x, y) with respect to every pair of its two variables, arranged in a 2 by 2 grid, with a note asking what this grid is called.
6. What does it mean for a matrix to be positive semidefinite?Define
A generic 2 by 2 matrix M with a question mark over a claim that both its eigenvalues are non-negative, asking what matrices with this property are called.
7. What is the condition number of a Hessian?Define
Two sets of contour rings seen from above, valley A nearly circular and valley B much more stretched out along one axis, asking what single number captures that difference.
8. What is Newton's method as an optimizer?Define
A flow of three boxes: the current point, fitting a local quadratic approximation using the Hessian, then jumping to that quadratic's minimum, with a note asking what this procedure is called.
9. What is a Lagrange multiplier?Define
A flow from a constrained problem, through adding one new unnamed variable, to an unconstrained problem built from it, asking what that new variable is called.
10. What are the KKT conditions?Define
A flow from Lagrange multipliers for equality constraints, through an unlabelled extension step, to a generalization that also handles inequality constraints, asking the name of the conditions that make this work.
11. In the KKT conditions, what does 'complementary slackness' (lambda_i * g_i(x) = 0) mean?Define
Two labelled scenarios, a constraint being active and a multiplier being zero, with a question mark between them asking whether both can be nonzero for the same constraint at once.
12. What is duality in constrained optimization?Define
A flow from a primal constrained problem, through an unnamed companion problem built from its Lagrangian, to a note that it comes from that Lagrangian, asking what the companion problem is called.
13. What is strong duality?Define
Two boxes, the primal optimal value p-star and the dual optimal value d-star, with a question mark between them, asking their relation under convexity and Slater's condition.
14. What is L-BFGS?Define
Newton's method, which stores the full Hessian, next to an unnamed method that stores only the last m gradient differences, asking which method that is.
15. What is a saddle point?Define
Two curve profiles crossing at point P, one slice curving upward on both sides and the other curving downward on both sides, with a note that the gradient is zero at P, asking what such a point is called.
16. What is overparameterization?Define
Two boxes, parameter count and training example count, with a question mark between them, asking which direction of comparison the term describes and what effect it has.
17. Which of these sets is not convex?Define
A plain triangle with two points inside it joined by a segment that stays inside, as a reminder of the general convexity test to apply to the four named sets in the question.
18. Why does the L1 constraint (diamond) tend to produce sparse solutions while the L2 constraint (circle) does not?Explain
A diamond labelled the L1 constraint next to a circle labelled the L2 constraint, with no loss contour drawn yet, asking why one tends to zero out weights and the other does not.
19. What does it mean if the Hessian at a point has all negative eigenvalues?Explain
A hill-shaped surface profile, curving downward in every direction around its peak, with a note that both Hessian eigenvalues there are negative, asking what kind of point it is.
20. Why does SGD find good solutions in non-convex neural network landscapes despite the lack of convexity guarantees?Explain
Two boxes contrasting a low-dimensional landscape's mix of minima and saddles with an unlabelled high-dimensional one, asking what kind of critical point dominates there and why SGD still works.
21. Why is the SVM dual formulation particularly useful compared to the primal?Explain
A flow from two data points to their dot product to an unlabelled final step, asking what the dual formulation can swap in for that dot product.
22. How does K-FAC make second-order optimization practical for neural networks?Explain
The lesson's own table of five optimization methods with their memory, per-step cost and when to use them, with a note asking how K-FAC achieves O(n) memory.
23. Why does convexity guarantee that gradient descent cannot get trapped in a bad local minimum?Explain
A single bowl-shaped curve with no other dips anywhere on it, with a note asking whether walking downhill from any starting point can ever end short of the true bottom.
24. Why does Newton's method need no learning rate to tune?Explain
Gradient descent's update rule with its chosen learning rate written out, next to Newton's method's update rule with a blank where a learning rate would normally go, asking what fills that blank.
25. Why is pure Newton's method not practical for a neural network with 1 million weights?Explain
A network with 1 million weights and two unresolved formulas, the Hessian's size and the cost to invert it, asking roughly how large those numbers are at this scale.
26. At a constrained minimum with an equality constraint g(x) = 0, how are the gradients of f and g related?Explain
Nested contour rings of a function f with a constraint curve crossing them, and only the gradient of f drawn at one marked point on the constraint, asking how the gradient of g relates to it there.
27. Minimize f(x,y) = x^2 + y^2 subject to x + y = 1 using L = x^2 + y^2 + lambda(x + y - 1). At the solution (0.5, 0.5), what is lambda?Apply
The stationarity equation 2x plus lambda equals 0, with x set to the given solution's value 0.5 but lambda not yet resolved, asking for lambda.
28. Minimizing f(x,y) = x^2 + y^2 subject to x + y = 1 with the Lagrangian method, which point is the solution?Apply
The three equations from the Lagrangian's stationarity and the original constraint, with a question mark asking to solve them for the point (x, y).
29. A function f(x,y) has Hessian H = [[2, 0], [0, -1]] at a point. Using the Hessian test, is f convex at that point?Apply
The given Hessian matrix [[2, 0], [0, -1]], with a question mark asking whether both its eigenvalues are non-negative.
30. Training loss stalls where the gradient norm is near zero and the Hessian has eigenvalues of mixed signs. What is this point, and what helps?Analyze
Two crossing axis lines over a faint contour pattern, meeting at a point where the gradient is near zero and the Hessian has mixed-sign eigenvalues, asking what kind of point this is and whether SGD's noise helps there.
31. An SVM solution has one training point with lambda = 0.7 and g_i(x) = 0, and another point with g_i(x) < 0. Which statement is consistent with the KKT conditions?Analyze
Point A given with lambda 0.7 and its constraint active, next to point B given with a slack constraint and an unresolved multiplier, asking what point B's multiplier must be.
32. A million-parameter network shows a bumpy training curve, and a teammate blames a bad local minimum. Which diagnosis fits the lesson's reasoning best?Analyze
A bumpy training loss curve over 60 steps, with a note that a teammate blames a bad local minimum in a million-parameter network, asking whether that is the best diagnosis.
33. A colleague argues that ReLU is convex, so a neural network built from ReLU units must have a convex loss. What is wrong with this argument?Analyze
A flow from ReLU alone being convex, through stacking ReLU units into hidden layers, to an unresolved conclusion about the network's loss, asking whether convexity survives that stacking.
34. You are fitting an SVM and want a nonlinear decision boundary without building the features explicitly. Which formulation should you use?Design
The primal formulation, which works with w directly, next to the dual formulation, which works only through the dot product x_i transpose x_j, asking which one has a slot for a kernel.

Build It

Step 1: Convexity checker

Build a function that tests convexity empirically by sampling points and checking the definition.

python
import random
import math
 
def check_convexity(f, dim, bounds=(-5, 5), samples=1000):
    violations = 0
    for _ in range(samples):
        x = [random.uniform(*bounds) for _ in range(dim)]
        y = [random.uniform(*bounds) for _ in range(dim)]
        t = random.uniform(0, 1)
        mid = [t * xi + (1 - t) * yi for xi, yi in zip(x, y)]
        lhs = f(mid)
        rhs = t * f(x) + (1 - t) * f(y)
        if lhs > rhs + 1e-10:
            violations += 1
    return violations == 0, violations

Step 2: Newton's method for 2D

Implement Newton's method using an explicit Hessian. Compare convergence speed against gradient descent.

python
def newtons_method(f, grad_f, hessian_f, x0, steps=50, tol=1e-12):
    x = list(x0)
    history = [x[:]]
    for _ in range(steps):
        g = grad_f(x)
        H = hessian_f(x)
        det = H[0][0] * H[1][1] - H[0][1] * H[1][0]
        if abs(det) < 1e-15:
            break
        H_inv = [
            [H[1][1] / det, -H[0][1] / det],
            [-H[1][0] / det, H[0][0] / det],
        ]
        dx = [
            H_inv[0][0] * g[0] + H_inv[0][1] * g[1],
            H_inv[1][0] * g[0] + H_inv[1][1] * g[1],
        ]
        x = [x[0] - dx[0], x[1] - dx[1]]
        history.append(x[:])
        if sum(gi ** 2 for gi in g) < tol:
            break
    return history

Step 3: Lagrange multiplier solver

Solve constrained optimization using gradient descent on the Lagrangian.

python
def lagrange_solve(f_grad, g_val, g_grad, x0, lr=0.01,
                   lr_lambda=0.01, steps=5000):
    x = list(x0)
    lam = 0.0
    history = []
    for _ in range(steps):
        fg = f_grad(x)
        gv = g_val(x)
        gg = g_grad(x)
        x = [
            xi - lr * (fgi + lam * ggi)
            for xi, fgi, ggi in zip(x, fg, gg)
        ]
        lam = lam + lr_lambda * gv
        history.append((x[:], lam, gv))
    return history

Step 4: Compare first-order vs second-order

Run gradient descent and Newton's method on the same quadratic function. Count the steps to convergence.

python
def quadratic(x):
    return 5 * x[0] ** 2 + x[1] ** 2
 
def quadratic_grad(x):
    return [10 * x[0], 2 * x[1]]
 
def quadratic_hessian(x):
    return [[10, 0], [0, 2]]

Newton's method will converge in 1 step (it is exact for quadratics). Gradient descent will take hundreds of steps because the eigenvalues of the Hessian differ by a factor of 5, creating an elongated valley.

Check: Build It0/5
1. In newtons_method, what happens when the determinant of the Hessian has absolute value below 1e-15?Apply
A short excerpt of the newtons_method code checking whether the inverted Hessian is None, with a question mark asking what happens when that branch is taken.
2. newtons_method is run on f(x) = 5*x0^2 + x1^2 starting at (3, 4), with the Hessian [[10, 0], [0, 2]]. Where is x after the first update?Apply
The gradient at the starting point (3, 4) computed as [30, 8], with the Newton update left unresolved, asking for x after one step.
3. In lagrange_solve, how is lambda updated on each iteration?Apply
A short excerpt of the lagrange_solve code, showing the x update and the lambda update, with a question mark asking what kind of step the lambda update is.
4. check_convexity is called on f(x) = x^3 with dim=1, bounds=(-5, 5), and 1000 samples. What does it return?Apply
The lesson's own check_convexity function called on f(x) = x cubed with 1000 samples over [-5, 5], asking what it returns.
5. On f(x,y) = 50x^2 + y^2, gradient descent is slow while Newton's method finishes in one step. What explains the gap?Analyze
A stretched set of contour rings for f(x,y) = 50x squared plus y squared, with a note asking what single property of its Hessian explains why gradient descent is so much slower than Newton's method here.

Use It

Convexity analysis applies directly when choosing ML models and solvers.

For convex problems (logistic regression, SVMs, LASSO):

  • Use dedicated solvers (liblinear, CVXPY, scipy.optimize.minimize with method='L-BFGS-B')
  • Expect a unique global solution
  • Second-order methods are practical and fast

For non-convex problems (neural networks):

  • Use first-order methods (SGD, Adam)
  • Accept that the solution depends on initialization and randomness
  • Use overparameterization, noise, and learning rate schedules as implicit regularization
  • Do not waste time searching for the global minimum. A good local minimum is sufficient.
python
from scipy.optimize import minimize
 
result = minimize(
    fun=lambda w: sum((y - X @ w) ** 2) + 0.1 * sum(w ** 2),
    x0=np.zeros(d),
    method='L-BFGS-B',
    jac=lambda w: -2 * X.T @ (y - X @ w) + 0.2 * w,
)

For SVMs, the dual formulation lets you use the kernel trick:

python
from sklearn.svm import SVC
 
svm = SVC(kernel='rbf', C=1.0)
svm.fit(X_train, y_train)
print(f"Support vectors: {svm.n_support_}")
Check: Use It0/3
1. A team fits logistic regression with SGD, a tuned learning rate schedule, and many random restarts to avoid bad local minima. What is wrong with this approach?Analyze
A flow of SGD, a tuned learning rate schedule and many random restarts, applied to fitting logistic regression, with a note that its loss is convex, asking whether this much machinery is justified.
2. You want a sparse regression model that selects features automatically, and you want it to stay a convex problem. Which choice fits?Design
Two candidate approaches, an L2 constraint and an L1 constraint, with a note asking which one gives a sparse model while staying a convex problem.
3. You must fit a convex logistic regression with about 5,000 parameters, exactly and quickly. Which optimizer fits best?Design
A scenario stating a medium-sized, 5,000 parameter convex problem, with a question mark asking which method from the lesson's own table fits it best.

Ship It

This lesson produces:

  • code/convex.py, with from-scratch implementations of the convexity checker, Newton's method, the Lagrange multiplier solver, and the Hessian eigenvalue analysis this lesson walks through
  • outputs/skill-convexity-checker.md, a skill that determines whether a given optimization problem is convex and points you to the right solver once you know
Author's Note

The mistake I see most often is treating a bumpy loss curve as a diagnosis instead of a symptom. In a million-parameter network, a rough training run is far more likely to be a bad learning rate, a gradient issue, or a data problem than an actual bad local minimum. Non-convex doesn't mean cursed. It means the guarantees are gone, so you have to go look at what actually broke instead of assuming the landscape is out to get you.

The flip side matters just as much. If you do have a convex problem on your hands, a logistic regression, an SVM, a LASSO fit, stop babysitting a learning rate schedule like it's a transformer. Reach for a real solver, get the exact answer, and spend your debugging time somewhere it will actually pay off.

Exercises

  1. Convexity gallery. Test these functions for convexity using the checker: f(x) = x^4, f(x) = sin(x), f(x,y) = x^2 + y^2, f(x,y) = x*y, f(x) = max(x, 0). Explain why each result makes sense.

  2. Newton vs gradient descent race. Run both methods on f(x,y) = 50*x^2 + y^2 from the starting point (10, 10). How many steps does each need to reach loss < 1e-10? What happens to gradient descent when the condition number (ratio of largest to smallest Hessian eigenvalue) increases?

  3. Lagrange multiplier geometry. Minimize f(x,y) = (x-3)^2 + (y-3)^2 subject to x + 2y = 4. Verify the solution by checking that the gradient of f is parallel to the gradient of g at the solution.

  4. Regularization constraint. Implement L1-constrained optimization: minimize (x-3)^2 + (y-2)^2 subject to |x| + |y| <= 1. Show that the solution has one coordinate equal to zero (sparsity from the diamond constraint).

  5. Hessian eigenvalue analysis. Compute the Hessian of the Rosenbrock function (the curved-valley benchmark from Lesson 08) at (1,1) and at (-1,1). Compute eigenvalues at both points. What do the eigenvalues tell you about the curvature at the minimum versus far from it?

Key Terms

TermWhat it means
Convex setA set where the line segment between any two points in the set stays inside the set
Convex functionA function where the line between any two points on its graph lies above or on the graph. Equivalently, Hessian is positive semidefinite everywhere
Local minimumA point lower than all nearby points. For convex functions, every local minimum is the global minimum
Global minimumThe lowest point of a function over its entire domain
Hessian matrixThe matrix of all second partial derivatives. Encodes curvature information
Positive semidefiniteA matrix whose eigenvalues are all non-negative. The multidimensional analogue of "second derivative >= 0"
Condition numberRatio of largest to smallest eigenvalue of the Hessian. High condition number means elongated valleys and slow gradient descent
Newton's methodSecond-order optimizer that uses the inverse Hessian to determine step direction and size. Quadratic convergence near the minimum
Lagrange multiplierA variable introduced to convert a constrained optimization problem into an unconstrained one
KKT conditionsNecessary conditions for optimality with inequality constraints. Generalize Lagrange multipliers
Complementary slacknessAt the solution, either a constraint is active or its multiplier is zero. Never both nonzero
DualityEvery constrained problem has a companion dual problem. For convex problems, both have the same optimal value
Strong dualityPrimal and dual optimal values are equal. Holds for convex problems satisfying Slater's condition
L-BFGSApproximate second-order method that stores the last m gradient differences instead of the full Hessian
Saddle pointA point where the gradient is zero but it is a minimum in some directions and a maximum in others
OverparameterizationUsing more parameters than training examples. Smooths the loss landscape and reduces bad local minima

Further Reading

Code

Ships With