Tight Probability Bounds for the Union of Two Events
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests whether a candidate can derive the Fréchet bounds on P(A ∪ B) given only the marginal probabilities of two events with unspecified dependence.
You are given two events $A$ and $B$ with known marginal probabilities $P(A) = p$ and $P(B) = q$. The relationship between the two events — whether they are independent, positively correlated, or negatively correlated — is completely unspecified.
Your task is to find the tightest possible range for $P(A \cup B)$: the smallest value it could take and the largest value it could take, over all valid joint distributions that are consistent with the two marginals.
Return both endpoints as a list [lower, upper].
A bound is considered tight only if there exists a real joint distribution that actually achieves it. Loose or approximate bounds are not accepted.
Background
By inclusion–exclusion:
$$P(A \cup B) = p + q - P(A \cap B)$$
The marginals $p$ and $q$ are fixed. The only free quantity is the overlap $P(A \cap B)$. Finding the range of $P(A \cup B)$ reduces to finding the range of the overlap, because the two are linearly related (and in opposite directions).
What to Return
Return a list of two floats [lower_bound, upper_bound] where:
lower_boundis the smallest possible value of $P(A \cup B)$upper_boundis the largest possible value of $P(A \cup B)$
Both values must be tight: each must be achievable by some valid joint distribution with marginals $p$ and $q$.
Constraints
- $0.0 \le p \le 1.0$
- $0.0 \le q \le 1.0$
- $p$ and $q$ are given as floats
- No assumption is made about the dependence structure between $A$ and $B$
- The returned bounds must be exact (no approximation)
- Return type is
List[float]with exactly two elements:[lower, upper]
Examples
Example 1
Input: union_probability_bounds(0.4, 0.3)
Output: [0.4, 0.7]
Example 2
Input: union_probability_bounds(0.7, 0.6)
Output: [0.7, 1]
Example 3
Input: union_probability_bounds(0.2, 0.2)
Output: [0.2, 0.4]
Hints
Hint 1
Use inclusion–exclusion: P(A ∪ B) = p + q − P(A ∩ B). The union is fully determined by the overlap, so bounding the union is equivalent to bounding P(A ∩ B).
Hint 2
For the overlap, think about what constraints probability axioms impose: the overlap cannot be negative, cannot exceed either marginal, and P(A ∪ B) cannot exceed 1. Each of those constraints pins one endpoint of the overlap's range.
Hint 3
The lower bound on the union has an intuitive reading: the union must contain each individual event, so it is at least as likely as the more probable of the two. The upper bound is the union bound, capped at 1.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is inclusion–exclusion:
$$P(A \cup B) = p + q - P(A \cap B)$$
With $p$ and $q$ fixed, the union is entirely determined by the overlap $x = P(A \cap B)$, and the relationship is linear with a negative slope. So:
- maximizing the union requires minimizing the overlap
- minimizing the union requires maximizing the overlap
Bounding the overlap (Fréchet inequalities)
Probability axioms give three constraints on $x$:
- $x \ge 0$ (an overlap is a probability)
- $x \le p$ and $x \le q$ (the intersection cannot exceed either marginal), so $x \le \min(p, q)$
- $P(A \cup B) \le 1$, so $p + q - x \le 1$, which means $x \ge p + q - 1$
Combining (1) and (3): $x \ge \max(0,; p + q - 1)$
The feasible range for the overlap is therefore $[\max(0,; p+q-1),; \min(p, q)]$.
Translating back to the union
Substituting the two extreme overlap values into $p + q - x$:
- Lower bound (largest overlap $\min(p,q)$): $\quad p + q - \min(p,q) = \max(p,q)$
- Upper bound (smallest overlap $\max(0, p+q-1)$): $\quad p + q - \max(0, p+q-1) = \min(1,; p+q)$
$$\boxed{\max(p,,q) ;\le; P(A \cup B) ;\le; \min(1,; p+q)}$$
Why both bounds are tight
You can construct explicit joint distributions that achieve each endpoint using a single uniform random variable $U \sim \text{Uniform}[0, 1]$:
Lower bound — perfectly nested events. Define $A = {U < p}$ and $B = {U < q}$. The smaller event is always contained inside the larger one, so $P(A \cup B) = \max(p, q)$.
Upper bound — events placed as far apart as possible. Define $A = {U < p}$ and $B = {U > 1 - q}$. When $p + q \le 1$ the intervals are disjoint and the union has probability $p + q$. When $p + q > 1$ they overlap by exactly $p + q - 1$ and their union covers the entire $[0,1]$ interval, giving probability $1$.
Since every value of $x$ between its two extremes is also achievable (for example by mixing the two constructions), the entire interval between the bounds is filled in.
Why it is correct
The lower bound $\max(p, q)$ also follows from monotonicity: the union contains $A$ and it contains $B$, so it is at least as probable as the larger of the two.
The upper bound $\min(1, p+q)$ is the classic union bound, which comes from simply adding up the two probabilities and capping at $1$.
Edge cases
- $p = 0$ or $q = 0$: the range collapses to a single point equal to the other marginal.
- $p = q$: the lower bound is $p$ itself, reached when the two events are identical.
- $p + q > 1$: the upper bound is capped at $1$, not $p + q$.
- $p = q = 1$: the range collapses to $[1, 1]$.
Complexity
- Time: $O(1)$
- Space: $O(1)$
from typing import List
def union_probability_bounds(p: float, q: float) -> List[float]:
# By inclusion-exclusion: P(A ∪ B) = p + q - overlap.
# The union is decreasing in the overlap, so:
# - maximum union <-> minimum overlap = max(0, p+q-1)
# => upper = p + q - max(0, p+q-1) = min(1, p+q)
# - minimum union <-> maximum overlap = min(p, q)
# => lower = p + q - min(p, q) = max(p, q)
lower = max(p, q)
upper = min(1.0, p + q)
return [lower, upper]