Math — Probability Distributions
A progressive study of four fundamental probability distributions implemented as Python classes — binomial, normal (Gaussian), Poisson, and exponential — with parameter estimation from data and PMF/PDF/CDF computation.
Learning Objectives
| # | Concept |
|---|---|
| 1 | Implement the binomial distribution: Bernoulli trials, nnn and ppp parameters |
| 2 | Implement the normal (Gaussian) distribution: mean μ\muμ, standard deviation σ\sigmaσ |
| 3 | Implement the Poisson distribution: rate parameter λ\lambdaλ, counting processes |
| 4 | Implement the exponential distribution: rate parameter λ\lambdaλ, waiting times |
| 5 | Estimate distribution parameters from data using the method of moments |
| 6 | Compute PMF (probability mass function) for discrete distributions |
| 7 | Compute PDF (probability density function) for continuous distributions |
| 8 | Compute CDF (cumulative distribution function) for all four distributions |
| 9 | Convert between z-scores and x-values on the normal curve |
Task-by-Task Reference
Each task below highlights the unique challenge it posed and the new technique introduced — techniques from earlier tasks are not repeated.
Task 0 — Binomial Distribution (binomial.py)
Challenge: Model the number of successes in nnn independent Bernoulli trials, each with probability ppp — implementing the binomial PMF from scratch using combinatorial formulas.
Approach: The constructor accepts either explicit nnn and ppp or estimates them from data. From data, compute the mean, then variance, then solve for p=1−σ2/μp = 1 - \sigma^2/\mup=1−σ2/μ and n=round(μ/p)n = \text{round}(\mu/p)n=round(μ/p). The PMF computes (nk)pk(1−p)n−k{n \choose k} p^k (1-p)^{n-k}(kn)pk(1−p)n−k using iterative factorial accumulation to avoid overflow.
New techniques introduced:
| Technique | Purpose |
|---|---|
| Method of moments estimation | Estimate nnn and ppp from sample mean and variance |
| Iterative binomial coefficient | Compute (nk){n \choose k}(kn) without factorials via product |
round() vs int() for parameter estimation | Round nnn to nearest integer (not truncate) |
self.p = float(p), self.n = int(n) | Explicit type casting for distribution parameters |
Key takeaway: The binomial distribution models "number of successes in nnn trials." Parameters can be estimated from data: p=1−variance/meanp = 1 - \text{variance}/\text{mean}p=1−variance/mean, then n=round(mean/p)n = \text{round}(\text{mean}/p)n=round(mean/p).
Task 1 — Normal Distribution (normal.py)
Challenge: Model the bell-shaped Gaussian distribution and compute probabilities on it — implementing the PDF formula f(x)=1σ2πe−(x−μ)22σ2f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}f(x)=σ2π1e−2σ2(x−μ)2 from scratch.
Approach: Store μ\muμ (mean) and σ\sigmaσ (stddev) as floats. Provide z_score(x) to convert x-values to z-scores, x_value(z) to convert back, pdf(x) for the density, and cdf(x) using the error function approximation. Class constants e and pi are hardcoded for precision control.
New techniques introduced:
| Technique | Purpose |
|---|---|
z = (x - mean) / stddev | Standardize a value to z-score (number of stddevs from mean) |
x = stddev * z + mean | Reverse standardization: z-score back to raw value |
| Gaussian PDF formula | f(x)=1σ2πexp(−(x−μ)22σ2)f(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)f(x)=σ2π1exp(−2σ2(x−μ)2) |
| CDF via error function approximation | Compute cumulative probability using polynomial approximation of erf |
Class constants e and pi | Pre-defined mathematical constants at module level |
Key takeaway: The normal distribution is defined by μ\muμ (center) and σ\sigmaσ (spread). Z-scores standardize any normal to N(0,1)\mathcal{N}(0,1)N(0,1). The CDF answers "what's the probability of being below x?"
Task 2 — Poisson Distribution (poisson.py)
Challenge: Model the number of events occurring in a fixed interval — implementing the Poisson PMF P(k)=λke−λk!P(k) = \frac{\lambda^k e^{-\lambda}}{k!}P(k)=k!λke−λ and CDF as a sum.
Approach: The rate parameter λ\lambdaλ (lambtha) is either given or estimated as the sample mean. The PMF computes λke−λ/k!\lambda^k e^{-\lambda} / k!λke−λ/k! using iterative factorial accumulation. The CDF sums PMF values from 000 to kkk using the same iterative factorial approach for efficiency.
New techniques introduced:
| Technique | Purpose |
|---|---|
| λ=1n∑xi\lambda = \frac{1}{n}\sum x_iλ=n1∑xi | Estimate Poisson rate as the arithmetic mean of the data |
| Iterative k!k!k! accumulation | Compute factorial incrementally to avoid recomputation |
| CDF = ∑j=0kPMF(j)\sum_{j=0}^{k} \text{PMF}(j)∑j=0kPMF(j) | Cumulative probability is the sum of individual PMF values |
Key takeaway: The Poisson distribution models count data — "how many events in a fixed interval?" λ\lambdaλ is both the mean AND the variance. The PMF uses e−λe^{-\lambda}e−λ as the base probability of zero events.
Task 3 — Exponential Distribution (exponential.py)
Challenge: Model the waiting time between events in a Poisson process — implementing the exponential PDF f(x)=λe−λxf(x) = \lambda e^{-\lambda x}f(x)=λe−λx and CDF F(x)=1−e−λxF(x) = 1 - e^{-\lambda x}F(x)=1−e−λx.
Approach: The rate λ\lambdaλ is either given or estimated as 1/mean1/\text{mean}1/mean of the data (the reciprocal of the sample mean). The PDF computes λe−λx\lambda e^{-\lambda x}λe−λx directly. The CDF uses 1−e−λx1 - e^{-\lambda x}1−e−λx — a simple closed form, unlike the Poisson which requires summation.
New techniques introduced:
| Technique | Purpose |
|---|---|
| λ=1/xˉ\lambda = 1 / \bar{x}λ=1/xˉ | Estimate exponential rate as reciprocal of sample mean |
| Exponential PDF: λe−λx\lambda e^{-\lambda x}λe−λx | Memoryless continuous distribution for waiting times |
| Exponential CDF: 1−e−λx1 - e^{-\lambda x}1−e−λx | Closed-form cumulative probability — no summation needed |
Key takeaway: The exponential distribution is the continuous counterpart to the discrete Poisson. It models waiting times with the "memoryless" property: P(X>s+t∣X>s)=P(X>t)P(X > s+t \mid X > s) = P(X > t)P(X>s+t∣X>s)=P(X>t). The rate λ\lambdaλ is the inverse of the expected waiting time.
Technique Inventory
| Task | New technique summarized | Category |
|---|---|---|
| 0 | Binomial PMF, method of moments for nnn and ppp | Discrete Distributions |
| 1 | Gaussian PDF/CDF, z-score standardization, erf approximation | Continuous Distributions |
| 2 | Poisson PMF/CDF, λ\lambdaλ as rate, iterative factorial summation | Discrete Distributions |
| 3 | Exponential PDF/CDF, λ=1/xˉ\lambda = 1/\bar{x}λ=1/xˉ, memoryless property | Continuous Distributions |