Optimisation & Mathematical Methods for Economics The University of Edinburgh · School of Economics
Convex optimisation is special: every local minimum is global, and powerful algorithms solve even large problems efficiently. This week covers convex theory and introduces CVXPY, a Python library for specifying and solving convex programs.
1. Convex Sets and Functions
A set \(C\) is convex if \(\lambda \mathbf{x} + (1-\lambda)\mathbf{y} \in C\) for all \(\mathbf{x}, \mathbf{y} \in C\) and \(\lambda \in [0,1]\).
A function \(f\) is convex if \(\text{dom}(f)\) is convex and \(f(\lambda x + (1-\lambda)y) \leq \lambda f(x) + (1-\lambda)f(y)\).
# Economic example: Production planning with CVXPY# A firm produces 3 goods using 2 inputs# Maximise revenue subject to input availabilityrevenue = np.array([10, 15, 8]) # price per unit of each goodinput_use = np.array([ # input requirements [2, 3, 1], # input 1 per unit [1, 2, 3], # input 2 per unit])capacity = np.array([120, 100]) # available inputsq = cp.Variable(3) # quantitiesobjective = cp.Maximize(revenue @ q)constraints = [ input_use @ q <= capacity, q >=0]prob = cp.Problem(objective, constraints)prob.solve()print("Production planning solution:")for i inrange(3):print(f" Good {i+1}: {q.value[i]:.2f} units")print(f"\nMaximum revenue: ${prob.value:.2f}")print(f"\nShadow prices (dual values):")for i inrange(2):print(f" Input {i+1}: ${constraints[0].dual_value[i]:.2f}")
Production planning solution:
Good 1: 52.00 units
Good 2: 0.00 units
Good 3: 16.00 units
Maximum revenue: $648.00
Shadow prices (dual values):
Input 1: $4.40
Input 2: $1.20
Exercises
Exercise 1: Formulate and solve a Markowitz portfolio optimisation problem in CVXPY: minimise portfolio variance subject to achieving a target return of 10% and weights summing to 1.
import cvxpy as cpw = cp.Variable(4)ret = mu @ wrisk = cp.quad_form(w, Sigma)prob = cp.Problem(cp.Minimize(risk), [cp.sum(w) ==1, ret >=0.10, w >=0])prob.solve()