Have you ever wondered how to predict the number of successes in a series of independent trials? Or perhaps you’ve been curious about the probability of achieving a specific outcome in a sequence of yes-or-no questions. If so, we are essentially talking about the binomial distribution. It’s important for data scientists to understand this concept as binomials are used often in business applications.
The binomial distribution is a discrete probability distribution that applies to binomial experiments (experiments with binary outcomes). It’s the number of successes in a specific number of trials. Sighting a simple yet real-life example, the binomial distribution may be imagined as the probability distribution of a number of heads that appear on a coin flip in a specific experiment comprising of a fixed number of coin flips. In this blog post, we will learn binomial distribution with the help of examples. If you are an aspiring data scientist looking forward to learning/understand the binomial distribution in a better manner, this post might be very helpful.
The binomial distribution is a discrete probability distribution that represents the probabilities of binomial random variables in a binomial experiment. The binomial distribution is defined as a probability distribution related to a binomial experiment where the binomial random variable specifies how many successes or failures occurred within the sample space. Here is a sample plot representing binomial probability distribution with number of trials, n = 10 and probability of success, p = 0.5. The probability mass function is used to calculate the probability distribution values.
A random variable represents a variable that could take random values in an experiment. Let’s say, the random variable representing the number of defective items found in 100 items picked randomly. Here, 100 items represent 100 trials. There could be multiple experiments comprising of randomly sampling 100 items and counting the number of defective items.
In the above experiment, the number of items found to be defective can be termed as a RANDOM variable. The random variable is also represented by the letter, X. The X takes the value of 5 and 9 in the above-mentioned experiments.
X = No. of defective items in 100 items
When the experiment is conducted for sampling 100 items 5 times, the value of X might look like the following:
X = {5, 9, 11, 4, 6}
When the value of the random variable can only take finite values, the random variable can also be called a random discrete variable. When the value of the random variable can take infinite values, the random variable can also be called a random continuous variable.
All possible values (or outcomes) that a random variable can take are also called a sample space.
In the binomial experiment, the outcome of each trial in an experiment could take one of the two values which are either success or failure. Each trial in the binomial experiment can also be termed as a Bernoulli trial. For a single trial, binomial distribution can also be termed a Bernoulli distribution. You may want to check my post on Bernoulli distribution explained with Python examples. In other words, the outcome of each trial gets classified according to two levels of a categorical variable. Here are some examples of Bernoulli trials:
The outcome of interest in a trial of an experiment is often termed as a success.
The binomial random variable could be the number of successes in an experiment consisting of N trials. Thus, the following are some examples of a binomial random variable:
A binomial experiment represents a binomial random variable X which counts the number “n” of successes in N trials when each trial has only two outcomes, success, and failure. Thus, an experiment could consist of 1 trial, 5 trials, 10 trials, 20 trials, etc. Sighting real-world examples, an experiment could be tossing a coin 10 times (10 trials), taking 10 items for examining whether the items are defective, etc. If the experiment consists of just one trial that has only two outcomes such as success or failure, the trial is called a Bernoulli trial.
The requirements for a random experiment to be a Binomial experiment are as follows:
The binomial distribution is a type of discrete probability distribution representing probabilities of different values of the binomial random variable (X) in repeated independent N trials in an experiment. Thus, in an experiment comprising of tossing a coin 10 times (N), the binomial random variable (number of heads represented as successes) could take the value of 0-10 and the binomial probability distribution is probability distribution representing the probabilities of a random variable taking the value of 0-10.
The probability that a random variable X with binomial distribution B(n,p) is equal to the value k, where k = 0, 1,….,n, is given by the following formula:
P(X = k) = [latex]\frac{n!}{k!(n-k)!}p^{k}(1-p)^{(n-k)}[/latex]
The mean and the variance of the binomial distribution of an experiment with n number of trials and the probability of success in each trial is p as following:
Mean = np
Variance = np(1-p)
In a binomial experiment consisting of N trials, all trials are independent and the sample is drawn with replacement. If the sample is drawn without replacement, it is called a hypergeometric distribution.
Here is the Python code for the binomial distribution. Pay attention to some of the following:
from scipy.stats import binom
import matplotlib.pyplot as plt
import numpy as np
#
# X = Discrete random variable representing number of successes
# p = Probability of the success
#
X = np.arange(0,21)
p = 0.6
n = 20
#
# Calculate binomial probability distribution
#
binom_pd = binom.pmf(X, n, p)
#
# Plot the probability distribution
#
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(X, binom_pd, 'bo', ms=8, label='geom pmf')
plt.ylabel("Probability", fontsize="18")
plt.xlabel("X - No. of Successes", fontsize="18")
plt.title("Binomial Distribution - No. of Successes Vs Probability", fontsize="18")
ax.vlines(X, 0, binom_pd, colors='b', lw=5, alpha=0.5)
Here is what the binomial distribution plot would look like. This plot is the outcome of executing the above code.
Here are some real-life examples of Binomial distribution:
Here is the summary of what you learned in this post in relation to Binomial distribution:
We’ve all been in that meeting. The dashboard on the boardroom screen is a sea…
When building a regression model or performing regression analysis to predict a target variable, understanding…
If you've built a "Naive" RAG pipeline, you've probably hit a wall. You've indexed your…
If you're starting with large language models, you must have heard of RAG (Retrieval-Augmented Generation).…
If you've spent any time with Python, you've likely heard the term "Pythonic." It refers…
Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…
View Comments
"When the value of the random variable can only take finite values, the random variable can also be called a random discrete variable. When the value of the random variable can take infinite values, the random variable can also be called a random continuous variable."
This should be taken with caution. Consider the Poisson Random Variable. It is discrete, but takes on an infinite range of values. You can use it to calculate the probability of (some very,very large number: tending to infinity) of car accidents at an intersection for instance. The resulting probability may be extremely small, but it will be greater than zero.
What distinguishes discrete and continuous random variables is whether or not that set of values is countable. In the case of a discrete RV: we can count the set of values that the function can take as input (1,2,3,4,..., could be infinite- but at least countable). With a continuous random variable, it is impossible to count the set of inputs.
Thank you Robert for providing clarity. Will include this in the blog