Binomial Distribution in Python

Arseny Turin
3 min readJan 3, 2020

--

In this post, I will describe what is the Binomial Distribution and where it can be applied.

The word binomial derived from the Latin binomium, where bi means ‘having two’, and nomos — ‘part’.

In statistics, the binomial distribution is a discrete probability of independent events, where each event has exactly two possible outcomes. For example, if we toss a coin 10 times and we are interested in how many times it will land on heads, the probability of it lending one, two, three, up to 10 head in a row will form a distribution of probabilities or binomial distribution. When we’re interested in a specific outcome, let's say 3 heads out of 10 tosses, we call it binomial probability, which is a probability of exactly x successes on n repeated trials.

Rules

  1. The number of observations or trials is fixed. It simply means that we have to know how many trials we are going to conduct, there is no set limit on the number of trials.
  2. Each observation or trial is independent. Like in the coin toss, the previous toss doesn’t affect the following.
  3. The probability of success (success/failure) is exactly the same from one trial to another. Each coin toss has 50% chances to fall in heads or tails.

The Formula

In practice, we use Probability Mass Function (PMF) to describe the probability of an even outcome, where n — number of observations, x — desired outcome, p — the probability of an outcome. To have a better understanding of how this formula works let's take a look at two examples below.

Probability Mass Function

First Example: A Fair Coin Flip

If we flip a fair coin 100 times, what is the probability of getting exactly 50 heads? Now we might think that since we have equal chances of getting heads or tails, the answer would be 50% probability. On the other hand, what are the odds that we will get 49 heads, or 51? Almost 50% chance too, but what exactly? Let’s first answer the first question and then try it for all the possible outcomes to see how our probabilities are distributed. So our parameters are the following: n = 100, x = 50, p = 0.5. Plugging numbers into the formula and we get:

There is an 8% chance of getting exactly 50 heads on 100 coin tosses. The chart below shows the binomial distribution for all possible outcomes.

Second Example: Success of Marketing Campaign

In 2014, 8.5% of adults aged 18 years and older had diabetes.

Let’s go back to 2014 and pretend we’re working for the company that developed a drug for treating diabetes and our job is to make phone calls to find potential clients. The only statistic we know is that 8.5% of adults in the US have diabetes. What is the probability that out of 50 calls at least 5 customers would have diabetes?

Once again let’s plug numbers into the formula: n = 50, x = 5, p = 0.085

There is a 17.3% chance that out of 50 calls we will find a customer with diabetes.

Conclusion

So there we have it. The binomial distribution is a very quick and easy way to calculate the probability of the desired outcome.

--

--