Show Negative Binomial Discrete Distribution in Statistics using Python

The Negative Binomial Distribution represents the number of failures that occur before achieving a fixed number of successes in a series of independent trials. We can visualize this distribution using Python's NumPy and Matplotlib libraries.

What is Negative Binomial Distribution?

The Negative Binomial Distribution models scenarios where we count failures before reaching a target number of successes. For example, how many coin flips result in tails before getting 5 heads?

The distribution has two key parameters:

  • r Number of successes required
  • p Probability of success on each trial

Basic Example

Let's generate and visualize a negative binomial distribution with r=5 successes and p=0.3 probability ?

import numpy as np
import matplotlib.pyplot as plt

# Parameters
r = 5  # number of successes needed
p = 0.3  # probability of success

# Generate 1000 random samples
samples = np.random.negative_binomial(r, p, size=1000)

# Create histogram
plt.figure(figsize=(10, 6))
plt.hist(samples, bins=30, density=True, alpha=0.7, color='skyblue', edgecolor='black')
plt.xlabel('Number of Failures')
plt.ylabel('Probability Density')
plt.title(f'Negative Binomial Distribution (r={r}, p={p})')
plt.grid(True, alpha=0.3)
plt.show()

print(f"Mean number of failures: {np.mean(samples):.2f}")
print(f"Standard deviation: {np.std(samples):.2f}")
Mean number of failures: 11.67
Standard deviation: 6.24

Comparing Different Parameters

Let's compare how different parameter values affect the distribution shape ?

import numpy as np
import matplotlib.pyplot as plt

# Different parameter combinations
params = [(5, 0.2), (5, 0.5), (10, 0.3)]
colors = ['red', 'blue', 'green']

plt.figure(figsize=(12, 6))

for i, (r, p) in enumerate(params):
    samples = np.random.negative_binomial(r, p, size=5000)
    plt.hist(samples, bins=50, density=True, alpha=0.6, 
             color=colors[i], label=f'r={r}, p={p}')

plt.xlabel('Number of Failures')
plt.ylabel('Probability Density')
plt.title('Negative Binomial Distribution - Parameter Comparison')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Statistical Properties

The negative binomial distribution has these key properties ?

import numpy as np

def neg_binomial_stats(r, p):
    """Calculate theoretical statistics for negative binomial distribution"""
    mean = r * (1 - p) / p
    variance = r * (1 - p) / (p ** 2)
    std = np.sqrt(variance)
    
    return mean, variance, std

# Calculate for different parameters
parameters = [(3, 0.2), (5, 0.4), (8, 0.6)]

print("Negative Binomial Distribution Statistics:")
print("-" * 50)
print(f"{'r':<3} {'p':<5} {'Mean':<8} {'Variance':<10} {'Std Dev':<8}")
print("-" * 50)

for r, p in parameters:
    mean, variance, std = neg_binomial_stats(r, p)
    print(f"{r:<3} {p:<5} {mean:<8.2f} {variance:<10.2f} {std:<8.2f}")
Negative Binomial Distribution Statistics:
--------------------------------------------------
r   p     Mean     Variance   Std Dev 
--------------------------------------------------
3   0.2   12.00    60.00      7.75    
5   0.4   7.50     18.75      4.33    
8   0.6   5.33     8.89       2.98    

Comparison with Related Distributions

Distribution What it Models Key Difference
Negative Binomial Failures before r successes Fixed number of successes
Binomial Successes in n trials Fixed number of trials
Geometric Trials until first success Special case (r=1)

Conclusion

The Negative Binomial Distribution models the number of failures before achieving a target number of successes. Use NumPy's random.negative_binomial() to generate samples and Matplotlib to visualize the distribution patterns.

Updated on: 2026-03-27T15:43:04+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements