Binomial Distribution

🎲 Binomial Distribution in Python

The Binomial Distribution models the number of successes in a fixed number of independent trials, each with the same probability of success.
It’s widely used in coin tosses, quality control, and yes/no experiments.


βœ… 1. Characteristics of Binomial Distribution

  • n β†’ number of trials

  • p β†’ probability of success in each trial

  • size β†’ number of experiments / samples

  • Values range from 0 to n

Probability Mass Function (PMF):

P(X=k)=(nk)pk(1βˆ’p)nβˆ’kP(X = k) = \binom{n}{k} p^k (1-p)^{n-k}

Where:

  • kk = number of successes

  • (nk)\binom{n}{k} = combinations of n choose k


βœ… 2. Generate Binomial Data Using NumPy

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Parameters
n = 10 # number of trials
p = 0.5 # probability of success
size = 1000 # number of experiments

# Generate random data
data = np.random.binomial(n, p, size)

print(data[:10]) # first 10 outcomes

Output (example):

[5 6 4 5 3 7 4 5 6 4]

βœ… 3. Visualize Binomial Distribution

sns.histplot(data, bins=range(n+2), kde=False, color='skyblue')
plt.title(f'Binomial Distribution (n={n}, p={p})')
plt.xlabel('Number of Successes')
plt.ylabel('Frequency')
plt.show()
  • Peaks around expected value: E[X] = n*p

  • Histogram shows discrete outcomes


βœ… 4. Change Probability or Number of Trials

# More trials
data = np.random.binomial(n=20, p=0.3, size=1000)

sns.histplot(data, bins=range(21), kde=False, color='orange')
plt.title('Binomial Distribution (n=20, p=0.3)')
plt.xlabel('Number of Successes')
plt.ylabel('Frequency')
plt.show()


βœ… 5. Compare Two Probabilities

data1 = np.random.binomial(n=10, p=0.3, size=1000)
data2 = np.random.binomial(n=10, p=0.7, size=1000)

sns.histplot(data1, bins=range(12), color='red', label='p=0.3', kde=False)
sns.histplot(data2, bins=range(12), color='green', label='p=0.7', kde=False)
plt.legend()
plt.title('Binomial Distribution Comparison')
plt.show()

  • Lower probability β†’ peak closer to 0

  • Higher probability β†’ peak closer to n


βœ… 6. Compute Mean and Variance

mean = np.mean(data)
var = np.var(data)
print("Mean:", mean)
print("Variance:", var)

Theoretical values:

  • Mean = nβˆ—pn * p

  • Variance = nβˆ—pβˆ—(1βˆ’p)n * p * (1-p)


🎯 Practice Exercise

  1. Simulate 50 coin flips (n=1) 1000 times and plot histogram.

  2. Simulate quality check: 20 items, success probability 0.8, 1000 experiments.

  3. Compare p=0.2 vs p=0.8 for 10 trials.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *