Day 25. Unconstrained Optimisation & Convexity
First- and second-order conditions, why convexity makes optimisation easy, and the two algorithms behind every estimator.
- ●Find and classify critical points with first- and second-order conditions in one and several variables.
- ●Prove that for a convex function every stationary point is a global minimum.
- ●Run gradient descent and Newton's method by hand and state their convergence conditions.
- ●Solve profit- and monopoly-pricing problems and recognise when an MLE does not exist.
Theory
Conditions for an optimum
For a smooth :
- First-order condition (FOC): at an interior optimum, .
- Second-order condition (SOC): if also is positive definite, is a strict local minimum; negative definite, a strict local maximum; indefinite, a saddle point.
Convexity
A function is convex if the chord lies above the graph:
For smooth functions, convex the tangent plane lies below: is PSD everywhere. The payoff: for a convex function, any point with zero gradient is a global minimum, and strict convexity makes it unique. Least squares, ridge, lasso and the (negative) logit log-likelihood are all convex.
Algorithms
When the FOC cannot be solved by hand:
- Gradient descent: . Cheap per step; needs a good step size . On a quadratic with Hessian eigenvalues it converges iff , and slowly when the condition number is large.
- Newton's method: . It minimises the local second-order Taylor approximation (Day 23) at each step; it solves a quadratic in one step and converges very fast near the optimum, but each step needs the Hessian.
For maximum likelihood, Newton's method applied to is the classic Newton–Raphson (for the logit it is called iteratively reweighted least squares).
Part 1 · Warm-up
Exercise 1 · A parabola
Minimise .
Show solution
; , a minimum with .
Exercise 2 · A cubic
Find and classify the critical points of .
Show solution
Step 1. .
Step 2. : local minimum at , local maximum at .
Step 3. Neither is global: is unbounded in both directions.
Exercise 3 · Two variables
Minimise .
Show solution
Step 1. FOC: and .
Step 2. From the second, ; substitute: , .
Step 3. Hessian has eigenvalues 3 and 1 (Day 21): PD, so is strictly convex and is the global minimum.
Step 4. .
Answer: minimum −3 at .
Exercise 4 · A saddle
Classify the critical point of .
Show solution
at the origin; is indefinite: a saddle (minimum along , maximum along ).
Exercise 5 · Step sizes
Run two steps of gradient descent on from with , then with and .
Show solution
Step 1. , so .
Step 2. : factor 0.5, so (converges).
Step 3. : factor −1, so (oscillates forever).
Step 4. : factor −1.2, so (diverges).
Answer: here the Hessian is , and convergence needs , i.e. (Exercise 10).
Part 2 · Proofs
Exercise 6 · Stationary means optimal under convexity
Prove that if is convex and differentiable and , then is a global minimum.
Show solution
By the tangent-plane characterisation, for every : .
Exercise 7 · Building convex functions
Prove that the sum of two convex functions is convex, and that is convex when is.
Show solution
Step 1. Sum: add the two chord inequalities.
Step 2. Composition: .
Step 3. Hence is convex in .
Exercise 8 · Uniqueness
Prove that a strictly convex function has at most one minimiser.
Show solution
Suppose both attain the minimum . Strict convexity gives , contradicting minimality.
Exercise 9 · Newton solves quadratics in one step
Show that Newton's method reaches the minimiser of ( PD) in one step from any start.
Show solution
Step 1. , .
Step 2. Then , which solves .
Step 3. So Newton applied to least squares returns immediately.
Exercise 10 · When gradient descent converges
For with PD, show gradient descent converges to 0 iff .
Show solution
Step 1. , so , and by induction .
Step 2. If , then : same eigenvectors, eigenvalues .
Step 3. Write . Then .
Step 4. for every start iff for all , i.e. for all , i.e. .
Step 5. Speed: the slowest direction shrinks by per step. With near and (ill-conditioning), this is close to 1, so progress is slow.
Part 3 · Applied
Exercise 11 · A competitive firm
A firm sells at , produces and pays wage . Find the profit-maximising labour, output and profit.
Show solution
Step 1. Profit: .
Step 2. FOC: .
Step 3. SOC: : a maximum.
Step 4. Output ; profit .
Answer: hire 4 units of labour, produce 4, earn profit 20 (the value of the marginal product equals the wage).
Exercise 12 · Monopoly pricing
Inverse demand , cost . Find the monopoly quantity, price and profit.
Show solution
Step 1. Profit: .
Step 2. FOC (marginal revenue = marginal cost): . SOC: .
Step 3. Price: .
Step 4. Profit: .
Answer: , , profit 800.
Exercise 13 · Newton–Raphson for a Poisson MLE
Ten counts have . Starting from , perform two Newton–Raphson steps on .
Show solution
Step 1. Derivatives: and . Newton update: .
Step 2. From : , , so .
Step 3. From : , , so .
Step 4. Further steps give 2.883, 2.995, 3.000 (code below).
Answer: the iterates converge to , slowly at first and then very fast near the optimum.
Exercise 14 · Estimating a CES production function
An economist fits a CES production function by non-linear least squares. The objective can have several local minima and flat regions (e.g. near , the Cobb–Douglas limit). What should she do?
Show solution
Step 1. Recognise the risk: a gradient method stops at a point with zero gradient, which need not be the global minimum.
Step 2. Start the optimiser from several points (or a grid over and ) and compare the objective values reached.
Step 3. Reparametrise where possible (e.g. profile out , which enters linearly) and check the curvature (Hessian) at the solution.
Answer: use multiple starts, report sensitivity, and prefer formulations that are convex. This is one reason linear and logit models, whose objectives are convex, dominate applied work.
Exercise 15 · When the MLE does not exist
In a logit of treatment on one covariate, every unit with is treated and every unit with is not. What happens to Newton–Raphson, and what does it mean for propensity-score methods?
Show solution
Step 1. Perfect separation: every unit with is treated and every unit with is not.
Step 2. A steeper slope always fits better: the log-likelihood keeps increasing as , so there is no finite maximum.
Step 3. Newton–Raphson therefore diverges, with fitted probabilities heading to exactly 0 and 1 (the code shows the slope growing every iteration).
Answer: no MLE exists. For causal inference this is a failure of overlap: some covariate values have no comparison units, so effects there cannot be estimated without extrapolation (Day 42).
Verify in Python
import numpy as np
# Gradient descent on a quadratic with condition number 100 vs Newton
A = np.diag([1.0, 100.0]); grad = lambda x: A @ x
x = np.array([1.0, 1.0]); eta = 1.9 / 100
for t in range(200):
x = x - eta * grad(x)
print("GD after 200 steps:", x.round(4), " (slow along the small-eigenvalue direction)")
x0 = np.array([1.0, 1.0]); print("Newton after 1 step:", x0 - np.linalg.solve(A, grad(x0)))
# Newton-Raphson for the Poisson MLE (Exercise 13)
lam, path = 1.0, []
for _ in range(6):
lam = lam - (30 / lam - 10) / (-30 / lam**2); path.append(round(lam, 4))
print("Newton path:", path)
# Perfect separation: the logit slope runs away
x = np.arange(1, 11.); T = (x > 5).astype(float); X = np.column_stack([np.ones(10), x]); b = np.zeros(2)
for it in range(8):
p = 1 / (1 + np.exp(-X @ b)); W = p * (1 - p)
b = b + np.linalg.solve(X.T @ (X * W[:, None]) + 1e-12 * np.eye(2), X.T @ (T - p))
print(f"iteration {it + 1}: slope = {b[1]:.2f}")
Key takeaways
- FOC finds candidates; the Hessian classifies them; convexity turns local into global.
- Gradient descent is cheap but slowed by ill-conditioning; Newton uses curvature and converges fast near the optimum.
- Non-existence of an MLE (separation) is a data problem, often an overlap problem, not a software problem.
Simulation Lab: Gradient Descent vs Newton
Open full screen →Race the two algorithms on an ill-conditioned bowl and on a curved valley; tune the step size until it breaks.
Click to choose a starting point. Gradient descent follows the downhill direction with a fixed step; Newton jumps to the minimum of the local quadratic approximation. On the ill-conditioned quadratic, gradient descent zig-zags or diverges once η > 2/λmax.