Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Shadow prices and sensitivity analysis

After this lesson: I can say how the best profit changes when a limit changes.

55 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

What is one more oven hour worth?

Sensitivity analysis asks how the best answer changes when a number in the model changes. The most useful figure it gives is the shadow price, the value of one more unit of a limit.

A manager can use it to decide whether a second oven shift is worth paying for.

By the end of this lesson

Note

I can say how the best profit changes when a limit changes.

Key words

WordWhat it means
Sensitivityhow much an answer moves when an input moves
Shadow pricethe extra profit from one more unit of a limit
Binding constrainta limit that the best plan uses up exactly
Slackthe part of a limit that the best plan leaves unused

The cells students fill in are left blank in this preview.

A shadow price values one more unit

Suppose the oven could run for 9 hours instead of 8. The baker solves the model again with the new limit and compares the two profits.

The difference is the shadow price of the oven. It says what one extra hour is worth, so it is the most the baker should pay for it.

Solve again with a 9 hour oven

Command
docker exec jupyter python3 -c "from scipy.optimize import linprog; r = linprog([-20, -30], A_ub=[[1, 2], [3, 2]], b_ub=[9, 18]); print(-r.fun)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Solve again with 19 kg of flour

Command
docker exec jupyter python3 -c "from scipy.optimize import linprog; r = linprog([-20, -30], A_ub=[[1, 2], [3, 2]], b_ub=[8, 19]); print(-r.fun)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: the two shadow prices

ChangeBest profitGain over 145Shadow price
Oven 9 hours instead of 8157.512.512.5 pounds an hour
Flour 19 kg instead of 18147.52.52.5 pounds a kilogram

The cells students fill in are left blank in this preview.

The solver reports the shadow prices itself

You do not have to solve twice. The result of linprog holds the prices in ineqlin.marginals, one for each limit row.

The numbers come out negative because the solver minimised the negated profit. Drop the minus sign and you have the shadow prices.

Read the prices from the solver

Command
docker exec jupyter python3 -c "from scipy.optimize import linprog; r = linprog([-20, -30], A_ub=[[1, 2], [3, 2]], b_ub=[8, 18]); print(r.ineqlin.marginals)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a shadow price holds however large the change. It holds only for small changes, while the same limits stay binding. In the bakery the oven price of 12.5 pounds is true from 6 to 18 hours and no further.

Slack means a shadow price of zero

A limit that the best plan does not use up has slack. More of it adds nothing, so its shadow price is 0.

Give the bakery a 20 hour oven and flour becomes the only real limit. The best plan is then 9 cake batches and no loaves, and the oven is never full.

Worked example: the oven price at each size

Oven hoursBest planBest profitValue of the next hour
66 loaves, 0 cakes12012.5
85 loaves, 1.5 cakes14512.5
123 loaves, 4.5 cakes19512.5
180 loaves, 9 cakes2700
200 loaves, 9 cakes2700

The cells students fill in are left blank in this preview.

Going further

Use the table and the price of 12.5 pounds to guess the best profit for an oven of 10 hours. Then check your guess by changing the limit in a command.

Explain why the guess fails for an oven of 30 hours.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.