Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 13 Solutions: Decision Tree Calculator

Open In Colab

Lab 13 Solutions: Decision Tree Calculator

These are the worked solutions, with outputs. Try the lab yourself first.

In the reading, decision analysis told Qahwa House’s owner not to buy a survey and showed how a risk-averse owner would choose a smaller kiosk. Now you build your own decision-tree calculator and use it on a bigger decision.

The question. Qahwa House can sell its cold brew in bottles in supermarkets. It can launch nationally, launch in Riyadh only, or not launch. First-year demand may be Strong, Moderate or Weak. Before deciding, it can run a pilot in 10 Riyadh stores for 40,000 SAR. Should it run the pilot, and what should it launch?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part D, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

Loading...
ColumnMeaning
casewhich decision: kiosk (the reading) or cold_brew (the lab)
alternativean option the decision maker can choose
statea state of the world: what may happen, which nobody controls
priorthe probability of the state, before any new information
p_positivethe probability that a study (a survey or a pilot) gives a positive result if this state is the true one
units, unit_margin_sar, fixed_cost_sarhow the profit is built: units sold × margin per unit − fixed cost
profit_sarthe payoff: profit of the alternative in this state, in SAR

Profits are for the first year. A national launch has large fixed costs (supermarket listing fees, national marketing and a co-packer’s minimum order: 1.1 million SAR), a Riyadh launch much smaller ones (300,000 SAR). The pilot’s record from similar products: it came out positive for 85% of products whose demand then proved strong, 50% of moderate ones and 15% of weak ones (p_positive).

Part A: The payoff table and simple criteria

A1. The payoff table

Pivot brew into payoff_table: one row per alternative (in the order ALTS), one column per state (in the order STATES), values profit_sar divided by 1,000 (thousand SAR).

Loading...
✅ A1 shape and order
✅ A1 a national launch loses 500 thousand SAR if demand is weak

A2. Three rules without probabilities

Compute the regret table regret (best payoff in each state minus each payoff), then the alternative chosen by maximax, by maximin and by minimax regret, as labels in choice_maximax, choice_maximin and choice_regret.

state        Strong  Moderate   Weak
alternative                         
National        0.0       0.0  500.0
Riyadh        550.0     100.0  100.0
No launch     900.0     250.0    0.0 

National No launch National
✅ A2 maximax
✅ A2 maximin
✅ A2 minimax regret

A3. Expected monetary value

Store the prior probability of each state (in the order STATES) in a Series prior, the EMV of each alternative in emv, and the label of the best one in best_emv.

alternative
National     187.5
Riyadh       125.0
No launch      0.0
dtype: float64 
best: National
✅ A3 the EMVs
✅ A3 the EMV choice

Part B: A decision tree calculator

A tree is built from three kinds of node, as nested dictionaries. Run the cell below: it defines them.

B1. Folding back

Complete rollback(node): a chance node is worth the probability-weighted sum of its children’s values; a decision node is worth its best option, and stores that option’s label in node["best"]. The test tree compares a sure 50 with a coin flip for 120 or 0.

60.0 coin
✅ B1 the coin flip is worth 60
✅ B1 and it is the better option

B2. The launch tree

Complete launch_choice(probs): a decision node whose options are the three alternatives, each followed by a chance node with one branch (state, probability, payoff(...)) per state. Fold it back: it must give the same answer as the EMV table.

187.5 National
✅ B2 the tree's value is the best EMV
✅ B2 the tree chooses a national launch

Part C: Is the pilot worth 40,000 SAR?

C1. The value of perfect information

Compute ev_perfect, the expected value with a perfect forecast (the best payoff in each state, weighted by its prior), and evpi.

337.5
 150.0
✅ C1 EVPI
✅ C1 so the pilot (40) is under the ceiling

C2. Bayes’ rule

Complete posterior(prior, likelihood): the joint probability of each state and the result, the probability of the result, and the updated probabilities. Then compute them for a positive and for a negative pilot.

0.4825 {'Strong': 0.4404, 'Moderate': 0.4663, 'Weak': 0.0933} {'Strong': 0.0725, 'Moderate': 0.4348, 'Weak': 0.4928}
✅ C2 P(positive pilot)
✅ C2 after a positive pilot, strong demand is much more likely
✅ C2 after a negative pilot, weak demand is the most likely state

C3. The full tree

The function below returns a copy of a subtree with every payoff changed by an amount: use it to charge the pilot’s price. Build full: a decision between No pilot (launch_choice(prior)) and Pilot, a chance node with a positive and a negative branch, each followed by the launch decision with the updated probabilities. Fold it back, then compute evsi.

{'No pilot': 187.5, 'Pilot': 206.38} Pilot National Riyadh 58.88
✅ C3 run the pilot
✅ C3 after a positive pilot: national; after a negative one: Riyadh
✅ C3 EVSI

C4. In the reading, the kiosk survey was not worth its price; here the pilot is. In one or two sentences, explain why, using EVSI, the price and what the study’s result changes in the decision.

The pilot is worth 58.9 thousand SAR (EVSI) and costs 40, so it adds about 18.9 in expectation; the kiosk survey was worth only 20.6 and cost 25. The pilot pays because its result changes a very large decision: after a negative pilot the company launches in Riyadh only instead of nationally, avoiding much of the national launch’s 500-thousand-SAR loss when demand is weak, and that loss is far bigger than anything at stake in the kiosk decision.

Part D: The owner’s risk attitude

D1. Certainty equivalents

The owner’s risk tolerance is RR = 400 thousand SAR. Complete certainty_equivalent(values, probs, R) for the exponential utility U(x)=1−e−x/RU(x) = 1 - e^{-x/R}: compute the expected utility, then return CE=−Rln⁡(1−E[U])\text{CE} = -R \ln(1 - E[U]).

National   -109.3
Riyadh       89.9
dtype: float64
✅ D1 the national launch's CE is negative
✅ D1 the Riyadh launch's CE
✅ D1 a risk-averse owner launches in Riyadh only

D2. Your recommendation

Write three sentences for the owner: what the EMV analysis recommends, with its expected profit; whether to run the pilot and what to do after each result; and how the advice changes for an owner with a risk tolerance of 400 thousand SAR, with the certainty equivalents.

On expected value, Qahwa House should not launch blind but run the 40,000-SAR pilot: the pilot raises the expected first-year profit from 187.5 thousand SAR (a national launch straight away) to 206.4 thousand after its price. If the pilot is positive, launch nationally; if it is negative, launch in Riyadh only. An owner with a risk tolerance of 400 thousand SAR should however avoid the national launch without more evidence: its certainty equivalent is −109.3 thousand SAR against +89.9 for a Riyadh launch, because a weak market would cost the national launch 500 thousand.

★ Extended challenges

E1. How robust is the national launch? Keep the ratio of Strong to Moderate at 25 : 45 and let pp = P(Weak) vary from 0 to 0.8. Above which pp does a Riyadh launch have a higher EMV than a national one? Plot both EMVs against pp.

E2. How risk-tolerant must the owner be? Find the risk tolerance RR above which the national launch has a higher certainty equivalent than the Riyadh launch (no pilot).

E3. A worse pilot. Suppose the pilot comes out positive for 40% of weak products instead of 15% (the other hit rates unchanged). What is its EVSI now, and is it still worth 40,000 SAR?

Riyadh beats National once P(Weak) is above about 0.395
<Figure size 700x320 with 1 Axes>

The national launch keeps the higher EMV only while weak demand is less likely than about 0.395; the team’s estimate is 0.30. Like the kiosk, the choice is not very robust, which is another argument for the pilot: it is exactly the probability of a weak market that the pilot helps to pin down.

National needs a risk tolerance above about 1932 thousand SAR

About 1,932 thousand SAR, nearly five times the owner’s 400. Only a decision maker who would accept a 50-50 gamble to win about 1.9 million or lose about 1 million SAR would launch nationally without a pilot.

EVSI with the worse pilot: 28.9 thousand SAR (price 40)

The EVSI falls to about 28.9 thousand SAR, below the 40-thousand price: the worse pilot is no longer worth running. Raising its false positives on weak products from 15% to 40% takes away about half of the pilot’s value, because a false positive leads to a national launch in a weak market, the one outcome the pilot exists to prevent.