KatInterview

Bayesian Origin Probability for Sampled Items

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashingstatisticsprobabilitybayes-theorem

Last updated

Calculate the posterior probability that two black items came from a specific facility using Bayes' Theorem.

Facility A produces items that are 40% red and 60% black. Facility B produces items that are 80% red and 20% black.

One facility is selected uniformly at random. Then, two items are sampled independently and uniformly at random from that chosen facility.

Given that both drawn items turn out to be black, calculate the posterior probability that the chosen facility was Facility A.

Constraints

  • Assume ideal independent draws with replacement (or an infinite production volume).
  • Return the exact calculated probability as a float.

Examples

Example 1

Input: calculate_facility_a_probability()

Output: 0.8999999999999999

Hints

Hint 1

Use Bayes' Theorem: P(Facility A | 2 Black) = P(2 Black | Facility A) * P(Facility A) / P(2 Black).

Hint 2

Because draws are independent given the facility, P(2 Black | Facility) is equal to the single-item black rate squared.

Hint 3

Since both facilities are initially chosen with equal probability (0.5), the prior probabilities cancel out.