Skip to content

PHP implementation of statistical probability distributions: normal distribution, beta distribution, gamma distribution and more.

License

Notifications You must be signed in to change notification settings

tanpuekai/Probability-Distributions-for-PHP

 
 

Repository files navigation

Probability Distributions for PHP

Build Status

A userland PHP implementation of a number of tools for working with statistical distributions in PHP.

Compatibility: PHP 5.3 and above. Tested and supported on 5.4 through 7.1 as well as nightly. We do not currently support hhvm. As Travis-CI stopped easily supporting PHP 5.3, we stopped testing on it.

Installation

This package is available in Packagist/Composer as gburtini/distributions. For noncomposer uses, clone the repository and require files directly.

For deprecated pre-PHP 5.3 uses, require the pseudonamespaced implementation classes in the ugly/ subdirectory and prefix all class names with GBPDP_ -- unfortunately, this strategy will be forked in to a branch and abandoned going forward, so only PHP 5.4+ will be supported.

Supported Distributions

The name given here is the name of the class.

  • Normal(location μ ∈ R, squared scale σ2 > 0)
  • Binomial(number of trials, probability of success per trial in [0,1])
  • Bernoulli(fraction in [0,1])
  • Beta(shape α > 0, shape β > 0)
  • Gamma(shape α > 0, rate β > 0)
  • T(degrees of freedom v > 0)
  • Dirichlet(array of concentration parameters α > 0)
  • Poisson(mean λ > 0)

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.

Interface

  • Constructor - takes in the parameters of the distribution and returns an instance.
  • public function pdf($x) - returns the density or mass at a given discretized point.
  • public function pmf($x) - alias for pdf.
  • public function cdf($x) - returns the cumulatfive density from -∞ to $x.
  • public function icdf($y) - inverse CDF function, for a given density, returns a point.
  • public function quantile($y) - alias for icdf.
  • public function rand() - draws a sample from this distribution.
  • public function rands($n) - draws a sample of length $n from this distribution.
  • public static function draw(...) - draws a sample from the distribution given by the parameters passed in, a static alternative to rand.

Namespaces

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.

While unsupported, if you are using a version of PHP pre-namespaces, the ugly/ directory implements pseudonamespaced ("ugly") names, with the prefix GBPDP_ and can be used directly. If you have access to namespaces (PHP 5.3+) you should use the composer-compatible namespacing to interact with the classes. You really should upgrade your PHP version to a supported version.

Example

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]

Alternatives

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.

Future Work

  • First, implement the interface for all distributions!
  • Add mean, median, mode, variance calculators.
  • Implement more univariate distributions. For example, any of: Cauchy, chi-squared, exponential, F, geometric, hypergeometric, Laplace, log-normal, Maxwell–Boltzmann, Pareto, Rademacher, Rayleigh, uniform, Wakeby, Weibull, Zipf, Zipf-Mandelbrot. Producing more distributions may be aided by the cool relational diagram on John D. Cook's website.
  • Implement support for multivariate distributions, especially the multivariate normal, but also: multinomial, etc.
  • Generalization of distributions' implementation where appropriate, such as an elliptical distributions approach to implementing the normal or a categorical distribution implementation of the Bernoulli.
  • Design a good interface for alternative parameterizations (for example, precision-denoted normal, mode and concentration denoted beta, and shape and rate denoted gamma).
  • Toolkit for performing auxiliary probability-related tasks such as method of moments fitting.
  • Add moment-generating and characteristic functions to distributions where they are meaningful and tractable. Generalize concepts like expectation and variance out of them with a clean interface.

Pull Requests

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.

License

MIT licensed. Please contact me if this does not work for your use-case.

About

PHP implementation of statistical probability distributions: normal distribution, beta distribution, gamma distribution and more.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%