Practice / Calculus
Lagrange multipliers
Before you start
When a point must stay on a curve or a surface, setting the gradient to zero is replaced by : the gradient of the objective lines up with the gradient of the constraint. These ten problems derive that condition, use it on a line, a circle, a box and a plane, then draw from it three results machine learning leans on — eigenvalues as the extremes of a quadratic form, the softmax as a maximum-entropy distribution and the minimum-norm solution of — and end with what measures and with inequality constraints. The five mistakes each lose a candidate or misread the multiplier.
- and are functions of (in the plane, of ) with continuous partial derivatives; is the column of partial derivatives, as on the partial-derivatives page. The constraint is . At a point of where , restricted to the constraint, has a local maximum or minimum and , there is a number , the Lagrange multiplier, with . The points satisfying this together with are the candidates.
- The same equations come from setting every partial derivative of the Lagrangian to ; is the constraint. Writing instead negates ; this page never does.
- With several constraints there is one multiplier for each: .
- Choosing among candidates: on a closed and bounded constraint set, such as a circle or a sphere, attains a maximum and a minimum, both are candidates, and comparing the values of picks them out. On an unbounded set, such as a line or a plane, a candidate can be a maximum, a minimum or neither, and needs a separate argument.
- Inequality constraints (Problem 10): to minimise subject to , the Karush–Kuhn–Tucker (KKT) conditions are , , and . The last says either (the constraint is inactive, and the point is an ordinary stationary point) or (it is active, and the point is on the boundary).
- is the natural log; the entropy of a distribution on outcomes is .
Builds on: Partial derivatives, gradients and Hessians
Problems
- ·
Explain why, at a point of the curve where restricted to the curve has a local maximum or minimum and , the gradients satisfy for some number .
- ·
Find the maximum of on the line , and the multiplier. Is there a minimum?
- ··
Find the maximum and minimum of on the unit circle . List every candidate.
- ··
An open-top box with a rectangular base by and height must hold volume . Find the dimensions that minimise the area of base and sides, .
- ··
Find the point of the plane closest to , and its distance from .
- ··
For a symmetric matrix , show that the largest value of over unit vectors is the largest eigenvalue . Find the maximum and minimum for .
- ···
Find the distribution on outcomes with the largest entropy when the only constraint is . Then give the outcomes values and also fix the mean, : show the maximiser has the form , and find and for and .
- ···
is with and independent rows, so has infinitely many solutions. Find the one of smallest norm by minimising subject to , and compute it for and .
- ···
Replace the constraint of Problem 2 by with . Find the maximum and the multiplier , and show . Then show the same for any and whose optimum moves differentiably with .
- ···
Minimise subject to (a) , and (b) . In each case say whether the constraint is active.
Answers
- for some number : both gradients are orthogonal to the tangent , and in the plane those vectors form a single line
- maximum at , with ; there is no minimum
- maximum at ; minimum at ; the candidate , with , is neither
- , ; least area
- closest point ; distance
- , attained at a unit eigenvector; here the maximum is at and the minimum at
- Normalisation only: , . With the mean: , and for , : ,
- ; for this and ,
- and , so ; in general
- (a) active: , , ; (b) inactive: , ,
Worked solutions
Problem 1
Explain why, at a point of the curve where restricted to the curve has a local maximum or minimum and , the gradients satisfy for some number .
- Near the curve is traced by a path with and a nonzero velocity , which is tangent to the curve. guarantees this: the implicit function theorem then solves for one coordinate in terms of the other near , so the curve is a smooth graph there.
- for every small , so .The path stays on the curve, so is constant along it; differentiate with the chain rule, , at .
- has a local maximum or minimum at , so .A differentiable function of one variable has derivative at an interior local extremum; the same chain rule gives the derivative.
- In the plane, the vectors orthogonal to the nonzero vector form a line through the origin, and spans it.A line through the origin is spanned by any nonzero vector on it, and step 2 puts on it.
- for some number : both gradients are orthogonal to the tangent , and in the plane those vectors form a single lineStep 3 puts on the same line as , so it is a multiple of it. Geometrically, the level curve of through touches the constraint curve there: if had a component along the tangent, moving along the curve in that direction would increase . In the same argument runs over every tangent direction of the surface .
Problem 2
Find the maximum of on the line , and the multiplier. Is there a minimum?
- and for . and ; is linear, so its partial derivatives are its coefficients.
- and ., one equation per component.
- , so and , with .Substitute step 2 into the constraint : the third equation, which fixes .
- Along the line , so .Parametrise the constraint by , then complete the square: .
- maximum at , with ; there is no minimumStep 4 is a downward parabola in : it peaks at with value and goes to as grows. The line is unbounded, so the single candidate needed this separate argument. At , .
Problem 3
Find the maximum and minimum of on the unit circle . List every candidate.
- and for .Partial derivatives term by term.
- , and . by components, and the constraint.
- , so or .Move everything to one side and factor rather than divide by , which may be . A product is when either factor is, so both cases must be followed.
- Case : , so or , with or .The constraint, then from the second equation. has two roots and both are candidates.
- Case : gives , and then , so .The second equation, then the constraint.
- , and .Evaluate at all four candidates.
- maximum at ; minimum at ; the candidate , with , is neitherThe circle is closed and bounded, so the maximum and minimum exist and are among the four candidates; comparing values picks them. Near the circle is , so : that candidate is only a local minimum along the circle.
Problem 4
An open-top box with a rectangular base by and height must hold volume . Find the dimensions that minimise the area of base and sides, .
- and for .Partial derivatives, holding the other two variables fixed.
- , and . by components.
- , and .Multiply the three equations by , and : every right side becomes , and nothing has been divided.
- , so .Subtract the second equation from the first. makes every dimension nonzero, so dividing by is safe.
- , so .Subtract the third equation from the first, then divide by .
- , so and .Substitute steps 4 and 5 into the constraint.
- .The first equation of step 2; Problem 9 uses this value.
- , ; least area . It is the minimum: with the area is , which grows without bound as or goes to or to , so a minimum exists inside and must be a candidate, and this is the only one. The base is square and the height is half its side.
Problem 5
Find the point of the plane closest to , and its distance from .
Write the plane as with .
- Minimise subject to .Squaring is increasing on nonnegative numbers, so the square has the same minimiser as the distance and no square root to differentiate.
- and . differentiates term by term; is linear.
- , so .Stationarity: the closest point is reached from by moving along the normal of the plane.
- , so .Substitute into the constraint; and .
- , and ., so ; check .
- closest point ; distance It is the minimum: any other point of the plane is with , and gives . In general step 4 gives distance for the plane , here .
Problem 6
For a symmetric matrix , show that the largest value of over unit vectors is the largest eigenvalue . Find the maximum and minimum for .
- and with ; and .The gradient of is , which is for symmetric ; is the case .
- , that is .Stationarity: the candidates are the unit eigenvectors of , and the multiplier is the eigenvalue.
- At a candidate, .Substitute , then the constraint .
- The unit sphere is closed and bounded, so the maximum exists and is a candidate; the largest candidate value is .By step 3 the candidate values are exactly the eigenvalues; likewise the minimum is the smallest eigenvalue.
- For the given : , so or , with eigenvectors and .; then and .
- , attained at a unit eigenvector; here the maximum is at and the minimum at Check: gives . This is why the first principal component, the direction of largest variance , is the top eigenvector of the covariance matrix.
Problem 7
Find the distribution on outcomes with the largest entropy when the only constraint is . Then give the outcomes values and also fix the mean, : show the maximiser has the form , and find and for and .
Every is taken positive. The derivative of tends to as , so moving a little probability onto an empty outcome always raises , and the maximum is not on the boundary.
- .Product rule on ; no other term of contains .
- Normalisation only: for every . with , whose gradient is the all-ones vector.
- , the same for every , so and .Solve step 2 for ; then fixes the common value.
- This is the maximum. has second derivative , so is concave, and on a constraint set cut out by linear equations a stationary point of a concave function is its maximum. The entropy page proved the same bound with the KL divergence.
- With the mean: .Two constraints, two multipliers: , and the second gradient is the vector of values .
- , and gives .Exponentiate step 5; the factor is the same for every , so normalisation determines it.
- For write : , with mean ., and the mean is ; the remaining constraint fixes .
- , so , and .Cross-multiply and collect terms; rules out .
- and .; . Check: mean .
- Normalisation only: , . With the mean: , and for , : , The second form is the softmax of , the Gibbs distribution; gives back the uniform distribution, whose mean here is . A mean below needs , which tilts the probability toward small . The concavity argument of step 4 again makes it the maximum.
Problem 8
is with and independent rows, so has infinitely many solutions. Find the one of smallest norm by minimising subject to , and compute it for and .
- The constraints are for , where is row of ; give them multipliers , collected in .One multiplier per constraint; avoids a clash with the eigenvalue of Problem 6.
- .Stationarity: and . The columns of are the , so the sum is .
- , so .Substitute into . is and invertible because the rows of are independent.
- .Substitute back into step 2.
- Any other solution is with , and ., so the cross term vanishes; every other solution is longer, and is the minimum.
- For the given : , , and .Entry of is row dotted with row ; the determinant is , and the inverse swaps the diagonal and negates the off-diagonal.
- . is times the first row of plus times the second. Check: .
- ; for this and , lies in the row space of , orthogonal to the null space: here . It is the pseudoinverse solution of the least-squares page, the wide-matrix counterpart of .
Problem 9
Replace the constraint of Problem 2 by with . Find the maximum and the multiplier , and show . Then show the same for any and whose optimum moves differentiably with .
- , and , so and .Problem 2's equations with replaced by .
- , so .Differentiate in ; at both are , as in Problem 2.
- In general , so .Chain rule through the path the optimum traces as changes.
- .Stationarity at the optimum, .
- for every , so .Differentiate the constraint, which holds identically in , with the same chain rule.
- and , so ; in general is the price of the constraint: raising from to raises the maximum by about (exactly ). For the box of Problem 4, : the least area for volume is , whose derivative at is , so one more unit of volume costs about one more unit of area.
Problem 10
Minimise subject to (a) , and (b) . In each case say whether the constraint is active.
- with or ; and .The KKT conditions of Before you start need the constraint in the form .
- and , so and .Stationarity, , by components.
- Inactive case, : , where .Complementary slackness allows ; then the point is the unconstrained minimum of .
- (a) , so is infeasible and the constraint must be active: gives , the point and .With the inactive case ruled out, forces ; and , so all four conditions hold.
- (b) , so is feasible, satisfies all four conditions, and . everywhere, so cannot be beaten.
- (a) active: , , ; (b) inactive: , , is convex and the constraint linear, so a KKT point is the minimum. is a sensitivity, as in Problem 9: in (a) the minimum is for , with at , so loosening the constraint lowers the minimum at rate ; in (b) loosening it changes nothing, and .
Where this goes wrong
1. Taking only the positive root of y² = 1
is the habit, and gets solved as if it were .
- In Problem 3 the case leaves the constraint Right so far: this is the branch from .
- “Take the square root.”The shortcut that causes the mistake: the square-root sign names one root, and the equation has two.
- , so the candidates are with and with , and the minimum is also has , and gives , the true minimum. Comparing values finds the maximum and minimum only among the candidates actually listed, so a dropped root can silently replace the answer with a merely local extremum, as is here.
2. Dividing by x and losing the x = 0 candidates
Cancelling a common factor from both sides is the first move in solving most equations.
- Problem 3's conditions are , and Right so far: stationarity and the constraint.
- “Cancel the common factor .”The habit that causes the mistake: it is valid only when the factor cannot be .
- , so and the only candidates are , both with Dividing by assumes ; the equation also holds when , and that branch holds , the minimum , and . The warning sign was there: the circle is closed and bounded, so a minimum must exist, and this list has nothing below . In Problem 4 the same division was safe because rules out zeros.
3. Choosing λ instead of solving the constraint
After stationarity, looks like a free parameter, and it is tempting to set it for convenience.
- In Problem 5, , so Right so far: the stationarity condition of Problem 5.
- “Pick the that makes the objective smallest.”The shortcut that causes the mistake: treating as a design choice rather than an unknown.
- gives , at distance is not on the plane: . is fixed by the constraint equation , which stationarity alone does not contain; solving it gives , the point and distance . Any answer that does not satisfy has solved an unconstrained problem.
4. Reading df*/dc as λ under the other sign convention
Many texts write the Lagrangian as , and the sensitivity rule gets carried over without its sign.
- For Problem 2, ; its derivatives give , , and the maximiser Right so far: this convention finds the same point, with negated.
- “ is the rate at which the optimum changes with .”The rule that causes the mistake: Problem 9's result, used without the convention it was proved in.
- , so raising from to lowers the maximum of by about With , stationarity says , so : the maximum rises. Check directly: is on and gives . The sign of is a convention; holds for .
5. Treating an inactive inequality as active
With equality constraints the answer always lies on the constraint, and that expectation carries over to inequalities.
- In Problem 10(b), stationarity gives and Right so far: the KKT stationarity condition.
- “The answer of a constrained problem lies on the constraint.”The analogy that causes the mistake: true for , not for .
- Set : then , the point is , and the minimum is breaks : it says moving into the region lowers , so the boundary is not where the minimum is. The unconstrained minimum has , so it is feasible, with and . Always try the inactive case first, and accept the active one only with .
Print this set: lagrange-multipliers.pdf (problems, answers, and worked solutions on separate pages).