Binomial Distribution Examples.
Tags: datascience, beginners
Published: 2020-09-21 02:49:10

In this blog, I would like to show a few examples of using a binomial distribution. 
Before applying the probability mass function formula lets start with classical examples of calculating binomial probability, to get a better intuition behind the binomial distribution.

###**Coins and Dice**

####*Example 1*
We a have a loaded coin with a probability of 0.8 in getting a head on any particular toss. Let’s flip the coin 3 times. What is the probability of getting at least 2 heads?

*Solution 1* 
a) For simplicity lets start with a fair coin, where the chance of getting tails and heads are 0.5.  
There are 2*2*2 = 8 possible outcomes of three tosses: 
HHH, HHT, HTH, THH, TTT, TTH, THT, HTT, where H and T are head/tail respectively.  For a fair coin, each outcome is equally possible, with a probability of 0.5*0.5*0.5 = ⅛.
We are interested in the following combinations: 2 heads and 3 heads, as this, represent ‘at least 2 heads’. 
Outcomes with exactly two heads = (3*2*1)/(2*1) = 3 or HHT, HTH,THH.
Outcomes when all three tosses are heads = (3*2*1)/(3*2*1) = 1 or HHH.
So, there are 4 total ‘interested’ cases out of 8. Because all scenarios are equally possible the probability of getting at least two heads P(X>=2) =  4 * (⅛) = 0.5.
As we can see, this is symmetrical distribution, so getting at least two tails also  = 0.5.

b) Now, let's recalculate the same with loaded coin where the probability of getting head = 0.8.
Now 4 ‘interested’ scenarios: HHH, HHT, HTH, and THH will have the following probabilities: 
P(HHH) = 0.8*0.8*0.8=0.512, 
P(any 2 Heads & 1Tail) = 0.8*0.8*0.2 = 0.128
And probability of getting at least two heads with a loaded coin:  
P(X>=2)  =  0.512+3*0.128 = 0.896

####*Example 2*
What if we want to use dice instead of coins, and we want to calculate the probability of getting at least 1 six when rolled 3 times. It could be a fair or weighted dice. 

*Solution 2*
We can start with calculating cases we are interested in, assuming die is a fair and p=1/6:
Exactly 1 six out of 3 rolls: 3 * (1 * 5 * 5) * (⅙ * ⅙ * ⅙)
Exactly 2 sixes out of 3 rolls: 3 * (1* 1 * 5) * (⅙ * ⅙ * ⅙)
Exactly all three are sixes: 1 * (1 * 1 * 1) * (⅙ * ⅙ * ⅙).
And finally add of all the above probabilities: P(X>=2) = 0.421.

###**Probability and Cumulative Mass Functions**

As we can see, doing all calculations manually is very tedious and may lead to mistakes. So, we can use a formula to calculate the probability mass function of a binomial distribution, which is the same as in the above examples.
 
![Formula](https://dev-to-uploads.s3.amazonaws.com/i/chs8rw0msv04b3olcn9z.png)

Let’s run a quick code in Python which calculates the binomial probability mass function for us.

```python
def binom_pmf(n,k,p):
    # calculates PMF of Binomial distribution
    # n= number of trials
    # k = number of succeses
    # p = probability of success k
    
    pmf = (math.factorial(n)/(math.factorial(k)*math.factorial(n-k)))*(p**k)*(1-p)**(n-k)
    return pmf

def binom_cmf(lower, upper, n, p):
    # calculates CMF of Binomial distribution
    # lower = min number of successful trials
    # upper = max number of successful trials
    # n = number of trials
    # p = probability of success
    cmf = 0
    for k in range(lower,upper+1):
        cmf += binomial(n,k,p)
    return cmf

```

####*Example 3*

A University Engineering Department has introduced a new software package called SOLVIT. To save money, the University’s Purchasing Department has negotiated a bargain price for a 4-user license that allows only four students to use SOLVIT at any one time. It is estimated that this should allow 90% of students to use the package when they need it. The Students’ Union has asked for more licenses to be bought since engineering students report having to queue excessively to use SOLVIT. As a result, the Computer Centre monitors the use of the software. Their findings show that on average 20 students are logged on at peak times and 4 of these want to use SOLVIT. Was the Purchasing Department’s estimate correct?
[Source for this example](https://learn.lboro.ac.uk/archive/olmp/olmp_resources/pages/workbooks_1_50_jan2008/Workbook37/37_2_bnml_dist.pdf)

Using Python we can calculate that: P(X<=4) = 0.6296. It means that there is a 37% probability that more than 4 students use the software. P(X>=5) = 1 - 0.6296 = 0.3704.
 
```python
binom_cmf(0,4,20,0.2)
```
If we use 6 licenses than probability will be  P(X<=6) = 0.9133 which would be sufficient.

```python
binom_cmf(0,6,20,0.2)
```

### **Conclusion**

Here we applied binomial distribution to calculate probabilities of 'successes' and 'failures'. In the next part, we can discuss the application of the binomial PMF for the binomial test.