Calculate the Cumulative Distribution Function CDF 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
In Python, calculating a CDF depends on what kind of distribution you have. If you know the theoretical distribution, use a statistics library such as SciPy to evaluate the distribution's cdf function directly. If you only have observed data, build an empirical CDF from the sorted sample.
Parametric CDF with SciPy
For a known distribution such as the normal distribution, SciPy gives you the CDF directly.
This returns the probability that a normal random variable with mean 0 and standard deviation 1 is less than or equal to 1.5.
The same pattern works for many other distributions in scipy.stats, such as expon, binom, or poisson. The main idea is always the same: choose the distribution object and call its cdf method with the relevant parameters.
Empirical CDF from Sample Data
If you do not want to assume a theoretical distribution, compute an empirical CDF from observed values.
Each point in cdf_values is the fraction of observations less than or equal to the corresponding sorted sample value. This is a direct, non-parametric description of the data.
If you want to evaluate the empirical CDF at a specific query point, count how many samples are less than or equal to that point.
That is the simplest possible empirical CDF calculation.
Plot the CDF
A CDF is often easier to understand visually than numerically. For an empirical CDF, a step plot is usually the clearest representation.
For a theoretical distribution, you can generate a grid of x values and call the distribution's cdf method across the grid.
This distinction between theoretical and empirical CDFs matters a lot in analysis. A theoretical CDF assumes a model, such as normal or exponential, while an empirical CDF is just a summary of what the sample actually contains. One is model-based, the other is data-based.
Choosing the wrong one can turn a simple probability question into a modeling mistake.
That is why good statistical code starts by deciding whether the problem is about a known distribution or about observed data only.
The CDF formula may be simple, but the modeling choice behind it is not.
Common Pitfalls
- Mixing up the CDF with the PDF or PMF.
- Using a theoretical distribution when the data should be handled empirically.
- Forgetting that the CDF is the probability of being less than or equal to a value.
- Comparing empirical and theoretical CDFs without checking parameter assumptions.
- Plotting noisy data without sorting it first for the empirical case.
Summary
- Use
scipy.stats.<distribution>.cdf(...)for theoretical distributions. - Use sorting and cumulative proportions for an empirical CDF.
- A simple
np.mean(samples <= x)computes the empirical CDF at one point. - Step plots are a natural way to visualize empirical CDFs.
- Pick the method based on whether you know the distribution or only have sample data.
Related reading
- Calculating AUC when using Vowpal Wabbit
- Calculating Nearest Match to Mean/Stddev Pair With LibSVM
- Calculating or Rows and Columns
- Calculating Pearson correlation and significance in Python
- Calculate the perimeter and area of intersecting rectangles?
- calculate the sum of diagonals in a matrix
- Calculating a directory's size using Python?
- Calculating arithmetic mean one type of average 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.