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.
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:
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.
For discrete distributions, use PMF instead of PDF.
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.
After fitting, inspect:
- histogram versus fitted curve,
- quantile-quantile plot,
- goodness-of-fit test where appropriate.
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.
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.
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
- Problems obtaining most informative features with scikit learn?
- Produce balanced mini batch with Dataset API
- Programmatically determine the relative popularities of a list of items books, songs, movies, etc
- Progress indicator during pandas operations
- Probability of collision when using a 32-bit hash
- Probability prediction method of KNeighborsClassifier returns only 0 and 1
- Problem with running object_detection_tutorial TypeError load missing 2 required positional arguments
- Problems using MySQL with AWS Lambda in Python

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 courseTrack 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.