# Binomial Distribution – Python

0
425 In Statistics, Binomial distribution is a probabilistic distribution with only two possible outcomes; with outcomes can either be categorized as a “Success” or as a “Failure” (or categories of “0” or “1”). Simply put, binomial distribution quantifies the likelihood of one of the two possible outcomes of an event in given number of trials.

Basics of Binomial Distribution:  Let us suppose we toss a coin then every flip would either result into head or tail. Two important things to consider are; One– Probability of either a head or a tail in every toss will remain constant (which in in this case .5) and Second– Every time we flip a coin it is completely independent of the previous flip’s outcome.

Definition: A binomial random variable may be defined as the number of “successes” in a given number of trials, whereby the outcome can either be a “success” or a “failure.”

Assumptions: A Binomial distribution as two assumptions.

1. The Probability of a success or failure should remain constant across events
2. Outcome of one event is completely independent of outcomes of other events

Special Case of Binomial Distribution:  When number of trail in Binomial Distribution is one it is called Bernoulli distribution.

Formula: If probability of success or favorable outcome is assigned as “p” and probability of failure or un-favorable outcome is assigned as “q” then probability of k successes in n trials can be calculated as

P(x) = N pk * qn-k

q = (1-p) ;

where N is possible number of ways to choose k successes from total of n trials.

So above formula can also be represented as: Or it may we represented as: Mean of Binomial Distribution: For a given series of n trials, the expected number of successes, or E(X), is given by the following formula:

E(X) = n*p

Variance of Binomial Distribution:  Variance of binomial distribution is calculated as product of probability of success and probability of failure for a given trial. Accordingly for n trials;

Variance = n*p*q = n*p*(1-p)

Python Code for Binomial Distribution

from scipy.stats import binom
import seaborn as sb
import matplotlib.pyplot as plt
data_binom = binom.rvs(n=20,p=0.8,loc=0,size=1000)
ax = sb.distplot(data_binom,
kde=True,
color=’blue’,
hist_kws={“linewidth”: 25,’alpha’:1}) ax.set(xlabel=’Binomial’, ylabel=’Frequency’)

Once we run the above code we get the following output. We can verify the formula given for mean and variance given above by running the following command.

mean , var , skew , kurt = binom.stats(n , p , moments = ‘mvsk’)