KatInterview

Binomial and Poisson Rare Event Tail Probabilities

Math & Bit ManipulationAuto-graded · 21 test cases
EasyAuto-gradedMath & Bit Manipulationmathstatisticsprobability

Last 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:

  1. The exact Binomial distribution: $X \sim \text{Binomial}(n, p)$
  2. 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.