Probability Distribution
Python
Data Analysis
Statistics
Python Programming

Probability distribution in Python

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Probability distributions let you model uncertainty instead of pretending every process is deterministic. In Python, NumPy and SciPy provide a practical toolkit for sampling, fitting, evaluating likelihoods, and building simulations. The key is selecting a distribution that matches the data-generating process, then validating that assumption with diagnostics instead of intuition.

Picking a Distribution Family

Start from mechanism, not convenience. If you model the wrong family, later analysis can look precise while being wrong.

Common choices:

  • normal distribution for measurement noise and many aggregated effects,
  • poisson for event counts in fixed intervals,
  • binomial for number of successes in fixed trials,
  • exponential for waiting times between independent arrivals.

Quick simulation examples:

python
1import numpy as np
2
3rng = np.random.default_rng(42)
4normal_data = rng.normal(loc=10.0, scale=2.0, size=5000)
5poisson_data = rng.poisson(lam=4.5, size=5000)
6
7print(normal_data.mean(), normal_data.std())
8print(poisson_data.mean())

Use seeded generators so experiments remain reproducible.

Working with PDF, CDF, and Quantiles

SciPy exposes a consistent API across distributions. For continuous distributions, use PDF for density, CDF for threshold probability, and PPF for quantiles.

python
1from scipy.stats import norm
2
3x = 1.2
4pdf = norm.pdf(x, loc=0, scale=1)
5cdf = norm.cdf(x, loc=0, scale=1)
6q95 = norm.ppf(0.95, loc=0, scale=1)
7
8print("pdf", pdf)
9print("cdf", cdf)
10print("95th percentile", q95)

For discrete distributions, use PMF instead of PDF.

python
1from scipy.stats import poisson
2
3k = 6
4lam = 4.0
5print("P(X = 6)", poisson.pmf(k, mu=lam))
6print("P(X <= 6)", poisson.cdf(k, mu=lam))

A frequent confusion is treating PDF value as direct probability for continuous variables. Probability over a point is zero; you need an interval from the CDF.

Fitting a Distribution to Data

Fitting estimates parameters from observed samples. This is useful, but fitting alone does not prove model quality.

python
1import numpy as np
2from scipy.stats import norm
3
4observed = np.array([12.1, 11.8, 12.5, 11.9, 12.0, 12.2, 11.7, 12.3])
5mu, sigma = norm.fit(observed)
6print("mu", mu)
7print("sigma", sigma)

After fitting, inspect:

  • histogram versus fitted curve,
  • quantile-quantile plot,
  • goodness-of-fit test where appropriate.
python
1import matplotlib.pyplot as plt
2from scipy.stats import kstest
3
4stat, pvalue = kstest(observed, "norm", args=(mu, sigma))
5print("KS stat", stat, "p-value", pvalue)
6
7x = np.linspace(observed.min() - 1, observed.max() + 1, 200)
8plt.hist(observed, bins=8, density=True, alpha=0.5)
9plt.plot(x, norm.pdf(x, mu, sigma))
10plt.title("Observed data and fitted normal")
11plt.show()

Treat tests as evidence, not absolute truth. Small datasets can fail to reveal mismatch, and large datasets can detect tiny differences with little practical impact.

Monte Carlo Simulation Pattern

Distributions become operational when you simulate outcomes under uncertainty.

Example: estimate chance that weekly demand exceeds inventory.

python
1import numpy as np
2
3rng = np.random.default_rng(7)
4inventory = 120
5simulated_demand = rng.poisson(lam=110, size=100000)
6stockout_probability = (simulated_demand > inventory).mean()
7
8print("stockout probability", stockout_probability)

From this you can test scenarios by changing inventory level or demand assumptions, then compare risk and cost tradeoffs.

Numerical Stability and Reproducibility

In tail regions, tiny probabilities can underflow in floating-point arithmetic. Prefer log-space methods when multiplying many probabilities.

python
1from scipy.stats import norm
2
3log_likelihood = norm.logpdf(8.0, loc=0, scale=1)
4print(log_likelihood)

For reproducible analyses:

  • set random seeds,
  • record package versions,
  • persist fitted parameters and assumptions,
  • keep code and data snapshots tied to experiment IDs.

Without this discipline, you may not be able to explain why yesterday and today produce different results.

Common Pitfalls

  • Choosing a familiar distribution without checking if it matches process mechanics.
  • Interpreting PDF values as probabilities for exact points in continuous models.
  • Fitting parameters and skipping fit diagnostics.
  • Ignoring random seeds and losing reproducibility.
  • Multiplying tiny probabilities directly instead of using log-probabilities.

Summary

  • Python supports full distribution workflows through NumPy and SciPy.
  • Select distribution family from the underlying data process.
  • Use CDF, PMF or PDF, and quantiles according to question type.
  • Fit parameters, then validate with plots and statistical checks.
  • Use seeded simulation and log-space methods for reliable production analysis.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.