Binomial Distribution¶

Consider \(n\) independent trials where the outcome of each trial is either a success with probability \(p\), or a failure with probability \(1-p\). The number of successes in \(n\) independent trials is described by a binomial distribution with two parameters \(n\) and \(p\). We denote it by \(\mathrm{Binom}(n,p)\).
From Coin Tosses to the PMF¶

Suppose we toss a coin \(n\) times with the probability of getting a head \(\mathbf{P}(\text{H})=p\) and, hence, the probability of getting a tail \(\mathbf{P}(\text{T})=1-p\). We assume that the tosses are independent, i.e., the outcome of previous tosses does not affect subsequent tosses. What is the probability of getting \(k\) heads?
Before we proceed, we consider the specific case \(n=6\) and compute the probability of getting the sequence \(\text{HTHHTH}\). We use the multiplication principle to get
To compute for the probability of getting any sequence with \(4\) heads, we have to multiply the above expression with the number of \(4\)-head sequences. This is just the same as number of \(4\)-element subsets taken from a set with \(6\) elements, i.e., \(\binom{6}{4}\). Therefore, the probability of getting \(4\) heads in \(6\) coin tosses is
Generalizing this result, the probability of getting \(k\) heads in \(n\) coin tosses is
Probability Mass Function¶
The probability mass function of a binomial distribution is given by
where \(k=0,1,2,\ldots,n.\)
Expectation¶
The expectation of a binomial distribution is
Proof
We let \(k=j+1\) so that
Now, from the binomial expansion formula, we have
so that
Simpler Calculation¶
A simpler way to calculate the expectation of a binomial random variable is to realize that it is the sum of \(n\) independent Bernoulli random variables, each with parameter \(p\), i.e.,
so that, by linearity,
Variance¶
The variance of a binomial distribution is
Proof
To calculate the variance, we have
Monte Carlo Simulation¶
We can simulate a binomial distribution by tossing a coin n times. For each toss, if a head comes up, we'll call it a success and map it to 1. Otherwise, it's a failure and map it to 0. The probability of success is p. After n trials, we will count the number of successes. We will replicate this 10000 times and calculate the proportion of replications with k successes. Finally, we plot the resulting distribution. The code below is an implementation of this in R.
library(tidyverse)
n <- 25 # number of trials
p <- 0.5 # probability of success
reps <- 10000 # number of replications
num_success <- replicate(reps, {
trials <- sample(c(0, 1), size = n, replace = TRUE, prob = c(1-p, p))
sum(trials)
})
# Plot the number of success versus the proportion of replications
data.frame(num_success) %>%
ggplot(aes(num_success)) +
geom_bar(aes(y = ..prop..)) +
labs(x = "Number of Successes", y = "Probability")
