PHPackages                             gburtini/distributions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gburtini/distributions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gburtini/distributions
======================

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

0.0.2(8y ago)59524.5k↓15.4%17[7 issues](https://github.com/gburtini/Probability-Distributions-for-PHP/issues)1MITPHPCI failing

Since Nov 5Pushed 4y ago8 watchersCompare

[ Source](https://github.com/gburtini/Probability-Distributions-for-PHP)[ Packagist](https://packagist.org/packages/gburtini/distributions)[ RSS](/packages/gburtini-distributions/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (7)Used By (1)

Probability Distributions for PHP
=================================

[](#probability-distributions-for-php)

[![Build Status](https://camo.githubusercontent.com/e0e218f2d46835385cb135ad64c279416e1e982340809b7b31431d6bb1ecd5dc/68747470733a2f2f7472617669732d63692e6f72672f6762757274696e692f50726f626162696c6974792d446973747269627574696f6e732d666f722d5048502e737667)](https://travis-ci.org/gburtini/Probability-Distributions-for-PHP.svg)

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
------------

[](#installation)

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

Supported Distributions
-----------------------

[](#supported-distributions)

The name given here is the name of the class.

- [Normal](https://en.wikipedia.org/wiki/Normal_distribution)(location μ ∈ R, squared scale σ2 &gt; 0)
- [Binomial](https://en.wikipedia.org/wiki/Binomial_distribution)(number of trials, probability of success per trial in \[0,1\])
- [Bernoulli](https://en.wikipedia.org/wiki/Bernoulli_distribution)(fraction in \[0,1\])
- [Beta](https://en.wikipedia.org/wiki/Beta_distribution)(shape α &gt; 0, shape β &gt; 0)
- [Gamma](https://en.wikipedia.org/wiki/Gamma_distribution)(shape α &gt; 0, rate β &gt; 0)
- [T](https://en.wikipedia.org/wiki/Student's_t_distribution)(degrees of freedom v &gt; 0)
- [Dirichlet](https://en.wikipedia.org/wiki/Dirichlet_distribution)(array of concentration parameters α &gt; 0)
- [Poisson](https://en.wikipedia.org/wiki/Poisson_distribution)(mean λ &gt; 0)
- [Weibull](https://en.wikipedia.org/wiki/Weibull_distribution)(shape k &gt; 0, scale lambda &gt; 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
---------

[](#interface)

- *Constructor* - takes in the parameters of the distribution and returns an instance.
- public function *pdf*($x) - returns the [density](https://en.wikipedia.org/wiki/Probability_density_function) or [mass](https://en.wikipedia.org/wiki/Probability_mass_function) at a given *discretized* point.
- public function *pmf*($x) - alias for pdf.
- public function *cdf*($x) - returns the cumulatfive [density](https://en.wikipedia.org/wiki/Probability_density_function) 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
----------

[](#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
-------

[](#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
------------

[](#alternatives)

There is a [Statistics Functions package](http://php.net/manual/en/ref.stats.php) 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
-----------

[](#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](http://www.johndcook.com/blog/distribution_chart/) on John D. Cook's website.
- Implement support for multivariate distributions, especially the [multivariate normal](https://en.wikipedia.org/wiki/Multivariate_normal_distribution), but also: multinomial, etc.
- Generalization of distributions' implementation where appropriate, such as an [elliptical distributions](https://en.wikipedia.org/wiki/Elliptical_distribution) 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](https://en.wikipedia.org/wiki/Normal_distribution#Alternative_parameterizations), 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
-------------

[](#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
-------------

[](#contributions)

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

```
composer install

```

Run tests

```
./vendor/bin/phpunit

```

License
-------

[](#license)

[MIT licensed](https://tldrlegal.com/license/mit-license). Please contact me if this does not work for your use-case.

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 63.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3008d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae4c24da012c0348f5c1555bd9aa3088c43ce6d4bcad11652f12a6c6e37a4f3b?d=identicon)[gburtini](/maintainers/gburtini)

---

Top Contributors

[![gburtini](https://avatars.githubusercontent.com/u/780031?v=4)](https://github.com/gburtini "gburtini (50 commits)")[![mossadal](https://avatars.githubusercontent.com/u/8174070?v=4)](https://github.com/mossadal "mossadal (12 commits)")[![gustawdaniel](https://avatars.githubusercontent.com/u/16663028?v=4)](https://github.com/gustawdaniel "gustawdaniel (11 commits)")[![splitbrain](https://avatars.githubusercontent.com/u/86426?v=4)](https://github.com/splitbrain "splitbrain (2 commits)")[![giftonian](https://avatars.githubusercontent.com/u/2361223?v=4)](https://github.com/giftonian "giftonian (1 commits)")[![nefski](https://avatars.githubusercontent.com/u/9434079?v=4)](https://github.com/nefski "nefski (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![gaswelder](https://avatars.githubusercontent.com/u/5239895?v=4)](https://github.com/gaswelder "gaswelder (1 commits)")

---

Tags

bernoulli-distributionbeta-distributionbinomial-distributiondirichlet-distributiongamma-distributionnormal-distributionpoisson-distributionprobability-distributionsstatistical-distributionsstatisticst-distributionweibull-distribution

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gburtini-distributions/health.svg)

```
[![Health](https://phpackages.com/badges/gburtini-distributions/health.svg)](https://phpackages.com/packages/gburtini-distributions)
```

###  Alternatives

[selvinortiz/flux

Fluent regular expressions in PHP.

3382.1k3](/packages/selvinortiz-flux)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
