Binomial Distribution
The binomial distribution is what you reach for when you’re counting “how many times did this happen?” across a fixed number of tries—like how many customers clicked an ad out of 100 views, or how many test samples a model got right out of 50.
What it describes
A binomial distribution models the number of successes in n independent trials, where each trial has only two outcomes (often “success/failure”) and the probability of success is constant at p. The random variable is usually written as X ~ Binomial(n, p), and it takes values 0, 1, 2, …, n.
The probability of getting exactly k successes is:
P(X = k) = C(n, k) p^k (1-p)^(n-k)
Key assumptions (the “binomial checklist”)
- Fixed number of trials: you decide n in advance.
- Independent trials: one outcome doesn’t change the next.
- Two outcomes per trial: success vs. failure.
- Constant success probability p.
Practical examples
- Marketing: clicks among 1,000 impressions (success = click).
- Medicine: patients responding to a treatment in a study of n people.
- Quality control: defective items in a batch when each item is inspected.
Why it matters in AI/ML
Many ML metrics are counts of binary outcomes. If a classifier has true accuracy p, then “number correct out of n test cases” is binomial, which lets you build confidence intervals for accuracy and reason about uncertainty. It also underpins Bernoulli likelihoods used in logistic regression and connects to the cross-entropy loss for binary classification.
The Binomial Distribution models the probability of getting exactly k successes in n independent trials, where each trial has the same success probability p (e.g., yes/no outcomes). It is central in AI/ML for modeling counts of events and uncertainty in binary data, supporting likelihood-based inference and evaluation. Example: predicting how many of 100 emails are spam given an estimated spam rate.
Imagine flipping a coin 10 times and counting how many heads you get. You might get 0 heads, 5 heads, or even 10 heads—and some counts are more likely than others. The binomial distribution is the “rulebook” that tells you the chance of getting each possible count of successes when you repeat the same yes/no trial many times.
In statistics and AI/ML, it’s used whenever something has two outcomes (like click/no click, pass/fail) and you want to model how many successes happen out of a fixed number of tries, given a constant success chance each time.
import numpy as np # numerical computing for simulation
import pandas as pd # tabular summaries
from scipy.stats import binom # binomial PMF/CDF and fitting utilities
# Set a reproducible seed so results are stable across runs
rng = np.random.default_rng(7)
# Scenario: A marketing email has an estimated open probability p; run N independent sends per campaign
n_sends = 50 # number of trials per campaign (emails sent)
p_open = 0.22 # success probability per trial (open rate)
# Simulate many campaigns to see the distribution of "number of opens" per campaign
n_campaigns = 5000 # number of repeated experiments
opens = rng.binomial(n=n_sends, p=p_open, size=n_campaigns) # simulated counts per campaign
# Summarize the simulated distribution in a compact table
counts = pd.Series(opens).value_counts().sort_index() # frequency of each possible open count
summary = pd.DataFrame({"k_opens": counts.index, "freq": counts.values})
summary["empirical_prob"] = summary["freq"] / n_campaigns # empirical PMF estimate from simulation
# Compute a theoretical probability for a specific outcome using the binomial PMF
k = 15 # e.g., probability of exactly 15 opens out of 50 sends
p_exact_k = binom.pmf(k, n_sends, p_open) # *** KEY LINE ***
# Compute a tail probability often used for thresholding/alerting (e.g., unusually high opens)
p_at_least_k = binom.sf(k - 1, n_sends, p_open) # P(X >= k) via survival function for stability
# Fit p from observed data (MLE) assuming n is known: p_hat = mean(X)/n
p_hat = opens.mean() / n_sends # estimated open probability from simulated campaigns
print(summary.head(8)) # show a few PMF rows
print(f"P(X = {k}) = {p_exact_k:.4f}")
print(f"P(X >= {k}) = {p_at_least_k:.4f}")
print(f"Estimated p from data (p_hat) = {p_hat:.3f}")
Code Notes
- The value from scipy.stats.binom.pmf is the theoretical probability of seeing exactly
ksuccesses inntrials for a givenp, useful for likelihood scoring or calibration checks. - scipy.stats.binom.sf computes tail probabilities (e.g., “at least k”), commonly used for alert thresholds, anomaly flags, or hypothesis-test style decision rules.
- A common mistake is mixing up
n(trials per experiment) withsize(number of repeated experiments) when simulating via numpy.random.Generator.binomial. - In ML workflows,
p_hatis a quick baseline estimate for a Bernoulli/binomial rate (e.g., CTR/open-rate) and can seed priors or initialize probabilistic models.
set.seed(7) # make results reproducible
# Scenario: A marketing email has an estimated open probability p; run N independent sends per campaign
n_sends <- 50 # number of trials per campaign (emails sent)
p_open <- 0.22 # success probability per trial (open rate)
# Simulate many campaigns to see the distribution of "number of opens" per campaign
n_campaigns <- 5000 # number of repeated experiments
opens <- rbinom(n_campaigns, size = n_sends, prob = p_open) # simulated counts per campaign
# Summarize the simulated distribution (empirical PMF)
tab <- table(opens) # frequency of each open count
summary <- data.frame(
k_opens = as.integer(names(tab)), # outcome value k
freq = as.integer(tab) # observed frequency
)
summary$empirical_prob <- summary$freq / n_campaigns # empirical probability estimate
# Compute a theoretical probability for a specific outcome using the binomial PMF
k <- 15
p_exact_k <- dbinom(k, size = n_sends, prob = p_open) # *** KEY LINE ***
# Compute a tail probability (e.g., unusually high opens)
p_at_least_k <- 1 - pbinom(k - 1, size = n_sends, prob = p_open) # P(X >= k)
# Fit p from observed data (MLE) assuming n is known: p_hat = mean(X)/n
p_hat <- mean(opens) / n_sends # estimated open probability from simulated campaigns
print(head(summary[order(summary$k_opens), ], 8)) # show a few PMF rows
cat(sprintf("P(X = %d) = %.4f\n", k, p_exact_k))
cat(sprintf("P(X >= %d) = %.4f\n", k, p_at_least_k))
cat(sprintf("Estimated p from data (p_hat) = %.3f\n", p_hat))
Code Notes
- The value from dbinom is the theoretical probability of exactly
ksuccesses givensize(trials) andprob(success rate), often used to compute likelihoods for count outcomes. - Tail probabilities via pbinom (converted to “at least” with
1 - ...) are commonly used for setting operational thresholds and detecting unusually high/low counts. - A common mistake is interpreting
sizeas the number of simulated samples; in rbinom,nis the number of samples andsizeis trials per sample. - In ML,
p_hatis a fast rate estimate for binomially distributed targets (e.g., conversions per batch) and can be used for monitoring drift or initializing probabilistic models.