Chapter 4 of 10 · ~26 min read & lab+65 XP
Chapter 4 · Probability

Essential Discrete Distributions (Bernoulli, Binomial, Geometric & Poisson)

Never memorize a formula blindly again: learn the 4 simple everyday stories behind every discrete distribution in A/B testing, queues, and LLMs

Step 0 · Test Your Gut Before Any Math!

You are flipping a completely fair coin. Somehow you just got 5 Tails in a row (T-T-T-T-T)! What is the probability that your 6th flip lands on Heads?

What You Will Master in This Chapter
  • Pick the right distribution in 3 seconds purely from the STORY (fixed flips → Binomial, wait for 1st win → Geometric, rare events per hour → Poisson).
  • Understand every piece of the Binomial formula visually so you never have to memorize it.
  • See why huge-n, tiny-p Binomial problems collapse effortlessly into a 1-number Poisson formula.
  • Defeat the Gambler's Fallacy using the Memoryless Property of Geometric waiting times.
Plain-English Symbol Decoder · Click Any Symbol Before We Begin
No mysterious math notation — translate everything into English first
How to Say It Aloud
“X follows a Binomial with n trials and probability p”
Plain-English Meaning
The tilde '~' simply means 'is generated by this story'. Here: repeat a p-chance coin flip n times and count the wins!
Everyday Example
Shooting n = 10 basketball free throws when you make p = 70% of shots.
Story-First Intuition, Visual Diagrams & Interactive Walkthroughs

Textbooks make discrete distributions feel like a scary zoo of unrelated equations. Here is the secret every expert knows: there are really just FOUR everyday stories, all built out of a simple yes/no coin flip (Bernoulli):

  1. Flip ONCE → Bernoulli(pp)
  2. Flip a FIXED number nn times, count total wins → Binomial(n,pn, p)
  3. Keep flipping UNTIL your very first win, count the tries → Geometric(pp)
  4. Watch a clock for 1 hour, count how many random events pop up → Poisson(λ\lambda)

Once you recognize the story, the math practically writes itself!

1. Bernoulli & Binomial — Counting Wins in a Fixed Number of Tries

Everyday Story First (Zero Math Yet!)

Imagine shooting 3 free throws in basketball, and you make each shot 70% of the time (p=0.7p = 0.7, miss =0.3= 0.3). What is the chance you make exactly 2 out of 3 shots?

Don't touch a formula! Just list the 3 ways to score 2 hits (H) and 1 miss (M):

  • H - H - M: probability =0.7×0.7×0.3=0.147= 0.7 \times 0.7 \times 0.3 = 0.147
  • H - M - H: probability =0.7×0.3×0.7=0.147= 0.7 \times 0.3 \times 0.7 = 0.147
  • M - H - H: probability =0.3×0.7×0.7=0.147= 0.3 \times 0.7 \times 0.7 = 0.147

All 3 patterns have the EXACT same probability (0.1470.147), so total chance =3×0.147=44.1%= 3 \times 0.147 = \mathbf{44.1\%}! You just derived the Binomial formula by hand!

Bernoulli(p)1 yes/no flipE=p · Var=p(1−p)Binomial(n, p)Hits in n flipsE=np · Var=np(1−p)Geometric(p)Trials until 1st hitE=1/p · Var=(1−p)/p²Poisson(λ)Rare counts / hourE=λ · Var=λ
Figure 4.1 — Discrete Distribution Map: choose by the generative story (1 trial → Bernoulli, fixed n trials → Binomial, wait for 1st hit → Geometric, rare events in time → Poisson).

Look at what you just did with those 3 basketball shots — and notice how it generalizes to ANY nn shots and kk hits:

  • One single yes/no shot is Bernoulli(pp): average =p= p, variance =p(1−p)= p(1 - p).
  • Adding nn independent shots together gives Binomial(n,pn, p):
    • Average wins: E[X]=np\mathbb{E}[X] = np (shoot 10 shots at 70%   ⟹  \implies expect 77 hits!)
    • Spread: Var⁡(X)=np(1−p)\operatorname{Var}(X) = np(1 - p)
  • And if each trial picks from KK categories instead of 2 (like an LLM choosing 1 word from 50,000 vocabulary tokens via Softmax!), that's Multinomial / Categorical(p\mathbf{p})!

Click each colored chip of the Binomial formula below — then run the live Coin-Flip Factory to watch random coins literally BUILD the Binomial curve bar by bar!

Formula Anatomy · Click Any Colored Piece to Decode It
Binomial Formula: P(X = k) = C(n, k) · p^k · (1 − p)^(n − k)

Literally the exact 3-free-throw calculation you just did — generalized to n tries!

How Many Ways to Arrange k Wins

Counts all different sequences that have k wins and n−k misses (e.g., HHM, HMH, MHH = 3 patterns!).

In our example: C(3, 2) = 3 patterns
Put it together with real numbers: 2 hits in 3 free throws (p = 0.7): 3 patterns × (0.7)² × (0.3)¹ = 3 × 0.147 = 0.441 (44.1%)!
Hands-On Mini-Lab · Live Coin-Flip Factory (Watch Binomial Build Itself!)Batches simulated: 0

Each batch flips 8 coins with Head probability p = 0.50 and drops a block into the column for how many Heads appeared. Watch the blue empirical bars hug the dashed green Binomial formula!

0H1H2H3H4H5H6H7H8H
Click 'Flip 8 Coins Once' or 'Run 100 Batches'! · Dashed green = exact Binomial(8, 0.50)
Try It YourselfWarm-Up

An ad has a 20% click rate (p = 0.2). You show it to n = 4 independent users. What is the probability of getting EXACTLY 1 click?

2. Poisson($\lambda$) — Counting Rare Events Without Knowing $n$

Everyday Story First (Zero Math Yet!)

Suppose you run a coffee shop and notice an average of 3 customers walk in every 10 minutes. Someone asks: "What's the chance ZERO customers walk in during the next 10 minutes?"

Wait — you can't use Binomial here! Why? Because there's no fixed number of trials nn (how many people in the whole city didn't walk into your shop?!). Instead, imagine chopping those 10 minutes into 10,000 tiny micro-seconds, each with a tiny 3/100003/10000 chance of a customer stepping through the door. When nn is huge and pp is tiny, Binomial magically melts into ONE super-clean formula depending ONLY on the average rate λ=3\lambda = 3: the Poisson distribution!

Binomial(n=100, p=0.03) ≈ Poisson(λ=3)01234567Poisson Limit Theorem:P(X = k) = e⁻λ · λᵏ / k!Blue bars = Binomial(100, 0.03)Gold dots = Poisson(λ = 3) — nearly identical!
Figure 4.2 — Law of Rare Events: as n → ∞ and p → 0 with λ = np fixed, Binomial(n, p) converges cleanly to Poisson(λ).

The Poisson Formula (Just ONE Parameter: Average Rate λ\lambda!)

P(X=k)=e−λλkk!,k=0,1,2,…P(X = k) = \frac{e^{-\lambda} \lambda^k}{k!}, \qquad k = 0, 1, 2, \dots

  • Signature Fun Fact: Both the Mean AND the Variance equal λ\lambda (E[X]=λ\mathbb{E}[X] = \lambda, Var⁡(X)=λ\operatorname{Var}(X) = \lambda)!
  • Zero-Event Shortcut (k=0k = 0): Plugging k=0k = 0 gives P(X=0)=e−λP(X = 0) = e^{-\lambda} — remember this, it's everywhere in system reliability!
  • Adding Independent Poissons: If Website A gets Poisson(4)\text{Poisson}(4) signups/min and Website B gets Poisson(6)\text{Poisson}(6) signups/min independently, combined signups follow Poisson(4+6=10)\text{Poisson}(4 + 6 = 10)!
Walk Through With Me · Interactive Worked Example

Server Crashes: Turning 100,000 Requests Into a 1-Line Poisson!

Your AI API serves n = 100,000 requests per hour, and each request independently fails with tiny probability p = 0.00003. Find the probability of (a) ZERO crashes, and (b) AT LEAST ONE crash this hour.

1Spot the 'Huge n, Tiny p' signal → compute λ = npStep 1 / 3

Instead of computing 100,000!100{,}000! (which crashes calculators!), multiply n×pn \times p to get the average failures per hour: λ=100,000×0.00003=3 failures/hour\lambda = 100{,}000 \times 0.00003 = \mathbf{3\text{ failures/hour}}

Teacher's Note: Whenever n ≥ 50 and p ≤ 0.05 (so λ = np is moderate), Poisson(λ) matches Binomial(n, p) to 4+ decimal places!
Pause & Check · Quick Intuition Pulse

Quick Story Matcher: Which distribution models 'The number of typos on a 500-word page, when typos happen randomly at an average rate of 1.5 per page'?

Try It YourselfStandard

A support inbox gets an average of λ = 2 urgent tickets per hour. What is the probability of receiving EXACTLY 2 tickets next hour? (Hint: e⁻² ≈ 0.1353)

3. Geometric($p$) — Waiting for Your First Win (No Memory!)

Everyday Story First (Zero Math Yet!)

Imagine opening mystery trading-card packs until you pull ONE rare holographic card. Each pack has a 10% chance (p=0.10p = 0.10) of containing it. How many packs should you expect to buy? Your gut instantly says: 10 packs! (1/0.10=101 / 0.10 = 10).

Now suppose you already opened 7 packs and got nothing. How many more packs do you expect to buy from right here? Still 10 more packs! Because the factory didn't secretly rig pack #8 just because you had bad luck on packs 1–7!

Memoryless Property: Past Failures Give Zero Boost to Future Trials!FFFFFP(X > s + t | X > s) = P(X > t)Remaining expected wait is STILL 1/p — no "due" effect!
Figure 4.3 — Memoryless Property of Geometric(p): after k failures, the remaining wait time has the exact same distribution as starting fresh!

The Geometric Formula Is Pure Common Sense!

To get your very first win on try #kk, two simple things must happen:

  1. You miss the first (k−1)(k - 1) tries: (1−p)k−1(1 - p)^{k - 1}.
  2. You win on try #kk: × p\times\, p. P(X=k)=(1−p)k−1p,P(takes more than k tries)=(1−p)kP(X = k) = (1 - p)^{k - 1} p, \qquad P(\text{takes more than } k \text{ tries}) = (1 - p)^k
  • Average wait: E[X]=1p\mathbb{E}[X] = \frac{1}{p} (1-in-6 die roll   ⟹  \implies average 6 rolls until your first 6!)
  • Memoryless Property: P(X>s+t∣X>s)=P(X>t)P(X > s + t \mid X > s) = P(X > t). Past misses evaporate completely — Geometric is the ONLY discrete distribution with zero memory!
Try It YourselfWarm-Up

An API call succeeds with probability p = 0.25 on each independent retry. (a) What is the expected number of tries until it succeeds? (b) What is the chance all 3 of the first 3 tries FAIL?

“Wait, I'm Confused...” · Teacher Office Hours Q&A

The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.

Ask two 1-second questions:

  1. Is there a fixed number of tries nn? → YES = Binomial(n,pn, p) (counting how many wins in nn tries).
  2. Are you waiting until the 1st success? → YES = Geometric(pp) (counting tries until the first win).
  3. Are you counting random events per minute/hour/page with only an average rate λ\lambda? → YES = Poisson(λ\lambda)!
Interactive Visual Lab
Lab Challenge: Explore all three discrete families (Binomial, Poisson, Geometric) in the Deep Lab below and watch their shapes & moments shift live!
Explored: 1/3 distributions
01234567891011121314
Trials n14
Success Prob p0.35
Mean E[X]
4.90
Variance
3.18
Intuition Takeaway: Memorize stories, not formulas: fixed n tries → Binomial(n, p); rare events per window → Poisson(λ); wait for 1st win → Geometric(p) (memoryless); K-class token picks → Multinomial/Categorical.
Where This Powers Real ML, Deep Learning & GenAI

LLM Next-Token Sampling (Categorical / Multinomial)

Every word ChatGPT/Gemini outputs is drawn from Categorical(softmax(logits / T)) — a Multinomial with n = 1 over the vocabulary!

A/B Testing Conversion Counts (Binomial)

Counting how many of n users clicked 'Subscribe' follows Binomial(n, p), driving z-tests and sample size calculators.

Demand Forecasting & Poisson Regression

Uber rides/hour, ER visits/night, and API traffic spikes are modeled with Poisson GLMs where λ = exp(wᵀx).

Naive Bayes Text Classification

Bag-of-words spam filters model word counts in an email as Multinomial(N_words, p_class).

Production NumPy & PyTorch Code Patterns
import math
import numpy as np

# 1. Numerically stable Binomial log-PMF (never overflows factorial!)
def binomial_pmf(k: int, n: int, p: float) -> float:
    log_p = (
        math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1)
        + k * math.log(p) + (n - k) * math.log(1.0 - p)
    )
    return math.exp(log_p)

# 2. Watch Binomial(100000, 3e-5) match Poisson(lambda = 3) to 4 decimals!
n, p = 100_000, 3e-5
lam = n * p
for k in range(5):
    pb = binomial_pmf(k, n, p)
    pp = math.exp(-lam + k * math.log(lam) - math.lgamma(k + 1))
    print(f"k={k}: Binomial={pb:.6f} | Poisson={pp:.6f}")
Common ML Interview Traps & Numerical Pitfalls
Trap #1: Mixing up the two Geometric conventions: total tries INCLUDING the first win (mean 1/p) vs. failures BEFORE the first win (mean (1−p)/p).
Best Practice: Always state explicitly in an interview: 'I'm defining X as total trials until the 1st success, starting at k = 1, so E[X] = 1/p.'
Trap #2: Using Poisson on viral/bursty count data where Variance >> Mean.
Best Practice: Check sample variance vs. sample mean first; if Var > Mean (overdispersion), recommend Negative Binomial!
Trap #3: Computing math.comb(100000, 50000) * (0.5 ** 100000) directly in Python floats → OverflowError / 0.0 underflow.
Best Practice: Compute in log-space using math.lgamma(n + 1) and exponentiate only at the final return!
Chapter Mastery Check (+65 XP)
Score: 0 / 5
11. If X ~ Poisson(4) and Y ~ Poisson(6) are independent, what is X + Y?
22. You flip a fair coin and get 5 Tails in a row. Expected ADDITIONAL flips until the first Head?
33. Which discrete distribution maximizes variance p(1 − p) at p = 0.5?
44. 50,000 API requests/hour each fail independently with p = 0.00004. Best clean approximation?
55. Drawing 1 next token from an LLM's Softmax output over 50,000 words follows:

Apply What You Learned — Solve Matching Coding Problems