Binomial Distribution
The Binomial Distribution is a discrete probability distribution which tells us that the number of successes in some sequence of independent events with parameter $n$ (# of independent events) and $p$ (probability of each event). If some random variable $X$ has binomial distribution, we denote it as $X \sim B(n, p)$.
Probability Mass function
Let
- $n$ be the # of independent events
- $k$ be the # of successes in $n$ events
- $p$ be the probability of each event
then, the probability mass function can be defined as
$$\begin{align*} p(X = k) = { n \choose k } p^k (1-p)^{(n-k)} \end{align*}$$where ${ n \choose k } = \frac{n!}{k!(n-k)!}$ is the binomial coefficient.
Binomial Probability in Code
Input
The following graph is implemented in D3.js and plots the probability of $k$ number of successes in $n$ independent trials. The condition of variables $n$ and $k$ is that $n >= k$ since there cannot be more successes than the total number of trials. Besides the actual code that draws the plots on the screen, there are parts required to calculate the probability.
In order to find the Binomial probability, it is required to find the Binomial Coefficient which is defined as ${ n \choose k } = \frac{n!}{k!(n-k)!}$ is the binomial coefficient. This involves finding factorial.
Factorial
The factorial is which is defined as
$$\begin{align*}
n! &= \prod_{i=1}^{n} i
\end{align*}$$
and $0! = 1$. In code this can be calculated as below.
|
|
Binomial Coefficient
As stated above, the Binomial Coefficient is defined as follow.
$$\begin{align*} { n \choose k } &= \frac{n!}{k!(n-k)!} \end{align*}$$Since we already have factorial in hands, this can be calculated easily.
|
|
Binomial Probability
Once we have Factorial and Binomial Coefficient in hands, finding the Binomial Probability with parameters $n$ and $k$ is easy. Since the Binomial Probability is defined as follow :
$$\begin{align*} p(X = k) = { n \choose k } p^k (1-p)^{(n-k)} \end{align*}$$we can just substitute the equations with code as follow :
|
|