Back to Blog

Math — Probability Distributions

·Reha Tuncer·Math
View source on GitHub

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
1Implement the binomial distribution: Bernoulli trials, nnn and ppp parameters
2Implement the normal (Gaussian) distribution: mean μ\muμ, standard deviation σ\sigmaσ
3Implement the Poisson distribution: rate parameter λ\lambdaλ, counting processes
4Implement the exponential distribution: rate parameter λ\lambdaλ, waiting times
5Estimate distribution parameters from data using the method of moments
6Compute PMF (probability mass function) for discrete distributions
7Compute PDF (probability density function) for continuous distributions
8Compute CDF (cumulative distribution function) for all four distributions
9Convert 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(1p)nk{n \choose k} p^k (1-p)^{n-k}(kn)pk(1p)nk using iterative factorial accumulation to avoid overflow.

New techniques introduced:

TechniquePurpose
Method of moments estimationEstimate nnn and ppp from sample mean and variance
Iterative binomial coefficientCompute (nk){n \choose k}(kn) without factorials via product
round() vs int() for parameter estimationRound 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=1variance/meanp = 1 - \text{variance}/\text{mean}p=1variance/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π1e2σ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:

TechniquePurpose
z = (x - mean) / stddevStandardize a value to z-score (number of stddevs from mean)
x = stddev * z + meanReverse standardization: z-score back to raw value
Gaussian PDF formulaf(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 approximationCompute cumulative probability using polynomial approximation of erf
Class constants e and piPre-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:

TechniquePurpose
λ=1nxi\lambda = \frac{1}{n}\sum x_iλ=n1xiEstimate Poisson rate as the arithmetic mean of the data
Iterative k!k!k! accumulationCompute 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)=1eλxF(x) = 1 - e^{-\lambda x}F(x)=1eλ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 1eλx1 - e^{-\lambda x}1eλx — a simple closed form, unlike the Poisson which requires summation.

New techniques introduced:

TechniquePurpose
λ=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λxMemoryless continuous distribution for waiting times
Exponential CDF: 1eλx1 - e^{-\lambda x}1eλxClosed-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+tX>s)=P(X>t)P(X > s+t \mid X > s) = P(X > t)P(X>s+tX>s)=P(X>t). The rate λ\lambdaλ is the inverse of the expected waiting time.


Technique Inventory

TaskNew technique summarizedCategory
0Binomial PMF, method of moments for nnn and pppDiscrete Distributions
1Gaussian PDF/CDF, z-score standardization, erf approximationContinuous Distributions
2Poisson PMF/CDF, λ\lambdaλ as rate, iterative factorial summationDiscrete Distributions
3Exponential PDF/CDF, λ=1/xˉ\lambda = 1/\bar{x}λ=1/xˉ, memoryless propertyContinuous Distributions

Resources