KatInterview

Posterior for the Favorite After Your Entry Loses

Math & Bit ManipulationAuto-graded · 19 test cases
EasyAuto-gradedMath & Bit Manipulationprobabilitybayes-ruleconditional-probability

Last 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 <= 1
  • p1 + p2 + p3 = 1 (up to floating-point rounding)
  • p1 is 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.