Convex constrained-optimization

Introduction to Optimization in Python

Jasmin Ludolf

Content Developer

Convex constrained-optimization

Convex function.

  • Constraint: limitation on variables
  • Indifference curve: represents a combination of variables
  • Find the point on the indifference curve that minimizes or maximizes the objective function
Introduction to Optimization in Python

The indifference curve

A young woman with dark hair works on a laptop

  • Freelance software engineer
  • Values work ($w$) and leisure ($l$)

 

Utility function:

  • $U(w, l)=w^{0.4}l^{0.6}$
Introduction to Optimization in Python

Plotting the indifference curve

import numpy as np
import matplotlib.pyplot as plt


w = np.linspace(1, 30, 100) l = np.linspace(1, 30, 100)
W, L = np.meshgrid(w, l)
F = W**0.4 * L**0.6
plt.figure(figsize=(8, 6))
contours = plt.contour(W, L, F, levels=[5, 10, 15, 20])
plt.clabel(contours)
plt.title('Indifference Curves for the function: w**0.4 * l**0.6') plt.xlabel('w') plt.ylabel('l') plt.grid(True) plt.show()
Introduction to Optimization in Python

The indifference curve

Visualizing the indifference curve

  • Alexia is indifferent about the specific combination of $w$ and $l$
Introduction to Optimization in Python

Time constraint

  • 24 hours per day
  • Linear equality constraint

$$ \max_{w,l} w^{0.4}l^{0.6}$$ $$s.t.\ \ \ w+l = 24$$

  • Add constraint to the graph:
    • l = 24 - w
    • plt.plot(w, l, color='red')

Indifference curve and linear constraint. Indifference curve at optimal level is tangent to constraint. At the tangency point the gradient is perpendicular to the constraint and the tangent.

Introduction to Optimization in Python

Solving with SciPy

def utility_function(vars):
    w, l = vars
    return -(w**0.4 * l**0.6)


def constraint(vars): return 24 - np.sum(vars)
initial_guess = [12, 12] constraint_definition = {'type': 'eq', 'fun': constraint} result = minimize(utility_function, initial_guess, constraints=constraint_definition) print(result.x)
[ 9.60001122 14.39998878]
Introduction to Optimization in Python

Let's practice!

Introduction to Optimization in Python

Preparing Video For Download...