## gburtini / distributions

PHP implementation of a number of statistical probability distributions: normal, beta, gamma, etc.

Installs: 407 192

Dependents: 2

Suggesters: 0

Security: 0

Stars: 57

Watchers: 10

Forks: 17

Open Issues: 7

Requires (Dev)

- phpunit/phpunit: ^4.8

This package is auto-updated.

Last update: 2024-05-17 23:43:42 UTC

# README

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`

.

## Installation

This package is available in Packagist/Composer as `gburtini/distributions`

. For noncomposer uses, clone the repository and require files directly.

## 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)
- Weibull(shape k > 0, scale lambda > 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.

## 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] // for BetaICDF there is optional paramerer maxIterations = 100, to change default value type // $beta->icdf($x, ["maxIterations" => 30])

## 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, 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.

## Contributions

Fork repository, clone to your own computer and install dependencies:

```
composer install
```

Run tests

```
./vendor/bin/phpunit
```

## License

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