Sahaj.blogs
3 Jun 2024 · StudyPhase 6 · Probability and Statistics

Probability and Statistics · Discrete distributions

Bernoulli, binomial, geometric and Poisson distributions: what each one counts, their means, and computing them with scipy.stats.

distributioncountsparametersmean
Bernoullione yes/no trialpp
Binomialsuccesses in n trialsn, pnp
Geometrictrials until the first successp1/p
Poissonevents in a fixed intervalλλ

Poisson is also the limit of Binomial(n, p) when n is large and p is small, with λ = np.

In scipy.stats, every distribution shares one interface: pmf (or pdf), cdf, sf (= 1 − cdf), mean, var, rvs (random samples).

from scipy import stats

b = stats.binom(n=10, p=0.3)
print("P(exactly 3 of 10) =", round(b.pmf(3), 4), "| P(at most 3) =", round(b.cdf(3), 4), "| mean", b.mean())

g = stats.geom(p=1 / 6)
print("P(first six on roll 4) =", round(g.pmf(4), 4), "| mean rolls", g.mean())

pois = stats.poisson(mu=4)          # e.g. 4 emails per hour
print("P(0 emails) =", round(pois.pmf(0), 4), "| P(more than 6) =", round(pois.sf(6), 4))

big = stats.binom(n=1000, p=0.004)  # Poisson approximation, lambda = 4
print("binomial vs poisson at k=2:", round(big.pmf(2), 5), round(pois.pmf(2), 5))
print("samples:", b.rvs(size=8, random_state=0))

Output:

P(exactly 3 of 10) = 0.2668 | P(at most 3) = 0.6496 | mean 3.0
P(first six on roll 4) = 0.0965 | mean rolls 6.0
P(0 emails) = 0.0183 | P(more than 6) = 0.1107
binomial vs poisson at k=2: 0.14638 0.14653
samples: [3 4 3 3 3 3 3 5]

#probability#distributions

Sources

  1. scipy.stats.binom — SciPy
  2. scipy.stats.poisson — SciPy
  3. MIT 18.05 Reading 4b: Discrete distributions — MIT OpenCourseWare