{"id":530,"date":"2017-01-07T16:21:34","date_gmt":"2017-01-07T16:21:34","guid":{"rendered":"http:\/\/muthu.co\/?p=530"},"modified":"2021-05-24T03:51:44","modified_gmt":"2021-05-24T03:51:44","slug":"understanding-binomial-distribution-using-python","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/understanding-binomial-distribution-using-python\/","title":{"rendered":"Understanding Binomial Distribution using Python"},"content":{"rendered":"\n
Binomial distribution is used to understand the probability of a particular outcome in repeated independent trials. The probability of a trial is either success or failure. The trials are independent as the outcome or the previous trial had no effect on the next trial, as happens in tossing of coins.<\/p>\n\n\n\n
If we flip a coin, it would either be HEADs or TAILs. Normal probability is 0.5 for both p(H) and p(T). In binomial terms we say p(H) is the probability of a HEAD showing up in a trial and 1-p(H) if head doesn’t show up.<\/p>\n\n\n\n
If the coin is flipped six times, then getting three heads has the maximum probability, whereas getting a single head or five heads has the least probability. This can be calculated using the below code in python.<\/p>\n\n\n\n
from scipy.stats import binom\nimport matplotlib.pyplot as plt\n\nfig, ax = plt.subplots(1, 1)\nx = range(7)\nn, p = 6, 0.5\nrv = binom(n, p)\nax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1,label='Probablity of Success') \nax.legend(loc='best', frameon=False) \nplt.show()<\/code><\/pre>\n\n\n\n