Day 26. Constrained Optimisation & Lagrange Multipliers
Optimising subject to budgets and adding-up constraints, the shadow price, and the experimental-design and weighting problems that use it.
- ●Set up a Lagrangian, solve the first-order conditions and interpret the multiplier as a shadow price.
- ●Prove the envelope theorem and handle simple inequality constraints (KKT).
- ●Derive consumer demand, cost-minimising inputs and minimum-variance weights.
- ●Derive optimal sample allocation across treatment arms and strata, and the form of entropy-balancing weights.
Theory
The Lagrangian
To maximise or minimise subject to , form
and solve together with the constraint. At the optimum, : the objective's gradient is parallel to the constraint's, so no feasible direction improves (geometrically, a level curve is tangent to the constraint). With several constraints, use one multiplier per constraint.
The shadow price
Let be the optimal value. The envelope theorem says
so the multiplier is the marginal value of relaxing the constraint: the marginal utility of income, the marginal cost of output, the value of one more unit of budget.
Inequality constraints (KKT)
For subject to : , , and complementary slackness . Either the constraint binds, or its multiplier is zero.
Where constraints appear in causal inference
- Allocating a fixed budget across treatment and control arms, or across strata.
- Synthetic control: weights that are non-negative and sum to one (Day 85).
- Entropy balancing / calibration: weights closest to uniform that exactly match covariate means (Day 84).
- Restricted least squares: imposing .
Part 1 · Warm-up
Exercise 1 · A classic
Maximise subject to .
Show solution
: , , so , , value 25.
Exercise 2 · Closest point on a line
Minimise subject to .
Show solution
Step 1. Lagrangian: .
Step 2. FOCs: and , so .
Step 3. Constraint: , , .
Answer: with value 5: the point on the line closest to the origin (a projection, Day 20).
Exercise 3 · Cobb–Douglas demand
Maximise with prices , and income .
Show solution
Step 1. Cobb–Douglas spends the share of income on and on (Exercise 6).
Step 2. .
Step 3. .
Answer: 15 units of and 10 of (spending $30 and $70).
Exercise 4 · Shadow price check
In Exercise 1 with constraint , compute and verify .
Show solution
, , at . ✓
Exercise 5 · A binding inequality
Minimise subject to .
Show solution
The unconstrained minimum is infeasible, so the constraint binds: . KKT: . ✓
Part 2 · Proofs
Exercise 6 · Cobb–Douglas demand
Derive the demand for when maximising subject to .
Show solution
Step 1. Maximise the log, .
Step 2. FOCs: and .
Step 3. So and ; adding, .
Step 4. Hence .
Exercise 7 · The envelope theorem
Prove for s.t. .
Show solution
Step 1. , so using the FOC.
Step 2. Differentiating the constraint gives .
Step 3. So .
Exercise 8 · Minimum-variance weights
Minimise subject to ( PD).
Show solution
Step 1. Lagrangian: .
Step 2. FOC (Day 24 rules, symmetric): .
Step 3. Impose the constraint: .
Step 4. Substitute back:
Step 5. With independent estimates, and : inverse-variance weighting (Day 8), the efficient way to pool estimates.
Exercise 9 · Restricted least squares
Minimise subject to and show
Show solution
Step 1. Lagrangian (the factor 2 on keeps the algebra tidy): .
Step 2. FOC in : , i.e. .
Step 3. Solve for : , where is unrestricted OLS.
Step 4. Impose : .
Step 5. Solve for the multiplier: .
Step 6. Substitute into Step 3:
The correction is large when badly violates the restriction: the idea behind Wald and LR tests.
Exercise 10 · Optimal allocation between arms
With outcome sds and per-unit costs , minimise subject to .
Show solution
Step 1. FOCs: and .
Step 2. Dividing,
Step 3. Put more units where outcomes are noisier and where they are cheaper.
Step 4. With equal costs and variances, split evenly.
Part 3 · Applied
Exercise 11 · An expensive treatment
Treatment costs $40 per unit, control $10, the budget is $12,000 and outcome variances are equal. How many units go to each arm?
Show solution
Step 1. Optimal ratio (Exercise 10) with equal variances: .
Step 2. Budget: with : .
Step 3. , . Check: . ✓
Answer: 200 treated and 400 controls.
Exercise 12 · Neyman allocation across strata
Two strata have sizes 6,000 and 4,000 and outcome sds 10 and 30. Allocate a sample of 300 to minimise the variance of the stratified mean (allocation ).
Show solution
Step 1. Allocation weights : and .
Step 2. Shares: and .
Step 3. Sample: and .
Answer: 100 from the large, homogeneous stratum and 200 from the smaller but noisier one.
Exercise 13 · Cost minimisation
Produce at minimum cost with , .
Show solution
Step 1. Tangency (MRTS = input-price ratio): , so .
Step 2. Output constraint: , .
Step 3. Cost: .
Answer: 5 units of labour, 20 of capital, minimum cost 40.
Exercise 14 · Synthetic-control weights are not always unique
A treated region has pre-period outcome 3; donor regions have 1, 5 and 6. Find two different weight vectors with , that reproduce 3 exactly.
Show solution
Step 1. We need with and .
Step 2. Try : . ✓
Step 3. Try : . ✓
Answer: both fit perfectly. With one pre-period outcome, many weight vectors match exactly, which is why synthetic control matches many pre-periods and why penalised versions exist.
Exercise 15 · Entropy-balancing weights
Minimise subject to and . Show the solution has the form .
Show solution
Step 1. Lagrangian:
Step 2. FOC for : .
Step 3. Solve: ; the factor is the same for everyone, so .
Step 4. Choose so the weighted mean of equals the target, and so the weights sum to one.
Answer: entropy-balancing weights tilt the base weights exponentially in the covariate (Day 84).
Verify in Python
import numpy as np
from scipy import optimize
res = optimize.minimize(lambda v: -(v[0]**0.3 * v[1]**0.7), [10, 10],
constraints={"type": "eq", "fun": lambda v: 2 * v[0] + 7 * v[1] - 100})
print("Cobb-Douglas demand:", res.x.round(3))
Sig = np.array([[100, 60], [60, 400.]]); one = np.ones(2)
w = np.linalg.solve(Sig, one); w /= w.sum()
print("minimum-variance weights:", w.round(4))
n0 = 12_000 / (40 * 0.5 + 10); print("arm sizes (n1, n0):", n0 * 0.5, n0)
N, s = np.array([6000, 4000]), np.array([10, 30]); print("Neyman allocation:", 300 * N * s / (N * s).sum())
# Entropy balancing: tilt uniform weights on x so the weighted mean is 4
x = np.array([1, 2, 3, 4, 5, 6.]); q = np.ones(6) / 6
lam = optimize.brentq(lambda l: (q * np.exp(l * x) * x).sum() / (q * np.exp(l * x)).sum() - 4, -5, 5)
wts = q * np.exp(lam * x); wts /= wts.sum()
print("entropy-balancing weights:", wts.round(3), " weighted mean:", round(wts @ x, 4))
Key takeaways
- At a constrained optimum, the objective's gradient is a multiple of the constraint's; the multiple is the shadow price.
- Budget allocation in experiments and stratified samples is a Lagrange problem: favour noisy and cheap arms.
- Weighting estimators (minimum variance, synthetic control, entropy balancing) are constrained optimisation problems.
Simulation Lab: Constrained Optimum: Budget & Indifference
Open full screen →Utility maximisation on a budget line: tangency, the Lagrange multiplier and the cost of a non-optimal bundle.
Maximise U = x^α y^(1−α) on the budget line. At the optimum the indifference curve is tangent to the budget line (MRS = pₓ/p_y), i.e. ∇U = λ∇g. The multiplier λ is the utility gained from one more dollar of income.