Contents
for
Deep Breakdown
What it tells me (Situation Awareness): The Bernoulli distribution models a single binary trial: success (1) with probability , failure (0) with probability . It is the building block of the Binomial distribution (sum of Bernoullis) and the foundation for logistic regression (models ), binary classification, and conversion rate modeling.
Flowchart
Real-world use
Where it is used
Binary classification target (click/no-click, churn/retain, fraud/legit). Logistic regression models as the Bernoulli parameter. A/B test outcome for each user. Conversion rate is $\hat{p} = $ sample mean of Bernoulli observations.
Problem it solves
Models the simplest possible random experiment with two outcomes — the atom from which all binary/count models are built.
How it solves the problem
Assigns probability to outcome 1 and to outcome 0. The elegant PMF form captures both cases in one expression.
Example in a business
Click-through rate: each impression is a Bernoulli trial. . The sum of impressions follows , from which confidence intervals for CTR are derived.
The formula, piece by piece
for
- : probability of success (the parameter).
- : success. : failure.
- When : .
- When : .
The formula in words
"The Bernoulli PMF gives probability for success and for failure in a single binary trial."
Critical Concepts
- (MLE of is sample mean).
- — maximized at .
- Log-loss: is the negative log-likelihood of Bernoulli.
- Bernoulli vs Binomial: Bernoulli is Binomial.
Step-by-step calculation (tiny example)
Step 1: CTR = 5%, evaluate PMF .
Step 2: P(click=1)
Step 3: P(no-click=0)
Step 4: Verify . Valid.
Code examples
Beginner example (Python)
from scipy.stats import bernoulli
p_ctr = 0.05
dist = bernoulli(p_ctr)
print(f"P(click) = {dist.pmf(1):.4f}") # 0.05
print(f"P(no-click) = {dist.pmf(0):.4f}") # 0.95
print(f"E[X] = {dist.mean():.4f}") # 0.05
print(f"Var[X] = {dist.var():.6f}") # 0.0475Beginner example (R)
p_ctr <- 0.05
cat(sprintf("P(click) = %.4f\n", dbinom(1, size=1, prob=p_ctr))) # 0.05
cat(sprintf("P(no-click) = %.4f\n", dbinom(0, size=1, prob=p_ctr))) # 0.95
cat(sprintf("E[X] = %.4f | Var[X] = %.6f\n", p_ctr, p_ctr*(1-p_ctr)))Professional example (Python)
import numpy as np
from scipy.stats import bernoulli, norm
def conversion_rate_mle_and_ci(
observations: np.ndarray,
confidence_level: float = 0.95,
) -> dict[str, float]:
"""Estimate Bernoulli p via MLE with asymptotic confidence interval.
MLE: p_hat = sample mean.
CI uses Normal approximation (Wilson better for small n).
Args:
observations: Binary array (0/1 outcomes).
confidence_level: Desired CI coverage.
Returns:
Dict with MLE, standard error, and confidence interval.
"""
n = len(observations)
p_hat = observations.mean()
se = np.sqrt(p_hat * (1 - p_hat) / n)
z_alpha = norm.ppf(1 - (1 - confidence_level) / 2)
return {
"n": n,
"p_hat": float(p_hat),
"se": float(se),
"ci_lower": float(max(0, p_hat - z_alpha * se)),
"ci_upper": float(min(1, p_hat + z_alpha * se)),
"margin_of_error": float(z_alpha * se),
}
rng = np.random.default_rng(seed=42)
clicks = bernoulli.rvs(p=0.047, size=10_000, random_state=rng)
result = conversion_rate_mle_and_ci(clicks)
print("CTR Estimation (Bernoulli MLE):")
for key, val in result.items():
print(f" {key}: {round(val, 6) if isinstance(val, float) else val}")Source: AIO p. 85