PROBABILITY & STATISTICS / 4. DISCRETE DISTRIBUTIONS

Random Variables & Discrete Distributions

Bernoulli, Binomial, Poisson — modeling count data


EXPLANATION

A Random Variable maps outcomes of a random experiment to numbers.
• Discrete RV → countable values (0,1,2,...). Described by PMF
• Continuous RV → any value in a range. Described by PDF

PMF (Probability Mass Function): P(X=x) — probability of each exact value.
CDF (Cumulative Distribution Function): F(x) = P(X ≤ x) — probability up to x.

Key discrete distributions:

Uniform: P(X=x) = 1/n for each of n outcomes. E[X]=(a+b)/2, Var=(b-a+1)²-1)/12

Bernoulli(p): single trial, success or failure.
P(X=1)=p, P(X=0)=1-p. E[X]=p, Var=p(1-p)

Binomial(n,p): n independent Bernoulli trials, count successes.
P(X=k) = C(n,k) × pᵏ × (1-p)ⁿ⁻ᵏ. E[X]=np, Var=np(1-p)

Poisson(λ): count of events in fixed time/space, when events are rare and independent.
P(X=k) = e^(-λ) × λᵏ / k!. E[X]=λ, Var=λ
Used for: website hits/hour, typos/page, calls/minute.

DIAGRAM

Bernoulli(p=0.3):         Binomial(n=10, p=0.3):
  P(X=0) = 0.7              P(X=k) = C(10,k)×0.3ᵏ×0.7^(10-k)
  P(X=1) = 0.3              Peak at k=np=3

  Poisson(λ=3):
  P(X=0) = e⁻³ ≈ 0.050
  P(X=1) = 3e⁻³ ≈ 0.149
  P(X=2) = 4.5e⁻³ ≈ 0.224
  P(X=3) = 4.5e⁻³ ≈ 0.224   ← peak at λ

  Key relationships:
  Binomial(n,p) → Poisson(λ=np) when n→∞, p→0, np=const
  Sum of Bernoullis = Binomial

CODE

PYTHON
1import numpy as np
2from scipy import stats
3import matplotlib.pyplot as plt
4
5# ── Bernoulli ─────────────────────────────────────────────────────
6p = 0.3
7bern = stats.bernoulli(p)
8print(f"Bernoulli(p=0.3)")
9print(f" P(X=0) = {bern.pmf(0):.4f}")
10print(f" P(X=1) = {bern.pmf(1):.4f}")
11print(f" Mean = {bern.mean():.4f}")
12print(f" Var = {bern.var():.4f}")
13
14# Simulate 1000 coin flips (biased)
15flips = bern.rvs(size=1000)
16print(f" Simulated mean: {flips.mean():.4f}")
17
18# ── Binomial ──────────────────────────────────────────────────────
19n, p = 20, 0.3
20binom = stats.binom(n, p)
21print(f"\nBinomial(n=20, p=0.3)")
22print(f" P(X=6) = {binom.pmf(6):.4f}")
23print(f" P(X≤6) = {binom.cdf(6):.4f}") # CDF
24print(f" P(X>10) = {1-binom.cdf(10):.4f}")
25print(f" Mean={binom.mean()}, Var={binom.var()}")
26
27# ── Poisson ───────────────────────────────────────────────────────
28lam = 4 # average events per interval
29pois = stats.poisson(lam)
30print(f"\nPoisson(λ=4)")
31print(f" P(X=0) = {pois.pmf(0):.4f}") # P(no events)
32print(f" P(X=4) = {pois.pmf(4):.4f}") # P(exactly λ events)
33print(f" P(X≤3) = {pois.cdf(3):.4f}")
34print(f" Mean=Var={pois.mean()}") # unique: mean=variance for Poisson
35
36# ── Plot all three PMFs ───────────────────────────────────────────
37fig, axes = plt.subplots(1, 3, figsize=(14, 4))
38
39# Bernoulli
40ax = axes[0]
41ax.bar([0, 1], [1-0.3, 0.3], color="#A855F7", alpha=0.8, width=0.4)
42ax.set_title("Bernoulli(p=0.3)"); ax.set_xlabel("x")
43
44# Binomial
45ax = axes[1]
46k = np.arange(0, 21)
47ax.bar(k, stats.binom(20, 0.3).pmf(k), color="#C084FC", alpha=0.8)
48ax.set_title("Binomial(n=20, p=0.3)"); ax.set_xlabel("k")
49
50# Poisson
51ax = axes[2]
52k = np.arange(0, 15)
53ax.bar(k, stats.poisson(4).pmf(k), color="#E879F9", alpha=0.8)
54ax.set_title("Poisson(λ=4)"); ax.set_xlabel("k")
55
56plt.tight_layout()
57plt.savefig("discrete_dists.png", dpi=150)
← PREV3. Conditional Prob & BayesNEXT →5. Continuous Distributions