Posterior Probability of Facility Given Red Samples
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests Bayesian reasoning: computing the posterior probability of a hypothesis using likelihoods and equal priors.
Two manufacturing facilities produce identical components in red and black colors.
- Facility 1 outputs
apercent red parts and(100-a)percent black parts. - Facility 2 outputs
bpercent red parts and(100-b)percent black parts.
You randomly choose one facility with equal probability (50% chance for each) and draw k parts from it. All k parts are red.
Calculate the probability that you chose Facility 1, given this observation.
Input
a: integer percentage (0-100) of red parts produced by Facility 1b: integer percentage (0-100) of red parts produced by Facility 2k: positive integer, number of parts drawn
Output
Return a float: the conditional probability that Facility 1 was chosen, given that all k drawn parts are red.
Constraints
- 0 ≤
a≤ 100 - 0 ≤
b≤ 100 k≥ 1- Each facility has unlimited parts; treat successive draws as independent
- If both facilities cannot produce red parts (
a = 0andb = 0), return0.0 - If both facilities have the same red rate (
a = b), return0.5(the prior remains unchanged)
Examples
Example 1
Input: facility_posterior(60, 40, 1)
Output: 0.6
Example 2
Input: facility_posterior(60, 40, 2)
Output: 0.6923076923076923
Example 3
Input: facility_posterior(30, 70, 3)
Output: 0.07297297297297299
Hints
Hint 1
Apply Bayes' theorem: the posterior probability is proportional to the likelihood times the prior.
Hint 2
The probability of drawing k red parts from a facility with red rate p is p^k.
Hint 3
Since both facilities start with equal probability, the posterior ratio depends only on the likelihood ratio.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem applies Bayes' theorem to update our initial belief about which facility was selected after observing that all drawn parts are red.
We begin with equal priors: each facility has probability 0.5 of being chosen.
The likelihood of observing k red parts from each facility is:
- From Facility 1: (a/100)^k
- From Facility 2: (b/100)^k
By Bayes' theorem, the posterior probability is:
P(Facility 1 | k red parts) = P(k red | Facility 1) × P(Facility 1) / P(k red parts)
The total probability of observing k red parts is: P(k red parts) = P(k red | Facility 1) × 0.5 + P(k red | Facility 2) × 0.5
Since both priors equal 0.5, they cancel in the computation:
P(Facility 1 | k red) = (a/100)^k / [(a/100)^k + (b/100)^k]
Why This Works
The formula automatically handles several edge cases:
- When a = b, both likelihoods are identical, so the ratio is 0.5 (no update from prior)
- When one facility cannot produce red parts (rate = 0), its likelihood is 0, giving all weight to the other facility
- When both rates are 0, the denominator is 0, and we return 0.0 by convention
Complexity
- Time: O(log k) for exponentiation (or O(1) with built-in power operator)
- Space: O(1)
Solution
def facility_posterior(a: int, b: int, k: int) -> float:
likelihood_1 = (a / 100) ** k
likelihood_2 = (b / 100) ** k
denominator = likelihood_1 + likelihood_2
if denominator == 0:
return 0.0
return likelihood_1 / denominator