BRAGA

02 Writing02-02 Notes02-02-399

Negative Binomial Distribution (Trials to n successes)

Published 16 Apr 20264 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
P(X=x)=(x−1n−1)pn(1−p)x−nP(X=x) = \binom{x-1}{n-1} p^n (1-p)^{x-n}
(1)

Deep Breakdown

What it tells me (Situation Awareness): The Negative Binomial distribution models the number of trials XX needed to achieve exactly nn successes, where each trial has success probability pp. It is also used as a generalization of Poisson for overdispersed count data (where variance > mean). In DA/DS: customer churn modeling (trials to first churn = Geometric = NB with n=1n=1), overdispersed web traffic modeling, and count regression.

Flowchart

Run trials untiln-th successLast trial mustbe a successFirst x-1 trials:exactly n-1successes in x-1trialsC(x-1, n-1) *p^(n-1) *(1-p)^(x-n)Last trial(success): * pP(X=x) =C(x-1,n-1) * p^n* (1-p)^(x-n)

Real-world use

Where it is used

Overdispersed count data (NB regression as alternative to Poisson when Var > Mean). Sales cycles: number of calls to close nn deals. User behavior: number of page views before nn conversions.

Problem it solves

Models count data where the variance exceeds the mean (overdispersion), which is common in web analytics, marketing, and biology where Poisson is too restrictive.

How it solves the problem

Adds an extra dispersion parameter compared to Poisson: Var[X]=μ+μ2/rVar[X] = \mu + \mu^2/r (for the rr-parameterized version), allowing mean and variance to vary independently.

Example in a business

Support tickets per user per month: if E[X]=2E[X] = 2 but Var[X]=6Var[X] = 6 (overdispersed vs Poisson where Var=E=2Var = E = 2), NB regression fits better than Poisson regression.

The formula, piece by piece

P(X=x)=(x−1n−1)pn(1−p)x−nP(X=x) = \binom{x-1}{n-1} p^n (1-p)^{x-n} for x=n,n+1,n+2,…x = n, n+1, n+2, \ldots

  • nn: number of required successes.
  • pp: probability of success per trial.
  • xx: total trials (must be ≥n\geq n).
  • (x−1n−1)\binom{x-1}{n-1}: ways to arrange n−1n-1 successes in first x−1x-1 trials.
  • pn(1−p)x−np^n (1-p)^{x-n}: probability of exactly nn successes and x−nx-n failures.

The formula in words

"The probability of needing exactly xx trials to get nn successes — the last trial must be the nn-th success."

Critical Concepts

  • E[X]=n/pE[X] = n/p. Var[X]=n(1−p)/p2Var[X] = n(1-p)/p^2.
  • Special case n=1n=1: Geometric distribution.
  • NB for counts: parameterized as P(Y=k)=(k+r−1k)(1−p)kprP(Y=k) = \binom{k+r-1}{k}(1-p)^k p^r — where YY counts failures.
  • Python: scipy.stats.nbinom(n, p).

Step-by-step calculation (tiny example)

Step 1: n=2 successes needed, p=0.4, query P(X=4) Need exactly 4 trials to get 2nd success (last trial is success #2).

Step 2: Apply formula P(X=4)=(31)(0.4)2(0.6)2=3×0.16×0.36=0.1728P(X=4) = \binom{3}{1}(0.4)^2(0.6)^2 = 3 \times 0.16 \times 0.36 = 0.1728

Step 3: Interpret 17.28% chance of needing exactly 4 trials to achieve 2 successes.

Code examples

Beginner example (Python)

python
from scipy.stats import nbinom
import numpy as np
n_successes = 2
p_success = 0.4
x_values = range(2, 12)
for x in x_values:
    # scipy nbinom: P(k failures before n successes) -> shift by n
    pmf = nbinom.pmf(x - n_successes, n_successes, p_success)
print(f"P(X={x:2d}) = {pmf:.6f}")

Beginner example (R)

r
n_succ <- 2; p <- 0.4
for (x in 2:11) {  # R's dnbinom: k failures before n successes, so
  x_failures = x - n
  pmf <- dnbinom(x - n_succ, size=n_succ, prob=p)
  cat(sprintf("P(X=%2d) = %.6f\n", x, pmf))
}

Professional example (Python)

python
import numpy as np
from scipy.stats import nbinom, poisson
from scipy.special import gammaln

def fit_negative_binomial(counts: np.ndarray) -> dict[str, float]:
    """Fit Negative Binomial distribution via MOM (Method of Moments). Uses: mean = μ, variance = μ + μ²/r → r = μ²/(var - μ). Args: counts: Non-negative integer count data. Returns: Dict with fitted r, p parameters and overdispersion check. """
    mu = counts.mean()
    var = counts.var()
    if var <= mu:
        return {"poisson_adequate": True, "message": "No overdispersion detected"}
    r = mu**2 / (var - mu)
    p = r / (r + mu)   # scipy parameterization
    return { "r": float(r), "p": float(p), "fitted_mean": float(mu), "fitted_var": float(var), "overdispersion_index": float(var / mu), "poisson_adequate": False, }
rng = np.random.default_rng(seed=42)
# Overdispersed count data
count_data = rng.negative_binomial(n=3, p=0.4, size=5_000)
result = fit_negative_binomial(count_data)
print("Negative Binomial fit:")
for key, val in result.items():
    print(f" {key}: {round(val, 4) if isinstance(val, float) else val}")

Source: domain knowledge

#probability-statistics#statistical-operations-and-random-variables#common-discrete-distributions

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close