πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Lagrange Multipliers

CalculusConstrained Optimization🟒 Free Lesson

Advertisement

Lagrange Multipliers

In real-world ML pipelines, unconstrained optimization is the exception, not the rule. Every time you add a constraint β€” a budget on inference time, a fairness requirement across demographic groups, a maximum norm on weight vectors β€” you enter the domain of constrained optimization. Lagrange multipliers are the foundational tool that makes these problems tractable, and understanding them is essential for anyone building or analyzing ML systems.


What are Lagrange Multipliers


Geometric Interpretation

At the point of tangency:

  • is perpendicular to the level curve of
  • is perpendicular to the constraint curve
  • Since both gradients are perpendicular to the same tangent line, they must be parallel
  • Therefore for some scalar

If , increasing the constraint (relaxing it in the direction of ) increases . If , relaxing the constraint decreases . This makes the sensitivity of the optimal value to the constraint.


Method of Lagrange Multipliers


Single Constraint Examples



Multiple Constraints


KKT Conditions


Saddle Points


Python Implementation

import numpy as np
from scipy.optimize import minimize, minimize_scalar

# ============================================
# Example 1: Single Equality Constraint
# Minimize x^2 + y^2 subject to x + y = 1
# ============================================

def lagrange_single_constraint():
    f = lambda x: x[0]**2 + x[1]**2
    constraint = {'type': 'eq', 'fun': lambda x: x[0] + x[1] - 1}
    
    result = minimize(f, x0=[0.5, 0.5], constraints=constraint)
    print(f"Optimal point: x = {result.x[0]:.4f}, y = {result.x[1]:.4f}")
    print(f"Optimal value: {result.fun:.4f}")
    print(f"Multiplier (approx): lambda = {2 * result.x[0]:.4f}")
    # Analytical solution: x = y = 1/2, lambda = 1

lagrange_single_constraint()

# ============================================
# Example 2: Minimize distance to a plane
# Minimize x^2 + y^2 + z^2 subject to 2x + y - 2z = 4
# ============================================

def closest_point_on_plane():
    f = lambda x: x[0]**2 + x[1]**2 + x[2]**2
    constraint = {'type': 'eq', 'fun': lambda x: 2*x[0] + x[1] - 2*x[2] - 4}
    
    result = minimize(f, x0=[1, 1, 1], constraints=constraint)
    print(f"\nClosest point: ({result.x[0]:.4f}, {result.x[1]:.4f}, {result.x[2]:.4f})")
    print(f"Distance: {np.sqrt(result.fun):.4f}")
    # Analytical: (8/9, 4/9, -8/9), distance = 4*sqrt(5)/3

closest_point_on_plane()

# ============================================
# Example 3: Multiple constraints
# Minimize x^2 + y^2 + z^2 subject to x+y+z=1 and x-y=0
# ============================================

def multiple_constraints():
    f = lambda x: x[0]**2 + x[1]**2 + x[2]**2
    constraints = [
        {'type': 'eq', 'fun': lambda x: x[0] + x[1] + x[2] - 1},
        {'type': 'eq', 'fun': lambda x: x[0] - x[1]}
    ]
    
    result = minimize(f, x0=[1/3, 1/3, 1/3], constraints=constraints)
    print(f"\nOptimal point: ({result.x[0]:.4f}, {result.x[1]:.4f}, {result.x[2]:.4f})")
    print(f"Optimal value: {result.fun:.4f}")

multiple_constraints()

# ============================================
# Example 4: KKT with inequality constraints
# Minimize x^2 + y^2 subject to x + y >= 1
# ============================================

def kkt_inequality():
    f = lambda x: x[0]**2 + x[1]**2
    # Convert >= to <=: -(x + y - 1) <= 0
    constraint = {'type': 'ineq', 'fun': lambda x: x[0] + x[1] - 1}
    
    result = minimize(f, x0=[0, 0], constraints=constraint)
    print(f"\nKKT optimal point: ({result.x[0]:.4f}, {result.x[1]:.4f})")
    print(f"Optimal value: {result.fun:.4f}")
    # Solution: x = y = 0.5, value = 0.5

kkt_inequality()

Applications in AI/ML

Support Vector Machines (SVM)

Regularization as Constraint

Fairness Constraints

Portfolio Optimization

In finance-inspired ML, the Markowitz mean-variance portfolio optimizes return subject to risk constraints. Lagrange multipliers solve this, and the resulting efficient frontier maps out the tradeoff between risk and return β€” parameterized by .


Common Mistakes

MistakeWhy It's WrongCorrect Approach
Forgetting the constraint equationSolving alone gives equations for unknownsAlways include as an equation
Ignoring the sign of vs indicates whether relaxing the constraint helps or hurtsTrack the sign; it has physical meaning
Confusing with Both conventions work, but mixing them leads to sign errors in Pick one convention and be consistent
Assuming all critical points are extremaLagrange finds stationary points of , including saddle pointsVerify with second-order conditions
Forgetting complementary slackness (KKT)With inequality constraints, assuming all is wrongCheck: either or
Not checking constraint qualificationIf at the candidate, the method may failVerify regularity (e.g., )
Using Lagrange for unconstrained problemsLagrange multipliers are for constrained optimizationUse gradient-based methods for unconstrained problems
Ignoring boundary effectsThe Lagrangian method finds interior critical points, not boundary solutionsCheck boundary conditions separately if the domain is bounded

Interview Questions

Q1: What is the geometric interpretation of a Lagrange multiplier?

Q2: Why does the Lagrange multiplier method work? What is the intuition behind ?

Q3: How do Lagrange multipliers relate to the dual problem in SVM?

Q4: What is complementary slackness and why does it matter?

Q5: When might Lagrange multipliers fail or give misleading results?

Q6: Explain the relationship between Lagrange multipliers and sensitivity analysis.


Practice Problems

Problem 1: Box Optimization


Problem 2: Nearest Point on an Ellipsoid


Problem 3: Constrained Exponential


Problem 4: KKT with Two Inequality Constraints


Quick Reference


Cross-References

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement