SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.5.1 Lagrange multipliersConcept · 30 min
      2. 4.5.2 Inequality constraints and the KKT conditionsConcept · 30 min
      3. 4.5.3 Convex functions and convex problemsConcept · 30 min
      4. 4.5.4 Practice set: constrained optimisationPractice set · 25 min
      5. 4.5.5 Case study: the minimum-variance portfolio and the efficient frontierCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.5 Constrained optimisation Lesson 2 of 5

ConceptLesson 4.5.2 in Level 4, Multivariable calculus and optimisation. About 30 minutes.

Inequality constraints and the KKT conditions

Allowed regions

An inequality constraint g(x,y)≤cg(x, y) \le c allows a whole region: the curve g=cg = c and everything on one side of it. Many limits are like this. A quantity may not be negative, or may not exceed a capacity, but anything within the limit is fine.

Start in one variable, as in lesson 2.4.1: minimise f(x)=(x−3)2f(x) = (x - 3)^2 subject to x≤cx \le c. The function's own lowest point is x=3x = 3.

  • c=5c = 5: the point 3 is allowed, so the constraint changes nothing. The minimum is f(3)=0f(3) = 0, inside the allowed interval.
  • c=1c = 1: 3 is not allowed. ff falls all the way to the edge x=1x = 1, where f=4f = 4 and the slope is f′(1)=−4f'(1) = -4.

Rule: Slack or binding

slack: g<c and ∇f=0binding: g=c and ∇f=λ ∇g\begin{gathered} \text{slack: } g < c \text{ and } \nabla f = \mathbf{0} \\[4pt] \text{binding: } g = c \text{ and } \nabla f = \lambda\,\nabla g \end{gathered}

At a minimum, an inequality constraint is slack, with the point inside the region, or binding, with the point on the edge. A slack constraint has λ=0\lambda = 0 and lesson 4.4.1's condition; a binding one has lesson 4.5.1's.

Slack or binding in two variables

Take the bowl f(x,y)=x2+2y2f(x, y) = x^2 + 2y^2 of lesson 4.5.1 again. Its lowest point is the origin, where f=0f = 0.

With x+y≤3x + y \le 3, the origin is allowed, since 0≤30 \le 3. The constraint is slack: the minimum is (0,0)(0, 0) and λ=0\lambda = 0.

With x+y≥3x + y \ge 3 the origin is not allowed, so the minimum must lie on the edge x+y=3x + y = 3. Lesson 4.5.1 found it: (2,1)(2, 1), with f=6f = 6 and λ=4\lambda = 4.

The shaded side is allowed. When it contains the bowl's lowest point, the constraint is slack. When it does not, the minimum sits on the edge, and ∇f\nabla f points into the allowed side.

The sign of λ\lambda

At a binding constraint the allowed region lies on one side of the edge. At the minimum, no step into the region may go downhill. So ∇f\nabla f, the uphill direction, points into the allowed side, and that fixes the sign of λ\lambda.

  • x+y≥3x + y \ge 3: the allowed side is the way ∇g=(1,1)\nabla g = (1, 1) points. At (2,1)(2, 1), ∇f=(4,4)=4∇g\nabla f = (4, 4) = 4\nabla g, so λ=4≥0\lambda = 4 \ge 0.
  • x+y≤−1.5x + y \le -1.5: the origin is not allowed, and the allowed side is the way −∇g-\nabla g points. Lesson 4.5.1's edge minimum is (−1,−0.5)(-1, -0.5), where ∇f=(−2,−2)=−2∇g\nabla f = (-2, -2) = -2\nabla g, so λ=−2≤0\lambda = -2 \le 0.
  • One variable: for x≤1x \le 1, f′(1)=−4f'(1) = -4 points to the left, into the allowed side. Here g=xg = x has slope 1, so λ=−4\lambda = -4.

The rate of lesson 4.5.1, λ=df∗dc\lambda = \frac{df^*}{dc}, says the same. Raising cc enlarges the region g≤cg \le c, so the best value can only fall: λ≤0\lambda \le 0. It shrinks the region g≥cg \ge c, so the best value can only rise: λ≥0\lambda \ge 0.

A wrong sign means a wrong guess

Suppose you guess that x+y≤3x + y \le 3 binds. The edge gives (2,1)(2, 1) with λ=4\lambda = 4, the wrong sign for a ≤\le constraint: ∇f\nabla f points out of the allowed side. A step inward then goes downhill, so the constraint is slack, not binding.

The KKT conditions

The two cases and the sign fit into one list, named after Karush, Kuhn and Tucker.

Rule: KKT conditions for g ≤ c

∇f=λ ∇g,g≤cλ≤0,λ (g−c)=0\begin{gathered} \nabla f = \lambda\,\nabla g, \quad g \le c \\[4pt] \lambda \le 0, \quad \lambda\,(g - c) = 0 \end{gathered}

A minimum of ff subject to g≤cg \le c satisfies all four. For a constraint g≥cg \ge c, the same list holds with λ≥0\lambda \ge 0.

The last condition, λ(g−c)=0\lambda(g - c) = 0, is called complementary slackness. A product is 0 only if a factor is 0, so either λ=0\lambda = 0 (slack) or g=cg = c (binding).

Both can be 0 at once. With x+y≤0x + y \le 0, the bowl's lowest point (0,0)(0, 0) lies on the edge itself, and there ∇f=0\nabla f = \mathbf{0}, so λ=0\lambda = 0 while g=cg = c.

To solve a problem, guess which case holds, solve it, and keep the guess that passes every condition. The table does this for the bowl:

constraintguesspointλ\lambdapasses?
x+y≤3x + y \le 3slack(0,0)(0, 0)0yes
x+y≤3x + y \le 3binding(2,1)(2, 1)4no: sign
x+y≥3x + y \ge 3slack(0,0)(0, 0)0no: not allowed
x+y≥3x + y \ge 3binding(2,1)(2, 1)4yes
x+y≤−1.5x + y \le -1.5binding(−1,−0.5)(-1, -0.5)−2-2yes

Try it

The first loops are the one-variable example: the lowest allowed point and λ=f′\lambda = f' there, for x≤cx \le c and for x≥cx \ge c.

kkt tests a guess for the bowl: is the point allowed, does λ\lambda have the right sign, and is λ(g−c)=0\lambda(g - c) = 0? Stationarity, ∇f=λ∇g\nabla f = \lambda\nabla g, holds for both guesses by construction.

⌘/Ctrl + Enter runs the cell

Optimisation books usually write every constraint of a minimum as g≤cg \le c and the first condition as ∇f+μ∇g=0\nabla f + \mu\nabla g = \mathbf{0}, with μ≥0\mu \ge 0. That is the same list with μ=−λ\mu = -\lambda. This module keeps lesson 4.5.1's form, so that λ\lambda is always the rate df∗dc\frac{df^*}{dc}.

Two constraints

With two constraints, each multiplier belongs to its own constraint.

Rule: KKT with two constraints

∇f=λ1∇g1+λ2∇g2λi (gi−ci)=0\begin{gathered} \nabla f = \lambda_1\nabla g_1 + \lambda_2\nabla g_2 \\[4pt] \lambda_i\,(g_i - c_i) = 0 \end{gathered}

The point must satisfy both constraints. Each λi\lambda_i is 0 if its constraint is slack, and otherwise has the sign set by its direction: ≤0\le 0 for gi≤cig_i \le c_i, ≥0\ge 0 for gi≥cig_i \ge c_i.

Minimise the bowl subject to x+y≥3x + y \ge 3 and x≤1x \le 1. Each constraint can be slack or binding, so there are four cases:

bindingpointx+y≥3x + y \ge 3?x≤1x \le 1?ff
neither(0,0)(0, 0)noyes0
only x+y=3x + y = 3(2,1)(2, 1)yesno6
only x=1x = 1(1,0)(1, 0)noyes1
both(1,2)(1, 2)yesyes9

The second row is lesson 4.5.1's answer. The third minimises 1+2y21 + 2y^2 on the line x=1x = 1. Only the last point is allowed.

Check its signs at (1,2)(1, 2), where ∇f=(2,8)\nabla f = (2, 8), ∇g1=(1,1)\nabla g_1 = (1, 1) and ∇g2=(1,0)\nabla g_2 = (1, 0). Solving (2,8)=λ1(1,1)+λ2(1,0)(2, 8) = \lambda_1(1, 1) + \lambda_2(1, 0) gives λ1=8\lambda_1 = 8 and λ2=−6\lambda_2 = -6. The first is ≥0\ge 0 for a ≥\ge constraint and the second ≤0\le 0 for a ≤\le constraint, so (1,2)(1, 2) is the minimum, with f=9f = 9.

Each multiplier is a rate again. Moving c1c_1 from 3 changes the best value at the rate 8; moving c2c_2 from 1, at the rate −6-6. Loosen x≤1x \le 1 to x≤2.5x \le 2.5 instead, and (2,1)(2, 1) is allowed: the second constraint goes slack, with λ2=0\lambda_2 = 0.

With kk inequality constraints there are 2k2^k cases. Solvers search them far more cleverly, but they test the same conditions.

Try it

The loop checks each guess against both constraints. Then np.linalg.solve finds λ1\lambda_1 and λ2\lambda_2 at (1,2)(1, 2) (lesson 3.3.1).

best_value is ff with both constraints binding: x=c2x = c_2 and y=c1−c2y = c_1 - c_2. Its central differences in c1c_1 and c2c_2 are the two multipliers.

⌘/Ctrl + Enter runs the cell

Checking with a solver

scipy.optimize.minimize, named in lesson 4.4.3, accepts constraints. Each is a dictionary: "type": "eq" means fun(v) = 0, and "type": "ineq" means fun(v) >= 0. The method "SLSQP" handles both.

So x+y≥3x + y \ge 3 is v[0] + v[1] - 3, and x≤1x \le 1 is 1 - v[0]. A ≤\le constraint must be turned round. Typed as v[0] + v[1] - 3, the constraint meant as x+y≤3x + y \le 3 silently becomes x+y≥3x + y \ge 3, and the solver returns (2,1)(2, 1) instead of (0,0)(0, 0).

Try it

The first call solves the two-constraint problem and returns (1,2)(1, 2) with f=9f = 9.

The loop then shows the trap: the wrong way round, then the right way round. ftol sets how precisely the solver stops; the answers are rounded to 4 decimals.

⌘/Ctrl + Enter runs the cell

Lesson 4.5.3 shows when a point that passes the KKT conditions is sure to be the minimum. Case study 4.5.5 uses them to forbid negative portfolio weights.

Common mistakes

  1. Do not assume a constraint binds. With x+y≤3x + y \le 3 the bowl's minimum is the origin, inside the region.
  2. Check that the point is allowed. The bowl's lowest point, (0,0)(0, 0), breaks x+y≥3x + y \ge 3.
  3. Check the sign of λ\lambda. A binding guess for x+y≤3x + y \le 3 gives λ=4\lambda = 4, the wrong sign: the guess is wrong.
  4. The sign depends on the direction. With ∇f=λ∇g\nabla f = \lambda\nabla g, a ≤\le constraint needs λ≤0\lambda \le 0 and a ≥\ge constraint λ≥0\lambda \ge 0. Books that write ∇f+μ∇g=0\nabla f + \mu\nabla g = \mathbf{0} have μ=−λ\mu = -\lambda.
  5. Complementary slackness allows both factors to be 0. With x+y≤0x + y \le 0 the minimum is on the edge and λ=0\lambda = 0.
  6. SciPy's "ineq" means ≥0\ge 0. Write x+y≤3x + y \le 3 as 3 - v[0] - v[1].

Inequality constraints and the KKT conditions

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • An inequality constraint g≤cg \le c allows a region. At a minimum it is slack (inside, ∇f=0\nabla f = \mathbf{0}, λ=0\lambda = 0) or binding (on the edge, ∇f=λ∇g\nabla f = \lambda\nabla g).
  • At a binding constraint ∇f\nabla f points into the allowed side. With ∇f=λ∇g\nabla f = \lambda\nabla g, a ≤\le constraint has λ≤0\lambda \le 0 and a ≥\ge constraint λ≥0\lambda \ge 0; λ=df∗dc\lambda = \frac{df^*}{dc} as before.
  • KKT for g≤cg \le c: ∇f=λ∇g\nabla f = \lambda\nabla g, g≤cg \le c, λ≤0\lambda \le 0, λ(g−c)=0\lambda(g - c) = 0 (complementary slackness).
  • With several constraints, ∇f=∑iλi∇gi\nabla f = \sum_i \lambda_i\nabla g_i, each λi\lambda_i with its own sign or 0. Solve by guessing which constraints bind, then check every condition.
  • In SciPy, "ineq" means fun(v) >= 0.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.