1
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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]' . $\endgroup$ Commented May 25, 2021 at 9:50
  • $\begingroup$ I think numpy.quantile would be what you're looking for. $\endgroup$
    – Oxbowerce
    Commented May 25, 2021 at 10:23
  • $\begingroup$ 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... $\endgroup$ Commented May 25, 2021 at 10:26
  • 1
    $\begingroup$ 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. $\endgroup$
    – Oxbowerce
    Commented May 25, 2021 at 10:30
  • $\begingroup$ Many thanks for the pointers. ;) $\endgroup$ Commented May 25, 2021 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.