ML Study Hub

Home / MLF / Week 9

Week 9 · Optimising with rules: Lagrange and KKT

Official syllabus line: Constrained Optimisation and Lagrange Multipliers. Logistic regression as an optimization problem
About 100 minEnd Term material (see exam radar)BSCS2004 · Harish Guruprasad Ramaswamy, Arun Rajkumar, Prashanth LA
Before you start: Week 2 (gradient), week 7 (critical points), week 8 (convexity). Comfort with solving two or three linear equations.
Watch first: the animated story of this weekAbout 1-2 minutes of real 3D animation with a friendly voice. Pause any time.
Read the words of the video
  1. 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.
  2. 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.
  3. 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.
  4. 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

Think of it like this

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:

  1. Lagrange multipliers: for rules that are exact equations ("stay on this road").
  2. KKT conditions: for rules that are limits ("stay at most this far").
  3. 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

Words you need
  • Objective - the quantity to minimise or maximise.
  • Constraint - a rule the answer must obey: an equation g(x)=0g(\mathbf x)=0 or an inequality g(x)≤0g(\mathbf x)\le0.
  • Feasible point - a point that obeys all the rules. The set of all of them is the feasible region.
  • Lagrange multiplier λ\lambda (or μ\mu) - 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 (g=0g=0).
  • Primal problem - the original problem you want to solve.
  • Linear programme (LP) - linear objective, linear constraints.

Idea 1: Word problem →\to optimisation problem

Almost every question in this chapter starts with a story. Translate it in three moves:

  1. What can I change? Name those numbers x,y,…x,y,\dots (the decision variables).
  2. What do I want? Write the objective in terms of them.
  3. What are the rules? Write each as an equation or inequality, and don't forget "x,y≥0x,y\ge0".
Solved example: 1. A shop's LP, set up and solved

Question. A workshop makes products P1P_1 and P2P_2. Each P1P_1 takes 1 labour hour and 3 machine hours; each P2P_2 takes 2 labour hours and 2 machine hours. There are 80 labour hours and 120 machine hours. Profit is Rs 20 per P1P_1 and Rs 30 per P2P_2. Find the best production plan.

Solution.

  1. Variables: xx = number of P1P_1, yy = number of P2P_2.
  2. Objective: maximise 20x+30y20x+30y.
  3. Constraints: labour x+2y≤80x+2y\le80; machine 3x+2y≤1203x+2y\le120; and x,y≥0x,y\ge0.
  4. Key fact: for a linear objective the best point of the feasible region is at a corner. List the corners:

    • (0,0)(0,0): profit 00.
    • (40,0)(40,0) (machine limit with y=0y=0): profit 800800.
    • (0,40)(0,40) (labour limit with x=0x=0): profit 12001200.
    • Where the two limits cross: from x+2y=80x+2y=80 and 3x+2y=1203x+2y=120, subtract: 2x=402x=40, so x=20x=20, y=30y=30. Profit 400+900=1300400+900=\mathbf{1300}.
  5. The best plan is 20 of P1P_1 and 30 of P2P_2, profit Rs 1300. Check both limits are tight there: 20+60=8020+60=80 ✓ and 60+60=12060+60=120 ✓.

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 f(x)f(\mathbf x) subject to g(x)=cg(\mathbf x)=c.

Think of it like this

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 gg. So: ∇f\nabla f and ∇g\nabla g must point the same (or opposite) way.

∇f(x)=λ ∇g(x),g(x)=c\nabla f(\mathbf x)=\lambda\,\nabla g(\mathbf x),\qquad g(\mathbf x)=c

The number λ\lambda is the Lagrange multiplier. Equivalent form: build the Lagrangian L(x,λ)=f(x)−λ(g(x)−c)L(\mathbf x,\lambda)=f(\mathbf x)-\lambda\big(g(\mathbf x)-c\big) and set all its partial derivatives to zero.

The exam recipe

  1. Write ∇f=λ∇g\nabla f=\lambda\nabla g: one equation per variable.
  2. Add the constraint g=cg=c as the last equation.
  3. Solve the system (it nearly always simplifies, often to a symmetry like x=yx=y or y=2xy=2x).
  4. Put the point back into ff.
Solved example: 2. Closest point to the origin on a line

Question. Minimise f(x,y)=x2+y2f(x,y)=x^2+y^2 subject to x+2y=5x+2y=5.

Solution.

  1. ∇f=(2x, 2y)\nabla f=(2x,\,2y), and g=x+2yg=x+2y has ∇g=(1, 2)\nabla g=(1,\,2). Recompute ∇g\nabla g every time; it is where marks are lost.
  2. 2x=λ⋅12x=\lambda\cdot1 and 2y=λ⋅22y=\lambda\cdot2. So y=2xy=2x.
  3. Constraint: x+2(2x)=5⇒x=1,  y=2x+2(2x)=5\Rightarrow x=1,\;y=2.
  4. f(1,2)=1+4=5f(1,2)=1+4=\mathbf5 (and λ=2\lambda=2).

Geometric check: the squared distance from the origin to the line x+2y=5x+2y=5 is 2512+22=5\frac{25}{1^2+2^2}=5. ✓

Watch out - real exam trap

Do not carry a relation from one problem into the next. In x+y=4x+y=4 you get x=yx=y because ∇g=(1,1)\nabla g=(1,1); in x+2y=5x+2y=5 you get y=2xy=2x because ∇g=(1,2)\nabla g=(1,2).

"Closest point" problems as optimisation problems

A very common question: "Choose the correct primal optimisation problem for finding the point of SS closest to the origin." The answer always has the same shape:

min⁡x  ∥x∥2subject tox∈S\min_{\mathbf x}\;\|\mathbf x\|^2\quad\text{subject to}\quad\mathbf x\in S

For a plane aTx=b\mathbf a^T\mathbf x=b the constraint is aTx=b\mathbf a^T\mathbf x=b; for a disc of radius rr it is ∥x∥2≤r2\|\mathbf x\|^2\le r^2; for a half-plane it is aTx≥b\mathbf a^T\mathbf x\ge b. Closest point to some other point p\mathbf p: minimise ∥x−p∥2\|\mathbf x-\mathbf p\|^2.

Sometimes geometry beats algebra: the point of a circle of radius 2 (centre at the origin) closest to (6,8)(6,8) is on the ray from the origin to (6,8)(6,8), at distance ∥(6,8)∥−2=10−2=8\|(6,8)\|-2=10-2=\mathbf8. No calculus at all.

Idea 3: Inequality constraints and the KKT conditions

Now the rule is a limit: minimise f(x)f(\mathbf x) subject to g(x)≤0g(\mathbf x)\le0. 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 g=0g=0. The constraint is active.

The KKT conditions (Karush-Kuhn-Tucker) cover both cases with one list. For min⁡f(x)\min f(\mathbf x) s.t. g(x)≤0g(\mathbf x)\le0 with multiplier μ\mu:

The idea
  1. Stationarity: ∇f(x)+μ ∇g(x)=0\nabla f(\mathbf x)+\mu\,\nabla g(\mathbf x)=\mathbf0
  2. Primal feasibility: g(x)≤0g(\mathbf x)\le0 (the rule is obeyed)
  3. Dual feasibility: μ≥0\mu\ge0 (the rule can only push back, never pull)
  4. Complementary slackness: μ g(x)=0\mu\,g(\mathbf x)=0 (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 (μ=0\mu=0); a rule that does influence you (μ>0\mu>0) must be exactly tight (g=0g=0). 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.

Solved example: 3. KKT in action, with two cases

Question. Minimise f(x)=(x−2)2f(x)=(x-2)^2 subject to x≤1x\le1. Then repeat with x≤3x\le3.

Solution, x≤1x\le1 (g=x−1≤0g=x-1\le0).

  • Try inactive (μ=0\mu=0): stationarity gives 2(x−2)=02(x-2)=0, x=2x=2. But 2≰12\not\le1: violates primal feasibility. Reject.
  • Try active (g=0g=0): x=1x=1. Stationarity: 2(1−2)+μ⋅1=0⇒μ=22(1-2)+\mu\cdot1=0\Rightarrow\mu=2. Dual feasibility: μ=2≥0\mu=2\ge0 ✓. Complementary slackness: μg=2⋅0=0\mu g=2\cdot0=0 ✓.
  • Answer: x∗=1x^*=1, μ=2\mu=2, f=1f=1. The wall at x=1x=1 is pushing back with strength 2.

Solution, x≤3x\le3 (g=x−3≤0g=x-3\le0). Try inactive: x=2x=2; feasible (2≤32\le3), μ=0\mu=0 ✓. The wall at 3 is irrelevant; the answer is x∗=2x^*=2.

Two-case reasoning ("active or inactive?") is the standard way to solve a KKT problem by hand.

Solved example: 4. Which KKT statements are true?

Question. At the optimum of a minimisation problem with inequality constraint g1≤0g_1\le0 and g2≤0g_2\le0, you find μ1=3\mu_1=3 and μ2=0\mu_2=0. Which of the following must be true? (a) g1(x∗)=0g_1(\mathbf x^*)=0 (b) g2(x∗)=0g_2(\mathbf x^*)=0 (c) g2(x∗)≤0g_2(\mathbf x^*)\le0 (d) multipliers must be non-negative.

Solution. μ1=3>0\mu_1=3>0, so by complementary slackness constraint 1 is active: (a) true. μ2=0\mu_2=0 says nothing about whether g2g_2 is tight: (b) not forced, it might be strictly negative. (c) true by primal feasibility. (d) true by dual feasibility for ≤\le constraints. Answer: (a), (c), (d).

Idea 4: Logistic regression as an optimisation problem

A spam filter wants to output a probability pp that an email is spam. For a feature vector x\mathbf x and weights w\mathbf w, take the score z=wTxz=\mathbf w^T\mathbf x and squash it into (0,1)(0,1) with the sigmoid

σ(z)=11+e−z,p=σ(wTx)\sigma(z)=\frac1{1+e^{-z}},\qquad p=\sigma(\mathbf w^T\mathbf x)

So z=0⇒p=0.5z=0\Rightarrow p=0.5; large positive z⇒p≈1z\Rightarrow p\approx1; large negative z⇒p≈0z\Rightarrow p\approx0.

Try it yourself

Make the average log-loss as small as you can by adjusting ww and bb. That is training, by hand.

How do we choose w\mathbf w? Pick the weights that make the observed labels most probable. For labels yi∈{0,1}y_i\in\{0,1\} the likelihood of the data is ∏piyi(1−pi)1−yi\prod p_i^{y_i}(1-p_i)^{1-y_i}. Taking −log⁡-\log and averaging gives the cross-entropy (log-loss):

L(w)=−1n∑i=1n[yilog⁡σ(wTxi)+(1−yi)log⁡(1−σ(wTxi))]\mathcal L(\mathbf w)=-\frac1n\sum_{i=1}^n\Big[y_i\log\sigma(\mathbf w^T\mathbf x_i)+(1-y_i)\log\big(1-\sigma(\mathbf w^T\mathbf x_i)\big)\Big]

Training = minimise L\mathcal L: an unconstrained optimisation problem, and convex (week 8), so any minimum is the global one. Its gradient has a lovely form:

∇L=1n∑i(σ(wTxi)−yi) xi\nabla\mathcal L=\frac1n\sum_i\big(\sigma(\mathbf w^T\mathbf x_i)-y_i\big)\,\mathbf x_i

"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.

Solved example: 5. A probability from a trained model

Question. A trained model has w=(1,−2)\mathbf w=(1,-2) and bias b=0.5b=0.5. For x=(2,1)\mathbf x=(2,1), what is the probability of class 1, and which class does it predict?

Solution. z=1⋅2−2⋅1+0.5=0.5z=1\cdot2-2\cdot1+0.5=0.5. p=σ(0.5)=11+e−0.5=11+0.6065≈0.622p=\sigma(0.5)=\frac1{1+e^{-0.5}}=\frac1{1+0.6065}\approx\mathbf{0.622}. Since p>0.5p>0.5 it predicts class 1. The decision boundary is where z=0z=0, a straight line.

Your turn

Your turn: 1. Lagrange

Minimise x2+y2x^2+y^2 subject to 2x+y=102x+y=10.

Need a hint?

∇g=(2,1)\nabla g=(2,1).

Show the full solution

2x=2λ2x=2\lambda, 2y=λ2y=\lambda, so x=λx=\lambda and y=λ/2y=\lambda/2, hence x=2yx=2y. Constraint: 4y+y=104y+y=10, y=2y=2, x=4x=4. Minimum value 16+4=2016+4=\mathbf{20}. (Check: distance2^2 from the origin to the line =1004+1=20=\frac{100}{4+1}=20 ✓.)

Your turn: 2. A different shape

Maximise f(x,y)=xyf(x,y)=xy subject to x+y=10x+y=10.

Need a hint?

∇f=(y,x)\nabla f=(y,x) and ∇g=(1,1)\nabla g=(1,1).

Show the full solution

y=λy=\lambda and x=λx=\lambda, so x=yx=y. Constraint: x=y=5x=y=5. Maximum value 25\mathbf{25}. (A rectangle of fixed perimeter has the biggest area when it is a square.)

Your turn: 3. KKT with two cases

Minimise f(x)=x2f(x)=x^2 subject to x≥1x\ge1 (write it as 1−x≤01-x\le0). Find x∗x^* and μ\mu.

Need a hint?

Try "inactive" first and see whether you obey the rule.

Show the full solution

Inactive: μ=0⇒x=0\mu=0\Rightarrow x=0, but 0≥10\ge1 is false. So the constraint is active: x=1x=1. Stationarity 2x+μ(−1)=0⇒μ=2≥02x+\mu(-1)=0\Rightarrow\mu=2\ge0 ✓. So x∗=1x^*=1, μ=2\mu=2, f=1f=1.

Your turn: 4. Set up an LP

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 3x+4y3x+4y s.t. 3x+y≥603x+y\ge60, 2x+4y≥802x+4y\ge80, x,y≥0x,y\ge0. Corners: (0,60)(0,60): cost 240. (40,0)(40,0): constraint 1 gives 120≥60120\ge60 ✓ and 80≥8080\ge80 ✓: cost 120. Intersection of the two limits: 3x+y=603x+y=60, 2x+4y=802x+4y=80: from the first y=60−3xy=60-3x, then 2x+240−12x=802x+240-12x=80, x=16x=16, y=12y=12: cost 48+48=9648+48=96. Minimum cost Rs 96 at (16,12)(16,12).

Your turn: 5. Which is the primal problem?

Write the problem of finding the point of the plane x+2y+3z=6x+2y+3z=6 closest to the origin.

Need a hint?

Closest to the origin means minimise the squared length.

Show the full solution

min⁡x,y,z  x2+y2+z2\min_{x,y,z}\;x^2+y^2+z^2 subject to x+2y+3z=6x+2y+3z=6. (Lagrange gives (x,y,z)=λ′(1,2,3)(x,y,z)=\lambda'(1,2,3) with 14λ′=614\lambda'=6: the point 37(1,2,3)\frac37(1,2,3) and minimum value 3614=187\frac{36}{14}=\frac{18}7.)

Exam radar

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.
Watch out - real exam trap
  • Recompute ∇g\nabla g for each new constraint; do not reuse a relation.
  • KKT dual feasibility is μ≥0\mu\ge0 for a ≤\le constraint. If the problem is written with ≥\ge, rewrite it as ≤\le first.
  • Zero multiplier does not mean the constraint is slack, only that it may be.
  • In an LP, don't forget x,y≥0x,y\ge0 when listing the corners.
  • Answer what is asked: sometimes the value of the objective, sometimes a variable, sometimes a multiplier.

Quick quiz

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
That is the Lagrange condition: grad f = lambda times grad g.

Complementary slackness says...

  • mu is always positive
  • mu times g(x) equals zero
  • g(x) is always zero
  • f(x) equals zero
Either the constraint is tight (g = 0) or its multiplier is 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
For a linear objective, an optimum is at a vertex (corner) of the feasible region.

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.
The sigmoid keeps probabilities in (0,1); training is done with gradient descent because there is no closed form.

Recap

Remember this
  • Translate stories into: variables, objective, constraints (and x,y≥0x,y\ge0).
  • Lagrange: ∇f=λ∇g\nabla f=\lambda\nabla g plus g=cg=c. Recompute ∇g\nabla g each time.
  • KKT for g≤0g\le0: stationarity, primal feasibility, dual feasibility (μ≥0\mu\ge0), complementary slackness (μg=0\mu g=0). Solve by "active or inactive?".
  • LP: optimum at a corner. Check each corner.
  • Closest point to the origin: min⁡∥x∥2\min\|\mathbf x\|^2 s.t. the set. Logistic regression: minimise convex log-loss with gradient descent.
Formulas to keep
  • Lagrange: ∇f=λ∇g\nabla f=\lambda\nabla g, g=cg=c; Lagrangian L=f−λ(g−c)L=f-\lambda(g-c)
  • KKT (g≤0g\le0): ∇f+μ∇g=0\nabla f+\mu\nabla g=0; g≤0g\le0; μ≥0\mu\ge0; μg=0\mu g=0
  • Distance2^2 from origin to aTx=b\mathbf a^T\mathbf x=b: b2/∥a∥2b^2/\|\mathbf a\|^2; closest point =b∥a∥2a=\frac{b}{\|\mathbf a\|^2}\mathbf a
  • LP: optimum at a vertex of the feasible region
  • Sigmoid: σ(z)=11+e−z\sigma(z)=\frac1{1+e^{-z}}; log-loss −1n∑[ylog⁡p+(1−y)log⁡(1−p)]-\frac1n\sum[y\log p+(1-y)\log(1-p)]; gradient 1n∑(pi−yi)xi\frac1n\sum(p_i-y_i)\mathbf x_i
Connected weeks (learn them together)

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