Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Binomial Distribution

Binomial Distribution

Binomial Distribution is a discrete distribution that describes the outcomes of binary scenarios, such as a coin toss, which can result in either heads or tails.

It has three parameters:

  • n: The number of trials.
  • p: The probability of occurrence for each trial (e.g., for a coin toss, this would be 0.5 for heads or tails).
  • size: The shape of the resulting array.
Discrete Distribution is defined at distinct events; for instance, the outcome of a coin toss is discrete because it can only result in heads or tails, whereas a person’s height is continuous, as it can take on values like 170, 170.1, 170.11, and so forth.

Example

Generate 10 data points based on 10 trials of a coin toss:

from numpy import random

x = random.binomial(n=10, p=0.5, size=10)

print(x)

Visualization of Binomial Distribution

Example

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.binomial(n=10, p=0.5, size=1000), hist=True, kde=False)

plt.show()

Result

bionomial1

Difference Between Normal and Binomial Distribution

The primary difference is that the normal distribution is continuous, while the binomial distribution is discrete; however, with a sufficient number of data points, the binomial distribution will resemble the normal distribution with specific values for loc and scale.

Example

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.normal(loc=50, scale=5, size=1000), hist=Falselabel=‘normal’)
sns.distplot(random.binomial(n=100, p=0.5, size=1000), hist=Falselabel=‘binomial’)

plt.show()

Result

bionomial2