Math for Causal Inference

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.

Video coming soon
Learning outcomes
  • ●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.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

The Lagrangian

To maximise or minimise f(x)f(x) subject to g(x)=cg(x) = c, form

L(x,λ)=f(x)−λ (g(x)−c)\mathcal{L}(x, \lambda) = f(x) - \lambda\,(g(x) - c)

and solve ∇xL=0\nabla_x\mathcal{L} = 0 together with the constraint. At the optimum, ∇f=λ∇g\nabla f = \lambda\nabla g: the objective's gradient is parallel to the constraint's, so no feasible direction improves ff (geometrically, a level curve is tangent to the constraint). With several constraints, use one multiplier per constraint.

The shadow price

Let V(c)V(c) be the optimal value. The envelope theorem says

dVdc=λ∗,\frac{dV}{dc} = \lambda^*,

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 min⁡f(x)\min f(x) subject to h(x)≤0h(x) \le 0: ∇f+μ∇h=0\nabla f + \mu\nabla h = 0, μ≥0\mu \ge 0, h(x)≤0h(x) \le 0 and complementary slackness μ h(x)=0\mu\,h(x) = 0. 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 Rβ=rR\beta = r.

Part 1 · Warm-up

Exercise 1 · A classic

Maximise xyxy subject to x+y=10x + y = 10.

Show solution

L=xy−λ(x+y−10)\mathcal L = xy - \lambda(x + y - 10): y=λy = \lambda, x=λx = \lambda, so x=y=5x = y = 5, λ=5\lambda = 5, value 25.

Exercise 2 · Closest point on a line

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

Show solution

Step 1. Lagrangian: L=x2+y2−λ(x+2y−5)\mathcal L = x^2 + y^2 - \lambda(x + 2y - 5).

Step 2. FOCs: 2x=λ2x = \lambda and 2y=2λ2y = 2\lambda, so y=2xy = 2x.

Step 3. Constraint: x+2(2x)=5⇒x=1x + 2(2x) = 5 \Rightarrow x = 1, y=2y = 2, λ=2\lambda = 2.

Answer: (1,2)(1, 2) with value 5: the point on the line closest to the origin (a projection, Day 20).

Exercise 3 · Cobb–Douglas demand

Maximise U=x0.3y0.7U = x^{0.3}y^{0.7} with prices px=2p_x = 2, py=7p_y = 7 and income M=100M = 100.

Show solution

Step 1. Cobb–Douglas spends the share α\alpha of income on xx and 1−α1 - \alpha on yy (Exercise 6).

Step 2. x=0.3×100/2=15x = 0.3 \times 100/2 = 15.

Step 3. y=0.7×100/7=10y = 0.7 \times 100/7 = 10.

Answer: 15 units of xx and 10 of yy (spending $30 and $70).

Exercise 4 · Shadow price check

In Exercise 1 with constraint x+y=cx + y = c, compute V(c)V(c) and verify dV/dc=λdV/dc = \lambda.

Show solution

x=y=c/2x = y = c/2, V(c)=c2/4V(c) = c^2/4, dV/dc=c/2=5=λdV/dc = c/2 = 5 = \lambda at c=10c = 10. ✓

Exercise 5 · A binding inequality

Minimise (x−3)2(x - 3)^2 subject to x≤2x \le 2.

Show solution

The unconstrained minimum x=3x = 3 is infeasible, so the constraint binds: x=2x = 2. KKT: 2(x−3)+μ=0⇒μ=2≥02(x - 3) + \mu = 0 \Rightarrow \mu = 2 \ge 0. ✓

Part 2 · Proofs

Exercise 6 · Cobb–Douglas demand

Derive the demand for xx when maximising xαy1−αx^{\alpha}y^{1-\alpha} subject to pxx+pyy=Mp_xx + p_yy = M.

Show solution

Step 1. Maximise the log, αln⁡x+(1−α)ln⁡y\alpha\ln x + (1 - \alpha)\ln y.

Step 2. FOCs: α/x=λpx\alpha/x = \lambda p_x and (1−α)/y=λpy(1 - \alpha)/y = \lambda p_y.

Step 3. So pxx=α/λp_xx = \alpha/\lambda and pyy=(1−α)/λp_yy = (1 - \alpha)/\lambda; adding, M=1/λM = 1/\lambda.

Step 4. Hence x=αM/pxx = \alpha M/p_x.

Exercise 7 · The envelope theorem

Prove dV/dc=λ∗dV/dc = \lambda^* for V(c)=max⁡f(x)V(c) = \max f(x) s.t. g(x)=cg(x) = c.

Show solution

Step 1. V(c)=f(x∗(c))V(c) = f(x^*(c)), so V′(c)=∇f⊤dx∗dc=λ∗∇g⊤dx∗dcV'(c) = \nabla f^{\top}\frac{dx^*}{dc} = \lambda^*\nabla g^{\top}\frac{dx^*}{dc} using the FOC.

Step 2. Differentiating the constraint g(x∗(c))=cg(x^*(c)) = c gives ∇g⊤dx∗dc=1\nabla g^{\top}\frac{dx^*}{dc} = 1.

Step 3. So V′(c)=λ∗V'(c) = \lambda^*.

Exercise 8 · Minimum-variance weights

Minimise w⊤Σww^{\top}\Sigma w subject to 1⊤w=1\mathbf 1^{\top}w = 1 (Σ\Sigma PD).

Show solution

Step 1. Lagrangian: L=w⊤Σw−λ(1⊤w−1)\mathcal L = w^{\top}\Sigma w - \lambda(\mathbf 1^{\top}w - 1).

Step 2. FOC (Day 24 rules, Σ\Sigma symmetric): 2Σw−λ1=0⇒w=λ2Σ−112\Sigma w - \lambda\mathbf 1 = 0 \Rightarrow w = \frac\lambda2\Sigma^{-1}\mathbf 1.

Step 3. Impose the constraint: 1⊤w=λ21⊤Σ−11=1⇒λ2=11⊤Σ−11\mathbf 1^{\top}w = \frac\lambda2\mathbf 1^{\top}\Sigma^{-1}\mathbf 1 = 1 \Rightarrow \frac\lambda2 = \frac{1}{\mathbf 1^{\top}\Sigma^{-1}\mathbf 1}.

Step 4. Substitute back:

w∗=Σ−111⊤Σ−11.w^* = \frac{\Sigma^{-1}\mathbf 1}{\mathbf 1^{\top}\Sigma^{-1}\mathbf 1}.

Step 5. With independent estimates, Σ=diag⁡(vj)\Sigma = \operatorname{diag}(v_j) and wj∗∝1/vjw_j^* \propto 1/v_j: inverse-variance weighting (Day 8), the efficient way to pool estimates.

Exercise 9 · Restricted least squares

Minimise ∥y−Xb∥2\|y - Xb\|^2 subject to Rb=rRb = r and show

b~=β^−(X⊤X)−1R⊤[R(X⊤X)−1R⊤]−1(Rβ^−r).\tilde b = \hat\beta - (X^{\top}X)^{-1}R^{\top}\big[R(X^{\top}X)^{-1}R^{\top}\big]^{-1}(R\hat\beta - r).
Show solution

Step 1. Lagrangian (the factor 2 on λ\lambda keeps the algebra tidy): L=∥y−Xb∥2+2λ⊤(Rb−r)\mathcal L = \|y - Xb\|^2 + 2\lambda^{\top}(Rb - r).

Step 2. FOC in bb: −2X⊤(y−Xb)+2R⊤λ=0-2X^{\top}(y - Xb) + 2R^{\top}\lambda = 0, i.e. X⊤Xb=X⊤y−R⊤λX^{\top}Xb = X^{\top}y - R^{\top}\lambda.

Step 3. Solve for bb: b~=β^−(X⊤X)−1R⊤λ\tilde b = \hat\beta - (X^{\top}X)^{-1}R^{\top}\lambda, where β^\hat\beta is unrestricted OLS.

Step 4. Impose Rb~=rR\tilde b = r: Rβ^−R(X⊤X)−1R⊤λ=rR\hat\beta - R(X^{\top}X)^{-1}R^{\top}\lambda = r.

Step 5. Solve for the multiplier: λ=[R(X⊤X)−1R⊤]−1(Rβ^−r)\lambda = \big[R(X^{\top}X)^{-1}R^{\top}\big]^{-1}(R\hat\beta - r).

Step 6. Substitute into Step 3:

b~=β^−(X⊤X)−1R⊤[R(X⊤X)−1R⊤]−1(Rβ^−r).\tilde b = \hat\beta - (X^{\top}X)^{-1}R^{\top}\big[R(X^{\top}X)^{-1}R^{\top}\big]^{-1}(R\hat\beta - r).

The correction is large when β^\hat\beta badly violates the restriction: the idea behind Wald and LR tests.

Exercise 10 · Optimal allocation between arms

With outcome sds σ1,σ0\sigma_1, \sigma_0 and per-unit costs c1,c0c_1, c_0, minimise Var⁡(τ^)=σ12/n1+σ02/n0\operatorname{Var}(\hat\tau) = \sigma_1^2/n_1 + \sigma_0^2/n_0 subject to c1n1+c0n0=Bc_1n_1 + c_0n_0 = B.

Show solution

Step 1. FOCs: σ12/n12=λc1\sigma_1^2/n_1^2 = \lambda c_1 and σ02/n02=λc0\sigma_0^2/n_0^2 = \lambda c_0.

Step 2. Dividing,

n1n0=σ1σ0c0c1.\frac{n_1}{n_0} = \frac{\sigma_1}{\sigma_0}\sqrt{\frac{c_0}{c_1}}.

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: n1/n0=c0/c1=10/40=0.5n_1/n_0 = \sqrt{c_0/c_1} = \sqrt{10/40} = 0.5.

Step 2. Budget: 40n1+10n0=12,00040n_1 + 10n_0 = 12{,}000 with n1=0.5n0n_1 = 0.5n_0: 20n0+10n0=30n0=12,00020n_0 + 10n_0 = 30n_0 = 12{,}000.

Step 3. n0=400n_0 = 400, n1=200n_1 = 200. Check: 40(200)+10(400)=12,00040(200) + 10(400) = 12{,}000. ✓

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 nh∝Nhσhn_h \propto N_h\sigma_h).

Show solution

Step 1. Allocation weights NhσhN_h\sigma_h: 6,000×10=60,0006{,}000 \times 10 = 60{,}000 and 4,000×30=120,0004{,}000 \times 30 = 120{,}000.

Step 2. Shares: 60,000/180,000=1/360{,}000/180{,}000 = 1/3 and 2/32/3.

Step 3. Sample: 300/3=100300/3 = 100 and 200200.

Answer: 100 from the large, homogeneous stratum and 200 from the smaller but noisier one.

Exercise 13 · Cost minimisation

Produce q=K0.5L0.5=10q = K^{0.5}L^{0.5} = 10 at minimum cost with w=4w = 4, r=1r = 1.

Show solution

Step 1. Tangency (MRTS = input-price ratio): MPLMPK=0.5K0.5L−0.50.5K−0.5L0.5=KL=wr=4\frac{MP_L}{MP_K} = \frac{0.5K^{0.5}L^{-0.5}}{0.5K^{-0.5}L^{0.5}} = \frac KL = \frac wr = 4, so K=4LK = 4L.

Step 2. Output constraint: 4L⋅L=2L=10⇒L=5\sqrt{4L \cdot L} = 2L = 10 \Rightarrow L = 5, K=20K = 20.

Step 3. Cost: 4(5)+1(20)=404(5) + 1(20) = 40.

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 w≥0w \ge 0, ∑w=1\sum w = 1 that reproduce 3 exactly.

Show solution

Step 1. We need w1(1)+w2(5)+w3(6)=3w_1(1) + w_2(5) + w_3(6) = 3 with w≥0w \ge 0 and w1+w2+w3=1w_1 + w_2 + w_3 = 1.

Step 2. Try w=(0.5,0.5,0)w = (0.5, 0.5, 0): 0.5+2.5=30.5 + 2.5 = 3. ✓

Step 3. Try w=(0.6,0,0.4)w = (0.6, 0, 0.4): 0.6+2.4=30.6 + 2.4 = 3. ✓

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 ∑iwiln⁡(wi/qi)\sum_iw_i\ln(w_i/q_i) subject to ∑iwi=1\sum_iw_i = 1 and ∑iwixi=xˉtarget\sum_iw_ix_i = \bar x_{\text{target}}. Show the solution has the form wi∝qi eλxiw_i \propto q_i\,e^{\lambda x_i}.

Show solution

Step 1. Lagrangian:

L=∑iwiln⁡(wi/qi)−λ0(∑iwi−1)−λ(∑iwixi−xˉtarget).\mathcal L = \sum_iw_i\ln(w_i/q_i) - \lambda_0\Big(\sum_iw_i - 1\Big) - \lambda\Big(\sum_iw_ix_i - \bar x_{\text{target}}\Big).

Step 2. FOC for wiw_i: ln⁡(wi/qi)+1−λ0−λxi=0\ln(w_i/q_i) + 1 - \lambda_0 - \lambda x_i = 0.

Step 3. Solve: wi=qi eλ0−1 eλxiw_i = q_i\,e^{\lambda_0 - 1}\,e^{\lambda x_i}; the factor eλ0−1e^{\lambda_0 - 1} is the same for everyone, so wi∝qieλxiw_i \propto q_ie^{\lambda x_i}.

Step 4. Choose λ\lambda so the weighted mean of xx equals the target, and λ0\lambda_0 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.

05101501020304050good xgood yoptimum
optimum (x*, y*)
(15.0, 10.0)
x* = αM/pₓ
λ (shadow price)
0.1129
dU*/dM
candidate utility
9.40
optimum 11.29
MRS at candidate
0.082
price ratio 0.286
budget lineindifference curve through the optimumcandidate 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.

Day 25Day 27