Math for Causal Inference

Day 25. Unconstrained Optimisation & Convexity

First- and second-order conditions, why convexity makes optimisation easy, and the two algorithms behind every estimator.

Video coming soon
Learning outcomes
  • ●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.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

Conditions for an optimum

For a smooth f:Rk→Rf: \mathbb{R}^k \to \mathbb{R}:

  • First-order condition (FOC): at an interior optimum, ∇f(x∗)=0\nabla f(x^*) = 0.
  • Second-order condition (SOC): if also H(x∗)H(x^*) is positive definite, x∗x^* 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:

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

For smooth functions, convex   ⟺  \iff the tangent plane lies below: f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^{\top}(y - x)   ⟺  \iff H(x)H(x) 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: xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta\nabla f(x_t). Cheap per step; needs a good step size η\eta. On a quadratic with Hessian eigenvalues λj\lambda_j it converges iff η<2/λmax⁡\eta < 2/\lambda_{\max}, and slowly when the condition number is large.
  • Newton's method: xt+1=xt−H(xt)−1∇f(xt)x_{t+1} = x_t - H(x_t)^{-1}\nabla f(x_t). 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 ℓ\ell is the classic Newton–Raphson (for the logit it is called iteratively reweighted least squares).

Part 1 · Warm-up

Exercise 1 · A parabola

Minimise f(x)=x2−6x+11f(x) = x^2 - 6x + 11.

Show solution

f′(x)=2x−6=0⇒x=3f'(x) = 2x - 6 = 0 \Rightarrow x = 3; f′′=2>0f''= 2 > 0, a minimum with f(3)=2f(3) = 2.

Exercise 2 · A cubic

Find and classify the critical points of f(x)=x3−3xf(x) = x^3 - 3x.

Show solution

Step 1. 3x2−3=0⇒x=±13x^2 - 3 = 0 \Rightarrow x = \pm1.

Step 2. f′′=6xf'' = 6x: local minimum at x=1x = 1, local maximum at x=−1x = -1.

Step 3. Neither is global: ff is unbounded in both directions.

Exercise 3 · Two variables

Minimise f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x.

Show solution

Step 1. FOC: fx=2x+y−3=0f_x = 2x + y - 3 = 0 and fy=x+2y=0f_y = x + 2y = 0.

Step 2. From the second, x=−2yx = -2y; substitute: −4y+y=3⇒y=−1-4y + y = 3 \Rightarrow y = -1, x=2x = 2.

Step 3. Hessian (2112)\begin{pmatrix}2 & 1\\1 & 2\end{pmatrix} has eigenvalues 3 and 1 (Day 21): PD, so ff is strictly convex and (2,−1)(2, -1) is the global minimum.

Step 4. f(2,−1)=4−2+1−6=−3f(2, -1) = 4 - 2 + 1 - 6 = -3.

Answer: minimum −3 at (2,−1)(2, -1).

Exercise 4 · A saddle

Classify the critical point of f(x,y)=x2−y2f(x, y) = x^2 - y^2.

Show solution

∇f=(2x,−2y)=0\nabla f = (2x, -2y) = 0 at the origin; H=diag⁡(2,−2)H = \operatorname{diag}(2, -2) is indefinite: a saddle (minimum along xx, maximum along yy).

Exercise 5 · Step sizes

Run two steps of gradient descent on f(x)=x2f(x) = x^2 from x0=4x_0 = 4 with η=0.25\eta = 0.25, then with η=1\eta = 1 and η=1.1\eta = 1.1.

Show solution

Step 1. f′(x)=2xf'(x) = 2x, so xt+1=xt−η(2xt)=(1−2η)xtx_{t+1} = x_t - \eta(2x_t) = (1 - 2\eta)x_t.

Step 2. η=0.25\eta = 0.25: factor 0.5, so 4→2→14 \to 2 \to 1 (converges).

Step 3. η=1\eta = 1: factor −1, so 4→−4→44 \to -4 \to 4 (oscillates forever).

Step 4. η=1.1\eta = 1.1: factor −1.2, so 4→−4.8→5.764 \to -4.8 \to 5.76 (diverges).

Answer: here the Hessian is λ=2\lambda = 2, and convergence needs ∣1−2η∣<1|1 - 2\eta| < 1, i.e. η<2/λ=1\eta < 2/\lambda = 1 (Exercise 10).

Part 2 · Proofs

Exercise 6 · Stationary means optimal under convexity

Prove that if ff is convex and differentiable and ∇f(x∗)=0\nabla f(x^*) = 0, then x∗x^* is a global minimum.

Show solution

By the tangent-plane characterisation, for every yy: f(y)≥f(x∗)+∇f(x∗)⊤(y−x∗)=f(x∗)f(y) \ge f(x^*) + \nabla f(x^*)^{\top}(y - x^*) = f(x^*).

Exercise 7 · Building convex functions

Prove that the sum of two convex functions is convex, and that g(x)=f(Ax+b)g(x) = f(Ax + b) is convex when ff is.

Show solution

Step 1. Sum: add the two chord inequalities.

Step 2. Composition: g(λx+(1−λ)y)=f(λ(Ax+b)+(1−λ)(Ay+b))≤λf(Ax+b)+(1−λ)f(Ay+b)g(\lambda x + (1 - \lambda)y) = f(\lambda(Ax + b) + (1 - \lambda)(Ay + b)) \le \lambda f(Ax + b) + (1 - \lambda)f(Ay + b).

Step 3. Hence ∥y−Xβ∥2+λ∥β∥2\|y - X\beta\|^2 + \lambda\|\beta\|^2 is convex in β\beta.

Exercise 8 · Uniqueness

Prove that a strictly convex function has at most one minimiser.

Show solution

Suppose x≠yx \ne y both attain the minimum mm. Strict convexity gives f(12x+12y)<12m+12m=mf(\tfrac12x + \tfrac12y) < \tfrac12m + \tfrac12m = m, contradicting minimality.

Exercise 9 · Newton solves quadratics in one step

Show that Newton's method reaches the minimiser of f(x)=12x⊤Ax−b⊤xf(x) = \frac12x^{\top}Ax - b^{\top}x (AA PD) in one step from any start.

Show solution

Step 1. ∇f=Ax−b\nabla f = Ax - b, H=AH = A.

Step 2. Then x1=x0−A−1(Ax0−b)=A−1bx_1 = x_0 - A^{-1}(Ax_0 - b) = A^{-1}b, which solves ∇f=0\nabla f = 0.

Step 3. So Newton applied to least squares returns (X⊤X)−1X⊤y(X^{\top}X)^{-1}X^{\top}y immediately.

Exercise 10 · When gradient descent converges

For f(x)=12x⊤Axf(x) = \frac12x^{\top}Ax with AA PD, show gradient descent converges to 0 iff η<2/λmax⁡\eta < 2/\lambda_{\max}.

Show solution

Step 1. ∇f=Ax\nabla f = Ax, so xt+1=xt−ηAxt=(I−ηA)xtx_{t+1} = x_t - \eta Ax_t = (I - \eta A)x_t, and by induction xt=(I−ηA)tx0x_t = (I - \eta A)^tx_0.

Step 2. If Aqj=λjqjAq_j = \lambda_jq_j, then (I−ηA)qj=(1−ηλj)qj(I - \eta A)q_j = (1 - \eta\lambda_j)q_j: same eigenvectors, eigenvalues 1−ηλj1 - \eta\lambda_j.

Step 3. Write x0=∑jcjqjx_0 = \sum_jc_jq_j. Then xt=∑jcj(1−ηλj)tqjx_t = \sum_jc_j(1 - \eta\lambda_j)^tq_j.

Step 4. xt→0x_t \to 0 for every start iff ∣1−ηλj∣<1|1 - \eta\lambda_j| < 1 for all jj, i.e. 0<η<2/λj0 < \eta < 2/\lambda_j for all jj, i.e. η<2/λmax⁡\eta < 2/\lambda_{\max}.

Step 5. Speed: the slowest direction shrinks by ∣1−ηλmin⁡∣|1 - \eta\lambda_{\min}| per step. With η\eta near 2/λmax⁡2/\lambda_{\max} and λmin⁡≪λmax⁡\lambda_{\min} \ll \lambda_{\max} (ill-conditioning), this is close to 1, so progress is slow.

Part 3 · Applied

Exercise 11 · A competitive firm

A firm sells at p=10p = 10, produces q=2L0.5q = 2L^{0.5} and pays wage w=5w = 5. Find the profit-maximising labour, output and profit.

Show solution

Step 1. Profit: π(L)=p q−wL=10(2L0.5)−5L=20L0.5−5L\pi(L) = p\,q - wL = 10(2L^{0.5}) - 5L = 20L^{0.5} - 5L.

Step 2. FOC: π′(L)=10L−0.5−5=0⇒L−0.5=0.5⇒L=4\pi'(L) = 10L^{-0.5} - 5 = 0 \Rightarrow L^{-0.5} = 0.5 \Rightarrow L = 4.

Step 3. SOC: π′′(L)=−5L−1.5<0\pi''(L) = -5L^{-1.5} < 0: a maximum.

Step 4. Output q=24=4q = 2\sqrt4 = 4; profit =40−20=20= 40 - 20 = 20.

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 P=100−2QP = 100 - 2Q, cost C=20QC = 20Q. Find the monopoly quantity, price and profit.

Show solution

Step 1. Profit: π(Q)=(100−2Q)Q−20Q=80Q−2Q2\pi(Q) = (100 - 2Q)Q - 20Q = 80Q - 2Q^2.

Step 2. FOC (marginal revenue = marginal cost): 80−4Q=0⇒Q=2080 - 4Q = 0 \Rightarrow Q = 20. SOC: π′′=−4<0\pi'' = -4 < 0.

Step 3. Price: P=100−2(20)=60P = 100 - 2(20) = 60.

Step 4. Profit: (60−20)×20=800(60 - 20) \times 20 = 800.

Answer: Q=20Q = 20, P=60P = 60, profit 800.

Exercise 13 · Newton–Raphson for a Poisson MLE

Ten counts have ∑xi=30\sum x_i = 30. Starting from λ0=1\lambda_0 = 1, perform two Newton–Raphson steps on ℓ(λ)=∑xiln⁡λ−nλ\ell(\lambda) = \sum x_i\ln\lambda - n\lambda.

Show solution

Step 1. Derivatives: ℓ′(λ)=30/λ−10\ell'(\lambda) = 30/\lambda - 10 and ℓ′′(λ)=−30/λ2\ell''(\lambda) = -30/\lambda^2. Newton update: λ←λ−ℓ′(λ)/ℓ′′(λ)\lambda \leftarrow \lambda - \ell'(\lambda)/\ell''(\lambda).

Step 2. From λ0=1\lambda_0 = 1: ℓ′=20\ell' = 20, ℓ′′=−30\ell'' = -30, so λ1=1+20/30≈1.667\lambda_1 = 1 + 20/30 \approx 1.667.

Step 3. From λ1=5/3\lambda_1 = 5/3: ℓ′=18−10=8\ell' = 18 - 10 = 8, ℓ′′=−30/(25/9)=−10.8\ell'' = -30/(25/9) = -10.8, so λ2=1.667+8/10.8≈2.407\lambda_2 = 1.667 + 8/10.8 \approx 2.407.

Step 4. Further steps give 2.883, 2.995, 3.000 (code below).

Answer: the iterates converge to λ^=xˉ=3\hat\lambda = \bar x = 3, slowly at first and then very fast near the optimum.

Exercise 14 · Estimating a CES production function

An economist fits a CES production function ln⁡Yi=ln⁡A+νρln⁡(δKiρ+(1−δ)Liρ)+εi\ln Y_i = \ln A + \frac{\nu}{\rho}\ln\big(\delta K_i^{\rho} + (1 - \delta)L_i^{\rho}\big) + \varepsilon_i by non-linear least squares. The objective can have several local minima and flat regions (e.g. near ρ=0\rho = 0, 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 ρ\rho and δ\delta) and compare the objective values reached.

Step 3. Reparametrise where possible (e.g. profile out ln⁡A\ln A, 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 x>5x > 5 is treated and every unit with x≤5x \le 5 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 x>5x > 5 is treated and every unit with x≤5x \le 5 is not.

Step 2. A steeper slope always fits better: the log-likelihood keeps increasing as β1→∞\beta_1 \to \infty, 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.

★ minimum-2-1012-101xy
Steps to reach the minimum
46
within 0.001
f at last point
0.000000
2/λmax
0.200
η below: converges

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.

Day 24Day 26