Week 9 · Optimising with rules: Lagrange and KKT
Read the words of the video
- Imagine you're a hiker wanting the lowest point. But real life has rules! A shop owner seeks profit with limited labour. A farmer wants the biggest field with only so much fence. Or you want the shortest walk to a river, but must stay on the road. These rules are called constraints.
- With constraints, the best spot isn't just where the ground is flat. It's where you cannot improve your goal without breaking a rule. The 'downhill arrow' of your objective and the 'wall' of the rule are in a perfect tug-of-war balance. This balance helps us find optimal solutions.
- Let's find the point closest to the origin on a line. We minimise x squared plus y squared, subject to the line equation x plus two y equals five. We find the best point is where x is one and y is two. The minimum value, or squared distance, is five.
- This topic is huge in exams! Linear programming questions about profit or diet, finding closest points, and KKT true/false problems appear often. Remember, constrained optimization is all about finding that perfect balance point, where you reach your goal without breaking any important rules. Keep practicing!
The one-minute story
In week 7 you were a hiker free to roam anywhere. Real life has rules. A shop owner wants the most profit, but only has 80 hours of labour. A farmer wants the biggest field, but only owns 120 metres of fence. You want the shortest walk to a river, but you must stay on the road.
Rules like these are constraints. The best point is no longer where the ground is flat, but where you can't improve without breaking a rule. There the downhill arrow and the wall of the rule are in a tug-of-war that exactly balances. This week gives you three tools:
- Lagrange multipliers: for rules that are exact equations ("stay on this road").
- KKT conditions: for rules that are limits ("stay at most this far").
- Linear programming: the everyday case where everything is a straight line (shops, farms, diets).
And at the end, a surprise: logistic regression, the classifier behind spam filters, is just an optimisation problem you can now read.
By the end of this week you can:
- turn a word problem into "maximise or minimise ... subject to ...";
- solve equality-constrained problems with Lagrange multipliers;
- state the four KKT conditions and use complementary slackness;
- solve a small linear programme by checking corner points;
- write "closest point to the origin" problems as optimisation problems;
- explain logistic regression as minimising a loss.
Words you need
- Objective - the quantity to minimise or maximise.
- Constraint - a rule the answer must obey: an equation or an inequality .
- Feasible point - a point that obeys all the rules. The set of all of them is the feasible region.
- Lagrange multiplier (or ) - a number attached to a constraint that tells how strongly the rule is pulling.
- Active constraint - an inequality that is exactly tight at the solution ().
- Primal problem - the original problem you want to solve.
- Linear programme (LP) - linear objective, linear constraints.
Idea 1: Word problem optimisation problem
Almost every question in this chapter starts with a story. Translate it in three moves:
- What can I change? Name those numbers (the decision variables).
- What do I want? Write the objective in terms of them.
- What are the rules? Write each as an equation or inequality, and don't forget "".
Question. A workshop makes products and . Each takes 1 labour hour and 3 machine hours; each takes 2 labour hours and 2 machine hours. There are 80 labour hours and 120 machine hours. Profit is Rs 20 per and Rs 30 per . Find the best production plan.
Solution.
- Variables: = number of , = number of .
- Objective: maximise .
- Constraints: labour ; machine ; and .
Key fact: for a linear objective the best point of the feasible region is at a corner. List the corners:
- : profit .
- (machine limit with ): profit .
- (labour limit with ): profit .
- Where the two limits cross: from and , subtract: , so , . Profit .
- The best plan is 20 of and 30 of , profit Rs 1300. Check both limits are tight there: ✓ and ✓.
Comprehension questions in the papers usually ask in three parts: (a) the objective function (MCQ), (b) the constraints (MSQ), (c) the optimal value (numeric). Part (a) and (b) are pure translation; practise them a lot.
Idea 2: Equality constraints and Lagrange multipliers
Problem: minimise subject to .
You are walking along a curved road (the constraint) and want the point where the altitude is lowest. At the lowest point on the road, if the ground still sloped downhill along the road you could keep walking and go lower. So at the best spot the downhill direction must point straight off the road, perpendicular to the road. And a road's perpendicular direction is exactly the gradient of its equation . So: and must point the same (or opposite) way.
The number is the Lagrange multiplier. Equivalent form: build the Lagrangian and set all its partial derivatives to zero.
The exam recipe
- Write : one equation per variable.
- Add the constraint as the last equation.
- Solve the system (it nearly always simplifies, often to a symmetry like or ).
- Put the point back into .
Question. Minimise subject to .
Solution.
- , and has . Recompute every time; it is where marks are lost.
- and . So .
- Constraint: .
- (and ).
Geometric check: the squared distance from the origin to the line is . ✓
Do not carry a relation from one problem into the next. In you get because ; in you get because .
"Closest point" problems as optimisation problems
A very common question: "Choose the correct primal optimisation problem for finding the point of closest to the origin." The answer always has the same shape:
For a plane the constraint is ; for a disc of radius it is ; for a half-plane it is . Closest point to some other point : minimise .
Sometimes geometry beats algebra: the point of a circle of radius 2 (centre at the origin) closest to is on the ray from the origin to , at distance . No calculus at all.
Idea 3: Inequality constraints and the KKT conditions
Now the rule is a limit: minimise subject to . Two situations:
- The unconstrained minimum is inside the allowed region: the rule does not bite. Solve as in week 7. The constraint is inactive.
- The unconstrained minimum is outside: the best allowed point sits on the boundary, where . The constraint is active.
The KKT conditions (Karush-Kuhn-Tucker) cover both cases with one list. For s.t. with multiplier :
- Stationarity:
- Primal feasibility: (the rule is obeyed)
- Dual feasibility: (the rule can only push back, never pull)
- Complementary slackness: (either the rule is tight, or its multiplier is zero)
Complementary slackness is the part to love. It says: a rule that is not touching you has no influence (); a rule that does influence you () must be exactly tight (). This is exactly why, in the support vector machine (MLT week 10), only the points on the margin carry any weight: those are the active constraints.
Question. Minimise subject to . Then repeat with .
Solution, ().
- Try inactive (): stationarity gives , . But : violates primal feasibility. Reject.
- Try active (): . Stationarity: . Dual feasibility: ✓. Complementary slackness: ✓.
- Answer: , , . The wall at is pushing back with strength 2.
Solution, (). Try inactive: ; feasible (), ✓. The wall at 3 is irrelevant; the answer is .
Two-case reasoning ("active or inactive?") is the standard way to solve a KKT problem by hand.
Question. At the optimum of a minimisation problem with inequality constraint and , you find and . Which of the following must be true? (a) (b) (c) (d) multipliers must be non-negative.
Solution. , so by complementary slackness constraint 1 is active: (a) true. says nothing about whether is tight: (b) not forced, it might be strictly negative. (c) true by primal feasibility. (d) true by dual feasibility for constraints. Answer: (a), (c), (d).
Idea 4: Logistic regression as an optimisation problem
A spam filter wants to output a probability that an email is spam. For a feature vector and weights , take the score and squash it into with the sigmoid
So ; large positive ; large negative .
How do we choose ? Pick the weights that make the observed labels most probable. For labels the likelihood of the data is . Taking and averaging gives the cross-entropy (log-loss):
Training = minimise : an unconstrained optimisation problem, and convex (week 8), so any minimum is the global one. Its gradient has a lovely form:
"prediction minus truth, times the features". There is no closed-form solution, so we use gradient descent (week 7). MLT week 9 and MLP week 5 build on exactly this.
Question. A trained model has and bias . For , what is the probability of class 1, and which class does it predict?
Solution. . . Since it predicts class 1. The decision boundary is where , a straight line.
Your turn
Minimise subject to .
Need a hint?
.
Show the full solution
, , so and , hence . Constraint: , , . Minimum value . (Check: distance from the origin to the line ✓.)
Maximise subject to .
Need a hint?
and .
Show the full solution
and , so . Constraint: . Maximum value . (A rectangle of fixed perimeter has the biggest area when it is a square.)
Minimise subject to (write it as ). Find and .
Need a hint?
Try "inactive" first and see whether you obey the rule.
Show the full solution
Inactive: , but is false. So the constraint is active: . Stationarity ✓. So , , .
A diet needs at least 60 units of vitamin A and 80 of vitamin B. Food 1 costs Rs 3 and gives (3 A, 2 B) per pack; Food 2 costs Rs 4 and gives (1 A, 4 B). Write the LP. Then find the minimum cost.
Need a hint?
Minimise cost; the constraints are "at least".
Show the full solution
Minimise s.t. , , . Corners: : cost 240. : constraint 1 gives ✓ and ✓: cost 120. Intersection of the two limits: , : from the first , then , , : cost . Minimum cost Rs 96 at .
Write the problem of finding the point of the plane closest to the origin.
Need a hint?
Closest to the origin means minimise the squared length.
Show the full solution
subject to . (Lagrange gives with : the point and minimum value .)
Exam radar
This is the single biggest block of the End Term, and it repeats the same shapes:
- Linear-programming comprehension sets appear in nearly every paper: TV sets at Rs 510/Rs 675 profit (7 times), products with labour and machine hours (8 times), hens and eggs, trucks vs vans, brownies vs cheesecake, and the consumer utility problem (7 times). Each has an objective (MCQ), constraints (MSQ) and an optimal value (SA).
- "Choose the correct primal optimisation problem" (closest point to the origin) about 14 times; KKT true/false about 12; Lagrange closest-point problems about 4; complementary slackness once in 2026.
- The summaries mention KKT conditions in 14 papers, Lagrange multipliers in 10, stationarity/dual feasibility/primal feasibility in 10 each, complementary slackness in 11, constrained optimisation in 8.
- Recompute for each new constraint; do not reuse a relation.
- KKT dual feasibility is for a constraint. If the problem is written with , rewrite it as first.
- Zero multiplier does not mean the constraint is slack, only that it may be.
- In an LP, don't forget when listing the corners.
- Answer what is asked: sometimes the value of the objective, sometimes a variable, sometimes a multiplier.
Quick quiz
At a constrained minimum with an equality constraint, the gradient of f and the gradient of g are...
- perpendicular to each other
- parallel (one is a multiple of the other)
- both zero
- equal in length
Complementary slackness says...
- mu is always positive
- mu times g(x) equals zero
- g(x) is always zero
- f(x) equals zero
The maximum of a linear function over a polygon is always attained...
- in the exact middle
- at a corner
- on the longest edge only
- nowhere
Select all TRUE statements about logistic regression. (select all that apply)
- The model outputs a probability between 0 and 1.
- Training minimises a convex loss.
- It has a simple closed-form solution like least squares.
- Its predicted probability can be negative.
Recap
- Translate stories into: variables, objective, constraints (and ).
- Lagrange: plus . Recompute each time.
- KKT for : stationarity, primal feasibility, dual feasibility (), complementary slackness (). Solve by "active or inactive?".
- LP: optimum at a corner. Check each corner.
- Closest point to the origin: s.t. the set. Logistic regression: minimise convex log-loss with gradient descent.
- Lagrange: , ; Lagrangian
- KKT (): ; ; ;
- Distance from origin to : ; closest point
- LP: optimum at a vertex of the feasible region
- Sigmoid: ; log-loss ; gradient
- MLT week 10 - Support vector machines: The SVM dual comes straight out of Lagrange multipliers and the KKT conditions.
- MLT week 9 - Perceptron and logistic regression: Logistic regression written as an optimisation problem.
- MLP week 5 - Logistic regression in sklearn: Logistic regression in sklearn.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 23 real questions on this week All End Term papers