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: Decision Tree Calculator

Open In Colab

Lab 13: Decision Tree Calculator

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.

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).

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.

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.

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.

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.

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.

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.

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.

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.

Your answer here (double-click to edit).

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]).

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.

Write your recommendation here (double-click to edit).

★ 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?

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.