# Computing a cumulative distribution function in Python

I'm trying to compute the distribution function of any of the usual distributions in Python... However, all the methods I've seen involve first drawing N samples from said distribution, and then order them somehow, and then do a cumulative sum.

In Mathematica, I can just do CDF[ChiSquaredDistribution[df],quantile]. If I want another distribution, I just substitute ChiSquaredDistribution for the name of that other distribution.

Is there a simple way, like in Mathematica, to compute a cumulative distribution function in Python?

If I understand you correctly, these can be found in scipy.stats. scipy has a long list of different distributions that you can use, both continuous as well as multivariate and discrete. All distribution functions have an underlying cdf method which allows you to calculate the cumulative distribution functions of that specific distribution. Using the Chi-squared distribution from your example would look as follows:

from scipy.stats import chi2

chi2.cdf(x=30, df=50)
# 0.011164780271550276


Using other distributions is as simple as importing that distribution and using the cdf method as shown above.

• Thanks Oxbowerce ;) May 24, 2021 at 17:28
• By the way, is there a similarly fast way to compute the quantiles of a general distribution? In your link, I only see a way to compute the quantiles given an array... In Mathematica, I can just do 'Quantile[Distribution[df], prob]' . May 25, 2021 at 9:50
• I think numpy.quantile would be what you're looking for. May 25, 2021 at 10:23
• Many thanks...+1. By the way, I'm a bit new to python, and all of this searching between packages to see which has which function is starting to be a bit confusing. What's your opinion, experience in this regard? I'm used to working with Mathematica/R where almost everything comes ready to use... May 25, 2021 at 10:26
• Generally doing a quick google search for a specific functionality gives you what you're looking for. Indeed, in R most of these funtions related to statistics are in the standard library because R is more focused on statistics whereas Python is more of a general purpose programming language. I suspect that you can find most of the functions related to statistics in either numpy or scipy. May 25, 2021 at 10:30