Loading content...
The Negative Binomial distribution is a powerful discrete probability model that captures the number of failures encountered before a predetermined number of successes is achieved in a sequence of independent Bernoulli trials. Unlike its close relative, the Binomial distribution (which counts successes in a fixed number of trials), the Negative Binomial answers a fundamentally different question: "How many failures will I observe before I succeed r times?"
This distribution is exceptionally valuable in machine learning and data science for modeling overdispersed count data—scenarios where the variance significantly exceeds the mean, which violates the assumptions of simpler models like the Poisson distribution. Such overdispersion naturally arises in phenomena with inherent clustering or heterogeneous success rates.
For a random variable X representing the number of failures before achieving r successes, where each trial has success probability p, the Probability Mass Function (PMF) is:
$$P(X = k) = \binom{k + r - 1}{k} \cdot p^r \cdot (1 - p)^k$$
Where:
The formula can be understood as follows:
Your Task: Write a function that computes the probability of observing exactly a specified number of failures before achieving a target number of successes in independent trials with a given success probability. Return the result rounded to 5 decimal places.
num_failures = 3, target_successes = 2, success_prob = 0.50.125We seek the probability of exactly 3 failures before achieving 2 successes with a 50% success rate per trial.
Step-by-step calculation:
Computing the binomial coefficient: $$\binom{k + r - 1}{k} = \binom{3 + 2 - 1}{3} = \binom{4}{3} = 4$$
Applying the PMF formula: $$P(X = 3) = 4 \times 0.5^2 \times 0.5^3 = 4 \times 0.25 \times 0.125 = 0.125$$
The 4 valid sequences are: FFFSFS, FFSFS, FSFFS, SFFFS (Each with F = Failure and ending with the 2nd Success)
num_failures = 0, target_successes = 1, success_prob = 0.50.5This represents the simplest case: achieving exactly 1 success with 0 failures means succeeding on the very first trial.
Calculation: $$\binom{0 + 1 - 1}{0} = \binom{0}{0} = 1$$ $$P(X = 0) = 1 \times 0.5^1 \times 0.5^0 = 0.5 \times 1 = 0.5$$
This reduces to a single Bernoulli trial: P(first trial success) = p = 0.5
num_failures = 5, target_successes = 3, success_prob = 0.40.10451We want exactly 5 failures before achieving 3 successes with 40% success probability.
Calculation:
$$\binom{5 + 3 - 1}{5} = \binom{7}{5} = \frac{7!}{5! \times 2!} = 21$$
$$P(X = 5) = 21 \times 0.4^3 \times 0.6^5$$ $$= 21 \times 0.064 \times 0.07776$$ $$= 21 \times 0.00497664$$ $$= 0.10450944 ≈ 0.10451$$
Constraints