Calculate Bag Selection Probability via Bayes' Theorem
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Given selection priors and ball counts for several containers, calculate the posterior probability of drawing from a specific container after observing a target outcome.
You are analyzing a set of containers, each filled with two types of objects: target objects (e.g., red balls) and non-target objects.
You are given three arrays of equal length representing the contents and selection probabilities of these containers:
red_counts: An array of integers wherered_counts[i]represents the number of target objects in container $i$.total_counts: An array of integers wheretotal_counts[i]represents the total number of objects in container $i$.priors: An array of numbers wherepriors[i]represents the prior probability of selecting container $i$. Note that priors do not necessarily sum to 1.0; they should be treated as proportional weights.
A container is selected at random according to the given priors, and then one object is drawn uniformly at random from that container.
Given that the selected object is a target object (red), compute the posterior probability that it originated from container index 1 (the second container, equivalent to "Bag B").
Constraints
len(red_counts) == len(total_counts) == len(priors)len(red_counts) >= 2- $0 \le \text{red_counts}[i] \le \text{total_counts}[i]$
- $\text{total_counts}[i] > 0$ for all $i$.
- $\text{priors}[i] \ge 0$ for all $i$, and at least one container with $\text{red_counts}[i] > 0$ has $\text{priors}[i] > 0$.
Examples
Example 1
Input: posterior_b_probability([4, 6, 3], [10, 10, 10], [1, 1, 1])
Output: 0.4615384615384615
Example 2
Input: posterior_b_probability([4, 6, 3], [10, 10, 10], [0.2, 0.5, 0.3])
Output: 0.6382978723404256
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can calculate the required posterior probability using Bayes' Theorem and the Law of Total Probability.
-
Likelihood: For container $i$, the likelihood of drawing a red object given that container $i$ was chosen is: $$P(\text{Red} \mid \text{Container } i) = \frac{\text{red_counts}[i]}{\text{total_counts}[i]}$$
-
Joint Probability: The unnormalized joint probability of selecting container $i$ and drawing a red object is: $$\text{Joint}[i] = \text{priors}[i] \times P(\text{Red} \mid \text{Container } i)$$
-
Evidence (Total Probability): The overall probability of drawing a red object across all containers is the sum of the joint probabilities: $$P(\text{Red}) = \sum_{i} \text{Joint}[i]$$
-
Posterior Probability: Using Bayes' Theorem, the posterior probability that container index 1 was selected given a red object was drawn is: $$P(\text{Container 1} \mid \text{Red}) = \frac{\text{Joint}[1]}{P(\text{Red})}$$
Because constant factors cancel out in the ratio, normalizing priors upfront is not strictly necessary.
Complexity
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the number of containers. We iterate through the arrays once to calculate joint probabilities and sum them.
- Space Complexity: $\mathcal{O}(N)$ (or $\mathcal{O}(1)$ if computed in a streaming fashion) to store the joint probabilities.
Python Source Code
from typing import List
def posterior_b_probability(red_counts: List[int], total_counts: List[int], priors: List[float]) -> float:
joint_probs = [p * (r / t) for p, r, t in zip(priors, red_counts, total_counts)]
total_red_prob = sum(joint_probs)
return joint_probs[1] / total_red_prob