Package: pyordstat
pyordstatOrder statistics for Python 📊IntroductionA package providing order statistics for Python. The package implementsthe well-known formulas for order statisticsin a format that is amenable to being used with either infinite distributions expressed as functions, or finite distributions expressed as lists of values and their probability mass. It is also compatible with the format ofSciPy'sstatsmodule.What are order statistics?Order statistics are the statistics of the k-th variable in a sample of N elements. Given a probability distribution from which identical and independent samples are drawn, the k-th order statistic is the k-th smallest value in the sample. For example, the median is the 50th order statistic in a sample of 100. This package provides functions for calculating the probability density function (PDF) and cumulative distribution function (CDF) of order statistics for arbitrary distributions.InstallationTo add and install this package as a dependency of your project, runpoetry add pyordstat.UsageContinuous distributionsUse the classContinuousOrderStatisticsto calculate the PDF and CDF of order statistics defined on a continuous support. The class takes callable PDF and CDF functions as its first arguments, and any successive parameters are passed to the PDF and CDF functions. For example, to calculate the PDF and CDF of the median of a standard normal distribution, you would do the following:frompyordstatimportContinuousOrderStatisticsdefnormal_pdf(x,mu,sigma):return1/(2*np.pi)**0.5*np.exp(-0.5*(x/mu)**2/sigma**2)defnormal_cdf(x,mu,sigma):return0.5*(1+erf((x-mu)/(2**0.5*sigma)))order_stats=ContinuousOrderStatistics(normal_pdf,normal_cdf,mu,sigma)pdf_4_2=order_stats.order_statistics_pdf(4,2)Discrete distributionsFor discrete distributions, use the classDiscreteOrderStatistics. This class takes callable PMF (Probability Mass Function) and CDF functions as its first arguments, and any successive parameters are passed to the PMF and CDF functions. It works the same asContinuousOrderStatistics, but the_pdfin methods is replaced by_pmf(as it's more appropriate to talk about probability mass functions for discrete distributions).Finite distributionsFor finite distributions, use the classFiniteOrderStatistics. This class allows you to just pass the values of the support and their probability mass as arrays, and it will calculate the CDF for you. For example, to calculate the PDF and CDF of the median for a sample of 4 drawn from a discrete distribution with support[1, 2, 3, 4, 5]and probability mass[0.1, 0.2, 0.3, 0.2, 0.2], you would do the following:frompyordstatimportFiniteOrderStatisticssupport=np.array([1,2,3,4,5])pmf=np.array([0.1,0.2,0.3,0.2,0.2])order_stats=FiniteOrderStatistics(support,pmf)pdf_4_2=order_stats.order_statistics_pdf(4,2)Scipy compatibilityThe classesRVContOrderStatisticsandRVDiscrOrderStatisticswill accept an instance of a SciPyrv_continuousorrv_discretedistribution respectively.
For convenience, a few common use cases of this are included in thepyordstat.functionsmodule.ContributingContributions are welcome! This project was based off the Poetry Cookiecutter template foundhere. Recommended steps when developing are:set up your poetry environment withpoetry installset up the pre-commit hooks withpre-commit install(make sure to have Ruff, Black, and MyPy installed in the main environment)lint the code withpoe lintrun the tests withpoe testrebuild the docs withpoe docsAny contributions should be sent as a PR to thedevelopbranch. Please make sure to include tests for any new functionality, and to update the docs accordingly.LicenseThis project is licensed under the terms of the MIT license.