Posterior for the Favorite After Your Entry Loses
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Apply Bayes' rule to find the chance a randomly chosen contestant was the strongest one, given that it did not win a three-way contest.
Three robots enter a single-winner obstacle race. Exactly one robot wins. The robots are ranked by strength:
- the strongest robot wins with probability
p1, - the middle robot wins with probability
p2, - the weakest robot wins with probability
p3.
Before the race, you choose one of the three robots completely at random, with each robot equally likely to be chosen. Your choice is independent of the race. After the race you learn only that your chosen robot did not win.
Write a function that returns the probability that the robot you chose was the strongest one, given that it lost.
Input
p1: a float, the winning probability of the strongest robot.p2: a float, the winning probability of the middle robot.p3: a float, the winning probability of the weakest robot.
The three values add up to 1.
Output
Return a single float: the conditional probability that your chosen robot was the strongest, given that it lost.
You are told only that your own pick lost. You are not told which robot won.
Constraints
0 <= p1, p2, p3 <= 1p1 + p2 + p3 = 1(up to floating-point rounding)p1is the winning probability of the strongest robot, so the arguments are given in the order strongest, middle, weakest.- Return a plain float. Answers are compared numerically, so ordinary floating-point rounding is acceptable.
Examples
Example 1
Input: prob_strongest_given_loss(0.5, 0.3, 0.2)
Output: 0.25
Example 2
Input: prob_strongest_given_loss(0.6, 0.25, 0.15)
Output: 0.2
Example 3
Input: prob_strongest_given_loss(0.34, 0.33, 0.33)
Output: 0.32999999999999996
Hints
Hint 1
Name the events: which robot you picked, and 'my robot lost'. You want P(picked strongest | lost), which is not the same as P(lost | picked strongest).
Hint 2
Use Bayes' rule. The prior for each pick is 1/3, and the chance that robot i loses is 1 - p_i. Use the law of total probability for the denominator.
Hint 3
Sum the three losing chances and use p1 + p2 + p3 = 1. The overall chance that a random pick loses is a constant that does not depend on the p values, and the whole answer simplifies to a one-line formula.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a direct application of Bayes' rule.
Define these events:
C1,C2,C3: you picked the strongest, middle, or weakest robot.L: the robot you picked lost.
Your pick is uniform and independent of the race, so P(Ci) = 1/3 for each i. If you picked robot i, it loses with probability 1 - pi, so P(L | Ci) = 1 - pi.
We want P(C1 | L):
P(C1 | L) = P(L | C1) * P(C1) / P(L)
The denominator comes from the law of total probability:
P(L) = (1/3) * [(1 - p1) + (1 - p2) + (1 - p3)] = (1/3) * (3 - 1) = 2/3
This uses p1 + p2 + p3 = 1. Exactly one robot wins, so two of the three possible picks are losers, and a random pick loses with probability 2/3 regardless of the p values.
Putting it together:
P(C1 | L) = ((1/3) * (1 - p1)) / (2/3) = (1 - p1) / 2
Why it is correct
The posterior for robot i is (1 - pi) / 2. These three values add up to (3 - 1) / 2 = 1, so they form a valid distribution. The equal priors of 1/3 cancel, so the posterior is just the share of the total "losing mass" (which is 2) that belongs to the strongest robot.
Sanity checks:
- If
p1 = 1, the answer is 0. A losing robot cannot be the one that always wins. - If all three p values are 1/3, the answer is 1/3. The loss tells you nothing, so the posterior equals the prior.
Common mistakes are returning 1 - p1, which is the likelihood and not the posterior, and returning 1/2 by treating the two possible losers as equally likely.
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
def prob_strongest_given_loss(p1, p2, p3):
# Prior for each pick is 1/3.
# P(lost | picked robot i) = 1 - p_i.
# P(lost) = (1/3) * ((1-p1) + (1-p2) + (1-p3)) = (1/3) * 2 = 2/3,
# because exactly one robot wins.
# Posterior = ((1/3) * (1 - p1)) / (2/3) = (1 - p1) / 2.
return (1.0 - p1) / 2.0