PHP implementation of statistical probability distributions: normal distribution, beta distribution, gamma distribution and more.
A userland PHP implementation of a number of tools for working with statistical distributions in PHP.
Compatibility: PHP 5.4 and above. Tested and supported on 5.4
through 7.1
as well as nightly
. We do not currently support hhvm
.
This package is available in Packagist/Composer as gburtini/distributions
. For noncomposer uses, clone the repository and require files directly.
The name given here is the name of the class.
All supported distributions are in the namespace gburtini\Distributions
and implement the following interface. Implementing new distributions is as easy as extending gburtini\Distributions\Distribution
or one of the existing implementations.
gburtini\Distributions
contains the distribution classes as indicated above.
gburtini\Distributions\Accessories
contains BetaFunction and GammaFunction, two classes containing accessory functions for computing complete, incomplete and inverse beta and gamma functions numerically.
Examples are provided in a comment at the top of most of the implementation files. In general, you should be able to use the parametrization listed above under "Supported Distributions" to create classes that implement the methods under "Interfaces".
use gburtini\Distributions\Beta;
$beta = new Beta(1, 100);
$draw = $beta->rand();
if($draw > 0.5) {
echo "We drew a number bigger than 0.5 from a Beta(1,100).\n";
}
// $beta->pdf($x) = [0,1]
// $beta->cdf($x) = [0,1] non-decreasing
// $beta::quantile($y in [0,1]) = [0,1] (aliased Beta::icdf)
// $beta->rand() = [0,1]
// for BetaICDF there is optional paramerer maxIterations = 100, to change default value type
// $beta->icdf($x, ["maxIterations" => 30])
There is a Statistics Functions package in PECL called stats
which I have never been able to get to work and has been very quiet since 2006. There is plenty of code for individual distributions around the web, StackOverflow, etc., but in my experience it is hit and miss. To whatever extent possible, I would be happy to (but have not yet) wrap the stats_ functions (if function_exists
) where they have functionality that this package does not.
I will happily merge any new distributions (ideally with tests, but I'm even happy to write the tests), improvements to my code, etc. Please submit a pull request or send me an email.
Fork repository, clone to your own computer and install dependencies:
composer install
Run tests
./vendor/bin/phpunit
MIT licensed. Please contact me if this does not work for your use-case.