Bayesian A/B Test Launch Decision with Cost Tradeoffs
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests whether a candidate can combine Bayesian posterior updating with expected-value analysis to make a statistically grounded product launch decision.
Problem
You are analyzing the result of an A/B test to decide whether to ship a new product feature. The test gives you a p-value and a pre-specified significance level, but a raw p-value alone does not tell you the probability that the feature actually works. You need to combine the test result with your prior belief about success, then weigh the expected financial outcomes of launching versus holding back.
Inputs
You receive the following eight parameters:
p_value— the p-value produced by the A/B testalpha— the significance threshold (e.g. 0.05)prior_benefit— your prior probability that the feature genuinely improves the metric (a value in [0, 1])power— the probability that the test detects a real effect when one exists (1 − β)benefit_npv— net present value of the outcome if the feature truly helps (can be positive or negative)failure_npv— net present value of the outcome if the feature has no real effect or harms the metric (can be positive or negative)rollout_cost— the fixed cost paid whenever you launch, regardless of whether the feature worksrollback_cost— the additional cost incurred only when you launch a feature that turns out not to work (reverting infrastructure, user disruption, etc.)
Task
Return True if launching the feature has a non-negative expected value compared to not launching, and False otherwise.
How to compute the answer
Step 1 — Classify the test result.
The test is significant when p_value <= alpha, and non-significant otherwise.
Step 2 — Choose the likelihoods.
The likelihood of observing this outcome depends on whether the test was significant:
| Effect is real | No real effect | |
|---|---|---|
| Significant result | power | alpha |
| Non-significant result | 1 − power | 1 − alpha |
Step 3 — Update the prior with Bayes' rule.
Compute the posterior probability that the effect is real:
posterior = (prior_benefit × likelihood_real) /
(prior_benefit × likelihood_real + (1 − prior_benefit) × likelihood_null)
If the denominator is zero, treat the posterior as 0.
Step 4 — Compute the expected value of launching.
rollout_cost is always paid when you launch. rollback_cost is only paid when you launch and the feature fails:
EV(launch) = posterior × benefit_npv
+ (1 − posterior) × failure_npv
− rollout_cost
− (1 − posterior) × rollback_cost
The expected value of not launching is 0.
Step 5 — Decide.
Return True when EV(launch) >= 0.
Constraints
0.0 < p_value <= 1.00.0 < alpha <= 1.00.0 <= prior_benefit <= 1.00.0 < power < 1.0benefit_npv,failure_npvare finite floats (may be negative)rollout_cost >= 0.0rollback_cost >= 0.0- All float inputs are well-formed (no NaN, no infinity)
Examples
Example 1
Input: evaluate_feature_launch(0.01, 0.05, 0.2, 0.8, 100000, -30000, 10000, 20000)
Output: True
Example 2
Input: evaluate_feature_launch(0.01, 0.05, 0.5, 0.8, 100000, -30000, 10000, 20000)
Output: True
Example 3
Input: evaluate_feature_launch(0.2, 0.05, 0.2, 0.8, 100000, -30000, 10000, 20000)
Output: False
Hints
Hint 1
A p-value is not the probability that the null hypothesis is true — you need Bayes' rule to convert the test outcome into a posterior probability. The likelihoods differ depending on whether the result was significant or not.
Hint 2
The rollout cost is paid in every launch scenario (both when the feature works and when it doesn't), but the rollback cost only applies when you launch a feature that has no real effect. Make sure you subtract them from the right branches of the expected-value calculation.
Hint 3
Set EV(launch) = 0 and solve for the break-even posterior to build intuition: p* = (rollout_cost + rollback_cost) / (benefit_npv − failure_npv + rollback_cost). When rollback costs are large relative to the upside, you need a much higher posterior before launching.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem has three distinct layers that must be composed in order.
Layer 1 — Convert the test result to a posterior probability
A p-value answers "how surprising is this data if there is no effect?" It says nothing directly about whether the effect exists. To get that probability you need Bayes' rule.
First, decide which likelihoods apply. If the test is significant (p_value <= alpha), the probability of seeing a significant result is power when the effect is real and alpha when it is not. Flip those to 1 − power and 1 − alpha when the test is non-significant. Then:
posterior = (prior × likelihood_real) /
(prior × likelihood_real + (1 − prior) × likelihood_null)
A key insight: even with a significant result, the posterior can be much lower than 1 − alpha when the prior is small. With prior = 0.2, alpha = 0.05, and power = 0.8:
posterior = (0.8 × 0.2) / (0.8 × 0.2 + 0.05 × 0.8) = 0.16 / 0.20 = 0.80
That 20% residual uncertainty is the false discovery rate, and it drives the financial calculation.
Layer 2 — Expected value with asymmetric costs
The no-launch baseline has EV = 0. The expected value of launching is:
EV(launch) = posterior × benefit_npv
+ (1 − posterior) × failure_npv
− rollout_cost
− (1 − posterior) × rollback_cost
Two subtleties matter here:
rollout_costappears unconditionally because it is paid regardless of whether the feature works.rollback_costis multiplied by(1 − posterior)because it is only incurred on the failure branch.
Layer 3 — The decision rule and break-even threshold
Return True when EV(launch) >= 0. Setting EV = 0 and solving for the posterior gives the break-even threshold:
p* = (rollout_cost + rollback_cost) / (benefit_npv − failure_npv + rollback_cost)
When rollback is cheap relative to the upside, you can launch on modest evidence. When rollback is costly, you need a high posterior. This is why the evidence bar should be set by costs, not by an arbitrary α convention.
Why the non-significant branch matters
The third sample call (p_value = 0.2, which is greater than alpha = 0.05) returns False. The posterior after a non-significant result uses likelihoods 1 − power and 1 − alpha, which dramatically shifts weight toward "no real effect," making the expected value negative even though the financial terms are otherwise attractive.
Correctness
The computation is a closed-form application of Bayes' rule followed by a linear expected-value expression — no approximations, no loops. Given the same inputs, it always produces the same result, and it degrades gracefully when the denominator is zero (posterior defaults to 0, which triggers a no-launch decision).
Complexity
- Time: O(1) — a fixed number of arithmetic operations
- Space: O(1) — no data structures beyond scalar variables
Full solution
def evaluate_feature_launch(
p_value: float,
alpha: float,
prior_benefit: float,
power: float,
benefit_npv: float,
failure_npv: float,
rollout_cost: float,
rollback_cost: float,
) -> bool:
# Step 1: was the test significant?
significant = p_value <= alpha
# Step 2: likelihoods of this outcome under each hypothesis
if significant:
likelihood_real = power # P(sig | effect is real)
likelihood_null = alpha # P(sig | no real effect)
else:
likelihood_real = 1.0 - power # P(not sig | effect is real)
likelihood_null = 1.0 - alpha # P(not sig | no real effect)
# Step 3: Bayesian posterior update
numerator = prior_benefit * likelihood_real
denominator = numerator + (1.0 - prior_benefit) * likelihood_null
posterior = numerator / denominator if denominator != 0.0 else 0.0
# Step 4: expected value of launching
# rollout_cost is always paid; rollback_cost is only paid on failure
ev_launch = (
posterior * benefit_npv
+ (1.0 - posterior) * failure_npv
- rollout_cost
- (1.0 - posterior) * rollback_cost
)
# Step 5: launch when EV >= 0 (no-launch baseline is 0)
return ev_launch >= 0.0