Optimisation & Mathematical Methods for Economics The University of Edinburgh · School of Economics
This week we develop the calculus toolkit for optimisation: partial derivatives, the gradient vector, the Hessian matrix, and the crucial concept of convexity.
1. Partial Derivatives and the Gradient
For \(f: \mathbb{R}^n \to \mathbb{R}\), the gradient is the vector of partial derivatives:
The gradient points in the direction of steepest ascent. At a local optimum, \(\nabla f(\mathbf{x}^*) = \mathbf{0}\) (first-order necessary condition).
import numpy as npimport matplotlib.pyplot as plt# Example: f(x1, x2) = -(x1 - 1)^2 - 2(x2 + 1)^2 + 5# Gradient: [-2(x1-1), -4(x2+1)]def f(x1, x2):return-(x1 -1)**2-2*(x2 +1)**2+5def grad_f(x1, x2):return np.array([-2*(x1 -1), -4*(x2 +1)])# Plot function with gradient arrowsx1 = np.linspace(-3, 5, 100)x2 = np.linspace(-5, 3, 100)X1, X2 = np.meshgrid(x1, x2)Z = f(X1, X2)fig, ax = plt.subplots(figsize=(8, 6))cp = ax.contourf(X1, X2, Z, levels=20, cmap='RdYlBu_r', alpha=0.8)plt.colorbar(cp, ax=ax)# Gradient vectors at a grid of pointsfor xi in np.linspace(-2, 4, 5):for xj in np.linspace(-4, 2, 5): g = grad_f(xi, xj) ax.arrow(xi, xj, g[0]*0.15, g[1]*0.15, head_width=0.15, head_length=0.08, fc='black', ec='black')ax.plot(1, -1, 'w*', markersize=15, markeredgecolor='black')ax.set_title('Gradient field of $f(x_1,x_2)$; star = maximum at (1, -1)', fontsize=12)ax.set_xlabel('$x_1$'); ax.set_ylabel('$x_2$')plt.tight_layout()plt.show()
2. The Hessian Matrix
The Hessian is the matrix of second partial derivatives:
Second-order conditions at a critical point \(\mathbf{x}^*\) where \(\nabla f = 0\): - \(H\) negative definite \(\Rightarrow\) local maximum - \(H\) positive definite \(\Rightarrow\) local minimum - \(H\) indefinite \(\Rightarrow\)saddle point
# Classify critical points using the Hessian# f(x,y) = x^3 - 3xy^2 (monkey saddle has critical point at origin)# f(x,y) = x^2 + y^2 (minimum at origin)# f(x,y) = -(x^2 + y^2) (maximum at origin)examples = {"x^2 + y^2 (minimum)": np.array([[2, 0], [0, 2]]),"-(x^2 + y^2) (maximum)": np.array([[-2, 0], [0, -2]]),"x^2 - y^2 (saddle)": np.array([[2, 0], [0, -2]]),}for name, H in examples.items(): eigvals = np.linalg.eigvalsh(H)ifall(eigvals >0): result ="Local MINIMUM (H positive definite)"elifall(eigvals <0): result ="Local MAXIMUM (H negative definite)"else: result ="SADDLE POINT (H indefinite)"print(f"f = {name}")print(f" H = {H.tolist()}, eigenvalues = {eigvals}")print(f" => {result}\n")
f = x^2 + y^2 (minimum)
H = [[2, 0], [0, 2]], eigenvalues = [2. 2.]
=> Local MINIMUM (H positive definite)
f = -(x^2 + y^2) (maximum)
H = [[-2, 0], [0, -2]], eigenvalues = [-2. -2.]
=> Local MAXIMUM (H negative definite)
f = x^2 - y^2 (saddle)
H = [[2, 0], [0, -2]], eigenvalues = [-2. 2.]
=> SADDLE POINT (H indefinite)
3. Convexity and Concavity
A function \(f\) is convex if for all \(\mathbf{x}, \mathbf{y}\) and \(\lambda \in [0,1]\): \[f(\lambda \mathbf{x} + (1-\lambda)\mathbf{y}) \leq \lambda f(\mathbf{x}) + (1-\lambda) f(\mathbf{y})\]
Geometrically: the line segment between any two points on the graph lies above (or on) the graph.
Key result: If \(f\) is convex, then every local minimum is a global minimum. If \(f\) is concave, every local maximum is a global maximum.
Characterisation via the Hessian: If \(f\) is twice differentiable, \(f\) is convex \(\iff H_f(\mathbf{x})\) is positive semi-definite for all \(\mathbf{x}\).
Optimal quantity: q* = 3.5275
Maximum profit: pi* = 3.1285
Exercises
Exercise 1: Find the gradient and Hessian of \(f(x,y) = x^2 y + xy^2 - 3xy\). Locate and classify the critical points.
# Gradient: [2xy + y^2 - 3y, x^2 + 2xy - 3x]# Setting both to zero and solving:# y(2x + y - 3) = 0 and x(x + 2y - 3) = 0# Critical points: (0,0), (3,0), (0,3), (1,1)# Check the Hessian at each to classify.from scipy.optimize import fsolve# ... (students work through this analytically and verify numerically)
Exercise 2: Show that \(f(x) = e^x\) is convex by verifying the Hessian (second derivative) condition.
\(f''(x) = e^x > 0\) for all \(x\), so \(f\) is strictly convex everywhere.