Binomial Distribution

Joel Sanqui

Introduction

Let \(X\) be a random variable representing the total number of successes in \(n\) independent trials that satisfy the following:

  • Each trial has two outcomes: Success (\(s\)) or Failure (\(f\)).
  • The probability of success is \(p\).
  • The probability of failure is \(q = 1-p\).

Finding the pmf

To find the total probability of exactly \(x\) successes, we split the problem into two distinct parts:

\[\begin{aligned} P(X = x) = &\quad \mathbf{\text{Combinations}} \\ &\times \mathbf{\text{Probability of a Specific Sequence}} \end{aligned}\]

1. Counting the Combinations

How many ways can we arrange exactly \(x\) successes across \(n\) total trials?

We use the binomial coefficient to choose \(x\) slots out of \(n\):

\[\binom{n}{x} = \frac{n!}{x!(n-x)!}\]

2. Probability of One Sequence

Since the trials are independent, we multiply their individual probabilities together.

For any specific sequence containing \(x\) successes and \(n-x\) failures:

\[\text{for } x = 0, 1, 2, \dots, n\]

\[\underbrace{p \cdot p \cdots p}_{x \text{ times}} \times \underbrace{(1-p) \cdot (1-p) \cdots (1-p)}_{n-x \text{ times}} = p^x (1-p)^{n-x}\]

3. The Final pmf

Combining the total number of possible arrangements with the probability of a single sequence yields the final formula:

\[f(x) = P(X = x) = \binom{n}{x} p^x (1-p)^{n-x}\]

\[\text{for } x = 0, 1, 2, \dots, n\]

We write \(X \sim \text{Binomial}(n,p)\).

Mean of Binomial(n,p)

A Binomial variable can be written as the sum of \(n\) independent Bernoulli random variables: \(X = \sum_{i=1}^n Y_i\), where \(Y_i \sim \text{Bernoulli}(p)\).

First, find the expectation of a single Bernoulli trial: \[\mathbb{E}[Y_i] = (1 \cdot p) + (0 \cdot (1-p)) = p\]

By the linearity of expectation: \[\mathbb{E}[X] = \mathbb{E}\left[\sum_{i=1}^n Y_i\right] = \sum_{i=1}^n \mathbb{E}[Y_i] = \sum_{i=1}^n p = np\]

Variance of Binomial(n,p)

First, we find the variance of a single Bernoulli trial \(Y_i\): \[\mathbb{E}[Y_i^2] = (1^2 \cdot p) + (0^2 \cdot (1-p)) = p\] \[\text{Var}(Y_i) = \mathbb{E}[Y_i^2] - (\mathbb{E}[Y_i])^2 = p - p^2 = p(1-p)\]

Because the \(n\) Bernoulli trials are independent, the variance of their sum equals the sum of their variances: \[\text{Var}(X) = \text{Var}\left(\sum_{i=1}^n Y_i\right) = \sum_{i=1}^n \text{Var}(Y_i) = n p(1-p)\]

Example 1: Quality Control

Manufacturing plants use the binomial distribution to predict and manage product defects.

  • Scenario: A factory tests a batch of microchips.
  • Parameters: Suppose historically, the probability of any single chip being defective is \(p = 0.02\) and the factory tests \(n=50\) microchips. The Binomial parameters are then \(n=50\) and \(p=0.02\).
  • Problem: Calculate the probability that \(x \le 2\) chips are defective and find the mean and variance of the number of defective chips.

Solutions

  • Exact Probability \(P(X \le 2) = f(0)+f(1)+f(2)\) = 92.16%
  • Mean or expected number of defective chips is (50)(.02) = 1
  • Variance of the number of defective chips is (50)(.02)(.98) = .98.

QC Visualization

library(ggplot2)

n <- 50
p <- 0.02
df_qc <- data.frame(x = 0:8, pmf = dbinom(0:8, size = n, prob = p))
df_qc$highlight <- ifelse(df_qc$x <= 2, "Target (<=2)", "Other")

ggplot(df_qc, aes(x = factor(x), y = pmf, fill = highlight)) +
  geom_col(width = 0.7) +
  scale_fill_manual(values = c("Target (<=2)" = "darkgreen", "Other" = "grey70")) +
  labs(title = "QC Defect Probability Matrix (n=50, p=0.02)",
       x = "Number of Defective Chips (x)", y = "Probability", fill = "Region") +
  theme_minimal()

QC Visualization

Example 2: A/B Testing

Digital marketers rely on the binomial distribution to see if website changes actually improve performance.

  • Scenario: An e-commerce site routes users to a new checkout page design.
  • Parameters: If the baseline checkout rate is known to be \(p = 0.08\) and the site routes \(n=500\) users to the new checkout page design, then the Binomial parameters are \(n=500\) and \(p=0.08\).
  • Question: How likely it is to observe \(x \ge 50\) successful purchases by pure chance?

Solution

  • Exact Binomial Probability \(P(X \ge 50) = 1 -P(X \le 49) = f(0) + f(1) + ...+f(49)\) = 6.22%
  • Mean or expected number of purchases/checkouts is (500)(.08) = 40
  • Variance of the number of purchases/checkouts is (500)(.08)(.92)

A/B Testing Visualization

library(ggplot2)

n <- 500
p <- 0.08
df_ab <- data.frame(x = 20:65, pmf = dbinom(20:65, size = n, prob = p))
df_ab$highlight <- ifelse(df_ab$x >= 50, "Target (>=50)", "Other")

ggplot(df_ab, aes(x = x, y = pmf, fill = highlight)) +
  geom_col(width = 0.9) +
  scale_fill_manual(values = c("Target (>=50)" = "firebrick", "Other" = "grey70")) +
  labs(title = "A/B Testing (n=500, p=0.08)",
       x = "Number of Checkouts/Purchases (x)", y = "Probability", fill = "Region") +
  theme_minimal()

A/B Testing Visualization

Binomial Distribution in R

In the above plots, we use the function dbinom to calculate the Binomial pmf. The probability in Example 1 is

dbinom(0,50,.02)+dbinom(1,50,.02)+dbinom(2,50,.02)
[1] 0.9215723

To calculate the Binomial cdf, we use the pbinom function. The probability in Example 1 is also given by

pbinom(2,50,.02)
[1] 0.9215723

To find quantiles of the Binomial distribution, we use the function qbinom.

To generate random numbers from a Binomial distribution, we use rbinom.

Example 3

What is the probability of getting exactly 50 heads if a fair coin is flipped 100 times?

Solution:

Let X = number of heads obtained after 100 flips. Then \(X\sim \text{Binomial}(100,0.5)\) and \(P(X = 50)\) is

dbinom(50,100,0.5)
[1] 0.07958924

Example 4

What is the probability of getting less than 45 heads?

Solution:

\(P(X < 45)\) is

pbinom(44,100,.5)
[1] 0.1356265

Example 5

What is the probability of getting anywhere from 45 to 55 heads?

Solution:

\(P(45 \le X \le 55)\) is calculated as

pbinom(55,100,.5)-pbinom(44,100,.5)
[1] 0.728747

Example 6

What is the 3rd quartile of the \(\text{Binomial}(100,0.5)\) distribution?

Solution:

The 3rd quartile is the 75th quantile and is calculated as

qbinom(.75,100,0.5)
[1] 53

Empirical Simulation: Coin Flipping

What happens if we simulate a Binomial process empirically?

  • Experiment: Simulate flipping a fair coin (\(p = 0.5\)) \(100\) times (\(n = 100\)).
  • Repetitions: Repeat this whole process \(1,000\) times.
  • Objective: Map out the generated values and approximate the distribution’s mean and variance from the simulation.

Simulation & Analysis

library(ggplot2)
set.seed(42) # Ensure reproducible results
# 100 trials, 1000 repetitions, p = 0.5
sim_trials <- rbinom(n = 1000, size = 100, prob = 0.5)
df_sim <- data.frame(heads = sim_trials)

# Calculate empirical values
sim_mean <- mean(sim_trials)
sim_var  <- var(sim_trials)

# Dynamic Title with results
plot_title <- paste0("Empirical Coin Flips (1000 trials of n=100)\n",
                     "Simulated Mean: ", round(sim_mean, 2), 
                     " | Simulated Variance: ", round(sim_var, 2))

# Plot histogram
ggplot(df_sim, aes(x = heads)) +
  geom_histogram(binwidth = 1, fill = "purple", color = "white", alpha = 0.8) +
  geom_vline(xintercept = sim_mean, color = "black", linetype = "dashed", size = 1) +
  labs(title = plot_title, x = "Number of Heads (X)", y = "Frequency") +
  theme_minimal()

Note that the theoretical mean is 50 and the theoretical variance is 25 so the simulation is not bad.

Simulation & Analysis