Binomial and Poisson Rare Event Tail Probabilities
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Computes the exact binomial tail probability P(X > k) and its Poisson approximation parameterised by lambda = n * p.
Given a total number of independent trials $n$, a success probability $p$ for each trial, and a count cutoff threshold $k$, calculate the tail probability $P(X > k)$ using both:
- The exact Binomial distribution: $X \sim \text{Binomial}(n, p)$
- The approximate Poisson distribution: $Y \sim \text{Poisson}(\lambda)$, where $\lambda = n \cdot p$
Return a list of two floating-point numbers: [binomial_tail, poisson_tail].
Details and Edge Cases
-
Binomial Tail Probability $P(X > k)$:
- If $k < 0$, $P(X > k) = 1.0$.
- If $k \ge n$, $P(X > k) = 0.0$.
- For $0 \le k < n$, $P(X > k) = \sum_{j=k+1}^{n} \binom{n}{j} p^j (1-p)^{n-j}$.
- To maintain numerical stability when $n$ is large, compute terms without naive factorial evaluation (e.g., using log-gamma or iterative recurrence transitions between consecutive PMF terms).
-
Poisson Tail Probability $P(Y > k)$:
- Parameterize with rate $\lambda = n \cdot p$.
- Compute the CDF up to $k$: $P(Y \le k) = \sum_{j=0}^{k} \frac{e^{-\lambda} \lambda^j}{j!}$ (for $k < 0$, $P(Y \le k) = 0.0$).
- Return $P(Y > k) = \max(0.0, 1.0 - P(Y \le k))$.
Constraints
- $1 \le n \le 10^7$
- $0.0 \le p \le 1.0$
- $-10 \le k \le 10^7$
Examples
Example 1
Input: rare_event_binomial_probability(10000, 0.0005, 5)
Output: [0.3840393415050155, 0.38403934516693683]
Hints
Hint 1
For the binomial tail, sum from k + 1 up to n using ratio updates between consecutive terms to avoid overflow.
Hint 2
For the Poisson tail, compute the cumulative distribution function up to k using iterative term updates, then subtract from 1.0.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to evaluate the upper tail probability $P(X > k)$ under both the exact Binomial distribution and the Poisson approximation.
1. Binomial Distribution
To compute $P(X > k)$ accurately without floating-point overflow or underflow:
- Handle extreme inputs such as $k < 0$ (probability is $1.0$), $k \ge n$ (probability is $0.0$), $p=0.0$, or $p=1.0$.
- For standard values, compute the logarithm of the probability at $j = k + 1$: $$\log P(X = k+1) = \ln\Gamma(n+1) - \ln\Gamma(k+2) - \ln\Gamma(n-k) + (k+1)\ln p + (n-k-1)\ln(1-p)$$
- Next, iteratively accumulate the terms $j = k+2, \dots, n$ using the recurrences: $$P(X = j+1) = P(X = j) \cdot \frac{n - j}{j + 1} \cdot \frac{p}{1 - p}$$
2. Poisson Distribution
Setting $\lambda = n \cdot p$:
- Compute the cumulative sum from $j=0$ up to $k$: $$P(Y \le k) = \sum_{j=0}^{k} \frac{e^{-\lambda} \lambda^j}{j!}$$
- Start with term $j=0$ equal to $e^{-\lambda}$, then update terms iteratively via $T_j = T_{j-1} \cdot \frac{\lambda}{j}$.
- The tail probability is given by $1.0 - P(Y \le k)$ (bounded below by $0.0$).
Complexity
- Time Complexity:
- Binomial tail: $O(n - k)$ steps.
- Poisson tail: $O(k)$ steps.
- Overall time complexity is $O(n)$.
- Space Complexity: $O(1)$ auxiliary space.
import math
from typing import List
def rare_event_binomial_probability(n: int, p: float, threshold: int) -> List[float]:
if threshold < 0:
return [1.0, 1.0]
if p == 0.0:
binomial_tail = 0.0
elif p == 1.0:
binomial_tail = 1.0 if threshold < n else 0.0
elif threshold >= n:
binomial_tail = 0.0
else:
k = threshold + 1
log_term = (
math.lgamma(n + 1)
- math.lgamma(k + 1)
- math.lgamma(n - k + 1)
+ k * math.log(p)
+ (n - k) * math.log1p(-p)
)
term = math.exp(log_term) if log_term > -745 else 0.0
binomial_tail = term
while k < n:
term *= ((n - k) / (k + 1)) * (p / (1 - p))
binomial_tail += term
k += 1
lam = n * p
poisson_term = math.exp(-lam)
poisson_cdf = poisson_term
for k in range(1, threshold + 1):
poisson_term *= lam / k
poisson_cdf += poisson_term
poisson_tail = max(0.0, 1.0 - poisson_cdf)
return [binomial_tail, poisson_tail]