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 1 of 5

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

Lagrange multipliers

A minimum with a constraint

Lesson 4.4.1 found the lowest point of a function f(x,y)f(x, y) where its gradient is 0\mathbf{0}. Often the point may not move freely. It must satisfy an equation g(x,y)=cg(x, y) = c, called a constraint: gg is a function and cc a fixed number.

Take the bowl f(x,y)=x2+2y2f(x, y) = x^2 + 2y^2 and the constraint x+y=3x + y = 3, so g(x,y)=x+yg(x, y) = x + y and c=3c = 3. The bowl's lowest point, the origin, is not on the line. The question is which point of the line gives the smallest ff.

On a line you can substitute. Put y=3−xy = 3 - x, and ff becomes a function of one variable, h(x)=x2+2(3−x)2=3x2−12x+18h(x) = x^2 + 2(3 - x)^2 = 3x^2 - 12x + 18. Its slope 6x−126x - 12 is 0 at x=2x = 2 (lesson 2.4.1), so the best point is (2,1)(2, 1) and the smallest value is 6.

xx011.522.53
y=3−xy = 3 - x321.510.50
f=x2+2y2f = x^2 + 2y^21896.7566.759

Substitution needs the constraint solved for one variable. On a circle that brings in square roots, and with many variables it gets long. Lagrange's method handles both, and it starts from a picture.

The level curve touches the constraint

The level curves of ff are the ellipses x2+2y2=kx^2 + 2y^2 = k (lesson 4.1.1). Walk along the line from (0,3)(0, 3): the ellipses you cross get smaller, down to f=6f = 6 at (2,1)(2, 1), then grow again.

At (2,1)(2, 1) the line does not cross its level curve: it touches it. A crossing would mean smaller ellipses on one side, and so lower values further along the line.

On the line, the smallest level curve that still meets it, f=6f = 6, touches it at (2,1)(2, 1): there ∇f\nabla f and ∇g\nabla g are parallel. On the circle two level lines touch, at the largest and the smallest value.

Gradients say the same thing. Lesson 4.2.1 showed that ∇f\nabla f is at right angles to the level curve of ff. The line is a level curve of gg, so ∇g=(1,1)\nabla g = (1, 1) is at right angles to the line.

Where the two curves touch, both gradients are at right angles to the same line, so they are parallel. Elsewhere ∇f\nabla f has a part along the line, and a step along the line changes ff:

  • At (1,2)(1, 2), ∇f=(2,8)\nabla f = (2, 8). Its slope along the unit direction 12(1,−1)\frac{1}{\sqrt{2}}(1, -1) is −4.2426-4.2426, so stepping that way lowers ff.
  • At (2,1)(2, 1), ∇f=(4,4)=4∇g\nabla f = (4, 4) = 4\nabla g. Its slope along the line is 0.
  • At (3,0)(3, 0), ∇f=(6,0)\nabla f = (6, 0) and the slope is 4.24264.2426: ff falls the other way.

Rule: Lagrange's condition

∇f=λ ∇gg(x,y)=c\begin{gathered} \nabla f = \lambda\,\nabla g \\[4pt] g(x, y) = c \end{gathered}

At a best point on the constraint, the gradient of ff is a multiple of the gradient of gg. The number λ\lambda (lambda) is the Lagrange multiplier. The second line keeps the point on the constraint.

Try it

The loop prints the table above. The second loop prints ∇f\nabla f at three points of the line and its slope along the line, ∇f⋅u\nabla f \cdot \mathbf{u} with u=12(1,−1)\mathbf{u} = \frac{1}{\sqrt{2}}(1, -1) (lesson 4.2.1).

Only at (2,1)(2, 1) is that slope 0.

⌘/Ctrl + Enter runs the cell

The explorer keeps the point on the line x+y=cx + y = c. Set cc to 3 and slide the point until ∇f\nabla f and ∇g\nabla g are parallel: near (2,1)(2, 1), with λ\lambda close to 4. Then move cc and find the best point again: λ\lambda shrinks to 0 at c=0c = 0 and turns negative below it.

Explore

The point can move only along the line x + y = c. Slide it and watch the two gradients: at the lowest point on the line they point the same way, and the level curve just touches the line.

−202−2−1012
Point
(1, 1), f = 3
Gradient ∇f (dark arrow)
(2, 4), length 4.472
∇g (green arrow)
(1, 1), at right angles to the line
So
∇f has a part along the line, so moving along the line still changes f.

The Lagrangian

Lagrange's condition in components is three equations for three unknowns: fx=λgxf_x = \lambda g_x, fy=λgyf_y = \lambda g_y and g=cg = c. One function collects them, the Lagrangian.

Rule: The Lagrangian

L(x,y,λ)=f(x,y)−λ (g(x,y)−c)L(x, y, \lambda) = f(x, y) - \lambda\,\big(g(x, y) - c\big)

Set all three partial derivatives of LL to 0. The ones in xx and yy give ∇f=λ∇g\nabla f = \lambda\nabla g; the one in λ\lambda gives back g=cg = c. With nn variables and one constraint there are n+1n + 1 equations.

For the bowl, L=x2+2y2−λ(x+y−3)L = x^2 + 2y^2 - \lambda(x + y - 3), and the equations are

2x−λ=0,4y−λ=0x+y=3\begin{gathered} 2x - \lambda = 0, \qquad 4y - \lambda = 0 \\[4pt] x + y = 3 \end{gathered}

The first two give x=λ2x = \frac{\lambda}{2} and y=λ4y = \frac{\lambda}{4}. Then 3λ4=3\frac{3\lambda}{4} = 3, so λ=4\lambda = 4 and the point is (2,1)(2, 1), as substitution found.

Try it

SymPy builds LL, takes its three partial derivatives and solves them together. It writes λ\lambda as lambda.

Each equation is printed as an expression that must equal 0.

⌘/Ctrl + Enter runs the cell

A maximum and a minimum on a circle

Now find the largest and smallest values of f(x,y)=x+2yf(x, y) = x + 2y on the circle x2+y2=5x^2 + y^2 = 5. Substitution would need y=±5−x2y = \pm\sqrt{5 - x^2}. Lagrange's method does not.

Here ∇f=(1,2)\nabla f = (1, 2) and ∇g=(2x,2y)\nabla g = (2x, 2y). The equations are 1=2λx1 = 2\lambda x, 2=2λy2 = 2\lambda y and x2+y2=5x^2 + y^2 = 5. The first shows λ≠0\lambda \neq 0, so dividing the second by the first is safe and gives y=2xy = 2x.

Then 5x2=55x^2 = 5, so x=1x = 1 or x=−1x = -1. There are two candidates:

  • (1,2)(1, 2), with f=5f = 5 and λ=12\lambda = \frac{1}{2}: the maximum.
  • (−1,−2)(-1, -2), with f=−5f = -5 and λ=−12\lambda = -\frac{1}{2}: the minimum.

The condition picks out every point where a level line touches the circle, the top and the bottom alike. Comparing the values of ff tells which is which. In the figure, ∇f\nabla f and ∇g\nabla g point the same way at the maximum and opposite ways at the minimum.

Try it

SymPy solves the three equations and finds both candidates. Then a scan of 3,601 points round the circle, one every tenth of a degree, confirms the largest and smallest values.

⌘/Ctrl + Enter runs the cell

Three variables: a linear system

The method works the same way in more variables. Minimise x2+2y2+3z2x^2 + 2y^2 + 3z^2 subject to x+y+z=1x + y + z = 1. The gradient condition gives 2x=λ2x = \lambda, 4y=λ4y = \lambda and 6z=λ6z = \lambda, and the constraint is a fourth equation.

All four are linear in xx, yy, zz and λ\lambda, so they form a 4×44 \times 4 system (lesson 3.3.1):

K(xyzλ)=(0001)K=(200−1040−1006−11110)\begin{gathered} K\begin{pmatrix} x \\ y \\ z \\ \lambda \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ 0 \\ 1 \end{pmatrix} \\[6pt] K = \begin{pmatrix} 2 & 0 & 0 & -1 \\ 0 & 4 & 0 & -1 \\ 0 & 0 & 6 & -1 \\ 1 & 1 & 1 & 0 \end{pmatrix} \end{gathered}

Its solution is x=611x = \frac{6}{11}, y=311y = \frac{3}{11}, z=211z = \frac{2}{11} and λ=1211\lambda = \frac{12}{11}, and the smallest value is 611≈0.545455\frac{6}{11} \approx 0.545455. Notice that 2x2x, 4y4y and 6z6z all equal the same number, λ\lambda.

A quadratic function with linear constraints always gives a linear system like this. Case study 4.5.5 solves one for a portfolio.

Try it

np.linalg.solve solves the system (lesson 3.3.1). The second line checks that 2x2x, 4y4y and 6z6z are all equal to λ\lambda, and the last compares the smallest value with 611\frac{6}{11}.

⌘/Ctrl + Enter runs the cell

What λ\lambda measures

Write f∗(c)f^*(c) for the best value on the constraint g=cg = c. Then λ\lambda is the rate at which f∗f^* changes as cc changes. Its units are units of ff per unit of cc.

Rule: The multiplier as a rate

df∗dc=λ\frac{df^*}{dc} = \lambda

Move the constraint from cc to c+Δcc + \Delta c and the best value changes by about λ Δc\lambda\,\Delta c. Economists call λ\lambda a shadow price: what one more unit of cc is worth.

For the bowl on x+y=cx + y = c, the same steps as before give the best point (2c3,c3)\left(\frac{2c}{3}, \frac{c}{3}\right), so f∗(c)=2c23f^*(c) = \frac{2c^2}{3} and λ=4c3\lambda = \frac{4c}{3}.

ccbest pointf∗(c)f^*(c)λ\lambda
−1.5-1.5(−1,−0.5)(-1, -0.5)1.5−2-2
0(0,0)(0, 0)00
2(1.3333,0.6667)(1.3333, 0.6667)2.66672.6667
3(2,1)(2, 1)64

From c=3c = 3 to 3.13.1, f∗f^* rises by 0.4067, close to λ×0.1=0.4\lambda \times 0.1 = 0.4. The central difference f∗(3.1)−f∗(2.9)0.2\frac{f^*(3.1) - f^*(2.9)}{0.2} gives 4: for a parabola the central difference is exact (lesson 2.8.1).

The sign has a meaning too:

  • Positive: raising cc moves the line away from the origin, and the best value rises.
  • Negative: at c=−1.5c = -1.5, raising cc moves the line towards the origin, and the best value falls.
  • Zero: at c=0c = 0 the line passes through the bowl's own lowest point. The constraint costs nothing, and ∇f=0\nabla f = \mathbf{0} there.

On the circle x2+y2=cx^2 + y^2 = c the largest value of x+2yx + 2y is 5c\sqrt{5c}, by the same steps. At c=5c = 5 its slope is 12\frac{1}{2}, the multiplier at the maximum. At c=5.1c = 5.1 the largest value is 5.0498, a rise of 0.4975 per unit of cc.

Try it

best(c) returns the best point, f∗(c)f^*(c) and λ=4c3\lambda = \frac{4c}{3} for the bowl. The loop prints the table.

The next line prints the change from c=3c = 3 to 3.1 and the central difference. The last prints 5c\sqrt{5c} at c=5.1c = 5.1 and its rate of change from c=5c = 5.

⌘/Ctrl + Enter runs the cell

Where the method needs care

It finds candidates, not answers. On the circle only their values told the maximum from the minimum. On x+y=2x + y = 2, the product xyxy has one candidate, (1,1)(1, 1), and it is the largest value, not the smallest.

A best point may not exist. The bowl has no largest value on x+y=3x + y = 3: f(10,−7)=198f(10, -7) = 198 and f(100,−97)=28,818f(100, -97) = 28{,}818. Lagrange's method still returns its one candidate, (2,1)(2, 1), which is the smallest value.

The constraint needs ∇g≠0\nabla g \neq \mathbf{0}. Write the same line as (x+y−3)2=0(x + y - 3)^2 = 0. Now ∇g=(2(x+y−3), 2(x+y−3))\nabla g = (2(x + y - 3),\ 2(x + y - 3)), which is (0,0)(0, 0) at every point of the line. At (2,1)(2, 1) the condition would read (4,4)=λ(0,0)(4, 4) = \lambda(0, 0), which no λ\lambda satisfies, so the method misses the answer.

Try it

The first line evaluates the bowl far along the line: it grows without limit.

Then ∇g\nabla g for the badly written constraint, next to ∇f\nabla f at (2,1)(2, 1).

⌘/Ctrl + Enter runs the cell

Lesson 4.5.2 allows inequalities, such as x+y≥3x + y \ge 3, and lesson 4.5.3 says when a single candidate is sure to be the answer.

Common mistakes

  1. Keep the constraint as an equation. ∇f=λ∇g\nabla f = \lambda\nabla g alone has too many unknowns; g=cg = c is the last of the n+1n + 1 equations.
  2. Compare the candidates. The condition holds at a maximum as well as at a minimum: on the circle, (1,2)(1, 2) gives 5 and (−1,−2)(-1, -2) gives −5-5.
  3. Check that a best value exists. The bowl has a smallest value on a line but no largest one.
  4. Do not divide by something that may be 0. On the circle, 1=2λx1 = 2\lambda x showed λ≠0\lambda \neq 0 before dividing.
  5. Write the constraint so that ∇g≠0\nabla g \neq \mathbf{0}. (x+y−3)2=0(x + y - 3)^2 = 0 is the right line with a useless gradient.
  6. Mind the sign convention. Some books write L=f+λ(g−c)L = f + \lambda(g - c), which flips the sign of λ\lambda. Here ∇f=λ∇g\nabla f = \lambda\nabla g, so λ=df∗dc\lambda = \frac{df^*}{dc}.

Lagrange multipliers

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

  • To find the best value of ff on a constraint g=cg = c: at the best point ∇f=λ∇g\nabla f = \lambda\nabla g and g=cg = c. The level curve of ff touches the constraint there.
  • The Lagrangian L=f−λ(g−c)L = f - \lambda(g - c): set every partial derivative to 0. With nn variables and one constraint that is n+1n + 1 equations; for a quadratic ff and a linear constraint they form a linear system.
  • The solutions are candidates. Compare their values, and check that a best value exists.
  • λ=df∗dc\lambda = \frac{df^*}{dc}: the rate at which the best value changes with cc, in units of ff per unit of cc. λ=0\lambda = 0 when the constraint costs nothing.
  • The method needs ∇g≠0\nabla g \neq \mathbf{0} at the best point.

Free further reading

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