TExcelTAD
Home › Optimization › Sensitivity and Shadow Prices

Sensitivity and Shadow Prices

An optimal solution answers "what should we do?". The multipliers answer the manager's next question: "what if we had a bit more of this resource?" Under strong duality, the optimal dual variables are exactly the sensitivities of the optimal value to the constraints, so a solver gives you this what-if analysis for free.

The perturbed problem

Relax each constraint by an amount:

p∗(u,v)=min⁡f0(x)s.t.fi(x)≤ui,hj(x)=vjp^*(\mathbf{u}, \mathbf{v}) = \min f_0(\mathbf{x}) \quad \text{s.t.} \quad f_i(\mathbf{x}) \le u_i, \quad h_j(\mathbf{x}) = v_j

If strong duality holds and p∗p^* is differentiable at 0\mathbf{0}:

λi∗=−∂p∗∂ui,νj∗=−∂p∗∂vj\lambda_i^* = -\frac{\partial p^*}{\partial u_i}, \qquad \nu_j^* = -\frac{\partial p^*}{\partial v_j}

Even without differentiability there is a global inequality: p∗(u,v)≥p∗(0,0)−λ∗Tu−ν∗Tvp^*(\mathbf{u}, \mathbf{v}) \ge p^*(\mathbf{0}, \mathbf{0}) - \boldsymbol{\lambda}^{*T}\mathbf{u} - \boldsymbol{\nu}^{*T}\mathbf{v}.

Multiplier Meaning
λi∗\lambda_i^* large the constraint binds hard; loosening it helps a lot
λi∗=0\lambda_i^* = 0 the constraint is slack; extra resource is worthless (locally)
νj∗<0\nu_j^* < 0 increasing vjv_j raises the cost; νj∗>0\nu_j^* > 0, lowers it

Shadow prices in a production plan

A factory makes three products with profits 20, 30 and 25 per unit, limited by machine hours, labour hours and material:

python · try it yourself
import numpy as np
from scipy.optimize import linprog

profit = np.array([20.0, 30.0, 25.0])
A = np.array([[2.0, 3.0, 2.0],     # machine hours per unit
              [1.0, 2.0, 2.0],     # labour hours per unit
              [3.0, 2.0, 2.0]])    # kg of material per unit
b = np.array([100.0, 80.0, 150.0])
res = linprog(-profit, A_ub=A, b_ub=b, bounds=[(0, None)] * 3, method="highs")
print("plan:", np.round(res.x, 4), " profit:", round(-res.fun, 4))
prices = -res.ineqlin.marginals                 # HiGHS reports d(objective)/d(b) for the min problem
for name, p, slack in zip(["machine", "labour", "material"], prices, res.ineqlin.residual):
    print(f"{name:9s} shadow price {p:7.4f}   slack {slack:7.4f}")
plan: [20.  0. 30.]  profit: 1150.0
machine   shadow price  7.5000   slack  0.0000
labour    shadow price  5.0000   slack  0.0000
material  shadow price  0.0000   slack 30.0000

The best plan makes no units of the second product. One more machine hour is worth 7.5 in extra profit and one more labour hour 5; material has 30 kg of slack left, so its price is zero, and buying more would be wasted money.

Checking the prediction

Shadow prices are local. Increase machine hours step by step and watch the profit grow at the predicted rate until the basis changes:

python · try it yourself
import numpy as np
from scipy.optimize import linprog

profit = np.array([20.0, 30.0, 25.0])
A = np.array([[2.0, 3.0, 2.0], [1.0, 2.0, 2.0], [3.0, 2.0, 2.0]])
def best(machine):
    r = linprog(-profit, A_ub=A, b_ub=[machine, 80.0, 150.0], bounds=[(0, None)] * 3, method="highs")
    return -r.fun
base = best(100)
for extra in (1, 10, 20, 40, 60):
    gain = best(100 + extra) - base
    print(f"+{extra:2d} machine hours: profit +{gain:8.4f}  (rate {gain / extra:.4f} per hour)")
+ 1 machine hours: profit +  7.5000  (rate 7.5000 per hour)
+10 machine hours: profit + 75.0000  (rate 7.5000 per hour)
+20 machine hours: profit +137.5000  (rate 6.8750 per hour)
+40 machine hours: profit +225.0000  (rate 5.6250 per hour)
+60 machine hours: profit +225.0000  (rate 3.7500 per hour)

The rate stays constant for small changes and drops once another constraint starts to bind: p∗p^* is piecewise linear for LPs.

Uses beyond pricing

  • Which constraint to relax: in design problems, the largest multiplier points at the bottleneck.
  • Regularization paths: in the lasso, the multiplier of a norm constraint ∥w∥1≤t\lVert \mathbf{w} \rVert_1 \le t is the penalty weight λ\lambda of the equivalent penalized form.
  • Economics: in market-clearing models, the multipliers of supply-demand balance constraints are the market prices (electricity markets compute locational prices exactly this way).

In software

  • SciPy's HiGHS interface reports res.ineqlin.marginals, res.eqlin.marginals and bound marginals; CVXPY uses constraint.dual_value; Gurobi uses Constr.Pi and reports ranges over which a price is valid (SARHSLow, SARHSUp).

Exercises

Exercise 1: Estimate a change

A cost-minimizing problem has optimal cost 500 and a capacity constraint with multiplier λ∗=12\lambda^* = 12. Estimate the cost if capacity is increased by 3 units.

Answer

About 500−36=464500 - 36 = 464.

Solution

Relaxing the constraint by u=3u = 3 changes the cost by about −λ∗u=−36-\lambda^*u = -36. For a convex problem, 464 is a lower bound on the new cost in any case.

Exercise 2: Slack constraint

In the production plan, why is the material shadow price exactly zero?

Answer

Because material is not used up (positive slack), so by complementary slackness its multiplier is zero.

Solution

λi∗gi(x∗)=0\lambda_i^*g_i(\mathbf{x}^*) = 0 with gi<0g_i < 0 forces λi∗=0\lambda_i^* = 0. A small change in the material limit does not change the optimal plan.

Exercise 3: Labour price (code)

Check the labour shadow price by solving the LP with 81 labour hours.

Answer

The profit rises by the labour shadow price.

Solution
python · try it yourself
import numpy as np
from scipy.optimize import linprog
profit = np.array([20.0, 30.0, 25.0]); A = np.array([[2.0, 3.0, 2.0], [1.0, 2.0, 2.0], [3.0, 2.0, 2.0]])
f = lambda lab: -linprog(-profit, A_ub=A, b_ub=[100.0, lab, 150.0], bounds=[(0, None)] * 3).fun
print(round(f(81) - f(80), 4))
5.0

Test yourself

Under strong duality, the multiplier λi∗\lambda_i^* of fi(x)≤uif_i(\mathbf{x}) \le u_i equals…

  1. ∂p∗/∂ui\partial p^*/\partial u_i
  2. −∂p∗/∂ui-\partial p^*/\partial u_i
  3. p∗p^*
  4. the number of active constraints

A resource with positive slack at the optimum has shadow price…

  1. positive
  2. negative
  3. zero
  4. infinite

For an LP, the optimal value as a function of a right-hand side is…

  1. quadratic
  2. piecewise linear and convex (for minimization)
  3. constant
  4. discontinuous everywhere