Binomial Distribution
Binomial experiment
A Binomial experiment has the following properties.
- Consists of fixed number of trials (n)
- Trials are independent of each other
- Each trial can be either success or failure
- Probability of success (P) on each trial remains the same
Example: Number of heads in after flipping a coin 10 times
- The experiment is conducted for fixed number of trials - 10
- Probability of getting head in one trial does not affect the other
- Probability of getting head in any trial remains same - 0.5 (in a non-biased coin)
Binomial variable
A binomial variable is the number of successes (x) out of all the trials (n).
What is the probability of getting 5 heads after flipping a coin 10 times? Here is a binomial variable.
Binomial distribution
The probability distribution of a binomial variable is called Binomial distribution.
Let's take the problem statement of flipping a coin - Probability of getting 5 heads after 10 flips? P(X = 5) can be calculated as
For 10 flips, we have a total of outcomes. Hence,
No. of outcomes where exactly 5 heads occur out of 10 flips =
Deriving General Binomial Probability equation
Let's take the example of a biased coin instead of a fair coin with 60% chance of heads and 40% chance of tails.
What is the probability of getting 2 heads out of 3 tosses?
$$ p = 0.6$$ (Probability of getting heads)
$$ x = 2$$ (no. of success i.e. heads)
$$ n = 3$$ (no. of trials)
No. of outcomes we want = $^{n}C_x = ^{3}C_2 = 3 $ (HHT, HTH, THH)
To calculate probability of each outcome, let's take one outcome- HHT
- Probability of getting H in trial 1 = 0.6
- Probability of getting H in trial 2 = 0.6
- Probability of getting T in trial 3 = 0.4
Hence, probability of getting HHT =
i.e.
Finally,
Probability of getting 2 heads out of 3 =
Putting it together,
Hence, the general binomial probability equation is,
Also,
Plotting Binomial Distribution
Let be a random variable = No. of heads from flipping a coin 5 times
import math
import matplotlib.pyplot as plt
def compute_binomial_probability(x, n, p):
"""Returns the probability of getting `x` success outcomes in `n` trials,
probability of getting success being `p`
Arguments:
x - number of trials of the event
n - number of trials
p - probability of the event
"""
outcomes = math.factorial(n) / (math.factorial(x) * math.factorial(n - x))
probability_of_each_outcome = p ** x * (1 - p) ** (n - x)
return outcomes * probability_of_each_outcome
def plot_binomial_distribution_graph(n, p):
"""Plots Binomial distribution graph of an event with `n` trials,
probability of getting success of the event being `p` for values `0` to `n`
Arguments:
n - number of trials
p - probability of the event
"""
probabilities = list(map(lambda x: compute_binomial_probability(x, n, p), range(0, n+1)))
plt.bar(list(range(0, n+1)), probabilities)
plot_binomial_distribution_graph(5, 0.5)
Let's plot the distribution for flipping a coin 10 times.
plot_binomial_distribution_graph(10, 0.5)
As we can observe, with more trials, the plot tends to look like Normal distribution
Plotting the graph for a biased coin -
plot_binomial_distribution_graph(10, 0.7)
Bernoulli distribution
Bernoulli distribution is a discrete probability distribution of a random variable which has only two outcomes ("success" or a "failure"). It is named after Swiss mathematician Jacon Bernoulli. It is a special case of Binomial distribution for n = 1.
For example, probability (p) of scoring a goal in last 10 minutes is 0.35 (success), probability of not scoring a goal in last 10 minutes (failure) is 1 - p = 0.65.
Plotting Bernoulli distribution with probability for p = 0.65,
plt.bar(['0', '1'], [0.35, 0.65])